Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions modules/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, pop_user_from_args
from mytoninstaller.config import get_own_ip


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

@staticmethod
def run_create_backup(args):
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] + args, timeout=5)
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 <user>]")
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}")
Expand All @@ -51,14 +55,17 @@ def create_backup(self, args):
# end define

@staticmethod
def run_restore_backup(args):
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] + 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:
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y] [--skip-create-backup]")
if len(args) == 0 or len(args) > 5:
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y] [--skip-create-backup] [-u <user>]")
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]')
Expand All @@ -79,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
Expand Down
18 changes: 11 additions & 7 deletions modules/btc_teleport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -56,28 +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 = os.environ.get("USER", "root")
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')
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch])
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
Expand Down
2 changes: 1 addition & 1 deletion mypylib
Submodule mypylib updated 1 files
+20 −8 mypylib.py
16 changes: 10 additions & 6 deletions mytonctrl/mytonctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 <user>]
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")
Expand Down Expand Up @@ -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():
Expand Down
5 changes: 3 additions & 2 deletions mytonctrl/scripts/btc_teleport1.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions mytonctrl/scripts/create_backup.sh
Original file line number Diff line number Diff line change
@@ -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 ;;
Expand Down
2 changes: 1 addition & 1 deletion mytonctrl/scripts/restore_backup.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions mytonctrl/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import pwd
import subprocess
import time

Expand Down Expand Up @@ -57,3 +59,16 @@ 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

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
8 changes: 5 additions & 3 deletions mytoninstaller/mytoninstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, pop_user_from_args

from mytoninstaller.config import GetLiteServerConfig, get_ls_proxy_config
from mytoninstaller.node_args import get_node_args
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 os.environ.get("USER", "root")
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
Expand Down
11 changes: 10 additions & 1 deletion mytoninstaller/scripts/ton_http_api_installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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}
Expand Down
10 changes: 6 additions & 4 deletions mytoninstaller/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1132,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)
Expand All @@ -1159,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
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions tools/extract_backup_node_keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;;
Expand Down