Skip to content

Commit 5294f12

Browse files
authored
Merge pull request #406 from yungwine/remote-controller
Remote controller
2 parents 5177df7 + 6595747 commit 5294f12

File tree

13 files changed

+162
-22
lines changed

13 files changed

+162
-22
lines changed

modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Setting:
6161
'ChatId': Setting('alert-bot', None, 'Alerting Telegram chat id'),
6262
'auto_backup': Setting('validator', None, 'Make validator backup every election'),
6363
'auto_backup_path': Setting('validator', '/tmp/mytoncore/auto_backups/', 'Path to store auto-backups'),
64+
'onlyNode': Setting(None, None, 'MyTonCtrl will work only for collecting validator telemetry (if `sendTelemetry` is True), without participating in Elections and etc.')
6465
}
6566

6667

modules/alert_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def init(self):
142142
from modules.validator import ValidatorModule
143143
self.validator_module = ValidatorModule(self.ton, self.local)
144144
self.hostname = get_hostname()
145-
self.ip = self.ton.get_validator_engine_ip()
145+
self.ip = self.ton.get_node_ip()
146146
self.set_global_vars()
147147
self.inited = True
148148

modules/backups.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def create_tmp_ton_dir(self):
2727
self.create_keyring(dir_name_db)
2828
return dir_name
2929

30+
@staticmethod
31+
def run_create_backup(args):
32+
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
33+
return subprocess.run(["bash", backup_script_path] + args, timeout=5)
34+
3035
def create_backup(self, args):
3136
if len(args) > 1:
3237
color_print("{red}Bad args. Usage:{endc} create_backup [filename]")
@@ -35,8 +40,7 @@ def create_backup(self, args):
3540
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir]
3641
if len(args) == 1:
3742
command_args += ["-d", args[0]]
38-
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
39-
process = subprocess.run(["bash", backup_script_path] + command_args, timeout=5)
43+
process = self.run_create_backup(command_args)
4044

4145
if process.returncode == 0:
4246
color_print("create_backup - {green}OK{endc}")
@@ -46,6 +50,11 @@ def create_backup(self, args):
4650
return process.returncode
4751
# end define
4852

53+
@staticmethod
54+
def run_restore_backup(args):
55+
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
56+
return run_as_root(["bash", restore_script_path] + args)
57+
4958
def restore_backup(self, args):
5059
if len(args) == 0 or len(args) > 2:
5160
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y]")
@@ -67,8 +76,7 @@ def restore_backup(self, args):
6776
ip = str(ip2int(get_own_ip()))
6877
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
6978

70-
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
71-
if run_as_root(["bash", restore_script_path] + command_args) == 0:
79+
if self.run_restore_backup(command_args) == 0:
7280
color_print("restore_backup - {green}OK{endc}")
7381
self.local.exit()
7482
else:

mytoncore/functions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,14 @@ def General(local):
551551
# scanner.Run()
552552

553553
# Start threads
554-
local.start_cycle(Elections, sec=600, args=(local, ton, ))
555554
local.start_cycle(Statistics, sec=10, args=(local, ))
555+
local.start_cycle(Telemetry, sec=60, args=(local, ton, ))
556+
local.start_cycle(OverlayTelemetry, sec=7200, args=(local, ton, ))
557+
if local.db.get("onlyNode"): # mytoncore service works only for telemetry
558+
thr_sleep()
559+
return
560+
561+
local.start_cycle(Elections, sec=600, args=(local, ton, ))
556562
local.start_cycle(Offers, sec=600, args=(local, ton, ))
557563
local.start_cycle(save_past_events, sec=300, args=(local, ton, ))
558564

@@ -562,8 +568,6 @@ def General(local):
562568
local.start_cycle(Complaints, sec=t, args=(local, ton, ))
563569
local.start_cycle(Slashing, sec=t, args=(local, ton, ))
564570

565-
local.start_cycle(Telemetry, sec=60, args=(local, ton, ))
566-
local.start_cycle(OverlayTelemetry, sec=7200, args=(local, ton, ))
567571
local.start_cycle(ScanLiteServers, sec=60, args=(local, ton,))
568572

569573
from modules.custom_overlays import CustomOverlayModule

mytoncore/mytoncore.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3820,13 +3820,16 @@ def GetNetworkName(self):
38203820
return "unknown"
38213821
#end define
38223822

3823-
def get_validator_engine_ip(self):
3823+
def get_node_ip(self):
38243824
try:
38253825
config = self.GetValidatorConfig()
38263826
return int2ip(config['addrs'][0]['ip'])
38273827
except:
38283828
return None
38293829

3830+
def get_validator_engine_ip(self):
3831+
return self.validatorConsole.addr.split(':')[0]
3832+
38303833
def GetFunctionBuffer(self, name, timeout=10):
38313834
timestamp = get_timestamp()
38323835
buff = self.local.buffer.get(name)

