Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions mytoninstaller/mytoninstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import json
import subprocess

import pkg_resources

from mypylib.mypylib import MyPyClass, run_as_root, color_print
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

Expand Down Expand Up @@ -139,14 +141,9 @@ def set_node_argument(local, args):
color_print("{red}Bad args. Usage:{endc} set_node_argument <arg-name> [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

Expand Down
34 changes: 0 additions & 34 deletions mytoninstaller/node_args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from mypylib.mypylib import run_as_root


def get_validator_service():
Expand Down Expand Up @@ -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
43 changes: 43 additions & 0 deletions mytoninstaller/scripts/set_node_argument.py
Original file line number Diff line number Diff line change
@@ -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)