Skip to content

Commit 9eaf688

Browse files
committed
allow optional user arg for some commands
1 parent 3e499ec commit 9eaf688

File tree

6 files changed

+51
-29
lines changed

6 files changed

+51
-29
lines changed

modules/backups.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from modules.module import MtcModule
99
from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass
10-
from mytonctrl.utils import get_current_user
10+
from mytonctrl.utils import get_current_user, pop_user_from_args
1111
from mytoninstaller.config import get_own_ip
1212

1313

@@ -29,20 +29,22 @@ def create_tmp_ton_dir(self):
2929
return dir_name
3030

3131
@staticmethod
32-
def run_create_backup(args):
33-
user = get_current_user()
32+
def run_create_backup(args, user: str = None):
33+
if user is None:
34+
user = get_current_user()
3435
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
3536
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
3637

3738
def create_backup(self, args):
38-
if len(args) > 1:
39-
color_print("{red}Bad args. Usage:{endc} create_backup [filename]")
39+
if len(args) > 3:
40+
color_print("{red}Bad args. Usage:{endc} create_backup [filename] [-u <user>]")
4041
return
4142
tmp_dir = self.create_tmp_ton_dir()
4243
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir]
44+
user = pop_user_from_args(args)
4345
if len(args) == 1:
4446
command_args += ["-d", args[0]]
45-
process = self.run_create_backup(command_args)
47+
process = self.run_create_backup(command_args, user=user)
4648

4749
if process.returncode == 0:
4850
color_print("create_backup - {green}OK{endc}")
@@ -53,15 +55,17 @@ def create_backup(self, args):
5355
# end define
5456

5557
@staticmethod
56-
def run_restore_backup(args):
57-
user = get_current_user()
58+
def run_restore_backup(args, user: str = None):
59+
if user is None:
60+
user = get_current_user()
5861
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
5962
return run_as_root(["bash", restore_script_path, "-u", user] + args)
6063

6164
def restore_backup(self, args):
62-
if len(args) == 0 or len(args) > 3:
63-
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y] [--skip-create-backup]")
65+
if len(args) == 0 or len(args) > 5:
66+
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y] [--skip-create-backup] [-u <user>]")
6467
return
68+
user = pop_user_from_args(args)
6569
if '-y' not in args:
6670
res = input(
6771
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):
8286
ip = str(ip2int(get_own_ip()))
8387
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
8488

85-
if self.run_restore_backup(command_args) == 0:
89+
if self.run_restore_backup(command_args, user=user) == 0:
8690
self.ton.local.load_db()
8791
if self.ton.using_validator():
8892
from modules.btc_teleport import BtcTeleportModule

modules/btc_teleport.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,31 @@ def create_env_file(self, reinit=False):
5757
with open(env_path, 'w') as f:
5858
f.write(text)
5959

60-
def add_daemon(self):
60+
def add_daemon(self, user: str = None):
6161
start = f'{self.bin_dir}/oracle'
6262
script_path = pkg_resources.resource_filename('mytoninstaller', 'scripts/add2systemd.sh')
63-
user = get_current_user()
63+
if user is None:
64+
user = get_current_user()
6465
run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir])
6566

66-
def install(self, branch):
67+
def install(self, branch: str, user: str = None):
6768
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport1.sh')
68-
user = get_current_user()
69+
if user is None:
70+
user = get_current_user()
6971
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user])
7072
if exit_code != 0:
7173
raise Exception('Failed to install btc_teleport')
7274
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh')
7375
subprocess.run(["bash", script_path, "-s", self.src_dir])
7476

75-
def init(self, reinstall=False, branch: str = 'master'):
77+
def init(self, reinstall=False, branch: str = 'master', user: str = None):
7678
if os.path.exists(self.src_dir) and not reinstall:
7779
return
7880
self.local.add_log('Installing btc_teleport', 'info')
7981
os.makedirs(self.keystore_path, mode=0o700, exist_ok=True)
80-
self.install(branch)
82+
self.install(branch, user=user)
8183
self.create_env_file()
82-
self.add_daemon()
84+
self.add_daemon(user=user)
8385
self.local.add_log('Installed btc_teleport', 'info')
8486

8587
@staticmethod

mytonctrl/mytonctrl.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
)
4646
from mytoncore.telemetry import is_host_virtual
4747
from mytonctrl.migrate import run_migrations
48-
from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt
48+
from mytonctrl.utils import GetItemFromList, timestamp2utcdatetime, fix_git_config, is_hex, GetColorInt, \
49+
pop_user_from_args
4950

