-
Notifications
You must be signed in to change notification settings - Fork 201
/
redcloud.py
418 lines (370 loc) · 17.1 KB
/
redcloud.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
import subprocess
from utils.colors import colors as c
DOCKER_DEPLOY = "docker-compose up --build -d"
DOCKER_DOWN = "docker-compose down --remove-orphans"
# DOCKER_INSTALL = "curl -fsSL https://get.docker.com -o get-docker.sh ; sh get-docker.sh"
DOCKER_INSTALL = "curl -fsSL https://get.docker.com -o get-docker.sh"
DOCKER_INSTALL2 = "sh get-docker.sh"
# DOCKER_COMPOSE_INSTALL = "curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose"
DOCKER_COMPOSE_INSTALL = "curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-{u_s}-{u_m} -o /usr/local/bin/docker-compose"
DOCKER_COMPOSE_INSTALL2 = "chmod +x /usr/local/bin/docker-compose"
REDCLOUD_INSTALL_GIT = "git clone https://github.com/khast3x/redcloud.git"
REDCLOUD_INSTALL_SCP = "scp -r ../redcloud {target}:~/"
SSH_OR = " || echo \"error\""
GET_IP = "curl -4 -s icanhazip.com"
def print_banner(arg = ""):
banner_top = '''
----__ ''"" ___``'/````\
,' ,' `--/ __, \-,,,`.
,""""' .' ;-. , ,' \ `"""".
,' `-( `._(_,' )_ `.
,' ,---. \ @ ; \ @ _,' `.
,-""' ,' ,--'- `;' `.
,' ,' ( `. ,' `.
; ,' \ _,',' Offensive `.
,' ; `--' ,' Infrastructure `.
,' ; __ ( Deployment `.
; `____... `78b `. ,' ,'
; ...----''" ) _.- .d8P `. ,' ,' ,'
'''
banner = '''_....----'" '. _..--"_.-:.-' .' `. ,''. ,' `--'
`" mGk "" _.-'' .-'`-.:..___...--' `-._ ,-"' `-'
_.--' _.-' .' .' .' `"""""
__.-'' _.-' .-' .' / ~~~
' _.-' .-' .-' .'
_.-' .-' .-' .' .' / R e d C l o u d
_.-' .-' .-' .' .'
_.-' .-' .' .' / ~~~
_.-' .-' .' .' github.com/khast3x
.-' .'
'''
print("\n\n")
if len(arg) != 0:
print(c.fg.red + c.bold + banner_top + banner + c.reset + "\n\n\n")
print("\n\n\t\tThank you for using" + c.fg.red+ " redcloud!" + c.bold + " <3" +c.reset)
input(c.bg.purple + "\n\n\t\t- Press Enter to get back to saving the planet -" + c.reset)
else:
print(c.fg.red + c.bold + banner + c.reset)
def is_tool(name):
'''Check whether `name` is on PATH.'''
from distutils.spawn import find_executable
return find_executable(name) is not None
def get_unames(prefix = ""):
# $(uname -s)-$(uname -m)
uname_s = run_cmd_output(prefix + "uname -s")
uname_m = run_cmd_output(prefix + "uname -m")
final_cmd = DOCKER_COMPOSE_INSTALL.format(u_s=uname_s, u_m=uname_m)
print(final_cmd)
return final_cmd
def run_cmd_output(cmd):
'''
Runs local command to shell, returns output. Splits string to tab before giving args to subprocess
'''
try:
output = subprocess.check_output(cmd.split(" "), stderr=sys.stdout.buffer)
output = output.decode("utf-8").strip()
return output
except subprocess.CalledProcessError as e:
c.bad_news(c, "Something went wrong with running command")
print(e)
def list_available():
'''
List available templates from the templates.yml
'''
import yaml
with open("templates/templates.yml", 'r') as stream:
try:
data = yaml.load(stream)
for templ in data:
print("---------------------")
print(c.fg.green + "[>] " + c.fg.purple + templ["title"] + c.reset + " : " + templ["description"])
# print(" source: " + c.reset + templ["image"])
print(c.bg.purple + "\n" + c.reset)
input("\n- Press Enter to continue -")
except yaml.YAMLError as exc:
print(exc)
def install_docker(prefix = ""):
'''
Runs the command to install docker. Can run with the SSH prefix to install remotly
Keep both seperated for later debugging
'''
c.info_news(c, "This might take a few minutes... Hang in there!")
if len(prefix) != 0:
output = run_cmd_output(prefix + DOCKER_INSTALL)
output += run_cmd_output(prefix + DOCKER_INSTALL2)
else:
output = run_cmd_output(DOCKER_INSTALL)
output += run_cmd_output(DOCKER_INSTALL2)
print(output)
def install_docker_compose(prefix = ""):
'''
Runs the command to install docker-compose. Can run with the SSH prefix to install remotly
Keep both seperated for later debugging
'''
cmd = get_unames(prefix)
if len(prefix) != 0:
output = run_cmd_output(prefix + cmd)
output += run_cmd_output(prefix + DOCKER_COMPOSE_INSTALL2)
else:
output = run_cmd_output(cmd)
output += run_cmd_output(DOCKER_COMPOSE_INSTALL2)
print(output)
def deploy_local():
'''
Check install, offer removing portainer persistent data if file exists.
Run docker-compose command, get IP, print with portainer URI
'''
# Check installs
if is_tool("curl"):
c.info_news(c, "curl installation found")
# Check docker; install ; check install
if is_tool("docker"):
c.info_news(c, "docker installation found")
else:
c.bad_news(c, "docker installation not found")
c.question_news(c, "Install docker? [Y/n]")
dockerq = input(">> ")
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker")
install_docker()
if is_tool("docker"):
c.good_news(c, "docker installation finished successfully")
# Check docker-compose; install ; check install
if is_tool("docker-compose"):
c.info_news(c, "docker-compose installation found")
else:
c.bad_news(c, "docker-compose installation not found")
c.question_news(c, "Install docker-compose? [Y/n]")
dockerq = input(">> ")
print(dockerq)
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker-composetoto")
install_docker_compose()
if is_tool("docker-compose"):
c.good_news(c, "docker-compose installation finished successfully")
# Check Portainer persistent data
if os.path.isdir("/tmp/portainer/data"):
c.question_news(c, "Portainer data detected, a user/pass already exists. Remove? [N/y]")
choice = input(">> ")
if choice == "y":
import shutil
shutil.rmtree("/tmp/portainer/data")
c.good_news(c, "Deleted portainer data")
# Start deploy
c.good_news(c, "Deploying redcloud")
output = run_cmd_output(DOCKER_DEPLOY)
print(output)
c.good_news(c, "Done")
ip = run_cmd_output(GET_IP)
print(c.bold + c.fg.green + "\n" + "=========================================================================" + c.reset)
c.good_news(c, "Please find your running instance at https://" + ip +"/portainer")
c.info_news(c, "Files are available at https://" + ip + "/files")
c.info_news(c, "Live Reverse Proxy data is available at https://" + ip + "/api")
print(c.bold + c.fg.green + "=========================================================================" + c.reset)
print(c.bg.orange + "\n" + c.reset)
input("\n- Press Enter to continue -")
def deploy_remote_ssh():
'''
Deploy redcloud to a remote server using SSH
'''
# Get user target, username
c.question_news(c, "Target IP or hostname?")
target = input(">> ").strip()
c.question_news(c, "Target username? (Default: root)")
username = input(">> ").strip()
if username == "":
username = "root"
SSH_CMD = "ssh " + username+"@"+target+" "
# Check remote curl install
output = run_cmd_output(SSH_CMD + "command -v curl" + SSH_OR)
if output != "error" and len(output) != 0:
c.info_news(c, "curl installation found")
elif output == "error":
c.bad_news(c, "curl installation not found")
# Check remote git install, if found git clone, else scp files to target
output = run_cmd_output(SSH_CMD + "command -v git" + SSH_OR)
if output != "error" and len(output) != 0:
c.info_news(c, "git installation found")
c.info_news(c, "Cloning latest redcloud repository")
run_cmd_output(SSH_CMD + REDCLOUD_INSTALL_GIT + SSH_OR)
elif output == "error":
c.bad_news(c, "git installation not found")
c.info_news(c, "Sending local files...")
run_cmd_output(REDCLOUD_INSTALL_SCP.format(target = username+"@"+target))
# Check remote docker install; Install; Check install
output = run_cmd_output(SSH_CMD + "command -v docker" + SSH_OR)
if output != "error" and len(output) != 0:
c.info_news(c, "docker installation found")
elif output == "error":
c.bad_news(c, "docker installation not found")
c.question_news(c, "Install docker? [Y/n]")
dockerq = input(">> ")
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker")
install_docker(SSH_CMD)
output = run_cmd_output(SSH_CMD + "command -v docker" + SSH_OR)
if output != "error" and len(output) != 0:
c.good_news(c, "docker installation finished sucessfully")
# Check remote docker-compose install; Install; Check install
output = run_cmd_output(SSH_CMD + "command -v docker-compose" + SSH_OR)
if output != "error" and len(output) != 0:
c.info_news(c, "docker-compose installation found")
elif output == "error":
c.bad_news(c, "docker-compose installation not found")
c.question_news(c, "Install docker-compose? [Y/n]")
dockerq = input(">> ")
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker-compose")
install_docker_compose(SSH_CMD)
output = run_cmd_output(SSH_CMD + "command -v docker" + SSH_OR)
if output != "error" and len(output) != 0:
c.good_news(c, "docker-compose installation finished sucessfully")
# Get redcloud; Deploy
c.good_news(c, "Deploying redcloud")
run_cmd_output(SSH_CMD + "cd redcloud ; " + DOCKER_DEPLOY + SSH_OR)
c.good_news(c, "Done")
ip = run_cmd_output(SSH_CMD + GET_IP)
print(c.bold + c.fg.green + "\n" + "=========================================================================" + c.reset)
c.good_news(c, "Please find your running instance at https://" + ip +"/portainer")
c.info_news(c, "Files are available at https://" + ip + "/files")
c.info_news(c, "Live reverse proxy data is available at https://" + ip + "/api")
print(c.bold + c.fg.green + "=========================================================================" + c.reset)
print(c.bg.purple + "\n" + c.reset)
input("\n- Press Enter to continue -")
def deploy_dockermachine():
'''
Check install, offer removing portainer persistent data if file exists.
Run docker-compose command, get IP, print with portainer URI
'''
# Check installs
if is_tool("curl"):
c.info_news(c, "curl installation found")
# Check docker; install ; check install
if is_tool("docker"):
c.info_news(c, "docker installation found")
else:
c.bad_news(c, "docker installation not found")
c.question_news(c, "Install docker? [Y/n]")
dockerq = input(">> ")
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker")
install_docker()
if is_tool("docker"):
c.good_news(c, "docker installation finished sucessfully")
# Check docker-compose; install ; check install
if is_tool("docker-compose"):
c.info_news(c, "docker-compose installation found")
else:
c.bad_news(c, "docker-compose installation not found")
c.question_news(c, "Install docker-compose? [Y/n]")
dockerq = input(">> ")
if dockerq == "n":
c.info_news(c, "Skipping...")
else:
c.info_news(c, "Installing docker-compose")
install_docker_compose()
if is_tool("docker-compose"):
c.good_news(c, "docker-compose installation finished sucessfully")
# Check env for docker-machine name, check machine status
try:
if 'DOCKER_MACHINE_NAME' in os.environ:
machine_name = os.environ['DOCKER_MACHINE_NAME']
else:
c.bad_news(c, "Could not find DOCKER_MACHINE_NAME environment variable")
input("\n- Press Enter to continue -")
return
except OSError as e:
c.bad_news(c, "Something went wrong when looking for docker-machine")
print(e)
out = run_cmd_output("docker-machine status " + machine_name)
if out != "Running":
c.bad_news(c, "It seems " + machine_name + " is not running?")
else:
c.good_news(c, "Seems " + c.bold + machine_name + c.reset + " is running correctly")
c.good_news(c, "Deploying redcloud remotely using docker-machine " + c.bold + machine_name + c.reset)
run_cmd_output(DOCKER_DEPLOY)
c.good_news(c, "Done")
ip = os.environ['DOCKER_HOST']
print(c.bold + c.fg.green + "\n" + "=========================================================================" + c.reset)
c.good_news(c, "Please find your running instance at https:" + ip.split(":")[1] +"/portainer")
c.info_news(c, "Files are available at https:" + ip.split(":")[1] + "/files")
c.info_news(c, "Live reverse proxy data is available at https:" + ip.split(":")[1] + "/api")
print(c.bold + c.fg.green + "=========================================================================" + c.reset)
print(c.bg.cyan + "\n" + c.reset)
input("\n- Press Enter to continue -")
def menu_deploy_target():
'''
Display the First Menu, prompting for deploy location
'''
while True:
print("\n" + c.bold + c.fg.blue + "[MAIN MENU]\nChoose deploy action:" + c.reset)
c.menu_item(c, "1", "Deploy redcloud on local machine")
c.menu_item(c, "2", "Deploy redcloud on remote ssh machine")
c.menu_item(c, "3", "Deploy redcloud on remote docker-machine")
c.menu_item(c, "4", "Stop local or docker-machine redcloud deployment")
c.menu_item(c, "5", "Stop remote ssh redcloud deployment")
c.menu_item(c, "6", "List available templates")
c.menu_item(c, "q", "Quit")
print("")
choice = input(">> ")
if choice == "1":
c.good_news(c, "Deploying redcloud locally")
deploy_local()
elif choice == "2":
deploy_remote_ssh()
elif choice == "3":
deploy_dockermachine()
elif choice == "4":
stop_out = run_cmd_output(DOCKER_DOWN)
# run_cmd_output("docker-compose kill")
if stop_out is not None:
c.good_news(c, "Stopped running redcloud stack")
else:
c.bad_news(c, "App Templates need to be stopped manually. Use 'docker-compose kill' to force Portainer down")
input("\n- Press Enter to continue -")
elif choice == "5": # Stop remote ssh.
c.question_news(c, "Target IP or hostname?")
target = input(">> ").strip()
c.question_news(c, "Target username? (Default: root)")
username = input(">> ").strip()
if username == "":
username = "root"
SSH_CMD = "ssh " + username+"@"+target+" "
stop_out = run_cmd_output(SSH_CMD + "cd redcloud;" + DOCKER_DOWN)
# run_cmd_output(SSH_CMD + "cd redcloud;" + "docker-compose kill")
if stop_out is not None:
c.good_news(c, "Stopped running ssh redcloud stack")
else:
c.bad_news(c, "App Templates need to be stopped manually. Use 'docker-compose kill' to force Portainer down")
input("\n- Press Enter to continue -")
elif choice == "6": # List templates
list_available()
elif choice == "q":
c.info_news(c, "Goodbye")
break
elif choice == "1337": # Heh
print_banner("top")
else:
c.bad_news(c, "Please choose a menu item number")
print_banner()
if __name__ == "__main__":
import sys
if sys.version_info[0] < 3:
raise Exception("\n\n\n== Must be using Python 3 ==\n")
print_banner()
if "bash" not in os.getenv("SHELL"):
c.info_news(c, "protip: redcloud works better using bash or sh")
if not os.geteuid()==0:
c.info_news(c, "protip: root on candidate is required to install docker")
menu_deploy_target()