-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_compose.py
More file actions
executable file
·103 lines (80 loc) · 3.33 KB
/
run_compose.py
File metadata and controls
executable file
·103 lines (80 loc) · 3.33 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
#!/usr/bin/env python
import glob
import os
import subprocess
import sys
SERVICE_LOCAL_PORT_MAPPING_TEMPLATE = """
{service_name}:
ports:
- "${{{service_port}}}:${{{service_port}}}"
"""
SERVICE_FILE_NAMES_TO_SERVICE_NAME = {
'irrigationservice.yml': 'irrigation',
'pestmanagement.yml': 'pdm',
'weatherservice.yml': 'weathersrv',
}
SERVICE_NAMES_TO_PORT_ENV_VARS = {
"gatekeeper": 'GATEKEEPER_APP_PORT',
"farmcalendar": 'FARM_CALENDAR_APP_PORT',
"pdm": 'PDM_SERVICE_PORT',
"weathersrv": 'WEATHER_SRV_PORT',
"irrigation": 'IRR_SERVICE_PORT',
"reporting": 'REPORTING_SERVICE_PORT',
"userdashboard": 'USER_DASHBOARD_SERVICE_PORT',
}
def create_override_file_if_not_exist(file_path, extra_content=None):
should_create_file = not os.path.exists(file_path)
if not should_create_file:
user_confirm = input('You are about to overwrite the existing override file. Do you want to continue? (y/[n]): ')
if user_confirm.lower() == 'y':
should_create_file = True
if should_create_file:
print(f"Creating override file: {file_path}")
with open(file_path, 'w') as f:
f.writelines([
'services:', '\n'
])
if extra_content is not None:
f.write(extra_content)
return True
return False
def deploy_localhost(override_compose_file, service_in_use_files):
extra_content = ""
for service_file_path in service_in_use_files:
file_name = os.path.basename(service_file_path)
service_name = SERVICE_FILE_NAMES_TO_SERVICE_NAME.get(file_name, file_name.split('.')[0])
service_port = SERVICE_NAMES_TO_PORT_ENV_VARS.get(service_name)
if service_port is None:
raise Exception(f"Warning: No port mapping found for service '{service_name}'.")
extra_content += SERVICE_LOCAL_PORT_MAPPING_TEMPLATE.format(service_name=service_name, service_port=service_port)
if create_override_file_if_not_exist(override_compose_file, extra_content=extra_content):
print("Override file created successfully.")
def handle_deploy_command(argv, override_compose_file, service_in_use_files):
if argv[0] == "deploy-localhost":
deploy_localhost(override_compose_file, service_in_use_files)
return True
return False
def main():
# Set the main docker-compose file
main_compose_file = "docker-compose.yml"
override_compose_file = "docker-compose.override.yml"
services_dir = "services_in_use"
service_in_use_files = glob.glob(os.path.join(services_dir, "*.yml"))
if handle_deploy_command(sys.argv[1:], override_compose_file, service_in_use_files):
# exists if used our deploy-localhost command
# we don't want to pass this to docker compose
return
compose_files = [main_compose_file] + service_in_use_files + [override_compose_file]
compose_command = ["docker", "compose"]
for compose_file in compose_files:
compose_command.extend(["-f", compose_file])
compose_command.extend(sys.argv[1:])
# Print the constructed command (for debugging purposes)
print("Running command:", " ".join(compose_command))
try:
# Run the docker compose command
subprocess.run(compose_command)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()