forked from fkie-cad/FACT_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_fact_frontend.py
executable file
·93 lines (70 loc) · 2.77 KB
/
start_fact_frontend.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
#! /usr/bin/env python3
'''
Firmware Analysis and Comparison Tool (FACT)
Copyright (C) 2015-2021 Fraunhofer FKIE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
'''
import logging
import pickle
import sys
import tempfile
from shlex import split
from subprocess import Popen, TimeoutExpired
from common_helper_process import execute_shell_command
from fact_base import FactBase
from helperFunctions.config import get_config_dir
from helperFunctions.fileSystem import get_src_dir
from install.frontend import COMPOSE_VENV
DOCKER_COMPOSE = COMPOSE_VENV / 'bin' / 'docker-compose'
COMPOSE_YAML = f'{get_src_dir()}/install/radare/docker-compose.yml'
class UwsgiServer:
def __init__(self, config_path: str = None):
self.config_path = config_path
self.process = None
def start(self):
config_parameter = f' --pyargv {self.config_path}' if self.config_path else ''
command = f'uwsgi --thunder-lock --ini {get_config_dir()}/uwsgi_config.ini{config_parameter}'
self.process = Popen(split(command), cwd=get_src_dir())
def shutdown(self):
if self.process:
try:
self.process.wait(timeout=30)
except TimeoutExpired:
logging.error('frontend did not stop in time -> kill')
self.process.kill()
def start_docker():
execute_shell_command(f'{DOCKER_COMPOSE} -f {COMPOSE_YAML} up -d')
def stop_docker():
execute_shell_command(f'{DOCKER_COMPOSE} -f {COMPOSE_YAML} down')
class FactFrontend(FactBase):
PROGRAM_NAME = 'FACT Frontend'
PROGRAM_DESCRIPTION = 'Firmware Analysis and Compare Tool Frontend'
COMPONENT = 'frontend'
def __init__(self):
super().__init__()
self.server = None
start_docker()
def main(self):
with tempfile.NamedTemporaryFile() as fp:
fp.write(pickle.dumps(self.args))
fp.flush()
self.server = UwsgiServer(fp.name)
self.server.start()
super().main()
def shutdown(self):
super().shutdown()
if self.server:
self.server.shutdown()
stop_docker()
if __name__ == '__main__':
FactFrontend().main()
sys.exit()