Skip to content

Commit 4776eaa

Browse files
Input license key from manage-deployment (#139)
1 parent ed24fd3 commit 4776eaa

File tree

1 file changed

+67
-49
lines changed

1 file changed

+67
-49
lines changed

manage-deployment.py

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,55 @@
1010

1111
METLO_DEFAULT_DIR = "/opt/metlo"
1212

13-
METLO_DIR = os.environ.get('METLO_DIR', METLO_DEFAULT_DIR)
14-
ENV_PATH = os.path.join(METLO_DIR, '.env')
15-
LICENSE_PATH = os.path.join(METLO_DIR, 'LICENSE_KEY')
16-
FILES_TO_PULL = ['docker-compose.yaml', 'init.sql', 'metlo-config.yaml']
17-
UPDATE_FILES_TO_PULL = ['docker-compose.yaml', 'init.sql']
18-
IMAGES = ['backend', 'frontend', 'jobrunner', 'suricata-daemon']
13+
METLO_DIR = os.environ.get("METLO_DIR", METLO_DEFAULT_DIR)
14+
ENV_PATH = os.path.join(METLO_DIR, ".env")
15+
LICENSE_PATH = os.path.join(METLO_DIR, "LICENSE_KEY")
16+
FILES_TO_PULL = ["docker-compose.yaml", "init.sql", "metlo-config.yaml"]
17+
UPDATE_FILES_TO_PULL = ["docker-compose.yaml", "init.sql"]
18+
IMAGES = ["backend", "frontend", "jobrunner", "suricata-daemon"]
1919

2020

2121
def get_file(file_name):
22-
src = f'https://raw.githubusercontent.com/metlo-labs/metlo/master/{file_name}'
22+
src = f"https://raw.githubusercontent.com/metlo-labs/metlo/master/{file_name}"
2323
request = urllib.request.Request(src)
2424
with urllib.request.urlopen(request) as response:
25-
data = response.read().decode('utf-8')
26-
with open(os.path.join(METLO_DIR, file_name), 'w') as f:
25+
data = response.read().decode("utf-8")
26+
with open(os.path.join(METLO_DIR, file_name), "w") as f:
2727
f.write(data)
2828

2929

3030
def get_current_ip():
31-
request = urllib.request.Request('http://checkip.amazonaws.com')
31+
request = urllib.request.Request("http://checkip.amazonaws.com")
3232
with urllib.request.urlopen(request) as response:
33-
data = response.read().decode('utf-8')
34-
return data.strip('\n')
33+
data = response.read().decode("utf-8")
34+
return data.strip("\n")
3535

3636

3737
def gen_secret(l):
38-
return ''.join(
38+
return "".join(
3939
secrets.choice(string.ascii_uppercase + string.ascii_lowercase)
4040
for _ in range(l)
4141
)
4242

4343

44-
def write_env():
45-
encryption_key = b64encode(secrets.token_bytes(32)).decode('UTF-8')
44+
def get_license_key(quiet):
45+
license = os.environ.get("LICENSE_KEY")
46+
if license is None:
47+
if quiet:
48+
license = ""
49+
else:
50+
license = input("[Optional] Please enter your license key: ")
51+
return license
52+
53+
54+
def write_env(quiet):
55+
encryption_key = b64encode(secrets.token_bytes(32)).decode("UTF-8")
4656
express_secret = gen_secret(32)
4757
clickhouse_user = gen_secret(16)
4858
clickhouse_password = gen_secret(16)
4959
instance_ip = get_current_ip()
50-
init_env_file = f'''
60+
license_key = get_license_key(quiet)
61+
init_env_file = f"""
5162
ENCRYPTION_KEY="{encryption_key}"
5263
BACKEND_URL="http://{instance_ip}:8081"
5364
EXPRESS_SECRET="{express_secret}"
@@ -56,58 +67,59 @@ def write_env():
5667
NUM_WORKERS=1
5768
SANDBOX_MODE="false"
5869
DISABLE_LOGGING_STATS="false"
59-
'''.strip()
60-
with open(ENV_PATH, 'w') as f:
70+
LICENSE_KEY="{license_key}"
71+
""".strip()
72+
with open(ENV_PATH, "w") as f:
6173
f.write(init_env_file)
6274

6375

64-
def init_env():
76+
def init_env(quiet=False):
6577
if os.path.exists(ENV_PATH):
6678
return
67-
print('Initializing Environment...')
68-
write_env()
79+
print("Initializing Environment...")
80+
write_env(quiet)
6981

7082

7183
def pull_files():
72-
print('Pulling Files...')
84+
print("Pulling Files...")
7385
for f in FILES_TO_PULL:
7486
get_file(f)
7587

7688

7789
def update_files():
78-
print('Pulling Updated Files...')
90+
print("Pulling Updated Files...")
7991
for f in UPDATE_FILES_TO_PULL:
8092
get_file(f)
8193

8294

8395
def pull_dockers():
84-
print('Pulling Docker Images...')
96+
print("Pulling Docker Images...")
8597
for e in IMAGES:
86-
subprocess.run(['docker', 'pull', f'metlo/{e}'])
98+
subprocess.run(["docker", "pull", f"metlo/{e}"])
8799

88100

89-
def init():
101+
def init(quiet=False):
90102
if not os.path.exists(METLO_DIR):
91103
os.mkdir(METLO_DIR)
92-
init_env()
104+
init_env(quiet)
93105
pull_files()
94106
pull_dockers()
95107

96108

97109
def start():
98-
subprocess.run(['docker-compose', 'up', '-d'], cwd=METLO_DIR)
110+
subprocess.run(["docker-compose", "up", "-d"], cwd=METLO_DIR)
99111

100112

101113
def stop():
102-
subprocess.run(['docker-compose', 'down'], cwd=METLO_DIR)
114+
subprocess.run(["docker-compose", "down"], cwd=METLO_DIR)
103115

104116

105117
def restart():
106-
subprocess.run(['docker-compose', 'restart'], cwd=METLO_DIR)
118+
subprocess.run(["docker-compose", "restart"], cwd=METLO_DIR)
107119

108120

109121
def status():
110-
subprocess.run(['docker-compose', 'ps'], cwd=METLO_DIR)
122+
subprocess.run(["docker-compose", "ps"], cwd=METLO_DIR)
111123

112124

113125
def update():
@@ -119,34 +131,40 @@ def update():
119131

120132
def main():
121133
parser = argparse.ArgumentParser()
122-
subparsers = parser.add_subparsers(dest='command')
134+
subparsers = parser.add_subparsers(dest="command")
123135

124-
init_cmd = subparsers.add_parser('init')
125-
init_env_cmd = subparsers.add_parser('init-env')
126-
start_cmd = subparsers.add_parser('start')
127-
stop_cmd = subparsers.add_parser('stop')
128-
status_cmd = subparsers.add_parser('status')
129-
restart_cmd = subparsers.add_parser('restart')
130-
update_cmd = subparsers.add_parser('update')
136+
init_cmd = subparsers.add_parser("init")
137+
init_cmd.add_argument(
138+
"-q", "--quiet", help="Do not prompt for license key", action="store_true"
139+
)
140+
init_env_cmd = subparsers.add_parser("init-env")
141+
init_env_cmd.add_argument(
142+
"-q", "--quiet", help="Do not prompt for license key", action="store_true"
143+
)
144+
start_cmd = subparsers.add_parser("start")
145+
stop_cmd = subparsers.add_parser("stop")
146+
status_cmd = subparsers.add_parser("status")
147+
restart_cmd = subparsers.add_parser("restart")
148+
update_cmd = subparsers.add_parser("update")
131149

132150
args = parser.parse_args()
133151

134152
command = args.command
135-
if command == 'init':
136-
init()
137-
elif command == 'init-env':
138-
init_env()
139-
elif command == 'start':
153+
if command == "init":
154+
init(args.quiet)
155+
elif command == "init-env":
156+
init_env(args.quiet)
157+
elif command == "start":
140158
start()
141-
elif command == 'stop':
159+
elif command == "stop":
142160
stop()
143-
elif command == 'restart':
161+
elif command == "restart":
144162
restart()
145-
elif command == 'update':
163+
elif command == "update":
146164
update()
147-
elif command == 'status':
165+
elif command == "status":
148166
status()
149167

150168

151-
if __name__ == '__main__':
169+
if __name__ == "__main__":
152170
main()

0 commit comments

Comments
 (0)