-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.py
181 lines (147 loc) · 7.5 KB
/
script.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
181
#!/usr/bin/env python3
import subprocess
import sys
import re
def display_help():
help_banner = """
__ __
) ) _ _ ( _ _ _ ) ) ) _ _ _ ) _ _)_ _
/_/ (_( ( ) ) )_) (_( /_/ ) (_) )_) ( )_) (_ (
_) (_ ( (_ _)
This is the CLI tool to manage your Droplets, but works for all docker containers.
To get started, try any of the following parameters:
-c | --check <container> This will tell you if your droplet is running or not.
-s | --start <container> This will start your droplet - and all its processes.
-d | --stop <container> This will turn off your droplet.
-r | --restart <container> This will restart your droplet and all its processes.
-n | --new <container> <type> This will create a new droplet with the name and hostname <container>, and type <type>.
[ apache2 | ubuntu ]
-i | --ips This will list all containers and their corresponding IP addresses (local network only).
-ip | --ip <container> This will return the IP address of a specific droplet.
-b | --backup <container> This will backup your droplet in a snapshot style.
-h | --help This displays this help prompt.
-a | --all List all the container names only with their images.
-at | --alltime List all the container names only with their images and time.
"""
print(help_banner)
def run_command(command):
try:
return subprocess.check_output(command, shell=True).decode().strip()
except subprocess.CalledProcessError as e:
return str(e)
def check_container(container):
status = run_command(f"docker container inspect -f '{{{{.State.Status}}}}' {container}")
return "True" if status == "running" else "False"
# def create_container(container, container_type, port_number, second_port, ssh_port):
# if container_type == "ubuntu":
# run_command(f"docker run --restart unless-stopped -d --name {container} -h {container} onionz/ubuntu:latest sleep infinity")
# run_command(f"docker exec -it {container} bash ddrun")
# else:
# run_command(f"docker create --restart unless-stopped -p {port_number}:80 -p {second_port}:7662 -p {ssh_port}:22 --name {container} -h {container} kooljool/droplet:latest")
# run_command(f"docker start {container}")
# run_command(f"docker exec -it {container} /bin/bash /usr/bin/ddrun")
# return f"Created {container}"
def is_container_allowed(container_name):
container_name_pattern = re.compile(r'^[a-zA-Z0-9]+$')
return container_name_pattern.match(container_name) is not None
def create_container(container, container_type, port_number, second_port, ssh_port):
results = []
if is_container_allowed(container):
if container_type == "ubuntu":
result1 = run_command(f"docker run --restart unless-stopped -d --name {container} -h {container} onionz/ubuntu:latest sleep infinity")
result2 = run_command(f"docker exec -it {container} bash ddrun")
results.append(result1)
results.append(result2)
else:
result1 = run_command(f"docker create --restart unless-stopped -p {port_number}:80 -p {second_port}:7662 -p {ssh_port}:22 --name {container} -h {container} kooljool/droplet:latest")
result2 = run_command(f"docker start {container}")
result3 = run_command(f"bash -c './dockerrun {container}' ")
link = run_command(f"docker exec {container} cat /var/www/dump/files.hostname")
result4 = f"{link}/info.server.php"
# results.append(result1)
# results.append(result2)
results.append(result4)
else:
results.append("The container name is not valid!")
results.append(f"Created {container} \n |->Goes to this complete url")
return "\n".join(results)
def get_link(container):
link = run_command(f"docker exec {container} cat /var/www/dump/files.hostname")
return f"{link}/info.server.php"
def backup_container(container):
run_command(f"docker stop {container}")
image_exist = run_command(f"docker images -q onionz/backups:{container}")
if image_exist:
run_command(f"docker rmi onionz/backups:{container}")
run_command(f"docker commit {container} onionz/backups:{container}")
run_command(f"docker push onionz/backups:{container}")
run_command(f"docker rmi onionz/backups:{container}")
run_command(f"docker start {container}")
run_command(f"docker exec -it {container} /bin/bash /usr/bin/ddrun")
print(f"Backed up {container}")
def delete_container(container):
run_command(f"docker stop {container}")
run_command(f"docker rm {container}")
print(f"Started {container}")
def start_container(container):
run_command(f"docker start {container}")
run_command(f"docker exec -it {container} /bin/bash /usr/bin/ddrun")
print(f"Started {container}")
def stop_container(container):
run_command(f"docker stop {container}")
print(f"Stopped {container}")
def restart_container(container):
run_command(f"docker stop {container}")
run_command(f"docker start {container}")
run_command(f"docker exec -it {container} /bin/bash /usr/bin/ddrun")
print(f"Restarted {container}")
def list_ips():
return run_command("docker ps | awk 'NR>1{ print $1 }' | xargs docker inspect -f '{{range .NetworkSettings.Networks}}{{$.Name}}{{\" \"}}{{.IPAddress}}{{end}}'")
def list_ip(container):
return run_command(f"docker inspect -f '{{{{ .NetworkSettings.IPAddress }}}}' {container}")
def list_all():
return run_command("docker ps -a --format 'table {{.Names}}\t{{.Image}}'")
def list_alltime():
return run_command("docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.RunningFor}}'")
def main():
args = sys.argv[1:]
while args:
arg = args.pop(0)
if arg in ['-c', '--check']:
container = args.pop(0)
print(check_container(container))
elif arg in ['-n', '--new']:
container = args.pop(0)
container_type = args.pop(0)
port_number = args.pop(0)
second_port = args.pop(0)
ssh_port = args.pop(0)
print(create_container(container, container_type, port_number, second_port, ssh_port)) # add print statement here
elif arg in ['-s', '--start']:
container = args.pop(0)
start_container(container)
elif arg in ['-d', '--stop']:
container = args.pop(0)
stop_container(container)
elif arg in ['-r', '--restart']:
container = args.pop(0)
restart_container(container)
elif arg in ['-i', '--ips']:
print(list_ips())
elif arg in ['-ip', '--ip']:
container = args.pop(0)
print(list_ip(container))
elif arg in ['-b', '--backup']:
container = args.pop(0)
backup_container(container)
elif arg in ['-a', '--all']:
print(list_all())
elif arg in ['-at', '--alltime']:
print(list_alltime())
elif arg in ['-h', '--help']:
display_help()
else:
print("Invalid argument. Please check the command-line arguments and try again.")
sys.exit(1)
if __name__ == '__main__':
main()