mytonctrl/mytonctrl.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,11 @@ def PrintLocalStatus(local, ton, adnlAddr, validatorIndex, validatorEfficiency,
762762
validatorVersion_text = local.translate("local_status_version_validator").format(validatorGitHash_text, validatorGitBranch_text)
763763

764764
color_print(local.translate("local_status_head"))
765+
node_ip = ton.get_validator_engine_ip()
766+
is_node_remote = node_ip != '127.0.0.1'
767+
if is_node_remote:
768+
nodeIpAddr_text = local.translate("node_ip_address").format(node_ip)
769+
color_print(nodeIpAddr_text)
765770
if ton.using_validator():
766771
print(validatorIndex_text)
767772
# print(validatorEfficiency_text)
@@ -776,7 +781,8 @@ def PrintLocalStatus(local, ton, adnlAddr, validatorIndex, validatorEfficiency,
776781

777782
print(disksLoad_text)
778783
print(mytoncoreStatus_text)
779-
print(validatorStatus_text)
784+
if not is_node_remote:
785+
print(validatorStatus_text)
780786
print(validator_out_of_sync_text)
781787
print(validator_out_of_ser_text)
782788
print(dbStatus_text)

mytonctrl/resources/translate.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@
249249
"ru": "{cyan}===[ Статус ноды ]==={endc}",
250250
"zh_TW": "{cyan}===[ 节点狀態 ]==={endc}"
251251
},
252+
"node_ip_address": {
253+
"en": "Node IP address: {{bold}}{0}{{endc}}",
254+
"ru": "IP адрес Ноды: {{bold}}{0}{{endc}}",
255+
"zh_TW": "節點 IP 地址: {{bold}}{0}{{endc}}"
256+
},
252257
"local_status_validator_index": {
253258
"en": "Validator index: {0}",
254259
"ru": "Индекс валидатора: {0}",

mytonctrl/scripts/restore_backup.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
name="backup.tar.gz"
22
mtc_dir="$HOME/.local/share/mytoncore"
33
ip=0
4+
user=$(logname)
45
# Get arguments
5-
while getopts n:m:i: flag
6+
while getopts n:m:i:u: flag
67
do
78
case "${flag}" in
89
n) name=${OPTARG};;
910
m) mtc_dir=${OPTARG};;
1011
i) ip=${OPTARG};;
12+
u) user=${OPTARG};;
1113
*)
1214
echo "Flag -${flag} is not recognized. Aborting"
1315
exit 1 ;;
@@ -38,19 +40,28 @@ if [ ! -d ${tmp_dir}/db ]; then
3840
fi
3941

4042
rm -rf /var/ton-work/db/keyring
41-
cp -rf ${tmp_dir}/db /var/ton-work
42-
cp -rf ${tmp_dir}/keys /var/ton-work
43-
cp -rfT ${tmp_dir}/mytoncore $mtc_dir
43+
44+
chown -R $user:$user ${tmp_dir}/mytoncore
45+
chown -R $user:$user ${tmp_dir}/keys
46+
47+
cp -rfp ${tmp_dir}/db /var/ton-work
48+
cp -rfp ${tmp_dir}/keys /var/ton-work
49+
cp -rfpT ${tmp_dir}/mytoncore $mtc_dir
4450

4551
chown -R validator:validator /var/ton-work/db/keyring
4652

4753
echo -e "${COLOR}[2/4]${ENDC} Extracted files from archive"
4854

4955
rm -r /var/ton-work/db/dht-*
5056

51-
python3 -c "import json;path='/var/ton-work/db/config.json';f=open(path);d=json.load(f);f.close();d['addrs'][0]['ip']=int($ip);f=open(path, 'w');f.write(json.dumps(d, indent=4));f.close()"
57+
if [ $ip -ne 0 ]; then
58+
echo "Replacing IP in node config"
59+
python3 -c "import json;path='/var/ton-work/db/config.json';f=open(path);d=json.load(f);f.close();d['addrs'][0]['ip']=int($ip);f=open(path, 'w');f.write(json.dumps(d, indent=4));f.close()"
60+
else
61+
echo "IP is not provided, skipping IP replacement"
62+
fi
5263

53-
echo -e "${COLOR}[3/4]${ENDC} Deleted DHT files, replaced IP in node config"
64+
echo -e "${COLOR}[3/4]${ENDC} Deleted DHT files"
5465

5566
systemctl start validator
5667
systemctl start mytoncore

mytoninstaller/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def backup_config(local, config_path):
4444

4545

4646
def BackupVconfig(local):
47+
if local.buffer.only_mtc:
48+
return
4749
local.add_log("Backup validator config file 'config.json' to 'config.json.backup'", "debug")
4850
vconfig_path = local.buffer.vconfig_path
4951
backupPath = vconfig_path + ".backup"

mytoninstaller/mytoninstaller.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
enable_ls_proxy,
3131
enable_ton_storage,
3232
enable_ton_storage_provider,
33-
EnableMode
33+
EnableMode, ConfigureFromBackup, ConfigureOnlyNode
3434
)
3535
from mytoninstaller.config import (
3636
CreateLocalConfig,
@@ -276,6 +276,17 @@ def General(local, console):
276276
mx = sys.argv.index("-m")
277277
mode = sys.argv[mx+1]
278278
local.buffer.mode = mode
279+
if "--only-mtc" in sys.argv:
280+
ox = sys.argv.index("--only-mtc")
281+
local.buffer.only_mtc = str2bool(sys.argv[ox+1])
282+
if "--only-node" in sys.argv:
283+
ox = sys.argv.index("--only-node")
284+
local.buffer.only_node = str2bool(sys.argv[ox+1])
285+
if "--backup" in sys.argv:
286+
bx = sys.argv.index("--backup")
287+
backup = sys.argv[bx+1]
288+
if backup != "none":
289+
local.buffer.backup = backup
279290
#end if
280291

281292
FirstMytoncoreSettings(local)
@@ -286,6 +297,8 @@ def General(local, console):
286297
BackupMconfig(local)
287298
CreateSymlinks(local)
288299
EnableMode(local)
300+
ConfigureFromBackup(local)
301+
ConfigureOnlyNode(local)
289302
#end define
290303

291304

0 commit comments

Comments
 (0)