-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudy.py
executable file
·105 lines (85 loc) · 2.73 KB
/
cloudy.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
#!/usr/bin/env python3
import subprocess
import os
import sys
LAUNCH_SCRIPT = './scripts/launch.sh'
CLEAN_SCRIPT = './scripts/clean.sh'
CONFIG_FILE = 'config.json'
RED = '\033[31m'
RESET = '\033[0m'
def run_command(command):
"""
Executes a command in the terminal and shows its output.
"""
print(f"[CLOUDY] Running: {command}")
try:
subprocess.run(command, shell=True, check=True,
text=True, stdout=sys.stdout, stderr=sys.stderr)
except subprocess.CalledProcessError as e:
print_error(
f"Command '{command}' failed with exit code {e.returncode}")
print_error(e.stderr)
sys.exit(1)
def print_error(message):
"""
Prints an error message in red.
"""
print(f"{RED}[CLOUDY] Error: {message}{RESET}", file=sys.stderr)
def launch():
"""
Creates VM instances using the launch script.
"""
if not os.path.isfile(CONFIG_FILE):
print_error(f"The configuration file '{CONFIG_FILE}' does not exist.")
sys.exit(1)
print(
f"[CLOUDY] Creating VM instances with the configuration in {CONFIG_FILE}...")
run_command(f"bash {LAUNCH_SCRIPT} {CONFIG_FILE}")
def clean():
"""
Cleans up Google Cloud resources using the cleanup script.
"""
if not os.path.isfile(CONFIG_FILE):
print_error(f"The configuration file '{CONFIG_FILE}' does not exist.")
sys.exit(1)
print("[CLOUDY] Clearing Google Cloud resources...")
run_command(f"bash {CLEAN_SCRIPT} {CONFIG_FILE}")
def reset():
"""
Deletes all cloud resources and then creates new ones.
"""
clean()
launch()
def help_message():
"""
Displays the help message.
"""
print("CLOUDY commands:")
print(
f" python cloudy.py launch [CONFIG_FILE={CONFIG_FILE}] - Creates VM instances according to the specified configuration (default: {CONFIG_FILE}).")
print(
f" python cloudy.py clean [CONFIG_FILE={CONFIG_FILE}] - Deletes all instances and buckets in Google Cloud (requires {CONFIG_FILE}).")
print(" python cloudy.py reset - Deletes all cloud resources and then creates new ones.")
print(" python cloudy.py help - Displays this help.")
def main():
"""
Processes command-line arguments.
"""
if len(sys.argv) < 2:
help_message()
sys.exit(1)
command = sys.argv[1].lower()
if command == 'launch':
launch()
elif command == 'clean':
clean()
elif command == 'reset':
reset()
elif command == 'help':
help_message()
else:
print_error(f"Unknown command '{command}'")
help_message()
sys.exit(1)
if __name__ == "__main__":
main()