-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
173 lines (153 loc) · 6.05 KB
/
app.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
#!/usr/bin/env python
import os
import sys
import pyotp
import pyqrcode
def help():
print()
print()
print(" __ __ ")
print(" ) ) _ _ ( _ _ _ ) ) ) _ _ _ ) _ _)_ _ ")
print(" /_/ (_( ( ) ) )_) (_( /_/ ) (_) )_) ( )_) (_ ( ")
print(" _) (_ ( (_ _) ")
print()
print()
print("This is the CLI tool to manage your Droplets, but works for all docker containers.")
print()
print("To get started, try any of the following parameters:")
print()
print(" -c | --check <container> This will tell you if your droplet is running or not.")
print(" -s | --start <container> This will start your droplet - and all it's processes.")
print(" -d | --stop <container> This will turn off your droplet.")
print(" -r | --restart <container> This will restart your droplet and all it's processes.")
print(" -l | --list This will list all your droplets.")
print(" -n | --new <container> <type> This will create a new droplet with the name and hostname <container>, and type <type>.")
print(" [ web | ubuntu ]")
print(" -i | --ips This will list all containers and their corresponding ip addressess (local network only).")
print(" -ip | --ip <container> This will return the ip address of a specific droplet.")
print(" -b | --backup <container> This will backup your droplet in a snapshot style.")
print(" -h | --help This will display this help message.")
print()
print("For more information, please visit https://droplets.dasho.dev")
print()
print()
print()
print()
print()
print()
print()
def check(droplet):
if os.system("docker container inspect -f '{{.State.Running}}' " + droplet) == "running":
return True
else:
return False
def start(droplet):
os.system("docker start " + droplet)
os.system("docker exec -d " + droplet + " /bin/bash ddrun")
if check(droplet) == True:
print("Droplet started.")
else:
print("Droplet failed to start.")
def stop(droplet):
os.system("docker stop " + droplet)
if check(droplet) == False:
print("Droplet stopped.")
else:
print("Droplet failed to stop.")
def restart(droplet):
stop(droplet)
start(droplet)
if check(droplet) == True:
print("Droplet restarted.")
else:
print("Droplet failed to restart.")
def new(droplet, type="web"):
if type == "web":
os.system("docker run -d -p 80:80 --name " + droplet + " -h " + droplet + " dasho/droplet-web")
elif type == "ubuntu":
os.system("docker run -d --name " + droplet + " -h " + droplet + " dasho/droplet-ubuntu")
if check(droplet) == True:
secret = generate_secret()
qr = generate_qr(secret, droplet)
with open(f"{droplet}.txt", "a") as f:
f.write(f"{droplet} => {secret}")
qr.svg(f"{droplet}.svg", scale=6)
print("Droplet created.")
else:
print("Droplet failed to create.")
def listIps():
os.system("docker ps | awk 'NR>1{ print $1 }' | xargs docker inspect -f '{{range .NetworkSettings.Networks}}{{$.Name}}{{\" \"}}{{.IPAddress}}{{end}}'")
def listIp(droplet):
os.system("docker inspect --format '{{ .NetworkSettings.IPAddress }}' " + sys.argv[2])
def ps():
os.system("docker ps")
def ls():
os.system("docker ps -a")
def lsi():
os.system("docker images")
def size():
os.system("docker system df")
def sizev():
os.system("docker system df -v")
def backup(droplet):
if(os.system(f"sudo docker images -q {droplet}") != 0):
print("Droplet does not exist.")
else:
os.system(f"sudo docker commit {droplet} itsokka/backups:{droplet}")
os.system(f"sudo docker push itsokka/backups:{droplet}")
os.system(f"sudo docker rmi itsokka/backups:{droplet}")
print("Droplet backed up.")
def main():
if len(sys.argv) == 1:
help()
elif sys.argv[1] == "-c" or sys.argv[1] == "--check":
if check(sys.argv[2]) == True:
print("Droplet is running.")
else:
print("Droplet is not running.")
elif sys.argv[1] == "-s" or sys.argv[1] == "--start":
start(sys.argv[2])
elif sys.argv[1] == "-d" or sys.argv[1] == "--stop":
stop(sys.argv[2])
elif sys.argv[1] == "-r" or sys.argv[1] == "--restart":
restart(sys.argv[2])
elif sys.argv[1] == "-l" or sys.argv[1] == "--list":
os.system("docker ps -a")
elif sys.argv[1] == "-n" or sys.argv[1] == "--new":
new(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "-i" or sys.argv[1] == "--ips":
listIps()
elif sys.argv[1] == "-ip" or sys.argv[1] == "--ip":
listIp(sys.argv[2])
elif sys.argv[1] == "-b" or sys.argv[1] == "--backup":
backup(sys.argv[2])
elif sys.argv[1] == "-h" or sys.argv[1] == "--help":
help()
elif sys.argv[1] == "-ps":
ps()
elif sys.argv[1] == "-ls":
ls()
elif sys.argv[1] == "-lsi":
lsi()
elif sys.argv[1] == "-size":
size()
elif sys.argv[1] == "-sizev":
sizev()
else:
help()
# def to generate a totp code from a secret
def generate_totp(secret):
return pyotp.TOTP(secret).now()
# def to generate a new secret
def generate_secret():
return pyotp.random_base32()
# def to generate a new qr code
def generate_qr(secret, name):
return pyqrcode.create(f"otpauth://totp/{name}?secret={secret}&issuer=Dashed%20Droplets")
main()
# temp_secret = generate_secret()
# temp_totp = generate_totp(temp_secret)
# print(temp_secret)
# print(temp_totp)
# print(generate_qr(temp_secret, "test").terminal())
# print(generate_qr(temp_secret, "test").svg("test.svg", scale=1))