from os.path import dirname as direction
from sys import path
from mypylib.mypylib import *
path.append(direction(path[0]))
# validator.service
with open("/etc/systemd/system/validator.service", 'rt') as file:
text = file.read()
lines = text.split('\n')
for i in range(len(lines)):
line = lines[i]
if 'ExecStart' not in line:
continue
if 'ton-global.config.json' in line:
lines[i] += line.replace('validator-engine/ton-global.config.json', 'global.config.json')
with open('/etc/systemd/system/validator.service', 'wt') as file:
text = "\n".join(lines)
file.write(text)
subprocess.run(['systemctl', 'daemon-reload'])
scripts/set_archive_ttl.py
import subprocess
import sys
with open('/etc/systemd/system/validator.service', 'r') as file:
service = file.read()
exec_start = None
for line in service.split('\n'):
if line.startswith('ExecStart'):
exec_start = line
break
if '--archive-ttl' in exec_start:
print('Archive TTL is already set')
sys.exit(100)
default_command = ('ExecStart = /usr/bin/ton/validator-engine/validator-engine --threads --daemonize --global-config '
'/usr/bin/ton/global.config.json --db /var/ton-work/db/ --logname /var/ton-work/log '
'--state-ttl 604800 --verbosity')
command_split = exec_start.split()
command_split.pop(command_split.index('--threads') + 1) # pop threads value since it's different for each node
command_split.pop(command_split.index('--verbosity') + 1) # pop verbosity value
if ' '.join(command_split) != default_command:
print('ExecStart is not default. Please set archive-ttl manually in `/etc/systemd/system/validator.service`.')
sys.exit(101)
archive_ttl = sys.argv[1]
new_exec_start = f"{exec_start} --archive-ttl {archive_ttl}"
with open('/etc/systemd/system/validator.service', 'w') as file:
file.write(service.replace(exec_start, new_exec_start))
subprocess.run(['systemctl', 'daemon-reload'])
subprocess.run(['systemctl', 'restart', 'validator'])
scripts/xrestart.py
import os
import pwd
import subprocess
import sys
import time
def x_guard():
timestamp = int(sys.argv[1])
args = sys.argv[2:]
while True:
time.sleep(1)
time_now = int(time.time())
if time_now > timestamp:
x_cmd(args)
print("exit")
sys.exit(0)
def x_cmd(input_args):
print(f"inputArgs: {input_args}")
# stop validator
args = ['systemctl', 'stop', 'validator']
subprocess.run(args)
with open('/etc/systemd/system/validator.service', 'rt') as file:
text = file.read()
lines = text.split('\n')
for line in lines:
if 'ExecStart' not in line:
continue
exec_start = line.replace('ExecStart = ', '')
args = exec_start.split()
print(f"ExecStart args: {args}")
args += input_args
pw_record = pwd.getpwnam('validator')
user_uid = pw_record.pw_uid
user_gid = pw_record.pw_gid
# start with args
print(f"args: {args}")
process = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
preexec_fn=demote(user_uid, user_gid)
)
process.wait()
text = process.stdout.read().decode()
print(f"text: {text}")
# Exit program
sys.exit(0)
def demote(user_uid, user_gid):
def result():
os.setgid(user_gid)
os.setuid(user_uid)
for flag in ['n', 'u', 'l']:
os.system(f"ulimit -{flag} 1")
return result
# Start of the program
if __name__ == "__main__":
x_guard()
scripts/upgrade.py
scripts/set_archive_ttl.py
scripts/xrestart.py