Skip to content

Commit daab5bd

Browse files
authored
Merge pull request #488 from yungwine/args
Add long installing options, add env file providing on install, add --print-env option
2 parents 1d97ca8 + a0d9fd2 commit daab5bd

File tree

4 files changed

+128
-38
lines changed

4 files changed

+128
-38
lines changed

mytoninstaller/mytoninstaller.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
from functools import partial
4141

4242

43+
def init_envs(local):
44+
local.buffer.cport = int(os.getenv('VALIDATOR_CONSOLE_PORT', random.randint(2000, 65000)))
45+
local.buffer.lport = int(os.getenv('LITESERVER_PORT', random.randint(2000, 65000)))
46+
local.buffer.vport = int(os.getenv('VALIDATOR_PORT', random.randint(2000, 65000)))
47+
local.buffer.archive_ttl = os.getenv('ARCHIVE_TTL')
48+
local.buffer.state_ttl = os.getenv('STATE_TTL')
49+
local.buffer.public_ip = os.getenv('PUBLIC_IP')
50+
local.buffer.add_shard = os.getenv('ADD_SHARD')
51+
local.buffer.archive_blocks = os.getenv('ARCHIVE_BLOCKS')
52+
local.buffer.collate_shard = os.getenv('COLLATE_SHARD', '')
53+
54+
4355
def Init(local, console):
4456
local.db.config.isStartOnlyOneProcess = False
4557
local.db.config.logLevel = "debug"
@@ -51,9 +63,7 @@ def Init(local, console):
5163
# create variables
5264
local.buffer.user = get_current_user()
5365
local.buffer.vuser = "validator"
54-
local.buffer.cport = int(os.getenv('VALIDATOR_CONSOLE_PORT', random.randint(2000, 65000)))
55-
local.buffer.lport = int(os.getenv('LITESERVER_PORT', random.randint(2000, 65000)))
56-
local.buffer.vport = int(os.getenv('VALIDATOR_PORT', random.randint(2000, 65000)))
66+
init_envs(local)
5767

5868
# this funciton injects MyPyClass instance
5969
def inject_globals(func):

mytoninstaller/settings.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def FirstNodeSettings(local):
4747
vconfig_path = local.buffer.vconfig_path
4848
vport = local.buffer.vport
4949

50-
if os.getenv('ARCHIVE_TTL'):
51-
archive_ttl = int(os.getenv('ARCHIVE_TTL'))
50+
if local.buffer.archive_ttl is not None:
51+
archive_ttl = int(local.buffer.archive_ttl)
5252
else:
5353
archive_ttl = 2592000 if local.buffer.mode == 'liteserver' else 86400
5454
state_ttl = None
55-
if os.getenv('STATE_TTL'):
56-
state_ttl = int(os.getenv('STATE_TTL'))
55+
if local.buffer.state_ttl is not None:
56+
state_ttl = int(local.buffer.state_ttl)
5757
archive_ttl -= state_ttl
5858
if archive_ttl == 0:
5959
archive_ttl = 1 # todo: remove this when archive_ttl==0 will be allowed in node
@@ -93,16 +93,16 @@ def FirstNodeSettings(local):
9393
cmd = f"{validatorAppPath} --threads {cpus} --daemonize --global-config {globalConfigPath} --db {ton_db_dir} --logname {tonLogPath} --verbosity 1"
9494
cmd += ttl_cmd
9595

96-
if os.getenv('ADD_SHARD'):
97-
add_shard = os.getenv('ADD_SHARD')
96+
if local.buffer.add_shard is not None:
97+
add_shard = local.buffer.add_shard
9898
cmd += f' -M'
9999
for shard in add_shard.split():
100100
cmd += f' --add-shard {shard}'
101101

102102
add2systemd(name="validator", user=vuser, start=cmd, pre='/bin/sleep 2') # post="/usr/bin/python3 /usr/src/mytonctrl/mytoncore.py -e \"validator down\""
103103

104-
if os.getenv('PUBLIC_IP'):
105-
ip = os.getenv('PUBLIC_IP')
104+
if local.buffer.public_ip is not None:
105+
ip = local.buffer.public_ip
106106
else:
107107
ip = get_own_ip()
108108
addr = "{ip}:{vport}".format(ip=ip, vport=vport)
@@ -204,13 +204,13 @@ def parse_block_value(local, block: str):
204204

205205

206206
def download_archive_from_ts(local):
207-
archive_blocks = os.getenv('ARCHIVE_BLOCKS')
207+
if local.buffer.archive_blocks is None:
208+
return
209+
archive_blocks = local.buffer.archive_blocks
208210
downloads_path = '/var/ton-work/ts-downloads/'
209211
os.makedirs(downloads_path, exist_ok=True)
210212
subprocess.run(["chmod", "o+wx", downloads_path])
211213

