-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·180 lines (153 loc) · 5.87 KB
/
manage.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python3
from urllib.request import urlopen
from subprocess import Popen, PIPE
from pathlib import Path
import random
import string
import socket
import sys
import os
from settings import *
# check python version 3.6+
if sys.version_info < (3, 6):
print("Python 3.6+ is required")
sys.exit(1)
DOCKERPATH = Path(DOCKERPATH)
reserv_port = []
users = []
#if os.system("docker-compose --version") == 0: dcommand = "docker-compose"
#elif os.system("docker compose --version") == 0: dcommand = "docker compose"
if Popen("docker-compose --version", shell=True, stdout=PIPE, stderr=PIPE).wait() == 0: dcommand = "docker-compose"
elif Popen("docker compose --version", shell=True, stdout=PIPE, stderr=PIPE).wait() == 0: dcommand = "docker compose"
else: raise FileNotFoundError("docker-compose not found")
def port_usage(port: int):
#if os.system("netstat --version") == 0:
# return os.system(f"netstat -tuln | grep {port}")
#elif os.system("lsof -h") == 0:
# return os.system(f"lsof -i :{port}")
if Popen("netstat --version", shell=True, stdout=PIPE, stderr=PIPE).wait() == 0:
return Popen(f"netstat -tuln | grep {port}", shell=True, stdout=PIPE, stderr=PIPE).wait()
elif Popen("lsof -h", shell=True, stdout=PIPE, stderr=PIPE).wait() == 0:
return Popen(f"lsof -i :{port}", shell=True, stdout=PIPE, stderr=PIPE).wait()
else:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("0.0.0.0", port))
sock.close()
return 0
except OSError:
return 1
def get_port():
while True:
port = random.randint(*PORTRANGE)
if port not in reserv_port and port_usage(port) != 0:
reserv_port.append(port)
return port
def create_three_ports():
while True:
port1 = get_port()
port2 = get_port()
port3 = get_port()
if port1 != port2 and port2 != port3 and port1 != port3:
reserv_port.append(port1)
reserv_port.append(port2)
reserv_port.append(port3)
return port1, port2, port3
def create(manual_user: str = None):
DOCKERPATH.mkdir(exist_ok=True)
deployComposePath = Path("docker-compose.deploy.yaml")
if deployComposePath.exists():
deploy = deployComposePath.read_text()
else:
url = urlopen("https://raw.githubusercontent.com/sparcs-kaist/newbie-image/main/docker-compose.deploy.yaml")
deploy = url.read().decode()
deployComposePath.write_text(deploy)
if manual_user:
_add(manual_user)
else:
for user in USERS:
_add(user)
def _add(name: str):
userPath = DOCKERPATH / name
userPath.mkdir(exist_ok=True)
userComposePath = userPath / "docker-compose.yaml"
upass = ''.join(random.choices(string.ascii_letters + string.digits, k=PASSLENGTH))
uc = Path("docker-compose.deploy.yaml").read_text()
uc = uc.replace("||NEW_PASSWORD||", upass)
uc = uc.replace("||MYSQL_ROOT_PASSWORD||", MYSQLPASSWD)
uc = uc.replace("||USER||", name)
port1, port2, port3 = create_three_ports()
uc = uc.replace("||PORT22||", str(port1))
uc = uc.replace("||PORT3000||", str(port2))
uc = uc.replace("||PORT8000||", str(port3))
userComposePath.write_text(uc)
def stop():
for user in Path(DOCKERPATH).iterdir():
ucPath = user / "docker-compose.yaml"
if not ucPath.exists():
raise FileNotFoundError(f"docker compose.yml not found for {user}")
os.system(f"cd {str(ucPath.parent)} && {dcommand} down")
def start():
for user in Path(DOCKERPATH).iterdir():
ucPath = user / "docker-compose.yaml"
if not ucPath.exists():
raise FileNotFoundError(f"docker-compose.yml not found for {user}")
os.system(f"cd {str(ucPath.parent)} && {dcommand} up -d --build")
def getpass():
for user in Path(DOCKERPATH).iterdir():
ucPath = user / "docker-compose.yaml"
if not ucPath.exists():
raise FileNotFoundError(f"docker compose.yml not found for {user}")
passwd = ucPath.read_text().split("NEW_PASSWORD=")[1].split("\n")[0]
portstr = ucPath.read_text().split("ports:")[1].split("volumes")[0].split("- \"")[1:]
port1 = portstr[0].split(":")[0]
port2 = portstr[1].split(":")[0]
port3 = portstr[2].split(":")[0]
print(PRINTINFO.format(user=user, passwd=passwd, port22=port1, port3000=port2, port8000=port3))
def create_menu():
while True:
print("1. Create based on settings.py")
print("2. Create manually")
print("3. Back")
choice = input("Enter your choice: ")
if choice == "1":
create()
elif choice == "2":
name = input("Enter the name of the user: ")
create(name)
elif choice == "3":
break
else:
print("Invalid choice")
def backup():
COMMAND="""docker exec -it {user}-app su - sparcs -c "mysqldump -u root --password=tnfqkrtm -h db --all-databases > ~/backup.sql" """
for user in Path(DOCKERPATH).iterdir():
user = user.name
print(f"Woring for {user} backup...")
os.system(COMMAND.format(user=user))
print("Backup done")
def main():
while True:
print("1. Create docker-compose files")
print("2. Start docker-compose")
print("3. Stop docker-compose")
print("4. Get passwords")
print("5. Backup")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == "1":
create_menu()
elif choice == "2":
start()
elif choice == "3":
stop()
elif choice == "4":
getpass()
elif choice == "5":
backup()
elif choice == "6":
break
else:
print("Invalid choice")
if __name__ == "__main__":
main()