-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_parallel.py
More file actions
executable file
·114 lines (94 loc) · 4.12 KB
/
deploy_parallel.py
File metadata and controls
executable file
·114 lines (94 loc) · 4.12 KB
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
#!/usr/bin/env python3
import subprocess
import concurrent.futures
import time
import json
import argparse
# --- CONFIGURAZIONE ---
GITHUB_TOKEN = ""
# --- FUNZIONI ---
def run_deploy(github_token, module, password):
cmd = [
"ansible-playbook", "vulnbox_deploy.yml",
"-i", "vulnbox,",
"-u", "root",
"--extra-vars", f"ansible_user=root ansible_password={password} token={github_token}",
"--extra-vars", "@.env.json",
"--extra-vars", f'{{"modules": ["{module}"]}}'
]
print(f"\n[INFO] Starting deploy for module: {module}\n{'='*50}")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in iter(process.stdout.readline, ''):
print(f"[{module}] {line.strip()}")
process.stdout.close()
returncode = process.wait()
if returncode == 0:
print(f"\n[SUCCESS] {module} completed successfully.\n{'='*50}")
else:
print(f"\n[ERROR] {module} failed with return code {returncode}.\n{'='*50}")
return (module, returncode == 0)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--modules', nargs='+', help='Lista dei moduli da deployare')
parser.add_argument('--token', default=GITHUB_TOKEN, help='GitHub token for authentication')
args = parser.parse_args()
selected_modules = args.modules
github_token = args.token
if not selected_modules:
selected_modules = ["common", "kickstarterpy", "s4dfarm", "packmate", "threesome", "dashboard", "wisscon"]
start_time = time.time()
kikstarter_flag = False
deploy_flag = False
if "common" in selected_modules:
selected_modules.remove("common")
# Step 1: deploy common
print("[STEP 1] Deploying 'common' module...")
module_common_result = run_deploy(github_token,"common", "root")
if not module_common_result[1]:
print("[FATAL] 'common' deploy failed. Aborting parallel deploys.")
return
if "kickstarterpy" in selected_modules:
selected_modules.remove("kickstarterpy")
kikstarter_flag = True
if "git_deploy" in selected_modules:
selected_modules.remove("git_deploy")
deploy_flag = True
# Step 2: read new root password
try:
with open(".env.json") as f:
data = json.load(f)
root_password = data["root_password"]
print(f"\n[INFO] Retrieved updated root password from .env.json.")
except Exception as e:
print(f"[ERROR] Failed to read new password: {e}")
return
# Step 3: deploy selected modules in parallel
print("\n[STEP 2] Deploying selected modules in parallel...\n" + "="*50)
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=len(selected_modules)) as executor:
futures = [executor.submit(run_deploy, github_token, mod, root_password) for mod in selected_modules]
for future in concurrent.futures.as_completed(futures):
module, success = future.result()
status = "[DONE]" if success else "[FAILED]"
print(f"[{module}] Deploy result: {status}")
except Exception as e:
print(f"[ERROR] An error occurred during parallel deployment: {e}")
if kikstarter_flag:
print("[STEP 3] Deploying 'kickstarterpy' module...")
module_kickstarterpy_result = run_deploy(github_token, "kickstarterpy", root_password)
if not module_kickstarterpy_result[1]:
print("[FATAL] 'kickstarterpy' deploy failed. Aborting parallel deploys.")
return
if deploy_flag:
print("[STEP 4] Deploying 'git_deploy' module...")
module_git_deploy_result = run_deploy(github_token, "git_deploy", root_password)
if not module_git_deploy_result[1]:
print("[FATAL] 'git_deploy' deploy failed. Aborting parallel deploys.")
return
# Profiling: tempo totale
end_time = time.time()
duration = end_time - start_time
minutes, seconds = divmod(duration, 60)
print(f"\n Tempo totale di esecuzione: {int(minutes)} min {seconds:.2f} sec")
if __name__ == "__main__":
main()