212-
if archive_blocks is None:
213-
return
214214
block_from, block_to = archive_blocks, None
215215
if len(archive_blocks.split()) > 1:
216216
block_from, block_to = archive_blocks.split()
@@ -1189,7 +1189,7 @@ def SetInitialSync(local):
11891189
def SetupCollator(local):
11901190
if local.buffer.mode != "collator":
11911191
return
1192-
shards = os.getenv('COLLATE_SHARD', '').split()
1192+
shards = local.buffer.collate_shard.split()
11931193
if not shards:
11941194
shards = ['0:8000000000000000']
11951195
local.add_log(f"Setting up collator for shards: {shards}", "info")

scripts/install.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ def run_install(answers: dict):
212212
args += f' -c {config}'
213213

214214
if archive_ttl:
215-
os.environ['ARCHIVE_TTL'] = archive_ttl # set env variable
215+
CONFIG['ARCHIVE_TTL'] = archive_ttl # set env variable
216216
if state_ttl:
217-
os.environ['STATE_TTL'] = state_ttl
217+
CONFIG['STATE_TTL'] = state_ttl
218218
if add_shard:
219-
os.environ['ADD_SHARD'] = add_shard
219+
CONFIG['ADD_SHARD'] = add_shard
220220
if collate_shard:
221-
os.environ['COLLATE_SHARD'] = collate_shard
221+
CONFIG['COLLATE_SHARD'] = collate_shard
222222
if archive_blocks:
223-
os.environ['ARCHIVE_BLOCKS'] = archive_blocks
223+
CONFIG['ARCHIVE_BLOCKS'] = archive_blocks
224224
command += ['-v', 'master']
225225

226226
if validator_mode and validator_mode not in ('Skip', 'Validator wallet'):
@@ -240,7 +240,7 @@ def run_install(answers: dict):
240240
log = None
241241
stdin = None
242242
if background:
243-
os.environ['PYTHONUNBUFFERED'] = '1'
243+
CONFIG['PYTHONUNBUFFERED'] = '1'
244244
log = open("mytonctrl_installation.log", "a")
245245
stdin=subprocess.DEVNULL
246246
command = ['nohup'] + command
@@ -250,6 +250,19 @@ def run_install(answers: dict):
250250

251251
print(command)
252252

