Skip to content

Commit b4ba97b

Browse files
authored
Merge pull request #221 from yungwine/mytonctrl2_dev
node args
2 parents abe5eaa + 3a0b71f commit b4ba97b

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

mytoninstaller/mytoninstaller.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mypyconsole.mypyconsole import MyPyConsole
1212

1313
from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config
14+
from mytoninstaller.node_args import get_node_args, set_node_arg
1415
from mytoninstaller.utils import GetInitBlock
1516
from mytoncore.utils import dict2b64, str2bool, b642dict
1617

@@ -62,6 +63,7 @@ def inject_globals(func):
6263
console.name = "MyTonInstaller"
6364
console.color = console.RED
6465
console.AddItem("status", inject_globals(Status), "Print TON component status")
66+
console.AddItem("set_node_argument", inject_globals(set_node_argument), "Set node argument")
6567
console.AddItem("enable", inject_globals(Enable), "Enable some function")
6668
console.AddItem("update", inject_globals(Enable), "Update some function: 'JR' - jsonrpc. Example: 'update JR'")
6769
console.AddItem("plsc", inject_globals(PrintLiteServerConfig), "Print lite-server config")
@@ -111,16 +113,50 @@ def Status(local, args):
111113
liteserver_key = keys_dir + "liteserver"
112114
liteserver_pubkey = liteserver_key + ".pub"
113115

116+
statuses = {
117+
'Full node status': os.path.isfile(local.buffer.vconfig_path),
118+
'Mytoncore status': os.path.isfile(local.buffer.mconfig_path),
119+
'V.console status': os.path.isfile(server_key) or os.path.isfile(client_key),
120+
'Liteserver status': os.path.isfile(liteserver_pubkey)
121+
}
122+
123+
color_print("{cyan}===[ Services status ]==={endc}")
124+
for item in statuses.items():
125+
status = '{green}enabled{endc}' if item[1] else '{red}disabled{endc}'
126+
color_print(f"{item[0]}: {status}")
127+
128+
node_args = get_node_args()
129+
color_print("{cyan}===[ Node arguments ]==={endc}")
130+
for key, value in node_args.items():
131+
print(f"{key}: {value}")
132+
#end define
133+
134+
135+
def restart_node():
136+
exit_code = run_as_root(["systemctl", "daemon-reload"])
137+
if not exit_code:
138+
raise Exception(f"`systemctl daemon-reload` failed with exit code {exit_code}")
139+
exit_code = run_as_root(["systemctl", "restart", "validator"])
140+
if not exit_code:
141+
raise Exception(f"`systemctl restart validator` failed with exit code {exit_code}")
142+
#end define
114143

115-
fnStatus = os.path.isfile(local.buffer.vconfig_path)
116-
mtcStatus = os.path.isfile(local.buffer.mconfig_path)
117-
vcStatus = os.path.isfile(server_key) or os.path.isfile(client_key)
118-
lsStatus = os.path.isfile(liteserver_pubkey)
119144

120-
print("Full node status:", fnStatus)
121-
print("Mytoncore status:", mtcStatus)
122-
print("V.console status:", vcStatus)
123-
print("Liteserver status:", lsStatus)
145+
def set_node_argument(local, args):
146+
if len(args) < 1:
147+
color_print("{red}Bad args. Usage:{endc} set_node_argument <arg-name> [arg-value] [-d (to delete)]")
148+
return
149+
arg_name = args[0]
150+
if len(args) == 1:
151+
set_node_arg(arg_name)
152+
else:
153+
arg_value = args[1]
154+
if arg_value == "-d":
155+
set_node_arg(arg_name, None)
156+
else:
157+
set_node_arg(arg_name, arg_value)
158+
restart_node()
159+
color_print("set_node_argument - {green}OK{endc}")
124160
#end define
125161

126162

mytoninstaller/node_args.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
3+
def get_validator_service():
4+
path = '/etc/systemd/system/validator.service'
5+
with open(path, 'r') as f:
6+
return f.read()
7+
8+
9+
def get_node_start_command():
10+
service = get_validator_service()
11+
for line in service.split('\n'):
12+
if 'ExecStart' in line:
13+
return line.split('=')[1].strip()
14+
15+
16+
def get_node_args(command: str = None):
17+
if command is None:
18+
command = get_node_start_command()
19+
result = {}
20+
key = ''
21+
for c in command.split(' ')[1:]:
22+
if c.startswith('--') or c.startswith('-'):
23+
if key:
24+
result[key] = ''
25+
key = c
26+
elif key:
27+
result[key] = c
28+
key = ''
29+
return result
30+
31+
32+
def set_node_arg(arg_name: str, arg_value: str = ''):
33+
"""
34+
:param arg_name:
35+
:param arg_value: arg value. if None, remove the arg; if empty string, argument is set without value
36+
:return:
37+
"""
38+
assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"'
39+
service = get_validator_service()
40+
command = get_node_start_command()
41+
if command is None:
42+
raise Exception('Cannot find node start command in service file')
43+
args = get_node_args(command)
44+
if arg_value is None:
45+
args.pop(arg_name, None)
46+
else:
47+
args[arg_name] = arg_value
48+
new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()])
49+
new_service = service.replace(command, new_command)
50+
with open('/etc/systemd/system/validator.service', 'w') as f:
51+
f.write(new_service)

0 commit comments

Comments
 (0)