-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.py
58 lines (46 loc) · 1.94 KB
/
cloud.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
import random
import typer
import os
import logging
import pathlib
import json
main_dir = pathlib.Path(__file__).parent.resolve()
app = typer.Typer()
logging.basicConfig(level="INFO")
shell_s = os.path.dirname(os.path.abspath(__file__))
@app.command()
def upload(storage_id: str, directory: str) -> None:
if not os.path.isabs(directory):
logging.info("The path you entered is not absolute. Please enter again")
exit()
logging.info("Uploading the package.....")
try:
os.system(f"/bin/bash {shell_s}/upload.sh {storage_id} {directory} {main_dir}")
except Exception as e:
logging.info("Error while uploading the package. Please try again")
@app.command()
def download(storage_id: str, directory: str, project_id: str) -> None:
try:
os.system(f"/bin/bash {shell_s}/download.sh {storage_id} {directory} {project_id} {main_dir}")
except Exception as e:
logging.info("Error while uploading the package. Please try again")
@app.command()
def run(storage_id: str, project_id: str, docker_image: str, command: str, machine: str) -> None:
os.chdir(main_dir)
variable = {}
variable['user_input'] = {"storage_id": storage_id, "project_id": project_id, "docker_image": docker_image, "command": command, "machine_type": machine}
variable['server_type'] = machine
variable['machine_name'] = storage_id + str(random.randint(1, 100))
variable['ip_name'] = storage_id + str(random.randint(101, 200))
variable['ssh_key_name'] = storage_id + str(random.randint(201, 300))
json_object = json.dumps(variable, indent = 4)
with open("variables.tfvars.json", "w") as f:
f.write(json_object)
try:
os.system(f"/bin/bash {shell_s}/terraform.sh {storage_id} {project_id} {docker_image} {command} {machine} {main_dir}")
except Exception as e:
logging.info("Error while executing the command. Please try again")
def main():
app()
if __name__ == "__main__":
app()