forked from FCG-LLC/aucote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aucote_cfg.py
135 lines (112 loc) · 3.67 KB
/
aucote_cfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'''
This module provides default configuration and the global instance for the utils.Config class.
'''
import os.path as path
from sys import stderr
import yaml
from pycslib.utils import Toucan
import utils.log as log_cfg
from utils import Config
# default values
# from utils.toucan import Toucan
from utils.toucan_monitor import ToucanMonitor
LOG_DIR = path.join(path.dirname(__file__), 'logs')
_DEFAULT = {
'logging': {
'root': {
'file': path.join(LOG_DIR, 'aucote.log'),
'level': 'info',
'max_file_size': 10 * 1024 * 1024,
'max_files': 5,
'format': '%(levelname)s %(asctime)s %(funcName)s: %(message)s',
'propagate': True
},
'storage': {
'file': path.join(LOG_DIR, 'storage.log'),
'level': 'info',
'max_file_size': 10 * 1024 * 1024,
'max_files': 10,
'format': '%(levelname)s %(asctime)s %(message)s',
'propagate': False
},
'pycslib': {
'file': path.join(LOG_DIR, 'pycslib.log'),
'level': 'info',
'max_file_size': 10 * 1024 * 1024,
'max_files': 10,
'format': '%(levelname)s %(asctime)s %(message)s',
'propagate': False
}
},
'fixtures': {
'exploits': {
'filename': 'fixtures/exploits/exploits.csv'
}
},
'topdis': {
'api': {
'host': 'localhost',
'port': 1234
},
'fetch_os': False
},
'toucan': {
'enable': True,
'api': 'http://toucan:3000',
'min_retry_time': 5,
'max_retry_time': 60 * 5,
'max_retry_count': 20
},
'pid_file': 'aucote.pid',
'default_config': 'aucote_cfg_default.yaml',
}
# global cfg
cfg = Config(_DEFAULT)
async def start_toucan(default_config):
"""
Initialize Toucan
Args:
default_config:
Returns:
None
"""
Toucan.min_retry_time = cfg['toucan.min_retry_time']
Toucan.max_retry_time = cfg['toucan.max_retry_time']
Toucan.max_retry_count = cfg['toucan.max_retry_count']
cfg.toucan = Toucan(api=cfg['toucan.api'], prefix='aucote')
# Add toucan monitor
cfg.toucan_monitor = ToucanMonitor(toucan=cfg.toucan)
cfg.toucan_monitor.start()
if cfg['rabbit.enable']:
await cfg.start_rabbit(cfg['rabbit.host'], int(cfg['rabbit.port']), cfg['rabbit.username'],
cfg['rabbit.password'])
if cfg['toucan.push_default']:
with open(default_config, "r") as file:
config = yaml.safe_load(file)
await cfg.toucan.async_push_config(config, overwrite=cfg['toucan.overwrite'])
async def load(file_name=None):
'''
Initializes this module.
Needs to be called before other functions are used.
'''
if file_name is None:
# by default search for "confg.yaml" in application dir
file_name = path.join(path.dirname(__file__), 'aucote_cfg.yaml')
file_name = path.abspath(file_name)
# at this point logs do not work, print info to stdout
print("Reading configuration from file:", file_name)
try:
cfg.load(file_name, _DEFAULT)
except Exception:
stderr.write("Cannot load configuration file {0}".format(file_name))
exit()
await log_cfg.config(cfg['logging'])
default_config_filename = cfg['default_config']
if cfg['toucan.enable']:
await start_toucan(default_config_filename)
else:
try:
cfg.load(default_config_filename, cfg.cfg)
except Exception:
stderr.write("Cannot load configuration file {0}".format(default_config_filename))
exit()