Skip to content

Commit cc60a2a

Browse files
committed
Added Python script
1 parent c21bf3d commit cc60a2a

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,32 @@ This project consists of the following scripts:
8080

8181
3. **Run CLOUDY**
8282

83+
a. **Using `Makefile`**
84+
8385
- To launch the virtual machine instances, run:
8486

8587
```bash
86-
make launch
88+
$ make launch
8789
```
8890

8991
- To clean up all instances and buckets, run:
9092

9193
```bash
92-
make clean
94+
$ make clean
9395
```
9496

9597
- To delete machines and buckets and then relaunch, run:
9698

9799
```bash
98-
make reset
100+
$ make reset
101+
```
102+
103+
b. **Using `cloudy.py`**
104+
105+
You can also use the `cloudy.py` script for the same purpose:
106+
107+
```bash
108+
$ ./cloudy.py launch
109+
$ ./cloudy.py clean
110+
$ ./cloudy.py reset
99111
```

cloudy.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
import os
5+
import sys
6+
7+
LAUNCH_SCRIPT = './scripts/launch.sh'
8+
CLEAN_SCRIPT = './scripts/clean.sh'
9+
CONFIG_FILE = 'config.json'
10+
11+
RED = '\033[31m'
12+
RESET = '\033[0m'
13+
14+
15+
def run_command(command):
16+
"""
17+
Executes a command in the terminal and shows its output.
18+
"""
19+
print(f"[CLOUDY] Running: {command}")
20+
try:
21+
subprocess.run(command, shell=True, check=True,
22+
text=True, stdout=sys.stdout, stderr=sys.stderr)
23+
except subprocess.CalledProcessError as e:
24+
print_error(
25+
f"Command '{command}' failed with exit code {e.returncode}")
26+
print_error(e.stderr)
27+
sys.exit(1)
28+
29+
def print_error(message):
30+
"""
31+
Prints an error message in red.
32+
"""
33+
print(f"{RED}[CLOUDY] Error: {message}{RESET}", file=sys.stderr)
34+
35+
36+
def launch():
37+
"""
38+
Creates VM instances using the launch script.
39+
"""
40+
if not os.path.isfile(CONFIG_FILE):
41+
print_error(f"The configuration file '{CONFIG_FILE}' does not exist.")
42+
sys.exit(1)
43+
print(
44+
f"[CLOUDY] Creating VM instances with the configuration in {CONFIG_FILE}...")
45+
run_command(f"bash {LAUNCH_SCRIPT} {CONFIG_FILE}")
46+
47+
48+
def clean():
49+
"""
50+
Cleans up Google Cloud resources using the cleanup script.
51+
"""
52+
if not os.path.isfile(CONFIG_FILE):
53+
print_error(f"The configuration file '{CONFIG_FILE}' does not exist.")
54+
sys.exit(1)
55+
print("[CLOUDY] Clearing Google Cloud resources...")
56+
run_command(f"bash {CLEAN_SCRIPT} {CONFIG_FILE}")
57+
58+
59+
def reset():
60+
"""
61+
Deletes all cloud resources and then creates new ones.
62+
"""
63+
clean()
64+
launch()
65+
66+
67+
def help_message():
68+
"""
69+
Displays the help message.
70+
"""
71+
print("CLOUDY commands:")
72+
print(
73+
f" python cloudy.py launch [CONFIG_FILE={CONFIG_FILE}] - Creates VM instances according to the specified configuration (default: {CONFIG_FILE}).")
74+
print(
75+
f" python cloudy.py clean [CONFIG_FILE={CONFIG_FILE}] - Deletes all instances and buckets in Google Cloud (requires {CONFIG_FILE}).")
76+
print(" python cloudy.py reset - Deletes all cloud resources and then creates new ones.")
77+
print(" python cloudy.py help - Displays this help.")
78+
79+
80+
def main():
81+
"""
82+
Processes command-line arguments.
83+
"""
84+
if len(sys.argv) < 2:
85+
help_message()
86+
sys.exit(1)
87+
88+
command = sys.argv[1].lower()
89+
90+
if command == 'launch':
91+
launch()
92+
elif command == 'clean':
93+
clean()
94+
elif command == 'reset':
95+
reset()
96+
elif command == 'help':
97+
help_message()
98+
else:
99+
print_error(f"Unknown command '{command}'")
100+
help_message()
101+
sys.exit(1)
102+
103+
104+
if __name__ == "__main__":
105+
main()

scripts/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if [ -f "$OUTPUT_FILE" ]; then
6767
gsutil cp "$OUTPUT_FILE" "gs://$BUCKET_NAME/" >/dev/null 2>&1
6868

6969
if [ $? -eq 0 ]; then
70-
info "File saved to gs://$BUCKET_NAME/$OUTPUT_FILE!"
70+
info "File saved to gs://$BUCKET_NAME/$OUTPUT_FILE"
7171
else
7272
error "Failed to save the file in Google Cloud Storage."
7373
exit 1

0 commit comments

Comments
 (0)