-
Notifications
You must be signed in to change notification settings - Fork 169
/
worker.py
executable file
·265 lines (208 loc) · 8.94 KB
/
worker.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import sys
import signal
import argparse
import requests
from urllib.parse import urljoin
from socket import gethostname
from io import BytesIO
from zipfile import ZipFile
from shutil import move, rmtree
from uuid import uuid4
from time import time, sleep
from subprocess import Popen, check_output, STDOUT, CalledProcessError
from fame.core import fame_init
from fame.core.module import ModuleInfo
from fame.core.internals import Internals
from fame.common.config import fame_config
from fame.common.constants import MODULES_ROOT
from fame.common.pip import pip_install
from fame.common.cleaner import get_old_analyses, get_old_disabled_users
UNIX_INSTALL_SCRIPTS = {
"install.sh": ["sh", "{}"],
"install.py": ["python", "{}"]
}
WIN_INSTALL_SCRIPTS = {
"install.cmd": ["{}"],
"install.py": ["python", "{}"]
}
class Worker:
def __init__(self, queues, celery_args, refresh_interval):
self.queues = list(set(queues))
self.celery_args = [arg for arg in celery_args.split(' ') if arg]
self.refresh_interval = refresh_interval
def update_modules(self):
# Module updates are only needed for remote workers
if fame_config.remote:
# First, backup current code
backup_path = os.path.join(fame_config.temp_path, 'modules_backup_{}'.format(uuid4()))
os.makedirs(backup_path, exist_ok=True)
for module in os.listdir(MODULES_ROOT):
move(os.path.join(MODULES_ROOT, module), backup_path)
# Replace current code with code fetched from web server
url = urljoin(fame_config.remote, '/modules/download')
try:
response = requests.get(url, stream=True, headers={'X-API-KEY': fame_config.api_key})
response.raise_for_status()
with ZipFile(BytesIO(response.content), 'r') as zipf:
zipf.extractall(MODULES_ROOT)
rmtree(backup_path)
print("Updated modules.")
except Exception as e:
print(("Could not update modules: '{}'".format(e)))
print("Restoring previous version")
move(backup_path, MODULES_ROOT)
self.update_module_requirements()
def update_module_requirements(self):
installed = []
for module in ModuleInfo.get_collection().find():
module = ModuleInfo(module)
if 'error' in module:
del(module['error'])
if module['type'] == "Processing":
should_update = (module['queue'] in self.queues)
elif module['type'] in ["Threat Intelligence", "Reporting", "Filetype"]:
should_update = True
else:
should_update = (not fame_config.remote)
if should_update:
installed += self.update_python_requirements(module, installed)
installed += self.launch_install_scripts(module, installed)
module.save()
def update_python_requirements(self, module, already_installed):
requirements = self._module_requirements(module)
if requirements and not requirements in already_installed:
print(("Installing requirements for '{}' ({})".format(module['name'], requirements)))
rcode, output = pip_install('-r', requirements)
# In case pip failed
if rcode:
self._module_installation_error(requirements, module, output.decode('utf-8', errors='replace'))
return [requirements]
def launch_install_scripts(self, module, already_installed):
scripts = self._module_install_scripts(module)
for script in scripts:
if script in already_installed:
continue
try:
print(("Launching installation script '{}'".format(' '.join(script))))
check_output(script, stderr=STDOUT)
except CalledProcessError as e:
self._module_installation_error(' '.join(script), module, e.output.decode('utf-8', errors='replace'))
except Exception as e:
self._module_installation_error(' '.join(script), module, e)
return scripts
def _module_installation_error(self, cmd, module, errors):
errors = "{}: error on '{}':\n\n{}".format(cmd, gethostname(), errors)
module['enabled'] = False
module['error'] = errors
print(errors)
def _module_requirements(self, module):
return module.get_file('requirements.txt')
def _module_install_scripts(self, module):
results = []
if sys.platform == "win32":
INSTALL_SCRIPTS = WIN_INSTALL_SCRIPTS
else:
INSTALL_SCRIPTS = UNIX_INSTALL_SCRIPTS
for filename in INSTALL_SCRIPTS:
filepath = module.get_file(filename)
if filepath:
cmdline = []
for arg in INSTALL_SCRIPTS[filename]:
cmdline.append(arg.format(filepath))
results.append(cmdline)
return results
# Delete files older than 7 days and empty directories
def clean_temp_dir(self, base):
current_time = time()
self.last_clean = current_time
fame_path = os.path.dirname(os.path.abspath(__file__))
if not fame_path in base:
print(
"WARNING: refusing to delete '{}' because it is outside of '{}'.".format(
base, fame_path
)
)
return
for root, dirs, files in os.walk(base, topdown=False):
for f in files:
filepath = os.path.join(root, f)
file_mtime = os.path.getmtime(filepath)
if (current_time - file_mtime) > (7 * 24 * 3600):
try:
os.remove(filepath)
except:
pass
for d in dirs:
dirpath = os.path.join(root, d)
try:
os.rmdir(dirpath)
except:
pass
def run_cleaner(self):
analyses, files = get_old_analyses()
users = get_old_disabled_users()
for analysis in analyses:
print('Cleaner: Deleting analysis {}'.format(analysis['_id']))
analysis.delete()
for f in files:
print('Cleaner: Deleting file {}'.format(f['_id']))
f.delete()
for user in users:
print('Cleaner: Deleting user {}'.format(user['_id']))
user.delete()
def start(self):
try:
self.last_run = time()
self.clean_temp_dir(fame_config.temp_path)
self.update_modules()
self.process = self._new_celery_worker()
while True:
updates = Internals.get(name='updates')
if time() > (self.last_clean + 3600):
self.clean_temp_dir(fame_config.temp_path)
if fame_config.remote:
self.clean_temp_dir(fame_config.storage_path)
if 'updates' in self.queues:
self.run_cleaner()
if updates['last_update'] > self.last_run:
# Stop running worker
os.kill(self.process.pid, signal.SIGTERM)
self.process.wait()
# Update modules if needed
self.update_modules()
# Restart worker
self.process = self._new_celery_worker()
self.last_run = time()
sleep(self.refresh_interval)
except KeyboardInterrupt:
not_finished = True
while not_finished:
try:
self.process.wait()
not_finished = False
except KeyboardInterrupt:
pass
def _new_celery_worker(self):
return Popen(['celery', '-A', 'fame.core.celeryctl', 'worker', '-Q', ','.join(self.queues)] + self.celery_args)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Launches a FAME worker.')
parser.add_argument('queues', metavar='queue', type=str, nargs='*',
help='The task queues that this worker will handle.')
parser.add_argument('-c', '--celery_args', type=str, default='',
help='Additional arguments for the celery worker.')
parser.add_argument('-r', '--refresh_interval', type=int, default=30,
help='Frequency at which the worker will check for updates.')
args = parser.parse_args()
queues = args.queues
# Default queue is 'unix'
if len(queues) == 0:
if sys.platform == 'win32':
queues = ['windows']
else:
queues = ['unix']
# A local worker should also take care of updates
if not fame_config.remote:
queues.append('updates')
fame_init()
Worker(queues, args.celery_args, args.refresh_interval).start()