-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
387 lines (325 loc) · 14.3 KB
/
manage.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
"""
CLI to install the backend and manage plugins and data entries
"""
import grp
import pwd
import sys
import os
import json
import subprocess
import getpass
import argparse
import redis
from typing import Dict, Callable, Sequence, Iterable, Tuple
from autotest_backend.config import config
from autotest_backend import run_test_command
REDIS_CONNECTION = redis.Redis.from_url(config["redis_url"], decode_responses=True)
def _schema() -> Dict:
"""
Return a dictionary representation of the json loaded from the schema_skeleton.json file or from the redis
database if it exists.
"""
schema = REDIS_CONNECTION.get("autotest:schema")
if schema:
return json.loads(schema)
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "schema_skeleton.json")) as f:
return json.load(f)
def _print(*args_, **kwargs) -> None:
"""
Exactly the same as the builtin print function but prepends "[AUTOTESTER]"
"""
print("[AUTOTESTER]", *args_, **kwargs)
class _Manager:
"""
Abstract Manager class used to manage resources
"""
args: argparse.Namespace
def __init__(self, args):
self.args = args
def parse_args() -> Callable:
"""
Parses command line arguments using the argparse module and returns a function to call to
execute the requested command.
"""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="manager")
subparsers.add_parser("tester", help="testers", description="testers")
subparsers.add_parser("plugin", help="plugins", description="plugins")
subparsers.add_parser("data", help="data", description="data")
for name, parser_ in subparsers.choices.items():
subsubparser = parser_.add_subparsers(dest="action")
install_parser = subsubparser.add_parser("install", help=f"install {parser_.description}")
if name == "data":
install_parser.add_argument("name", help="unique name to give the data")
install_parser.add_argument("path", help="path to a file or directory on disk that contains the data")
else:
install_parser.add_argument("paths", nargs="+")
remove_parser = subsubparser.add_parser("remove", help=f"remove {parser_.description}")
remove_parser.add_argument("names", nargs="+")
subsubparser.add_parser("list", help=f"list {parser_.description}")
subsubparser.add_parser("clean", help=f"remove {parser_.description} that have been deleted on disk.")
subparsers.add_parser("install", help="install backend")
managers = {"install": BackendManager, "tester": TesterManager, "plugin": PluginManager, "data": DataManager}
args = parser.parse_args()
if args.manager == "install":
args.action = "install"
return getattr(managers[args.manager](args), args.action)
class PluginManager(_Manager):
"""
Manger for plugins
"""
def install(self) -> None:
"""
Install plugins
"""
schema = _schema()
for path in self.args.paths:
cli = os.path.join(path, "classic.cli")
if os.path.isfile(cli):
proc = subprocess.run([cli, "settings"], capture_output=True, check=False, universal_newlines=True)
if proc.returncode:
_print(
f"Plugin settings could not be retrieved from plugin at {path}. Failed with:\n{proc.stderr}",
file=sys.stderr,
flush=True,
)
continue
settings = json.loads(proc.stdout)
plugin_name = list(settings.keys())[0]
installed_plugins = schema["definitions"]["plugins"]["properties"]
if plugin_name in installed_plugins:
_print(f"A plugin named {plugin_name} is already installed", file=sys.stderr, flush=True)
continue
proc = subprocess.run([cli, "install"], capture_output=True, check=False, universal_newlines=True)
if proc.returncode:
_print(f"Plugin installation at {path} failed with:\n{proc.stderr}", file=sys.stderr, flush=True)
continue
installed_plugins.update(settings)
REDIS_CONNECTION.set(f"autotest:plugin:{plugin_name}", path)
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
def remove(self, additional: Sequence = tuple()):
"""
Removes installed plugins specified in self.args. Additional plugins to remove can be specified
with the additional keyword
"""
schema = _schema()
installed_plugins = schema["definitions"]["plugins"]["properties"]
for name in self.args.names + list(additional):
REDIS_CONNECTION.delete(f"autotest:plugin:{name}")
if name in installed_plugins:
installed_plugins.remove(name)
try:
installed_plugins.pop(name)
except KeyError:
continue
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
@staticmethod
def _get_installed() -> Iterable[Tuple[str, str]]:
"""
Yield the name and path of all installed plugins
"""
for plugin_key in REDIS_CONNECTION.keys("autotest:tuple:*"):
plugin_name = plugin_key.split(":")[-1]
path = REDIS_CONNECTION.get(plugin_key)
yield plugin_name, path
def list(self) -> None:
"""
Print the name and path of all installed plugins
"""
for plugin_name, path in self._get_installed():
print(f"{plugin_name} @ {path}")
def clean(self) -> None:
"""
Remove all plugins that are installed but whose data has been removed from disk
"""
to_remove = [plugin_name for plugin_name, path in self._get_installed() if not os.path.isdir(path)]
_print("Removing the following plugins:", *to_remove, sep="\t\n")
self.remove(additional=to_remove)
class TesterManager(_Manager):
"""
Manager for testers
"""
def install(self) -> None:
"""
Install testers
"""
schema = _schema()
for path in self.args.paths:
cli = os.path.join(path, "classic.cli")
if os.path.isfile(cli):
proc = subprocess.run([cli, "settings"], capture_output=True, check=False, universal_newlines=True)
if proc.returncode:
_print(
f"Tester settings could not be retrieved from tester at {path}. Failed with:\n{proc.stderr}",
file=sys.stderr,
flush=True,
)
continue
settings = json.loads(proc.stdout)
tester_name = settings["properties"]["tester_type"]["const"]
installed_testers = schema["definitions"]["installed_testers"]["enum"]
if tester_name in installed_testers:
_print(f"A tester named {tester_name} is already installed", file=sys.stderr, flush=True)
continue
proc = subprocess.run([cli, "install"], capture_output=True, check=False, universal_newlines=True)
if proc.returncode:
_print(f"Tester installation at {path} failed with:\n{proc.stderr}", file=sys.stderr, flush=True)
continue
installed_testers.append(tester_name)
schema["definitions"]["tester_schemas"]["oneOf"].append(settings)
REDIS_CONNECTION.set(f"autotest:tester:{tester_name}", path)
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
def remove(self, additional: Sequence = tuple()) -> None:
"""
Removes installed testers specified in self.args. Additional testers to remove can be specified
with the additional keyword
"""
schema = _schema()
tester_settings = schema["definitions"]["tester_schemas"]["oneOf"]
installed_testers = schema["definitions"]["installed_testers"]["enum"]
for name in self.args.names + list(additional):
REDIS_CONNECTION.delete(f"autotest:tester:{name}")
if name in installed_testers:
installed_testers.remove(name)
for i, settings in enumerate(tester_settings):
if name == settings["properties"]["tester_type"]["const"]:
tester_settings.pop(i)
break
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
@staticmethod
def _get_installed() -> Iterable[Tuple[str, str]]:
"""
Yield the name and path of all installed testers
"""
for tester_key in REDIS_CONNECTION.keys("autotest:tester:*"):
tester_name = tester_key.split(":")[-1]
path = REDIS_CONNECTION.get(tester_key)
yield tester_name, path
def list(self) -> None:
"""
Print the name and path of all installed testers
"""
for tester_name, path in self._get_installed():
print(f"{tester_name} @ {path}")
def clean(self) -> None:
"""
Remove all testers that are installed but whose data has been removed from disk
"""
to_remove = [tester_name for tester_name, path in self._get_installed() if not os.path.isdir(path)]
_print("Removing the following testers:", *to_remove, sep="\t\n")
self.remove(additional=to_remove)
class DataManager(_Manager):
"""
Manager for data entries
"""
def install(self) -> None:
"""
Install a data entry
"""
schema = _schema()
# the data_entries key is used to be consistent with the autotest-backend-docker project
installed_volumes = schema["definitions"]["data_entries"]["items"]["enum"]
name = self.args.name
path = os.path.abspath(self.args.path)
if name in installed_volumes:
_print(f"A data mapping named {name} is already installed", file=sys.stderr, flush=True)
return
if not os.path.exists(path):
_print(f"No file or directory can be found at {path}", file=sys.stderr, flush=True)
return
installed_volumes.append(name)
REDIS_CONNECTION.set(f"autotest:data:{name}", path)
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
def remove(self, additional: Sequence = tuple()) -> None:
"""
Removes installed data entries specified in self.args. Additional entries to remove can be specified
with the additional keyword
"""
schema = _schema()
installed_volumes = schema["definitions"]["data_entries"]["items"]["enum"]
for name in self.args.names + list(additional):
installed_volumes.remove(name)
REDIS_CONNECTION.delete(f"autotest:data:{name}")
REDIS_CONNECTION.set("autotest:schema", json.dumps(schema))
@staticmethod
def _get_installed() -> Iterable[Tuple[str, str]]:
"""
Yield the name and path of all installed data entries
"""
for data_key in REDIS_CONNECTION.keys("autotest:data:*"):
data_name = data_key.split(":")[-1]
path = REDIS_CONNECTION.get(data_key)
yield data_name, path
def list(self) -> None:
"""
Print the name and path of all installed entries
"""
for data_name, path in self._get_installed():
print(f"{data_name} @ {path}")
def clean(self) -> None:
"""
Remove all data entries that are installed but whose data has been removed from disk
"""
to_remove = [data_name for data_name, path in self._get_installed() if not os.path.isdir(path)]
_print("Removing the following data mappings:", *to_remove, sep="\t\n")
self.remove(additional=to_remove)
class BackendManager(_Manager):
"""
Manager for the autotest backend
"""
@staticmethod
def _check_dependencies() -> None:
"""
Check if all dependencies are installed and accessible
"""
_print("checking if redis url is valid:")
try:
REDIS_CONNECTION.keys()
except Exception as e:
raise Exception(f'Cannot connect to redis database with url: {config["redis_url"]}') from e
@staticmethod
def _check_users_exist() -> None:
"""
Checks that all users specified in the configuration files:
- exist
- can be sudo'd to from the current user (ex: "sudo -u other_user -- some command")
- has a primary group that the current user belongs to as well
"""
groups = {grp.getgrgid(g).gr_name for g in os.getgroups()}
for w in config["workers"]:
username = w["user"]
_print(f"checking if worker with username {username} exists")
try:
pwd.getpwnam(username)
except KeyError:
raise Exception(f"user with username {username} does not exist")
_print(
f"checking if worker with username {username} can be accessed by the current user {getpass.getuser()}"
)
try:
subprocess.run(
run_test_command(username).format("echo", "test"), stdout=subprocess.DEVNULL, shell=True, check=True
)
except Exception as e:
raise Exception(f"user {getpass.getuser()} cannot run commands as the {username} user") from e
_print(f"checking if the current user belongs to the {username} group")
if username not in groups:
raise Exception(f"user {getpass.getuser()} does not belong to group: {username}")
@staticmethod
def _create_workspace() -> None:
"""
Creates the workspace directory on disk if it doesn't exist already
"""
_print(f'creating workspace at {config["workspace"]}')
os.makedirs(config["workspace"], exist_ok=True)
def install(self) -> None:
"""
Check that the server is set up properly and create the workspace.
"""
self._check_dependencies()
self._check_users_exist()
self._create_workspace()
REDIS_CONNECTION.set("autotest:schema", json.dumps(_schema()))
if __name__ == "__main__":
parse_args()()