5051
import sys, getopt, os
5152

@@ -331,9 +332,12 @@ def Update(local, args):
331332
#end define
332333

333334
def Upgrade(local, ton, args: list):
334-
if '--btc-teleport' in args: # upgrade --btc-teleport [branch]
335-
branch = args[args.index('--btc-teleport') + 1] if len(args) > args.index('--btc-teleport') + 1 else 'master'
336-
upgrade_btc_teleport(local, ton, reinstall=True, branch=branch)
335+
if '--btc-teleport' in args: # upgrade --btc-teleport [branch] [-u <user>]
336+
branch = 'master'
337+
user = pop_user_from_args(args)
338+
if len(args) > args.index('--btc-teleport') + 1:
339+
branch = args[args.index('--btc-teleport') + 1]
340+
upgrade_btc_teleport(local, ton, reinstall=True, branch=branch, user=user)
337341
return
338342
repo = "ton"
339343
author, repo, branch = check_git(args, repo, "upgrade")
@@ -378,10 +382,10 @@ def Upgrade(local, ton, args: list):
378382
#end define
379383

380384

381-
def upgrade_btc_teleport(local, ton, reinstall=False, branch: str = 'master'):
385+
def upgrade_btc_teleport(local, ton, reinstall=False, branch: str = 'master', user = None):
382386
from modules.btc_teleport import BtcTeleportModule
383387
module = BtcTeleportModule(ton, local)
384-
local.try_function(module.init, args=[reinstall, branch])
388+
local.try_function(module.init, args=[reinstall, branch, user])
385389

386390

387391
def get_clang_major_version():

mytonctrl/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ def GetColorInt(data, border, logic, ending=None):
6262

6363
def get_current_user():
6464
return pwd.getpwuid(os.getuid()).pw_name
65+
66+
def pop_user_from_args(args: list):
67+
if '-u' in args:
68+
user_index = args.index('-u') + 1
69+
if user_index >= len(args):
70+
raise Exception(f'User value not found after "-u" in args: {args}')
71+
user = args.pop(user_index)
72+
args.pop(args.index('-u'))
73+
return user
74+
return None

mytoninstaller/mytoninstaller.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from mypylib.mypylib import MyPyClass, run_as_root, color_print
1313
from mypyconsole.mypyconsole import MyPyConsole
14-
from mytonctrl.utils import get_current_user
14+
from mytonctrl.utils import get_current_user, pop_user_from_args
1515

1616
from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config
1717
from mytoninstaller.node_args import get_node_args
@@ -207,7 +207,9 @@ def CreateLocalConfigFile(local, args):
207207
init_block["rootHash"] = b642hex(config_init_block['root_hash'])
208208
init_block["fileHash"] = b642hex(config_init_block['file_hash'])
209209
init_block_b64 = dict2b64(init_block)
210-
user = local.buffer.user or get_current_user()
210+
user = pop_user_from_args(args)
211+
if user is None:
212+
user = local.buffer.user or get_current_user()
211213
args = ["python3", "-m", "mytoninstaller", "-u", user, "-e", "clc", "-i", init_block_b64]
212214
run_as_root(args)
213215
#end define

mytoninstaller/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,10 @@ def ConfigureFromBackup(local):
11341134
os.makedirs(local.buffer.ton_work_dir, exist_ok=True)
11351135
if not local.buffer.only_mtc:
11361136
ip = str(ip2int(get_own_ip()))
1137-
BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file, "-i", ip])
1137+
BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file, "-i", ip], user=local.buffer.user)
11381138

11391139
if local.buffer.only_mtc:
1140-
BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file])
1140+
BackupModule.run_restore_backup(["-m", mconfig_dir, "-n", backup_file], user=local.buffer.user)
11411141
local.add_log("Installing only mtc", "info")
11421142
vconfig_path = local.buffer.vconfig_path
11431143
vconfig = GetConfig(path=vconfig_path)
@@ -1161,7 +1161,7 @@ def ConfigureOnlyNode(local):
11611161
mconfig_dir = get_dir_from_path(mconfig_path)
11621162
local.add_log("start ConfigureOnlyNode function", "info")
11631163

1164-
process = BackupModule.run_create_backup(["-m", mconfig_dir, ])
1164+
process = BackupModule.run_create_backup(["-m", mconfig_dir], user=local.buffer.user)
11651165
if process.returncode != 0:
11661166
local.add_log("Backup creation failed", "error")
11671167
return

0 commit comments

Comments
 (0)