-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.py
185 lines (158 loc) · 4.54 KB
/
docker.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
#!/usr/bin/env python3
import argparse
import subprocess
PROJECT_NAME = "alfawal-uav-project"
COMPOSE_FILE = "./docker/docker-compose.yaml"
CONTAINER_NAME = f"{PROJECT_NAME}-django"
CYAN = "\033[96m"
NC = "\033[0m" # No color (aka. RESET)
def run_command(command: str) -> None:
"""Run a command in the shell.
Args:
command (str): Command to run.
"""
print(f"{CYAN}Running the command:\n{command}{NC}")
try:
subprocess.run(command, shell=True)
except KeyboardInterrupt:
print(f"{CYAN}Interrupted the command:\n{command}{NC}")
def docker_command(*args) -> None:
"""Run a docker command with the specified arguments.
Args:
*args: Command line arguments to pass to docker.
"""
command_parts = (
"docker",
"exec",
"-it",
CONTAINER_NAME,
*args,
)
full_command = " ".join(command_parts)
run_command(full_command)
def docker_compose_command(*args) -> None:
"""Run a docker-compose command with the specified arguments.
Args:
*args: Command line arguments to pass to docker-compose.
"""
command_parts = (
"docker-compose",
"-f",
COMPOSE_FILE,
"-p",
PROJECT_NAME,
*args,
)
full_command = " ".join(command_parts)
run_command(full_command)
def manage_py(*args) -> None:
"""Run a Django management command in the Django container.
Args:
*args: Command line arguments to pass to manage.py.
"""
docker_command("python", "manage.py", *args)
def get_parser() -> argparse.Namespace:
"""Parse command line arguments.
Returns:
argparse.Namespace: Parsed arguments namespace object.
"""
parser = argparse.ArgumentParser(
description="Manage docker-compose and Django management commands."
)
parser.add_argument(
"-c",
"--command",
nargs=argparse.REMAINDER,
help="Command to pass to manage.py",
)
subparsers = parser.add_subparsers(dest="compose_command")
# Docker compose commands
subparsers.add_parser(
"build",
help="Build the docker containers.",
)
up_parser = subparsers.add_parser(
"up",
help=(
"Start the docker containers,"
" -d (--detached) to run in detached mode."
),
)
up_parser.add_argument(
"-d",
"--detached",
action="store_true",
help="Run in detached mode.",
)
subparsers.add_parser(
"down",
help="Stop and remove the docker containers.",
)
subparsers.add_parser(
"stop",
help="Stop the docker containers.",
)
subparsers.add_parser(
"remove",
help="Remove the docker containers.",
)
subparsers.add_parser(
"shell",
help="Open the Django shell in the Django container.",
)
subparsers.add_parser(
"console",
help="Open a console in the Django container.",
)
subparsers.add_parser(
"makemigrations",
help="Make migrations.",
)
subparsers.add_parser(
"migrate",
help="Run migrations.",
)
subparsers.add_parser(
"seed",
help="Seed the database with initial data.",
)
return parser
def main() -> int:
"""Parse arguments and execute the appropriate command."""
parser = get_parser()
args = parser.parse_args()
compose_command = args.compose_command
if compose_command is None and args.command is None:
print("No command specified.")
parser.print_help()
return 1
if args.command:
manage_py(*args.command)
elif compose_command == "build":
docker_compose_command("build")
elif compose_command == "up":
if args.detached:
docker_compose_command("up", "-d")
else:
docker_compose_command("up")
elif compose_command == "down":
docker_compose_command("down")
elif compose_command == "stop":
docker_compose_command("stop")
elif compose_command == "remove":
docker_compose_command("rm", "-f")
elif compose_command == "shell":
manage_py("shell")
elif compose_command == "console":
docker_command("sh")
elif compose_command == "makemigrations":
manage_py("makemigrations")
elif compose_command == "migrate":
manage_py("migrate")
elif compose_command == "seed":
manage_py("seed")
else:
print(f"Unknown command: {compose_command}")
return 0
if __name__ == "__main__":
raise SystemExit(main())