253+
if os.getenv('PRINT_ENV'):
254+
print('command:')
255+
print(' '.join(command))
256+
print('envs:')
257+
for k, v in CONFIG.items():
258+
if ' ' in v:
259+
v = f'"{v}"'
260+
print(f'{k}={v}')
261+
sys.exit(0)
262+
263+
for k, v in CONFIG.items():
264+
os.environ[k] = v
265+
253266
process = subprocess.Popen(
254267
command,
255268
stdout=log,
@@ -262,6 +275,9 @@ def run_install(answers: dict):
262275
print("="*100 + f"\nRunning installation in the background. Check './mytonctrl_installation.log' for progress. PID: {process.pid}\n" + "="*100)
263276

264277

278+
CONFIG = {}
279+
280+
265281
def main():
266282
try:
267283
answers = run_cli()

scripts/install.sh

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@ config_overridden=false
2222

2323
show_help_and_exit() {
2424
echo 'Supported arguments:'
25-
echo ' -c PATH Provide custom config for toninstaller.sh'
26-
echo ' -t Disable telemetry'
27-
echo ' -i Ignore minimum requirements'
28-
echo ' -d Use pre-packaged dump. Reduces duration of initial synchronization.'
29-
echo ' -a Set MyTonCtrl git repo author'
30-
echo ' -r Set MyTonCtrl git repo'
31-
echo ' -b Set MyTonCtrl git repo branch'
32-
echo ' -g URL TON node git repo URL (default: https://github.com/ton-blockchain/ton.git)'
33-
echo ' -m MODE Install MyTonCtrl with specified mode (validator or liteserver)'
34-
echo ' -n NETWORK Specify the network (mainnet or testnet)'
35-
echo ' -v VERSION Specify the ton node version (commit, branch, or tag)'
36-
echo ' -u USER Specify the user to be used for MyTonCtrl installation'
37-
echo ' -p PATH Provide backup file for MyTonCtrl installation'
38-
echo ' -o Install only MyTonCtrl. Must be used with -p'
39-
echo ' -l Install only TON node'
40-
echo ' -h Show this help'
25+
echo ' -c, --config URL Provide custom network config'
26+
echo ' -e, --env-file PATH Provide env file with installation parameters'
27+
echo ' --print-env Print result command and envs after interactive installer without installing MyTonCtrl'
28+
echo ' -t, --telemetry Disable telemetry'
29+
echo ' -i, --ignore-reqs Ignore minimum requirements'
30+
echo ' -d, --dump Use pre-packaged dump. Reduces duration of initial synchronization'
31+
echo ' -a, --author Set MyTonCtrl git repo author'
32+
echo ' -r, --repo Set MyTonCtrl git repo name'
33+
echo ' -b, --branch Set MyTonCtrl git repo branch'
34+
echo ' -m, --mode MODE Install MyTonCtrl with specified mode (validator or liteserver). Leave empty to launch interactive installer'
35+
echo ' -n, --network NETWORK Specify the network (mainnet or testnet)'
36+
echo ' -g, --node-repo URL TON node git repo URL (default: https://github.com/ton-blockchain/ton.git)'
37+
echo ' -v, --node-version VERSION Specify the TON node version (commit, branch, or tag)'
38+
echo ' -u, --user USER Specify the user to be used for MyTonCtrl installation'
39+
echo ' -p, --backup PATH Provide backup file for MyTonCtrl installation'
40+
echo ' -o, --only-mtc Install only MyTonCtrl. Must be used with -p'
41+
echo ' -l, --only-node Install only TON node'
42+
echo ' -h, --help Show this help'
4143
exit
4244
}
4345

@@ -47,6 +49,7 @@ fi
4749

4850
# node install parameters
4951
config="https://ton-blockchain.github.io/global.config.json"
52+
env_file=""
5053
telemetry=true
5154
ignore=false
5255
dump=false
@@ -57,7 +60,58 @@ mode=none
5760
cpu_required=16
5861
mem_required=64000000 # 64GB in KB
5962

60-
while getopts ":c:tidola:r:b:m:n:v:u:p:g:h" flag; do
63+
# transform --long options to short, because getopts only supports short ones
64+
65+
newargv=()
66+
while (($#)); do
67+
case "$1" in
68+
--) # end of options
69+
shift
70+
newargv+=( -- "$@" )
71+
break
72+
;;
73+
74+
# no arg
75+
--dump) newargv+=(-d) ;;
76+
--only-mtc) newargv+=(-o) ;;
77+
--only-node) newargv+=(-l) ;;
78+
--help) newargv+=(-h) ;;
79+
--telemetry) newargv+=(-t) ;;
80+
--ignore-reqs) newargv+=(-i) ;;
81+
--print-env) export PRINT_ENV=true ;;
82+
83+
# with arg
84+
--config|--author|--repo|--branch|--mode|--network|--node-repo|--backup|--user|--node-version|--env-file)
85+
if (($# < 2)); then
86+
echo "Error: option $1 requires value" >&2; exit 2
87+
fi
88+
case "$1" in
89+
--config) newargv+=(-c "$2") ;;
90+
--author) newargv+=(-a "$2") ;;
91+
--repo) newargv+=(-r "$2") ;;
92+
--branch) newargv+=(-b "$2") ;;
93+
--mode) newargv+=(-m "$2") ;;
94+
--network) newargv+=(-n "$2") ;;
95+
--node-repo) newargv+=(-g "$2") ;;
96+
--backup) newargv+=(-p "$2") ;;
97+
--user) newargv+=(-u "$2") ;;
98+
--node-version) newargv+=(-v "$2") ;;
99+
--env-file) newargv+=(-e "$2") ;;
100+
esac
101+
shift ;;
102+
--*)
103+
echo "Error: unknown option '$1'" >&2; exit 2 ;;
104+
*)
105+
newargv+=("$1") ;;
106+
esac
107+
shift
108+
done
109+
110+
#printf ' %q' "${newargv[@]}"
111+
#printf '\n'
112+
set -- "${newargv[@]}"
113+
114+
while getopts ":c:tidola:r:b:m:n:v:u:p:g:e:h" flag; do
61115
case "${flag}" in
62116
c) config=${OPTARG}; config_overridden=true;;
63117
t) telemetry=false;;
@@ -74,13 +128,23 @@ while getopts ":c:tidola:r:b:m:n:v:u:p:g:h" flag; do
74128
o) only_mtc=true;;
75129
l) only_node=true;;
76130
p) backup=${OPTARG};;
131+
e) env_file=${OPTARG};;
77132
h) show_help_and_exit;;
78133
*)
79134
echo "Flag -${flag} is not recognized. Aborting"
80135
exit 1 ;;
81136
esac
82137
done
83138

139+
if [ -n "$env_file" ]; then
140+
if [ ! -f "$env_file" ]; then
141+
echo "Env file not found, aborting."
142+
exit 1
143+
fi
144+
set -a
145+
source "$env_file"
146+
set +a
147+
fi
84148

85149
if [ "$only_mtc" = true ] && [ "$backup" = "none" ]; then
86150
echo "Backup file must be provided if only mtc installation"

0 commit comments

Comments
 (0)