-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathrun
More file actions
executable file
·121 lines (91 loc) · 2.74 KB
/
run
File metadata and controls
executable file
·121 lines (91 loc) · 2.74 KB
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
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# This script is designed to alias common docker-compose commands used for
# running tests, development, etc. Any complicated processing should
# be done inside the container in a proper command line application
# rather than this script.
import getpass
import grp
import os
import pwd
import sys
from typing import (
Tuple,
)
ENV_FILE_TEMPLATE = """
UID={uid}
GID={gid}
DOCKER_GID={docker_gid}
""".strip()
def get_user_uid_gid(user: str) -> Tuple[int, int]:
"""Get the uid and gid for the specified user."""
try:
pw_entry = pwd.getpwnam(user)
return pw_entry.pw_uid, pw_entry.pw_gid
except KeyError:
sys.exit(f"Couldn't find user `{user}`")
def get_gid(group: str) -> int:
"""Get the gid for the specified group."""
try:
return grp.getgrnam(group).gr_gid
except KeyError:
sys.exit(f"Couldn't find group `{group}`")
SYNC_GROUPS = ["deploy", "dev", "test"]
SYNC_PYTHON = "3.9"
def env_cmd(args):
user = getpass.getuser()
uid, gid = get_user_uid_gid(user)
docker_gid = get_gid("docker")
env_content = ENV_FILE_TEMPLATE.format(
uid=uid,
gid=gid,
docker_gid=docker_gid,
)
print(env_content)
return 0
def sync_cmd(args):
if not args or args[0] not in SYNC_GROUPS:
print("./run sync [group]\n")
print("Create a local dev environment via `uv sync`.\n")
print("Available groups:")
for group in SYNC_GROUPS:
print(f"\t{group}")
return 1
group = args[0]
cmd = ["uv", "sync", "--only-group", group, "--python", SYNC_PYTHON]
print("$ {}".format(" ".join(cmd)))
os.execvp(cmd[0], cmd)
COMMANDS = {
"build": ("docker", "compose", "build"),
"env": env_cmd,
"sync": sync_cmd,
"hgmo": (
"docker",
"compose",
"run",
"-e",
"TESTNAME=test",
"--rm",
"test-runner",
"/app/vct/hgmo",
),
"shell": ("docker", "compose", "run", "--rm", "test-runner", "/bin/bash"),
"tests": ("docker", "compose", "run", "--rm", "test-runner", "/app/vct/run-tests"),
}
def main(args):
if not args or args[0] not in COMMANDS:
print("./run [command]\n")
print("Possible Commands:")
for cmd in COMMANDS:
print(f"\t{cmd}")
return 1
cmd = COMMANDS[args[0]]
if callable(cmd):
return cmd(args[1:])
cmd = list(cmd) + args[1:]
print("$ {}".format(" ".join(cmd)))
os.execvp(cmd[0], cmd)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))