From 13ffb279d9d89f1f85f3e42e41c3876633f1832a Mon Sep 17 00:00:00 2001 From: yungwine Date: Mon, 1 Apr 2024 19:46:37 +0800 Subject: [PATCH 1/2] fix set node args --- mytoninstaller/mytoninstaller.py | 13 +++---- mytoninstaller/node_args.py | 34 ---------------- mytoninstaller/scripts/set_node_argument.py | 43 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 mytoninstaller/scripts/set_node_argument.py diff --git a/mytoninstaller/mytoninstaller.py b/mytoninstaller/mytoninstaller.py index 026c2423..0915c253 100644 --- a/mytoninstaller/mytoninstaller.py +++ b/mytoninstaller/mytoninstaller.py @@ -7,6 +7,8 @@ import json import subprocess +import pkg_resources + from mypylib.mypylib import MyPyClass, run_as_root, color_print from mypyconsole.mypyconsole import MyPyConsole @@ -139,14 +141,9 @@ def set_node_argument(local, args): color_print("{red}Bad args. Usage:{endc} set_node_argument [arg-value] [-d (to delete)]") return arg_name = args[0] - if len(args) == 1: - set_node_arg(arg_name) - else: - arg_value = args[1] - if arg_value == "-d": - set_node_arg(arg_name, None) - else: - set_node_arg(arg_name, arg_value) + args = [arg_name, args[1] if len(args) > 1 else ""] + script_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'set_node_argument.py') + run_as_root(['python3', script_path] + args) color_print("set_node_argument - {green}OK{endc}") #end define diff --git a/mytoninstaller/node_args.py b/mytoninstaller/node_args.py index 6b451ec4..ec99eea3 100644 --- a/mytoninstaller/node_args.py +++ b/mytoninstaller/node_args.py @@ -1,4 +1,3 @@ -from mypylib.mypylib import run_as_root def get_validator_service(): @@ -34,36 +33,3 @@ def get_node_args(command: str = None): return result #end define - -def restart_node(): - exit_code = run_as_root(["systemctl", "daemon-reload"]) - if exit_code: - raise Exception(f"`systemctl daemon-reload` failed with exit code {exit_code}") - exit_code = run_as_root(["systemctl", "restart", "validator"]) - if exit_code: - raise Exception(f"`systemctl restart validator` failed with exit code {exit_code}") -#end define - - -def set_node_arg(arg_name: str, arg_value: str = ''): - """ - :param arg_name: - :param arg_value: arg value. if None, remove the arg; if empty string, argument is set without value - :return: - """ - assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"' - service = get_validator_service() - command = get_node_start_command() - if command is None: - raise Exception('Cannot find node start command in service file') - args = get_node_args(command) - if arg_value is None: - args.pop(arg_name, None) - else: - args[arg_name] = arg_value - new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()]) - new_service = service.replace(command, new_command) - c = f"with open('/etc/systemd/system/validator.service', 'w') as f: f.write('''{new_service}''')" - run_as_root(['python3', '-c', f'"{c}"']) - restart_node() -#end define diff --git a/mytoninstaller/scripts/set_node_argument.py b/mytoninstaller/scripts/set_node_argument.py new file mode 100644 index 00000000..b347b5b5 --- /dev/null +++ b/mytoninstaller/scripts/set_node_argument.py @@ -0,0 +1,43 @@ +import sys +import subprocess +from mytoninstaller.node_args import get_node_args, get_node_start_command, get_validator_service + + +def set_node_arg(arg_name: str, arg_value: str = ''): + """ + :param arg_name: + :param arg_value: arg value. if None, remove the arg; if empty string, argument is set without value + :return: + """ + assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"' + service = get_validator_service() + command = get_node_start_command() + if command is None: + raise Exception('Cannot find node start command in service file') + args = get_node_args(command) + if arg_value == '-d': + args.pop(arg_name, None) + else: + args[arg_name] = arg_value + new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()]) + new_service = service.replace(command, new_command) + with open('/etc/systemd/system/validator.service', 'w') as f: + f.write(new_service) + restart_node() +#end define + + +def restart_node(): + exit_code = subprocess.run(["systemctl", "daemon-reload"]).returncode + if exit_code: + raise Exception(f"`systemctl daemon-reload` failed with exit code {exit_code}") + exit_code = subprocess.run(["systemctl", "restart", "validator"]).returncode + if exit_code: + raise Exception(f"`systemctl restart validator` failed with exit code {exit_code}") +#end define + + +if __name__ == '__main__': + name = sys.argv[1] + value = sys.argv[2] if len(sys.argv) > 2 else '' + set_node_arg(name, value) From 53a5171db972c9a37947a7c56c575b27cb6bd24e Mon Sep 17 00:00:00 2001 From: yungwine Date: Mon, 1 Apr 2024 22:45:40 +0800 Subject: [PATCH 2/2] bugifx --- mytoninstaller/mytoninstaller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mytoninstaller/mytoninstaller.py b/mytoninstaller/mytoninstaller.py index 0915c253..56daf47e 100644 --- a/mytoninstaller/mytoninstaller.py +++ b/mytoninstaller/mytoninstaller.py @@ -13,7 +13,7 @@ from mypyconsole.mypyconsole import MyPyConsole from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config -from mytoninstaller.node_args import get_node_args, set_node_arg +from mytoninstaller.node_args import get_node_args from mytoninstaller.utils import GetInitBlock from mytoncore.utils import dict2b64, str2bool, b642dict