-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·73 lines (60 loc) · 2.38 KB
/
run.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
#!/usr/bin/env scriptisto
# scriptisto-begin
# script_src: script.py
# build_once_cmd: virtualenv -p python3 . && . ./bin/activate && pip install mypy click sh
# build_cmd: . ./bin/activate && mypy script.py && python3 -m compileall . && chmod +x ./run.sh
# target_bin: ./run.sh
# files:
# - path: run.sh
# content: |
# #!/bin/sh
# export DIR=$(dirname $0)
# . $DIR/bin/activate
# python3 $DIR/script.py $@
# scriptisto-end
"""
This script is used to run docker-compose commands.
ref:
- https://martinheinz.dev/blog/98
"""
import click
import sh # type: ignore
import os
git = sh.Command("git")
class ComposeCmd:
def __init__(self, verb: str = "up", dryrun: bool = False, dev: bool = False, detach: bool = False):
self.compose_files = [
"Deployment/compose.yml",
"Deployment/compose.tunnel.yml"
]
if dev:
self.compose_files.append("Deployment/compose.dev.yml")
self.verb = verb
self.verb_additional_flags = {
"up": "--build --remove-orphans "
}
if dev:
self.verb_additional_flags["up"] += "--watch "
if detach:
self.verb_additional_flags["up"] += "--detach "
self.dryrun = dryrun
def run(self):
click.echo(click.style(f"Running docker-compose {self.verb}...", fg="green"))
# cd to root project directory(git root dir)
git_root = git("rev-parse", "--show-toplevel").strip()
os.chdir(git_root)
compose_files_cmdpart = " ".join([f"-f {file}" for file in self.compose_files])
final_cmd = f"docker-compose -p gebwai {compose_files_cmdpart} --project-directory . {self.verb} {self.verb_additional_flags.get(self.verb, '')}"
click.echo(click.style(f"Running command: {final_cmd}\ncwd: {os.getcwd()}", fg="yellow"))
if not self.dryrun:
os.system(final_cmd)
@click.command()
@click.argument("verb", default="up")
@click.option("--dryrun", is_flag=True, help="Perform a dry run without executing actions", default=False)
@click.option("--dev", is_flag=True, help="Run in development mode", default=False)
@click.option("--detach", is_flag=True, help="Run containers in the background", default=False)
def main(verb: str, dryrun: bool, dev: bool, detach: bool):
compose_cmd = ComposeCmd(verb, dryrun, dev, detach)
compose_cmd.run()
if __name__ == "__main__":
main()