From 3e499ecf5f7e81c0e6ec74c4531c1071c6126a59 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 29 Jul 2025 18:07:53 +0800 Subject: [PATCH 1/4] update getting launched user in various scripts --- modules/backups.py | 7 +++++-- modules/btc_teleport.py | 6 ++++-- mytonctrl/scripts/btc_teleport1.sh | 5 +++-- mytonctrl/scripts/create_backup.sh | 5 +++-- mytonctrl/scripts/restore_backup.sh | 2 +- mytonctrl/utils.py | 5 +++++ mytoninstaller/mytoninstaller.py | 6 +++--- mytoninstaller/scripts/ton_http_api_installer.sh | 11 ++++++++++- mytoninstaller/settings.py | 4 +++- scripts/install.sh | 2 +- tools/extract_backup_node_keys.sh | 5 +++-- 11 files changed, 41 insertions(+), 17 deletions(-) diff --git a/modules/backups.py b/modules/backups.py index 36fb8bd7..b062b32d 100644 --- a/modules/backups.py +++ b/modules/backups.py @@ -7,6 +7,7 @@ from modules.module import MtcModule from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass +from mytonctrl.utils import get_current_user from mytoninstaller.config import get_own_ip @@ -29,8 +30,9 @@ def create_tmp_ton_dir(self): @staticmethod def run_create_backup(args): + user = get_current_user() backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh') - return subprocess.run(["bash", backup_script_path] + args, timeout=5) + return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5) def create_backup(self, args): if len(args) > 1: @@ -52,8 +54,9 @@ def create_backup(self, args): @staticmethod def run_restore_backup(args): + user = get_current_user() restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh') - return run_as_root(["bash", restore_script_path] + args) + return run_as_root(["bash", restore_script_path, "-u", user] + args) def restore_backup(self, args): if len(args) == 0 or len(args) > 3: diff --git a/modules/btc_teleport.py b/modules/btc_teleport.py index 4299c685..606d4670 100644 --- a/modules/btc_teleport.py +++ b/modules/btc_teleport.py @@ -6,6 +6,7 @@ from modules.module import MtcModule from mypylib.mypylib import run_as_root, color_print, bcolors, print_table +from mytonctrl.utils import get_current_user class BtcTeleportModule(MtcModule): @@ -59,12 +60,13 @@ def create_env_file(self, reinit=False): def add_daemon(self): start = f'{self.bin_dir}/oracle' script_path = pkg_resources.resource_filename('mytoninstaller', 'scripts/add2systemd.sh') - user = os.environ.get("USER", "root") + user = get_current_user() run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir]) def install(self, branch): script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport1.sh') - exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch]) + user = get_current_user() + exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user]) if exit_code != 0: raise Exception('Failed to install btc_teleport') script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh') diff --git a/mytonctrl/scripts/btc_teleport1.sh b/mytonctrl/scripts/btc_teleport1.sh index 94c88db7..cf1f2082 100644 --- a/mytonctrl/scripts/btc_teleport1.sh +++ b/mytonctrl/scripts/btc_teleport1.sh @@ -1,14 +1,15 @@ REPO="" SRC_DIR="" -USER=$(logname) +USER=${SUDO_USER:-$(logname)} BRANCH=master -while getopts s:r:b: flag +while getopts s:r:b:u: flag do case "${flag}" in s) SRC_DIR=${OPTARG};; r) REPO=${OPTARG};; b) BRANCH=${OPTARG};; + u) USER=${OPTARG};; *) echo "Flag -${flag} is not recognized. Aborting"; exit 1 ;; esac done diff --git a/mytonctrl/scripts/create_backup.sh b/mytonctrl/scripts/create_backup.sh index dbcfe5b8..5255e54b 100644 --- a/mytonctrl/scripts/create_backup.sh +++ b/mytonctrl/scripts/create_backup.sh @@ -1,16 +1,17 @@ dest="mytonctrl_backup_$(hostname)_$(date +%s).tar.gz" mtc_dir="$HOME/.local/share/mytoncore" -user=$(logname) +user=${SUDO_USER:-$(logname)} ton_dir="/var/ton-work" keys_dir="/var/ton-work/keys" # Get arguments -while getopts d:m:t:k: flag +while getopts d:m:t:k:u: flag do case "${flag}" in d) dest=${OPTARG};; m) mtc_dir=${OPTARG};; t) ton_dir=${OPTARG};; k) keys_dir=${OPTARG};; + u) user=${OPTARG};; *) echo "Flag -${flag} is not recognized. Aborting" exit 1 ;; diff --git a/mytonctrl/scripts/restore_backup.sh b/mytonctrl/scripts/restore_backup.sh index 042657d7..1cf3c61e 100644 --- a/mytonctrl/scripts/restore_backup.sh +++ b/mytonctrl/scripts/restore_backup.sh @@ -1,7 +1,7 @@ name="backup.tar.gz" mtc_dir="$HOME/.local/share/mytoncore" ip=0 -user=$(logname) +user=${SUDO_USER:-$(logname)} # Get arguments while getopts n:m:i:u: flag do diff --git a/mytonctrl/utils.py b/mytonctrl/utils.py index 643e7e80..a28451a7 100644 --- a/mytonctrl/utils.py +++ b/mytonctrl/utils.py @@ -1,3 +1,5 @@ +import os +import pwd import subprocess import time @@ -57,3 +59,6 @@ def GetColorInt(data, border, logic, ending=None): result = bcolors.red_text(data, ending) return result # end define + +def get_current_user(): + return pwd.getpwuid(os.getuid()).pw_name diff --git a/mytoninstaller/mytoninstaller.py b/mytoninstaller/mytoninstaller.py index 285efd3d..42a7c94c 100644 --- a/mytoninstaller/mytoninstaller.py +++ b/mytoninstaller/mytoninstaller.py @@ -11,6 +11,7 @@ from mypylib.mypylib import MyPyClass, run_as_root, color_print from mypyconsole.mypyconsole import MyPyConsole +from mytonctrl.utils import get_current_user from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config from mytoninstaller.node_args import get_node_args @@ -48,8 +49,7 @@ def Init(local, console): # create variables - user = os.environ.get("USER", "root") - local.buffer.user = user + local.buffer.user = get_current_user() local.buffer.vuser = "validator" local.buffer.cport = random.randint(2000, 65000) local.buffer.lport = random.randint(2000, 65000) @@ -207,7 +207,7 @@ def CreateLocalConfigFile(local, args): init_block["rootHash"] = b642hex(config_init_block['root_hash']) init_block["fileHash"] = b642hex(config_init_block['file_hash']) init_block_b64 = dict2b64(init_block) - user = local.buffer.user or os.environ.get("USER", "root") + user = local.buffer.user or get_current_user() args = ["python3", "-m", "mytoninstaller", "-u", user, "-e", "clc", "-i", init_block_b64] run_as_root(args) #end define diff --git a/mytoninstaller/scripts/ton_http_api_installer.sh b/mytoninstaller/scripts/ton_http_api_installer.sh index a5eab69c..30af759b 100644 --- a/mytoninstaller/scripts/ton_http_api_installer.sh +++ b/mytoninstaller/scripts/ton_http_api_installer.sh @@ -7,6 +7,16 @@ if [ "$(id -u)" != "0" ]; then exit 1 fi +user=${SUDO_USER:-$(logname)} + +while getopts u: flag +do + case "${flag}" in + u) user=${OPTARG};; + *) echo "Flag -${flag} is not recognized. Aborting"; exit 1 ;; + esac +done + # Цвета COLOR='\033[92m' ENDC='\033[0m' @@ -21,7 +31,6 @@ virtualenv ${venv_path} # install python3 packages echo -e "${COLOR}[2/4]${ENDC} Installing required packages" -user=$(logname) venv_pip3="${venv_path}/bin/pip3" ${venv_pip3} install ton-http-api chown -R ${user}:${user} ${venv_path} diff --git a/mytoninstaller/settings.py b/mytoninstaller/settings.py index 7d352223..872562f0 100644 --- a/mytoninstaller/settings.py +++ b/mytoninstaller/settings.py @@ -22,6 +22,7 @@ ip2int, Dict, int2ip ) +from mytonctrl.utils import get_current_user from mytoninstaller.utils import StartValidator, StartMytoncore, start_service, stop_service, get_ed25519_pubkey, \ disable_service, is_testnet, get_block_from_toncenter from mytoninstaller.config import SetConfig, GetConfig, get_own_ip, backup_config @@ -729,8 +730,9 @@ def do_enable_ton_http_api(local): if not os.path.exists('/usr/bin/ton/local.config.json'): from mytoninstaller.mytoninstaller import CreateLocalConfigFile CreateLocalConfigFile(local, []) + user = local.buffer.user or get_current_user() ton_http_api_installer_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'ton_http_api_installer.sh') - exit_code = run_as_root(["bash", ton_http_api_installer_path]) + exit_code = run_as_root(["bash", ton_http_api_installer_path, "-u", user]) if exit_code == 0: text = "do_enable_ton_http_api - {green}OK{endc}" else: diff --git a/scripts/install.sh b/scripts/install.sh index 1321f27c..d1341a8a 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -169,7 +169,7 @@ if [ "${user}" = "" ]; then # no user parent_name=$(ps -p $PPID -o comm=) user=$(whoami) if [ "$parent_name" = "sudo" ] || [ "$parent_name" = "su" ] || [ "$parent_name" = "python3" ]; then - user=$(logname) + user=${SUDO_USER:-$(logname)} fi fi echo "User: $user" diff --git a/tools/extract_backup_node_keys.sh b/tools/extract_backup_node_keys.sh index adc4448c..9ad0a786 100644 --- a/tools/extract_backup_node_keys.sh +++ b/tools/extract_backup_node_keys.sh @@ -2,16 +2,17 @@ name="backup.tar.gz" dest="cleared_backup_$(hostname)_$(date +%s).tar.gz" ton_db="" tmp_dir="tmp/backup" -user=$(logname) +user=${SUDO_USER:-$(logname)} # Get arguments -while getopts n:d:t: flag +while getopts n:d:t:u: flag do case "${flag}" in n) name=${OPTARG};; d) dest=${OPTARG};; t) ton_db=${OPTARG};; + u) user=${OPTARG};; *) echo "Flag -${flag} is not recognized. Aborting" exit 1 ;; From 9eaf688f58f47db81ebe39e09f05e93cf736fa9e Mon Sep 17 00:00:00 2001 From: yungwine Date: Wed, 30 Jul 2025 14:01:14 +0800 Subject: [PATCH 2/4] allow optional user arg for some commands --- modules/backups.py | 26 +++++++++++++++----------- modules/btc_teleport.py | 16 +++++++++------- mytonctrl/mytonctrl.py | 16 ++++++++++------ mytonctrl/utils.py | 10 ++++++++++ mytoninstaller/mytoninstaller.py | 6 ++++-- mytoninstaller/settings.py | 6 +++--- 6 files changed, 51 insertions(+), 29 deletions(-) diff --git a/modules/backups.py b/modules/backups.py index b062b32d..c79ca67a 100644 --- a/modules/backups.py +++ b/modules/backups.py @@ -7,7 +7,7 @@ from modules.module import MtcModule from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass -from mytonctrl.utils import get_current_user +from mytonctrl.utils import get_current_user, pop_user_from_args from mytoninstaller.config import get_own_ip @@ -29,20 +29,22 @@ def create_tmp_ton_dir(self): return dir_name @staticmethod - def run_create_backup(args): - user = get_current_user() + def run_create_backup(args, user: str = None): + if user is None: + user = get_current_user() backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh') return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5) def create_backup(self, args): - if len(args) > 1: - color_print("{red}Bad args. Usage:{endc} create_backup [filename]") + if len(args) > 3: + color_print("{red}Bad args. Usage:{endc} create_backup [filename] [-u ]") return tmp_dir = self.create_tmp_ton_dir() command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir] + user = pop_user_from_args(args) if len(args) == 1: command_args += ["-d", args[0]] - process = self.run_create_backup(command_args) + process = self.run_create_backup(command_args, user=user) if process.returncode == 0: color_print("create_backup - {green}OK{endc}") @@ -53,15 +55,17 @@ def create_backup(self, args): # end define @staticmethod - def run_restore_backup(args): - user = get_current_user() + def run_restore_backup(args, user: str = None): + if user is None: + user = get_current_user() restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh') return run_as_root(["bash", restore_script_path, "-u", user] + args) def restore_backup(self, args): - if len(args) == 0 or len(args) > 3: - color_print("{red}Bad args. Usage:{endc} restore_backup [-y] [--skip-create-backup]") + if len(args) == 0 or len(args) > 5: + color_print("{red}Bad args. Usage:{endc} restore_backup [-y] [--skip-create-backup] [-u ]") return + user = pop_user_from_args(args) if '-y' not in args: res = input( f'This action will overwrite existing configuration with contents of backup archive, please make sure that donor node is not in operation prior to this action. Proceed [y/n]') @@ -82,7 +86,7 @@ def restore_backup(self, args): ip = str(ip2int(get_own_ip())) command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip] - if self.run_restore_backup(command_args) == 0: + if self.run_restore_backup(command_args, user=user) == 0: self.ton.local.load_db() if self.ton.using_validator(): from modules.btc_teleport import BtcTeleportModule diff --git a/modules/btc_teleport.py b/modules/btc_teleport.py index 606d4670..80a7eafa 100644 --- a/modules/btc_teleport.py +++ b/modules/btc_teleport.py @@ -57,29 +57,31 @@ def create_env_file(self, reinit=False): with open(env_path, 'w') as f: f.write(text) - def add_daemon(self): + def add_daemon(self, user: str = None): start = f'{self.bin_dir}/oracle' script_path = pkg_resources.resource_filename('mytoninstaller', 'scripts/add2systemd.sh') - user = get_current_user() + if user is None: + user = get_current_user() run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir]) - def install(self, branch): + def install(self, branch: str, user: str = None): script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport1.sh') - user = get_current_user() + if user is None: + user = get_current_user() exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user]) if exit_code != 0: raise Exception('Failed to install btc_teleport') script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh') subprocess.run(["bash", script_path, "-s", self.src_dir]) - def init(self, reinstall=False, branch: str = 'master'): + def init(self, reinstall=False, branch: str = 'master', user: str = None): if os.path.exists(self.src_dir) and not reinstall: return self.local.add_log('Installing btc_teleport', 'info') os.makedirs(self.keystore_path, mode=0o700, exist_ok=True) - self.install(branch) + self.install(branch, user=user) self.create_env_file() - self.add_daemon() + self.add_daemon(user=user) self.local.add_log('Installed btc_teleport', 'info') @staticmethod diff --git a/mytonctrl/mytonctrl.py b/mytonctrl/mytonctrl.py index 8ae323fc..3160fa26 100755 --- a/mytonctrl/mytonctrl.py +++ b/mytonctrl/mytonctrl.py @@ -45,7 +45,8 @@ ) from mytoncore.telemetry import is_host_virtual from mytonctrl.migrate import run_migrations -from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt +from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt, \ + pop_user_from_args import sys, getopt, os @@ -331,9 +332,12 @@ def Update(local, args): #end define def Upgrade(local, ton, args: list): - if '--btc-teleport' in args: # upgrade --btc-teleport [branch] - branch = args[args.index('--btc-teleport') + 1] if len(args) > args.index('--btc-teleport') + 1 else 'master' - upgrade_btc_teleport(local, ton, reinstall=True, branch=branch) + if '--btc-teleport' in args: # upgrade --btc-teleport [branch] [-u ] + branch = 'master' + user = pop_user_from_args(args) + if len(args) > args.index('--btc-teleport') + 1: + branch = args[args.index('--btc-teleport') + 1] + upgrade_btc_teleport(local, ton, reinstall=True, branch=branch, user=user) return repo = "ton" author, repo, branch = check_git(args, repo, "upgrade") @@ -378,10 +382,10 @@ def Upgrade(local, ton, args: list): #end define -def upgrade_btc_teleport(local, ton, reinstall=False, branch: str = 'master'): +def upgrade_btc_teleport(local, ton, reinstall=False, branch: str = 'master', user = None): from modules.btc_teleport import BtcTeleportModule module = BtcTeleportModule(ton, local) - local.try_function(module.init, args=[reinstall, branch]) + local.try_function(module.init, args=[reinstall, branch, user]) def get_clang_major_version(): diff --git a/mytonctrl/utils.py b/mytonctrl/utils.py index a28451a7..16806da8 100644 --- a/mytonctrl/utils.py +++ b/mytonctrl/utils.py @@ -62,3 +62,13 @@ def GetColorInt(data, border, logic, ending=None): def get_current_user(): return pwd.getpwuid(os.getuid()).pw_name + +def pop_user_from_args(args: list): + if '-u' in args: + user_index = args.index('-u') + 1 + if user_index >= len(args): + raise Exception(f'User value not found after "-u" in args: {args}') + user = args.pop(user_index) + args.pop(args.index('-u')) + return user + return None diff --git a/mytoninstaller/mytoninstaller.py b/mytoninstaller/mytoninstaller.py index 42a7c94c..4d54d087 100644 --- a/mytoninstaller/mytoninstaller.py +++ b/mytoninstaller/mytoninstaller.py @@ -11,7 +11,7 @@ from mypylib.mypylib import MyPyClass, run_as_root, color_print from mypyconsole.mypyconsole import MyPyConsole -from mytonctrl.utils import get_current_user +from mytonctrl.utils import get_current_user, pop_user_from_args from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config from mytoninstaller.node_args import get_node_args @@ -207,7 +207,9 @@ def CreateLocalConfigFile(local, args): init_block["rootHash"] = b642hex(config_init_block['root_hash']) init_block["fileHash"] = b642hex(config_init_block['file_hash']) init_block_b64 = dict2b64(init_block) - user = local.buffer.user or get_current_user() + user = pop_user_from_args(args) + if user is None: + user = local.buffer.user or get_current_user() args = ["python3", "-m", "mytoninstaller", "-u", user, "-e", "clc", "-i", init_block_b64] run_as_root(args) #end define diff --git a/mytoninstaller/settings.py b/mytoninstaller/settings.py index 872562f0..a8144378 100644 --- a/mytoninstaller/settings.py +++ b/mytoninstaller/settings.py @@ -1134,10 +1134,10 @@ def ConfigureFromBackup(local): os.makedirs(local.buffer.ton_work_dir, exist_ok=True) if not local.buffer.only_mtc: ip = str(ip2int(get_own_ip())) - BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file, "-i", ip]) + BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file, "-i", ip], user=local.buffer.user) if local.buffer.only_mtc: - BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file]) + BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file], user=local.buffer.user) local.add_log("Installing only mtc", "info") vconfig_path = local.buffer.vconfig_path vconfig = GetConfig(path=vconfig_path) @@ -1161,7 +1161,7 @@ def ConfigureOnlyNode(local): mconfig_dir = get_dir_from_path(mconfig_path) local.add_log("start ConfigureOnlyNode function", "info") - process = BackupModule.run_create_backup(["-m", mconfig_dir, ]) + process = BackupModule.run_create_backup(["-m", mconfig_dir], user=local.buffer.user) if process.returncode != 0: local.add_log("Backup creation failed", "error") return From 9dd68600ca61332392c79b28ba166a9071705adf Mon Sep 17 00:00:00 2001 From: yungwine Date: Wed, 30 Jul 2025 18:25:29 +0800 Subject: [PATCH 3/4] update mypylib --- mypylib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypylib b/mypylib index 9fb05aea..a82601ba 160000 --- a/mypylib +++ b/mypylib @@ -1 +1 @@ -Subproject commit 9fb05aea6fefb2f0b12db1854050b23ce29cba67 +Subproject commit a82601bab8299347a3d8360fdb6c8045ce60d86f From cd911b490ad2071fc6ec04aae5c664726e9143c1 Mon Sep 17 00:00:00 2001 From: yungwine Date: Thu, 31 Jul 2025 21:28:43 +0800 Subject: [PATCH 4/4] update mypylib --- mypylib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypylib b/mypylib index a82601ba..3ea7f463 160000 --- a/mypylib +++ b/mypylib @@ -1 +1 @@ -Subproject commit a82601bab8299347a3d8360fdb6c8045ce60d86f +Subproject commit 3ea7f4630cf40c294454d512c05c708b8f7f5ab0