Skip to content

Commit 5b457de

Browse files
authored
Merge pull request #479 from yungwine/sudo
Update getting mtc user for some commands, update run_as_root in mypylib
2 parents 74bdb33 + cd911b4 commit 5b457de

File tree

13 files changed

+86
-40
lines changed

13 files changed

+86
-40
lines changed

modules/backups.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +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, pop_user_from_args
1011
from mytoninstaller.config import get_own_ip
1112

1213

@@ -28,19 +29,22 @@ def create_tmp_ton_dir(self):
2829
return dir_name
2930

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

3538
def create_backup(self, args):
36-
if len(args) > 1:
37-
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>]")
3841
return
3942
tmp_dir = self.create_tmp_ton_dir()
4043
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir]
44+
user = pop_user_from_args(args)
4145
if len(args) == 1:
4246
command_args += ["-d", args[0]]
43-
process = self.run_create_backup(command_args)
47+
process = self.run_create_backup(command_args, user=user)
4448

4549
if process.returncode == 0:
4650
color_print("create_backup - {green}OK{endc}")
@@ -51,14 +55,17 @@ def create_backup(self, args):
5155
# end define
5256

5357
@staticmethod
54-
def run_restore_backup(args):
58+
def run_restore_backup(args, user: str = None):
59+
if user is None:
60+
user = get_current_user()
5561
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
56-
return run_as_root(["bash", restore_script_path] + args)
62+
return run_as_root(["bash", restore_script_path, "-u", user] + args)
5763

5864
def restore_backup(self, args):
59-
if len(args) == 0 or len(args) > 3:
60-
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>]")
6167
return
68+
user = pop_user_from_args(args)
6269
if '-y' not in args:
6370
res = input(
6471
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]')
@@ -79,7 +86,7 @@ def restore_backup(self, args):
7986
ip = str(ip2int(get_own_ip()))
8087
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
8188

82-
if self.run_restore_backup(command_args) == 0:
89+
if self.run_restore_backup(command_args, user=user) == 0:
8390
self.ton.local.load_db()
8491
if self.ton.using_validator():
8592
from modules.btc_teleport import BtcTeleportModule

modules/btc_teleport.py

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

77
from modules.module import MtcModule
88
from mypylib.mypylib import run_as_root, color_print, bcolors, print_table
9+
from mytonctrl.utils import get_current_user
910

1011

1112
class BtcTeleportModule(MtcModule):
@@ -56,31 +57,34 @@ def create_env_file(self, reinit=False):
5657
with open(env_path, 'w') as f:
5758
f.write(text)
5859

59-
def add_daemon(self):
60+
def add_daemon(self, user: str = None):
6061
start = f'{self.bin_dir}/oracle'
6162
script_path = pkg_resources.resource_filename('mytoninstaller', 'scripts/add2systemd.sh')
62-
user = os.environ.get("USER", "root")
63+
if user is None:
64+
user = get_current_user()
6365
run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir])
6466

65-
def install(self, branch):
67+
def install(self, branch: str, user: str = None):
6668
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport1.sh')
67-
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch])
69+
if user is None:
70+
user = get_current_user()
71+
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user])
6872
if exit_code != 0:
6973
raise Exception('Failed to install btc_teleport')
7074
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh')
7175
subprocess.run(["bash", script_path, "-s", self.src_dir])
7276

73-
def init(self, reinstall=False, branch: str = 'master'):
77+
def init(self, reinstall=False, branch: str = 'master', user: str = None):
7478
if os.path.exists(self.src_dir) and not reinstall:
7579
return
7680
if self.ton.local.db.get('btcTeleportDisabled'):
7781
self.local.add_log('Skip BTC Teleport installation', 'info')
7882
return
7983
self.local.add_log('Installing btc_teleport', 'info')
8084
os.makedirs(self.keystore_path, mode=0o700, exist_ok=True)
81-
self.install(branch)
85+
self.install(branch, user=user)
8286
self.create_env_file()
83-
self.add_daemon()
87+
self.add_daemon(user=user)
8488
self.local.add_log('Installed btc_teleport', 'info')
8589

