|
| 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() |
0 commit comments