forked from DIRACGrid/DIRAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_tests.py
executable file
·733 lines (631 loc) · 23.7 KB
/
integration_tests.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#!/usr/bin/env python
import fnmatch
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from typing import Optional
import click
import git
import typer
import yaml
from packaging.version import Version
from typer import colors as c
# Editable configuration
DEFAULT_HOST_OS = "cc7"
DEFAULT_MYSQL_VER = "mysql:8.0"
DEFAULT_ES_VER = "elasticsearch:7.9.1"
FEATURE_VARIABLES = {
"DIRACOSVER": "master",
"DIRACOS_TARBALL_PATH": None,
"TEST_HTTPS": "No",
"DIRAC_FEWER_CFG_LOCKS": None,
}
DEFAULT_MODULES = {
"DIRAC": Path(__file__).parent.absolute(),
}
# Static configuration
DB_USER = "Dirac"
DB_PASSWORD = "Dirac"
DB_ROOTUSER = "root"
DB_ROOTPWD = "password"
DB_HOST = "mysql"
DB_PORT = "3306"
# Implementation details
LOG_LEVEL_MAP = {
"ALWAYS": (c.BLACK, c.WHITE),
"NOTICE": (None, c.MAGENTA),
"INFO": (None, c.GREEN),
"VERBOSE": (None, c.CYAN),
"DEBUG": (None, c.BLUE),
"WARN": (None, c.YELLOW),
"ERROR": (None, c.RED),
"FATAL": (c.RED, c.BLACK),
}
LOG_PATTERN = re.compile(r"^[\d\-]{10} [\d:]{8} UTC [^\s]+ ([A-Z]+):")
class NaturalOrderGroup(click.Group):
"""Group for showing subcommands in the correct order"""
def list_commands(self, ctx):
return self.commands.keys()
app = typer.Typer(
cls=NaturalOrderGroup,
help=f"""Run the DIRAC integration tests.
A local DIRAC setup can be created and tested by running:
\b
./integration_tests.py create
This is equivalent to running:
\b
./integration_tests.py prepare-environment
./integration_tests.py install-server
./integration_tests.py install-client
./integration_tests.py test-server
./integration_tests.py test-client
The test setup can be shutdown using:
\b
./integration_tests.py destroy
See below for additional subcommands which are useful during local development.
## Features
The currently known features and their default values are:
\b
HOST_OS: {DEFAULT_HOST_OS!r}
MYSQL_VER: {DEFAULT_MYSQL_VER!r}
ES_VER: {DEFAULT_ES_VER!r}
{(os.linesep + ' ').join(['%s: %r' % x for x in FEATURE_VARIABLES.items()])}
All features can be prefixed with "SERVER_" or "CLIENT_" to limit their scope.
## Extensions
Integration tests can be ran for extensions to DIRAC by specifying the module
name and path such as:
\b
./integration_tests.py create --extra-module MyDIRAC=/path/to/MyDIRAC
This will modify the setup process based on the contents of
`MyDIRAC/tests/.dirac-ci-config.yaml`. See the Vanilla DIRAC file for the
available options.
## Command completion
Command completion of typer based scripts can be enabled by running:
typer --install-completion
After restarting your terminal you command completion is available using:
typer ./integration_tests.py run ...
""",
)
@app.command()
def create(
flags: Optional[list[str]] = typer.Argument(None),
editable: Optional[bool] = None,
extra_module: Optional[list[str]] = None,
release_var: Optional[str] = None,
run_server_tests: bool = True,
run_client_tests: bool = True,
):
"""Start a local instance of the integration tests"""
prepare_environment(flags, editable, extra_module, release_var)
install_server()
install_client()
exit_code = 0
if run_server_tests:
try:
test_server()
except TestExit as e:
exit_code += e.exit_code
else:
raise NotImplementedError()
if run_client_tests:
try:
test_client()
except TestExit as e:
exit_code += e.exit_code
else:
raise NotImplementedError()
if exit_code != 0:
typer.secho("One or more tests failed", err=True, fg=c.RED)
raise typer.Exit(exit_code)
@app.command()
def destroy():
"""Destroy a local instance of the integration tests"""
typer.secho("Shutting down and removing containers", err=True, fg=c.GREEN)
with _gen_docker_compose(DEFAULT_MODULES) as docker_compose_fn:
os.execvpe(
"docker-compose",
["docker-compose", "-f", docker_compose_fn, "down", "--remove-orphans", "-t", "0"],
_make_env({}),
)
@app.command()
def prepare_environment(
flags: Optional[list[str]] = typer.Argument(None),
editable: Optional[bool] = None,
extra_module: Optional[list[str]] = None,
release_var: Optional[str] = None,
):
"""Prepare the local environment for installing DIRAC."""
_check_containers_running(is_up=False)
if editable is None:
editable = sys.stdout.isatty()
typer.secho(
f"No value passed for --[no-]editable, automatically detected: {editable}",
fg=c.YELLOW,
)
typer.echo(f"Preparing environment")
modules = DEFAULT_MODULES | dict(f.split("=", 1) for f in extra_module)
modules = {k: Path(v).absolute() for k, v in modules.items()}
flags = dict(f.split("=", 1) for f in flags)
docker_compose_env = _make_env(flags)
server_flags = {}
client_flags = {}
for key, value in flags.items():
if key.startswith("SERVER_"):
server_flags[key[len("SERVER_") :]] = value
elif key.startswith("CLIENT_"):
client_flags[key[len("CLIENT_") :]] = value
else:
server_flags[key] = value
client_flags[key] = value
server_config = _make_config(modules, server_flags, release_var, editable)
client_config = _make_config(modules, client_flags, release_var, editable)
typer.secho("Running docker-compose to create containers", fg=c.GREEN)
with _gen_docker_compose(modules) as docker_compose_fn:
subprocess.run(
["docker-compose", "-f", docker_compose_fn, "up", "-d"],
check=True,
env=docker_compose_env,
)
typer.secho("Creating users in server and client containers", fg=c.GREEN)
for container_name in ["server", "client"]:
if os.getuid() == 0:
continue
cmd = _build_docker_cmd(container_name, use_root=True, cwd="/")
gid = str(os.getgid())
uid = str(os.getuid())
ret = subprocess.run(cmd + ["groupadd", "--gid", gid, "dirac"], check=False)
if ret.returncode != 0:
typer.secho(f"Failed to add add group dirac with id={gid}", fg=c.YELLOW)
subprocess.run(
cmd
+ [
"useradd",
"--uid",
uid,
"--gid",
gid,
"-s",
"/bin/bash",
"-d",
"/home/dirac",
"dirac",
],
check=True,
)
subprocess.run(cmd + ["chown", "dirac", "/home/dirac"], check=True)
typer.secho("Creating MySQL user", fg=c.GREEN)
cmd = ["docker", "exec", "mysql", "mysql", f"--password={DB_ROOTPWD}", "-e"]
# It sometimes takes a while for MySQL to be ready so wait for a while if needed
for _ in range(10):
ret = subprocess.run(
cmd + [f"CREATE USER '{DB_USER}'@'%' IDENTIFIED BY '{DB_PASSWORD}';"],
check=False,
)
if ret.returncode != 0:
typer.secho("Failed to connect to MySQL, will retry in 10 seconds", fg=c.YELLOW)
time.sleep(10)
break
else:
raise Exception(ret)
subprocess.run(
cmd + [f"CREATE USER '{DB_USER}'@'localhost' IDENTIFIED BY '{DB_PASSWORD}';"],
check=True,
)
subprocess.run(
cmd + [f"CREATE USER '{DB_USER}'@'mysql' IDENTIFIED BY '{DB_PASSWORD}';"],
check=True,
)
typer.secho("Copying files to containers", fg=c.GREEN)
for name, config in [("server", server_config), ("client", client_config)]:
if path := config.get("DIRACOS_TARBALL_PATH"):
path = Path(path)
config["DIRACOS_TARBALL_PATH"] = f"/{path.name}"
subprocess.run(
["docker", "cp", str(path), f"{name}:/{config['DIRACOS_TARBALL_PATH']}"],
check=True,
)
config_as_shell = _dict_to_shell(config)
typer.secho(f"## {name.title()} config is:", fg=c.BRIGHT_WHITE, bg=c.BLACK)
typer.secho(config_as_shell)
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "CONFIG"
path.write_text(config_as_shell)
subprocess.run(
["docker", "cp", str(path), f"{name}:/home/dirac"],
check=True,
)
for module_name, module_configs in _load_module_configs(modules).items():
for command in module_configs.get("commands", {}).get("post-prepare", []):
typer.secho(
f"Running post-prepare command for {module_name}: {command}",
err=True,
fg=c.GREEN,
)
subprocess.run(command, check=True, shell=True)
@app.command()
def install_server():
"""Install DIRAC in the server container."""
_check_containers_running()
typer.secho("Running server installation", fg=c.GREEN)
base_cmd = _build_docker_cmd("server", tty=False)
subprocess.run(
base_cmd + ["bash", "/home/dirac/LocalRepo/TestCode/DIRAC/tests/CI/install_server.sh"],
check=True,
)
typer.secho("Copying credentials and certificates", fg=c.GREEN)
base_cmd = _build_docker_cmd("client", tty=False)
subprocess.run(
base_cmd
+ [
"mkdir",
"-p",
"/home/dirac/ServerInstallDIR/user",
"/home/dirac/ClientInstallDIR/etc",
"/home/dirac/.globus",
],
check=True,
)
for path in [
"etc/grid-security",
"user/client.pem",
"user/client.key",
f"/tmp/x509up_u{os.getuid()}",
]:
source = os.path.join("/home/dirac/ServerInstallDIR", path)
ret = subprocess.run(
["docker", "cp", f"server:{source}", "-"],
check=True,
text=False,
stdout=subprocess.PIPE,
)
if path.startswith("user/"):
dest = f"client:/home/dirac/ServerInstallDIR/{os.path.dirname(path)}"
elif path.startswith("/"):
dest = f"client:{os.path.dirname(path)}"
else:
dest = f"client:/home/dirac/ClientInstallDIR/{os.path.dirname(path)}"
subprocess.run(["docker", "cp", "-", dest], check=True, text=False, input=ret.stdout)
subprocess.run(
base_cmd
+ [
"bash",
"-c",
"cp /home/dirac/ServerInstallDIR/user/client.* /home/dirac/.globus/",
],
check=True,
)
@app.command()
def install_client():
"""Install DIRAC in the client container."""
_check_containers_running()
typer.secho("Running client installation", fg=c.GREEN)
base_cmd = _build_docker_cmd("client")
subprocess.run(
base_cmd + ["bash", "/home/dirac/LocalRepo/TestCode/DIRAC/tests/CI/install_client.sh"],
check=True,
)
@app.command()
def test_server():
"""Run the server integration tests."""
_check_containers_running()
typer.secho("Running server tests", err=True, fg=c.GREEN)
base_cmd = _build_docker_cmd("server")
ret = subprocess.run(base_cmd + ["bash", "TestCode/DIRAC/tests/CI/run_tests.sh"], check=False)
color = c.GREEN if ret.returncode == 0 else c.RED
typer.secho(f"Server tests finished with {ret.returncode}", err=True, fg=color)
raise TestExit(ret.returncode)
@app.command()
def test_client():
"""Run the client integration tests."""
_check_containers_running()
typer.secho("Running client tests", err=True, fg=c.GREEN)
base_cmd = _build_docker_cmd("client")
ret = subprocess.run(base_cmd + ["bash", "TestCode/DIRAC/tests/CI/run_tests.sh"], check=False)
color = c.GREEN if ret.returncode == 0 else c.RED
typer.secho(f"Client tests finished with {ret.returncode}", err=True, fg=color)
raise TestExit(ret.returncode)
@app.command()
def exec_server():
"""Start an interactive session in the server container."""
_check_containers_running()
cmd = _build_docker_cmd("server")
cmd += [
"bash",
"-c",
". $HOME/CONFIG && . $HOME/ServerInstallDIR/bashrc && exec bash",
]
typer.secho("Opening prompt inside server container", err=True, fg=c.GREEN)
os.execvp(cmd[0], cmd)
@app.command()
def exec_client():
"""Start an interactive session in the client container."""
_check_containers_running()
cmd = _build_docker_cmd("client")
cmd += [
"bash",
"-c",
". $HOME/CONFIG && . $HOME/ClientInstallDIR/bashrc && exec bash",
]
typer.secho("Opening prompt inside client container", err=True, fg=c.GREEN)
os.execvp(cmd[0], cmd)
@app.command()
def exec_mysql():
"""Start an interactive session in the server container."""
_check_containers_running()
cmd = _build_docker_cmd("mysql", use_root=True, cwd="/")
cmd += [
"bash",
"-c",
f"exec mysql --user={DB_USER} --password={DB_PASSWORD}",
]
typer.secho("Opening prompt inside server container", err=True, fg=c.GREEN)
os.execvp(cmd[0], cmd)
@app.command()
def list_services():
"""List the services which have been running.
Only the services for which /log/current exists are shown.
"""
_check_containers_running()
typer.secho("Known services:", err=True)
for service in _list_services()[1]:
typer.secho(f"* {service}", err=True)
@app.command()
def runsvctrl(command: str, pattern: str):
"""Execute runsvctrl inside the server container."""
_check_containers_running()
runit_dir, services = _list_services()
cmd = _build_docker_cmd("server", cwd=runit_dir)
services = fnmatch.filter(services, pattern)
if not services:
typer.secho(f"No services match {pattern!r}", fg=c.RED)
raise typer.Exit(code=1)
cmd += ["runsvctrl", command] + services
os.execvp(cmd[0], cmd)
@app.command()
def logs(pattern: str = "*", lines: int = 10, follow: bool = True):
"""Show DIRAC's logs from the service container.
For services matching [--pattern] show the most recent [--lines] from the
logs. If [--follow] is True, continiously stream the logs.
"""
_check_containers_running()
runit_dir, services = _list_services()
base_cmd = _build_docker_cmd("server", tty=False) + ["tail"]
base_cmd += [f"--lines={lines}"]
if follow:
base_cmd += ["-f"]
with ThreadPoolExecutor(len(services)) as pool:
for service in fnmatch.filter(services, pattern):
cmd = base_cmd + [f"{runit_dir}/{service}/log/current"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=None, text=True)
pool.submit(_log_popen_stdout, p)
class TestExit(typer.Exit):
pass
@contextmanager
def _gen_docker_compose(modules):
# Load the docker-compose configuration and mount the necessary volumes
input_fn = Path(__file__).parent / "tests/CI/docker-compose.yml"
docker_compose = yaml.safe_load(input_fn.read_text())
volumes = [f"{path}:/home/dirac/LocalRepo/ALTERNATIVE_MODULES/{name}" for name, path in modules.items()]
volumes += [f"{path}:/home/dirac/LocalRepo/TestCode/{name}" for name, path in modules.items()]
docker_compose["services"]["dirac-server"]["volumes"] = volumes[:]
docker_compose["services"]["dirac-client"]["volumes"] = volumes[:]
# Add any extension services
for module_name, module_configs in _load_module_configs(modules).items():
for service_name, service_config in module_configs["extra-services"].items():
typer.secho(f"Adding service {service_name} for {module_name}", err=True, fg=c.GREEN)
docker_compose["services"][service_name] = service_config.copy()
docker_compose["services"][service_name]["volumes"] = volumes[:]
# Write to a tempory file with the appropriate profile name
prefix = "ci"
with tempfile.TemporaryDirectory() as tmpdir:
input_docker_compose_dir = Path(__file__).parent / "tests/CI/"
output_fn = Path(tmpdir) / prefix / "docker-compose.yml"
output_fn.parent.mkdir()
output_fn.write_text(yaml.safe_dump(docker_compose, sort_keys=False))
shutil.copytree(input_docker_compose_dir / "envs", str(Path(tmpdir) / prefix), dirs_exist_ok=True)
yield output_fn
def _check_containers_running(*, is_up=True):
with _gen_docker_compose(DEFAULT_MODULES) as docker_compose_fn:
running_containers = subprocess.run(
["docker-compose", "-f", docker_compose_fn, "ps", "-q", "-a"],
stdout=subprocess.PIPE,
env=_make_env({}),
check=True,
text=True,
).stdout.split("\n")
if is_up:
if not any(running_containers):
typer.secho(
f"No running containers found, environment must be prepared first!",
err=True,
fg=c.RED,
)
raise typer.Exit(code=1)
else:
if any(running_containers):
typer.secho(
f"Running instance already found, it must be destroyed first!",
err=True,
fg=c.RED,
)
raise typer.Exit(code=1)
def _find_dirac_release_and_branch():
# Start by looking for the GitHub/GitLab environment variables
ref = os.environ.get("CI_COMMIT_REF_NAME", os.environ.get("GITHUB_REF"))
if ref == "refs/heads/integration":
return "integration", ""
ref = os.environ.get("CI_MERGE_REQUEST_TARGET_BRANCH_NAME", os.environ.get("GITHUB_BASE_REF"))
if ref == "integration":
return "integration", ""
repo = git.Repo(os.getcwd())
# Try to make sure the upstream remote is up to date
try:
upstream = repo.remote("upstream")
except ValueError:
typer.secho("No upstream remote found, adding", err=True, fg=c.YELLOW)
upstream = repo.create_remote("upstream", "https://github.com/DIRACGrid/DIRAC.git")
try:
upstream.fetch()
except Exception:
typer.secho("Failed to fetch from remote 'upstream'", err=True, fg=c.YELLOW)
# Find the most recent tag on the current branch
version = Version(
repo.git.describe(
dirty=True,
tags=True,
long=True,
match="*[0-9]*",
exclude=["v[0-9]r*", "v[0-9][0-9]r*"],
).split("-")[0]
)
# See if there is a remote branch named "rel-vXrY"
version_branch = f"rel-v{version.major}r{version.minor}"
try:
upstream.refs[version_branch]
except IndexError:
typer.secho(
f"Failed to find branch for {version_branch}, defaulting to integration",
err=True,
fg=c.YELLOW,
)
return "integration", ""
else:
return "", f"v{version.major}r{version.minor}"
def _make_env(flags):
env = os.environ.copy()
env["DIRAC_UID"] = str(os.getuid())
env["DIRAC_GID"] = str(os.getgid())
env["HOST_OS"] = flags.pop("HOST_OS", DEFAULT_HOST_OS)
env["CI_REGISTRY_IMAGE"] = flags.pop("CI_REGISTRY_IMAGE", "diracgrid")
env["MYSQL_VER"] = flags.pop("MYSQL_VER", DEFAULT_MYSQL_VER)
env["ES_VER"] = flags.pop("ES_VER", DEFAULT_ES_VER)
return env
def _dict_to_shell(variables):
lines = []
for name, value in variables.items():
if value is None:
continue
elif isinstance(value, list):
lines += [f"declare -a {name}"]
lines += [f"{name}+=({shlex.quote(v)})" for v in value]
elif isinstance(value, bool):
lines += [f"export {name}={'Yes' if value else 'No'}"]
elif isinstance(value, str):
lines += [f"export {name}={shlex.quote(value)}"]
else:
raise NotImplementedError(name, value, type(value))
return "\n".join(lines)
def _make_config(modules, flags, release_var, editable):
config = {
"DEBUG": "True",
# MYSQL Settings
"DB_USER": DB_USER,
"DB_PASSWORD": DB_PASSWORD,
"DB_ROOTUSER": DB_ROOTUSER,
"DB_ROOTPWD": DB_ROOTPWD,
"DB_HOST": DB_HOST,
"DB_PORT": DB_PORT,
# ElasticSearch settings
"NoSQLDB_HOST": "elasticsearch",
"NoSQLDB_PORT": "9200",
# Hostnames
"SERVER_HOST": "server",
"CLIENT_HOST": "client",
# Test specific variables
"WORKSPACE": "/home/dirac",
}
if editable:
config["PIP_INSTALL_EXTRA_ARGS"] = "-e"
required_feature_flags = []
for module_name, module_ci_config in _load_module_configs(modules).items():
config |= module_ci_config["config"]
required_feature_flags += module_ci_config.get("required-feature-flags", [])
config["DIRAC_CI_SETUP_SCRIPT"] = "/home/dirac/LocalRepo/TestCode/" + config["DIRAC_CI_SETUP_SCRIPT"]
# This can likely be removed after the Python 3 migration
if release_var:
config |= dict([release_var.split("=", 1)])
else:
config["DIRAC_RELEASE"], config["DIRACBRANCH"] = _find_dirac_release_and_branch()
for key, default_value in FEATURE_VARIABLES.items():
config[key] = flags.pop(key, default_value)
for key in required_feature_flags:
try:
config[key] = flags.pop(key)
except KeyError:
typer.secho(f"Required feature variable {key!r} is missing", err=True, fg=c.RED)
raise typer.Exit(code=1)
config["TESTREPO"] = [f"/home/dirac/LocalRepo/TestCode/{name}" for name in modules]
config["ALTERNATIVE_MODULES"] = [f"/home/dirac/LocalRepo/ALTERNATIVE_MODULES/{name}" for name in modules]
# Exit with an error if there are unused feature flags remaining
if flags:
typer.secho(f"Unrecognised feature flags {flags!r}", err=True, fg=c.RED)
raise typer.Exit(code=1)
return config
def _load_module_configs(modules):
module_ci_configs = {}
for module_name, module_path in modules.items():
module_ci_config_path = module_path / "tests/.dirac-ci-config.yaml"
if not module_ci_config_path.exists():
continue
module_ci_configs[module_name] = yaml.safe_load(module_ci_config_path.read_text())
return module_ci_configs
def _build_docker_cmd(container_name, *, use_root=False, cwd="/home/dirac", tty=True):
if use_root or os.getuid() == 0:
user = "root"
else:
user = "dirac"
cmd = ["docker", "exec"]
if tty:
if sys.stdout.isatty():
cmd += ["-it"]
else:
typer.secho(
'Not passing "-it" to docker as stdout is not a tty',
err=True,
fg=c.YELLOW,
)
cmd += [
"-e=TERM=xterm-color",
"-e=INSTALLROOT=/home/dirac",
f"-e=INSTALLTYPE={container_name}",
f"-u={user}",
f"-w={cwd}",
container_name,
]
return cmd
def _list_services():
# The Python 3 runit dir ends up in /diracos
for runit_dir in ["ServerInstallDIR/runit", "ServerInstallDIR/diracos/runit"]:
cmd = _build_docker_cmd("server")
cmd += [
"bash",
"-c",
f'cd {runit_dir}/ && for fn in */*/log/current; do echo "$(dirname "$(dirname "$fn")")"; done',
]
ret = subprocess.run(cmd, check=False, stdout=subprocess.PIPE, text=True)
if not ret.returncode:
return runit_dir, ret.stdout.split()
else:
typer.secho("Failed to find list of available services", err=True, fg=c.RED)
typer.secho(f"stdout was: {ret.stdout!r}", err=True)
typer.secho(f"stderr was: {ret.stderr!r}", err=True)
raise typer.Exit(1)
def _log_popen_stdout(p):
while p.poll() is None:
line = p.stdout.readline().rstrip()
if not line:
continue
bg, fg = None, None
if match := LOG_PATTERN.match(line):
bg, fg = LOG_LEVEL_MAP.get(match.groups()[0], (bg, fg))
typer.secho(line, err=True, bg=bg, fg=fg)
if __name__ == "__main__":
app()