Add progress hint

This commit is contained in:
Kirigaya Kazuto 2022-02-05 23:26:26 +00:00
parent f4d46d9f94
commit 55e412fcb1
2 changed files with 20 additions and 6 deletions

View File

@ -1,12 +1,22 @@
import os
import sys
import json
import uuid
import time
import subprocess
def write_progress(content):
sys.stdout.write("\033[2K\r{}".format(content))
time.sleep(0.1)
def write_finish(content):
sys.stdout.write("\033[2K\r{}\n".format(content))
def add_runner(runner_pool, content):
runner_id = str(uuid.uuid4())
print('Adding runner {}...'.format(runner_id))
with open("/root/runner/{}.sh".format(runner_id), "w") as f:
f.write(content)
@ -16,7 +26,7 @@ def add_runner(runner_pool, content):
def run_cmd(args):
print("[Run] {}".format(' '.join(args)))
write_progress("[Run] {}".format(' '.join(args)))
subprocess.check_call(args)
@ -38,7 +48,7 @@ if __name__ == "__main__":
config = json.loads(config)
for idx, info in enumerate(config):
print('Loading {} of {} parts with type {}...'.format(idx + 1, len(config), info['type']))
write_progress('Loading {} of {} parts, type: {}...'.format(idx + 1, len(config), info['type']))
if info['type'] == 'mux':
add_runner(runners, '''#!/bin/bash
@ -137,7 +147,9 @@ systemctl start nginx
exec /root/bin/trojan-go -config /root/conf/{}.json
'''.format(config_id))
else:
print('Unknown type: {}'.format(info['type']))
write_finish('Unknown type: {}'.format(info['type']))
write_finish('{} parts loaded.'.format(len(config)))
print('Adding service template...')
with open("/lib/systemd/system/wg-ops-runner@.service", "w") as f:
@ -157,7 +169,9 @@ WantedBy=multi-user.target
run_cmd(["systemctl", "daemon-reload"])
for idx, runner_id in enumerate(runners):
print("Starting runner {} of {}...".format(idx + 1, len(runners)))
write_progress("Starting runner {} of {}...".format(idx + 1, len(runners)))
run_cmd(["systemctl", "start", "wg-ops-runner@{}".format(runner_id)])
write_finish('{} runner started.'.format(len(runners)))
print('Bootstrap finished.')

View File

@ -362,7 +362,7 @@ class Parser:
self.result_postup.extend(self.result_container_prebootstrap)
self.result_postup.append('PostUp={}'.format(
self.get_podman_cmd_with('CT_GATEWAY=$(/usr/bin/python3 {} {}); podman exec -e GATEWAY_IP=$CT_GATEWAY -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format(
self.get_podman_cmd_with('CT_GATEWAY=$(/usr/bin/python3 {} {}); podman exec -t -e GATEWAY_IP=$CT_GATEWAY -e WG_PORT={} {} /usr/bin/python3 /root/app/bootstrap.py'.format(
path_get_gateway, self.get_container_network_name(), self.wg_port, self.get_container_name()))
))