8690
@staticmethod

mypylib

Submodule mypylib updated 1 file

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/scripts/btc_teleport1.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
REPO=""
22
SRC_DIR=""
3-
USER=$(logname)
3+
USER=${SUDO_USER:-$(logname)}
44
BRANCH=master
55

6-
while getopts s:r:b: flag
6+
while getopts s:r:b:u: flag
77
do
88
case "${flag}" in
99
s) SRC_DIR=${OPTARG};;
1010
r) REPO=${OPTARG};;
1111
b) BRANCH=${OPTARG};;
12+
u) USER=${OPTARG};;
1213
*) echo "Flag -${flag} is not recognized. Aborting"; exit 1 ;;
1314
esac
1415
done

mytonctrl/scripts/create_backup.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
dest="mytonctrl_backup_$(hostname)_$(date +%s).tar.gz"
22
mtc_dir="$HOME/.local/share/mytoncore"
3-
user=$(logname)
3+
user=${SUDO_USER:-$(logname)}
44
ton_dir="/var/ton-work"
55
keys_dir="/var/ton-work/keys"
66
# Get arguments
7-
while getopts d:m:t:k: flag
7+
while getopts d:m:t:k:u: flag
88
do
99
case "${flag}" in
1010
d) dest=${OPTARG};;
1111
m) mtc_dir=${OPTARG};;
1212
t) ton_dir=${OPTARG};;
1313
k) keys_dir=${OPTARG};;
14+
u) user=${OPTARG};;
1415
*)
1516
echo "Flag -${flag} is not recognized. Aborting"
1617
exit 1 ;;

mytonctrl/scripts/restore_backup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name="backup.tar.gz"
22
mtc_dir="$HOME/.local/share/mytoncore"
33
ip=0
4-
user=$(logname)
4+
user=${SUDO_USER:-$(logname)}
55
# Get arguments
66
while getopts n:m:i:u: flag
77
do

mytonctrl/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import pwd
13
import subprocess
24
import time
35

@@ -57,3 +59,16 @@ def GetColorInt(data, border, logic, ending=None):
5759
result = bcolors.red_text(data, ending)
5860
return result
5961
# end define
62+
63+
def get_current_user():
64+
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +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, pop_user_from_args
1415

1516
from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config
1617
from mytoninstaller.node_args import get_node_args
@@ -48,8 +49,7 @@ def Init(local, console):
4849

4950

5051
# create variables
51-
user = os.environ.get("USER", "root")
52-
local.buffer.user = user
52+
local.buffer.user = get_current_user()
5353
local.buffer.vuser = "validator"
5454
local.buffer.cport = random.randint(2000, 65000)
5555
local.buffer.lport = random.randint(2000, 65000)
@@ -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 os.environ.get("USER", "root")
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/scripts/ton_http_api_installer.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ if [ "$(id -u)" != "0" ]; then
77
exit 1
88
fi
99

10+
user=${SUDO_USER:-$(logname)}
11+
12+
while getopts u: flag
13+
do
14+
case "${flag}" in
15+
u) user=${OPTARG};;
16+
*) echo "Flag -${flag} is not recognized. Aborting"; exit 1 ;;
17+
esac
18+
done
19+
1020
# Цвета
1121
COLOR='\033[92m'
1222
ENDC='\033[0m'
@@ -21,7 +31,6 @@ virtualenv ${venv_path}
2131

2232
# install python3 packages
2333
echo -e "${COLOR}[2/4]${ENDC} Installing required packages"
24-
user=$(logname)
2534
venv_pip3="${venv_path}/bin/pip3"
2635
${venv_pip3} install ton-http-api
2736
chown -R ${user}:${user} ${venv_path}

0 commit comments

Comments
 (0)