Skip to content

Commit 99d8c92

Browse files
authored
Merge pull request #228 from yungwine/mytonctrl2_dev
fix node args
2 parents d00a8d0 + 53a5171 commit 99d8c92

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

mytoninstaller/mytoninstaller.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import json
88
import subprocess
99

10+
import pkg_resources
11+
1012
from mypylib.mypylib import MyPyClass, run_as_root, color_print
1113
from mypyconsole.mypyconsole import MyPyConsole
1214

1315
from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config
14-
from mytoninstaller.node_args import get_node_args, set_node_arg
16+
from mytoninstaller.node_args import get_node_args
1517
from mytoninstaller.utils import GetInitBlock
1618
from mytoncore.utils import dict2b64, str2bool, b642dict
1719

@@ -139,14 +141,9 @@ def set_node_argument(local, args):
139141
color_print("{red}Bad args. Usage:{endc} set_node_argument <arg-name> [arg-value] [-d (to delete)]")
140142
return
141143
arg_name = args[0]
142-
if len(args) == 1:
143-
set_node_arg(arg_name)
144-
else:
145-
arg_value = args[1]
146-
if arg_value == "-d":
147-
set_node_arg(arg_name, None)
148-
else:
149-
set_node_arg(arg_name, arg_value)
144+
args = [arg_name, args[1] if len(args) > 1 else ""]
145+
script_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'set_node_argument.py')
146+
run_as_root(['python3', script_path] + args)
150147
color_print("set_node_argument - {green}OK{endc}")
151148
#end define
152149

mytoninstaller/node_args.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from mypylib.mypylib import run_as_root
21

32

43
def get_validator_service():
@@ -34,36 +33,3 @@ def get_node_args(command: str = None):
3433
return result
3534
#end define
3635

37-
38-
def restart_node():
39-
exit_code = run_as_root(["systemctl", "daemon-reload"])
40-
if exit_code:
41-
raise Exception(f"`systemctl daemon-reload` failed with exit code {exit_code}")
42-
exit_code = run_as_root(["systemctl", "restart", "validator"])
43-
if exit_code:
44-
raise Exception(f"`systemctl restart validator` failed with exit code {exit_code}")
45-
#end define
46-
47-
48-
def set_node_arg(arg_name: str, arg_value: str = ''):
49-
"""
50-
:param arg_name:
51-
:param arg_value: arg value. if None, remove the arg; if empty string, argument is set without value
52-
:return:
53-
"""
54-
assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"'
55-
service = get_validator_service()
56-
command = get_node_start_command()
57-
if command is None:
58-
raise Exception('Cannot find node start command in service file')
59-
args = get_node_args(command)
60-
if arg_value is None:
61-
args.pop(arg_name, None)
62-
else:
63-
args[arg_name] = arg_value
64-
new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()])
65-
new_service = service.replace(command, new_command)
66-
c = f"with open('/etc/systemd/system/validator.service', 'w') as f: f.write('''{new_service}''')"
67-
run_as_root(['python3', '-c', f'"{c}"'])
68-
restart_node()
69-
#end define
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
import subprocess
3+
from mytoninstaller.node_args import get_node_args, get_node_start_command, get_validator_service
4+
5+
6+
def set_node_arg(arg_name: str, arg_value: str = ''):
7+
"""
8+
:param arg_name:
9+
:param arg_value: arg value. if None, remove the arg; if empty string, argument is set without value
10+
:return:
11+
"""
12+
assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"'
13+
service = get_validator_service()
14+
command = get_node_start_command()
15+
if command is None:
16+
raise Exception('Cannot find node start command in service file')
17+
args = get_node_args(command)
18+
if arg_value == '-d':
19+
args.pop(arg_name, None)
20+
else:
21+
args[arg_name] = arg_value
22+
new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()])
23+
new_service = service.replace(command, new_command)
24+
with open('/etc/systemd/system/validator.service', 'w') as f:
25+
f.write(new_service)
26+
restart_node()
27+
#end define
28+
29+
30+
def restart_node():
31+
exit_code = subprocess.run(["systemctl", "daemon-reload"]).returncode
32+
if exit_code:
33+
raise Exception(f"`systemctl daemon-reload` failed with exit code {exit_code}")
34+
exit_code = subprocess.run(["systemctl", "restart", "validator"]).returncode
35+
if exit_code:
36+
raise Exception(f"`systemctl restart validator` failed with exit code {exit_code}")
37+
#end define
38+
39+
40+
if __name__ == '__main__':
41+
name = sys.argv[1]
42+
value = sys.argv[2] if len(sys.argv) > 2 else ''
43+
set_node_arg(name, value)

0 commit comments

Comments
 (0)