-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
83 lines (74 loc) · 3.69 KB
/
config.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
#!/usr/bin/env python3
##############################################################
# #
# .::::::. :: :: #
# ;;;` ` ;;; ;;; #
# '[==/[[[[,[[[[cc,,. ,ccc, ,c[[[cc, ,ccc,'[[, [[, [[' #
# ''' $$$$"""$$$ $$$cc$$$ $$""""Y$$$$$"c$$$Y$ $$$ $P #
# 88b dP888 "88o888 88888o,,od8P888 88 "88"888 #
# "YMmMY" MMM YMM "YUM" MP"YUMMMP" "YUMMP "M "M" #
# #
# .::::::..:: . .::::::. :::::::.. . : #
# ;;;` `';;, ;; ;;;' ;;`;; ;;;;``;;;; ;;,. ;;; #
# '[==/[[[[,'[[, [[, [[' ,[[ '[[, [[[,/[[[' [[[[, ,[[[[, #
# ''' $ Y$c$$$c$P c$$$cc$$$c $$$$$$c $$$$$$$$"$$$ #
# 88b dP "88"888 888 888 888b "88bo,888 Y88" 888o #
# "YMmMY" "M "M" YMM \"\"`MMMM "W" MMM M' "MMM #
# #
# ShadowSWARM Setup Utility #
# DJ Stomp 2025 #
# "No Rights Reserved" #
# #
##############################################################
import os
import subprocess
from pathlib import Path
def prompt_user_input(prompt, default=None):
"""Prompt the user for input with an optional default."""
if default:
user_input = input(f"{prompt} [{default}]: ")
return user_input.strip() if user_input.strip() else default
else:
return input(f"{prompt}: ").strip()
def gather_configuration():
"""Prompt the user to gather configuration for Swarm setup."""
print("Welcome to ShadowSWARM.")
master_hostname = prompt_user_input("Enter the master node hostname", "localhost")
master_ip = subprocess.check_output(["hostname", "-I"]).decode().strip().split()[0]
print(f"Detected master node IP: {master_ip}")
print("Enter the hostnames or IPs of the worker nodes, separated by commas.")
worker_nodes = prompt_user_input("Worker node hostnames/IPs")
print("\nConfiguration Summary:")
print(f"Master Hostname: {master_hostname}")
print(f"Master IP: {master_ip}")
print(f"Worker Nodes: {worker_nodes}")
confirm = prompt_user_input("Is this configuration correct? (y/n)", "y").lower()
if confirm != "y":
print("Setup aborted. Please run the script again.")
exit(1)
return master_hostname, master_ip, worker_nodes
def write_env_file(master_hostname, master_ip, worker_nodes):
"""Write environment variables to a .env file."""
env_file = Path(".env")
worker_hostnames = ",".join(worker_nodes.split(","))
with open(env_file, "w") as f:
f.write(f"MASTER_HOSTNAME={master_hostname}\n")
f.write(f"MASTER_IP={master_ip}\n")
f.write(f"WORKER_HOSTNAMES={worker_hostnames}\n")
print(f"Configuration saved to {env_file.absolute()}.")
def run_bootstrap_script():
"""Run the bootstrap utility."""
try:
print("Starting ShadowSWARM bootstrap...")
subprocess.run(["bash", "./bootstrap.sh"], check=True)
print("Swarm setup complete!")
except subprocess.CalledProcessError as e:
print(f"An error occurred during Swarm setup: {e}")
exit(1)
def main():
"""Main function for the CLI."""
master_hostname, master_ip, worker_nodes = gather_configuration()
write_env_file(master_hostname, master_ip, worker_nodes)
run_bootstrap_script()
if __name__ == "__main__":
main()