Skip to content

Commit 3e499ec

Browse files
committed
update getting launched user in various scripts
1 parent 0dd02e3 commit 3e499ec

File tree

11 files changed

+41
-17
lines changed

11 files changed

+41
-17
lines changed

modules/backups.py

Lines changed: 5 additions & 2 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
1011
from mytoninstaller.config import get_own_ip
1112

1213

@@ -29,8 +30,9 @@ def create_tmp_ton_dir(self):
2930

3031
@staticmethod
3132
def run_create_backup(args):
33+
user = get_current_user()
3234
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
33-
return subprocess.run(["bash", backup_script_path] + args, timeout=5)
35+
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
3436

3537
def create_backup(self, args):
3638
if len(args) > 1:
@@ -52,8 +54,9 @@ def create_backup(self, args):
5254

5355
@staticmethod
5456
def run_restore_backup(args):
57+
user = get_current_user()
5558
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
56-
return run_as_root(["bash", restore_script_path] + args)
59+
return run_as_root(["bash", restore_script_path, "-u", user] + args)
5760

5861
def restore_backup(self, args):
5962
if len(args) == 0 or len(args) > 3:

modules/btc_teleport.py

Lines changed: 4 additions & 2 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):
@@ -59,12 +60,13 @@ def create_env_file(self, reinit=False):
5960
def add_daemon(self):
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+
user = get_current_user()
6364
run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir])
6465

6566
def install(self, branch):
6667
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])
68+
user = get_current_user()
69+
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user])
6870
if exit_code != 0:
6971
raise Exception('Failed to install btc_teleport')
7072
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh')

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: 5 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,6 @@ 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

mytoninstaller/mytoninstaller.py

Lines changed: 3 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
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,7 @@ 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 = local.buffer.user or get_current_user()
211211
args = ["python3", "-m", "mytoninstaller", "-u", user, "-e", "clc", "-i", init_block_b64]
212212
run_as_root(args)
213213
#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}

mytoninstaller/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
ip2int,
2323
Dict, int2ip
2424
)
25+
from mytonctrl.utils import get_current_user
2526
from mytoninstaller.utils import StartValidator, StartMytoncore, start_service, stop_service, get_ed25519_pubkey, \
2627
disable_service, is_testnet, get_block_from_toncenter
2728
from mytoninstaller.config import SetConfig, GetConfig, get_own_ip, backup_config
@@ -729,8 +730,9 @@ def do_enable_ton_http_api(local):
729730
if not os.path.exists('/usr/bin/ton/local.config.json'):
730731
from mytoninstaller.mytoninstaller import CreateLocalConfigFile
731732
CreateLocalConfigFile(local, [])
733+
user = local.buffer.user or get_current_user()
732734
ton_http_api_installer_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'ton_http_api_installer.sh')
733-
exit_code = run_as_root(["bash", ton_http_api_installer_path])
735+
exit_code = run_as_root(["bash", ton_http_api_installer_path, "-u", user])
734736
if exit_code == 0:
735737
text = "do_enable_ton_http_api - {green}OK{endc}"
736738
else:

scripts/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ if [ "${user}" = "" ]; then # no user
169169
parent_name=$(ps -p $PPID -o comm=)
170170
user=$(whoami)
171171
if [ "$parent_name" = "sudo" ] || [ "$parent_name" = "su" ] || [ "$parent_name" = "python3" ]; then
172-
user=$(logname)
172+
user=${SUDO_USER:-$(logname)}
173173
fi
174174
fi
175175
echo "User: $user"

0 commit comments

Comments
 (0)