-
Notifications
You must be signed in to change notification settings - Fork 1
/
prodigy_model.py
229 lines (171 loc) · 5.96 KB
/
prodigy_model.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
import json
import os
import shutil
import socket
import time
import zipfile
from datetime import datetime
import psutil
from flask import abort
from psutil import NoSuchProcess
from prodigy_constants import *
__all__ = [
'port_used',
'check_dir_exists', 'check_file_exists',
'prodigy_config_fn', 'prodigy_sys_fn', 'prodigy_pid_fn',
'get_work_dir_or_none', 'get_work_dir_or_404', 'get_pid_or_clean',
'copy_config_safe', 'read_config_or_404', 'read_config_or_default',
'write_config_or_404', 'write_config_or_raise',
'iter_prodigy_services', 'zip_prodigy_instance',
'cleanup_temp_dir'
]
def port_used(port: int) -> bool:
"""
Return True if the port is used.
:param port: Port to test
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('127.0.0.1', port))
used = False
except OSError:
used = True
s.close()
return used
def check_file_exists(fn):
return os.path.exists(fn) and os.path.isfile(fn)
def check_dir_exists(fn):
return os.path.exists(fn) and os.path.isdir(fn)
def prodigy_config_fn(work_dir):
return os.path.join(work_dir, PRODIGY_CONFIG_JSON)
def prodigy_sys_fn(work_dir):
return os.path.join(work_dir, PRODIGY_SYS_JSON)
def prodigy_pid_fn(work_dir):
return os.path.join(work_dir, PRODIGY_PID_FILE)
def get_work_dir_or_none(prodigy_id):
true_path = os.path.join(PRODIGY_INSTANCES_DIR, prodigy_id)
if not check_dir_exists(true_path):
return None
return true_path
def get_work_dir_or_404(prodigy_id):
true_path = get_work_dir_or_none(prodigy_id)
if true_path is None:
return abort(404)
return true_path
def get_pid_or_clean(pid_fn_or_prodigy_id: str) -> [None, int]:
"""Get pid or clean up the pid file"""
if len(pid_fn_or_prodigy_id.split(os.sep)) > 1:
pid_fn = pid_fn_or_prodigy_id
else:
pid_fn = prodigy_pid_fn(pid_fn_or_prodigy_id)
if os.path.exists(pid_fn):
with open(pid_fn) as f:
pid = int(f.read())
try:
process = psutil.Process(pid)
if process.status() == psutil.STATUS_ZOMBIE:
# uwsgi will do this for us
# process.wait()
raise NoSuchProcess('Zombie')
return pid
except NoSuchProcess:
os.unlink(pid_fn)
return None
def copy_config_safe(config):
return {
'uuid': str(config['uuid']),
'name': str(config['name']),
'db_collection': str(config['db_collection']),
'arguments': str(config['arguments']),
'work_dir': str(config['work_dir']),
'share': [
{'to': str(x['to']), 'id': str(x['id']), 'email': str(x['email'])}
for x in config.get('share', [])
]
}
def read_config_or_default(prodigy_id_or_work_dir, default=None):
if len(prodigy_id_or_work_dir.split(os.sep)) > 1:
work_dir = prodigy_id_or_work_dir
else:
work_dir = get_work_dir_or_none(prodigy_id_or_work_dir)
if work_dir is None:
return default
config_fn = prodigy_config_fn(work_dir)
if not check_file_exists(config_fn):
return default
with open(config_fn) as f:
config = json.load(f)
# Make a copy to avoid arbitrary code execution
try:
return copy_config_safe(config)
except KeyError:
return default
def read_config_or_404(prodigy_id):
config = read_config_or_default(prodigy_id, default=None)
if config is None:
return abort(404)
return config
def write_config_or_raise(prodigy_id, config):
true_path = get_work_dir_or_none(prodigy_id)
config_fn = prodigy_config_fn(true_path)
if check_dir_exists(config_fn):
shutil.rmtree(config_fn)
with open(config_fn, 'w') as f:
# Make a copy to avoid arbitrary code execution
json.dump(copy_config_safe(config), f)
def write_config_or_404(prodigy_id, config):
true_path = get_work_dir_or_404(prodigy_id)
config_fn = prodigy_config_fn(true_path)
if check_dir_exists(config_fn):
shutil.rmtree(config_fn)
with open(config_fn, 'w') as f:
# Make a copy to avoid arbitrary code execution
json.dump(copy_config_safe(config), f)
def iter_prodigy_services():
for i in os.listdir(PRODIGY_INSTANCES_DIR):
work_dir = get_work_dir_or_none(i)
if work_dir is None:
continue
config_fn = prodigy_config_fn(work_dir)
if not os.path.exists(config_fn):
continue
pid_fn = prodigy_pid_fn(work_dir)
pid = get_pid_or_clean(pid_fn)
alive = pid is not None
listening = False
if alive:
try:
sys_fn = prodigy_sys_fn(work_dir)
with open(sys_fn) as f:
port = int(json.load(f)['port'])
listening = port_used(port)
except FileNotFoundError:
pass
yield {
'name': i,
'work_dir': work_dir,
'alive': alive,
'listening': listening,
'pid': pid}
def zip_prodigy_instance(service_id, work_dir):
fn = '%s_%s.zip' % (service_id, datetime.now().strftime('%Y%m%d_%H%M%S'))
zip_file_path = os.path.join(TEMP_DIR, fn)
zip_file = zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(work_dir):
for file in files:
if root == work_dir and file in PRODIGY_SYS_FILES:
continue
this_fn = os.path.join(root, file)
zip_file.write(
this_fn,
arcname=os.path.relpath(this_fn, PRODIGY_INSTANCES_DIR))
zip_file.close()
return zip_file_path
def cleanup_temp_dir():
for i in os.listdir(TEMP_DIR):
filename = os.path.join(TEMP_DIR, i)
mtime = os.stat(filename).st_mtime
if mtime < time.time() - 3600:
# Remove files older than 1 hour
os.unlink(filename)