Skip to content

Commit b794ec2

Browse files
authored
Merge pull request #337 from yungwine/cli
Cli
2 parents 607dfdd + b12669d commit b794ec2

File tree

4 files changed

+124
-10
lines changed

4 files changed

+124
-10
lines changed

mytoncore/functions.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def Event(local, event_name):
5555
ValidatorDownEvent(local)
5656
elif event_name == "enable_ton_storage_provider":
5757
enable_ton_storage_provider_event(local)
58-
elif event_name == "enable_liteserver_mode":
59-
enable_liteserver_mode(local)
58+
elif event_name.startswith("enable_mode"):
59+
enable_mode(local, event_name)
6060
local.exit()
6161
# end define
6262

@@ -93,10 +93,12 @@ def enable_ton_storage_provider_event(local):
9393
#end define
9494

9595

96-
def enable_liteserver_mode(local):
96+
def enable_mode(local, event_name):
9797
ton = MyTonCore(local)
98-
ton.disable_mode('validator')
99-
ton.enable_mode('liteserver')
98+
mode = event_name.split("_")[-1]
99+
if mode == "liteserver":
100+
ton.disable_mode('validator')
101+
ton.enable_mode(mode)
100102
#end define
101103

102104

mytoninstaller/settings.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ def FirstNodeSettings(local):
3434
validatorAppPath = local.buffer.validator_app_path
3535
globalConfigPath = local.buffer.global_config_path
3636
vconfig_path = local.buffer.vconfig_path
37-
archive_ttl = 2592000 if local.buffer.mode == 'liteserver' else 86400
37+
38+
if os.getenv('ARCHIVE_TTL'):
39+
archive_ttl = int(os.getenv('ARCHIVE_TTL'))
40+
else:
41+
archive_ttl = 2592000 if local.buffer.mode == 'liteserver' else 86400
3842

3943
# Проверить конфигурацию
4044
if os.path.isfile(vconfig_path):
@@ -892,8 +896,8 @@ def CreateSymlinks(local):
892896

893897
def EnableMode(local):
894898
args = ["python3", "-m", "mytoncore", "-e"]
895-
if local.buffer.mode == 'liteserver':
896-
args.append("enable_liteserver_mode")
899+
if local.buffer.mode:
900+
args.append("enable_mode_" + local.buffer.mode)
897901
else:
898902
return
899903
args = ["su", "-l", local.buffer.user, "-c", ' '.join(args)]

scripts/install.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import os
2+
import subprocess
3+
import inquirer
4+
5+
6+
def run_cli():
7+
questions = [
8+
inquirer.List(
9+
"mode",
10+
message="Select installation mode (More on https://docs.ton.org/participate/nodes/node-types)",
11+
choices=["validator", "liteserver"],
12+
),
13+
inquirer.List(
14+
"network",
15+
message="Select network",
16+
choices=["Mainnet", "Testnet", "Other"],
17+
),
18+
inquirer.Text(
19+
"config",
20+
message="Provide network config uri",
21+
ignore=lambda x: x["network"] != "Other", # do not ask this question if network is not 'Other'
22+
validate=lambda _, x: x.startswith("http"),
23+
),
24+
inquirer.Text(
25+
"archive-ttl",
26+
message="Send the number of seconds to keep the block data in the node database. Default is 2592000 (30 days)",
27+
ignore=lambda x: x["mode"] != "liteserver", # do not ask this question if mode is not liteserver
28+
validate=lambda _, x: not x or x.isdigit(), # must be empty string or a number
29+
# default=2592000
30+
),
31+
inquirer.List(
32+
"validator-mode",
33+
message="Select mode for validator usage. You can skip and set up this later",
34+
ignore=lambda x: x["mode"] != "validator", # do not ask this question if mode is not validator
35+
choices=["Validator wallet", "Nominator pool", "Single pool", "Liquid Staking", "Skip"],
36+
),
37+
inquirer.Confirm(
38+
"dump",
39+
message="Do you want to download blockchain's dump? "
40+
"This reduces synchronization time but requires to download a large file",
41+
),
42+
inquirer.Confirm(
43+
"telemetry",
44+
message="Are you agree with sending your node performance statistics?"
45+
)
46+
]
47+
48+
answers = inquirer.prompt(questions)
49+
50+
return answers
51+
52+
53+
def parse_args(answers: dict):
54+
mode = answers["mode"]
55+
network = answers["network"].lower()
56+
config = answers["config"]
57+
archive_ttl = answers["archive-ttl"]
58+
validator_mode = answers["validator-mode"]
59+
dump = answers["dump"]
60+
telemetry = answers["telemetry"]
61+
62+
res = f' -n {network}'
63+
64+
if network not in ('mainnet', 'testnet'):
65+
res += f' -c {config}'
66+
67+
if archive_ttl:
68+
os.putenv('ARCHIVE_TTL', archive_ttl) # set env variable
69+
70+
if validator_mode and validator_mode not in ('Skip', 'Validator wallet'):
71+
if validator_mode == 'Nominator pool':
72+
validator_mode = 'nominator-pool'
73+
elif validator_mode == 'Single pool':
74+
validator_mode = 'single-pool'
75+
elif validator_mode == 'Liquid Staking':
76+
validator_mode = 'liquid-staking'
77+
res += f' -m {validator_mode}'
78+
else:
79+
res += f' -m {mode}'
80+
81+
if dump:
82+
res += ' -d'
83+
if not telemetry:
84+
res += ' -t'
85+
86+
return res
87+
88+
89+
def main():
90+
answers = run_cli()
91+
command = parse_args(answers)
92+
# subprocess.run('bash scripts/install.sh ' + command, shell=True)
93+
print('bash install.sh ' + command)
94+
subprocess.run(['bash', 'install.sh'] + command.split())
95+
96+
97+
if __name__ == '__main__':
98+
main()

scripts/install.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ while getopts ":c:tida:r:b:m:n:v:h" flag; do
6767
esac
6868
done
6969

70+
71+
if (( $# == 0 )); then # no arguments
72+
echo "Running cli installer"
73+
wget https://raw.githubusercontent.com/ton-blockchain/mytonctrl/master/scripts/install.py
74+
pip3 install inquirer
75+
python3 install.py
76+
# python3 scripts/install.py
77+
exit
78+
fi
79+
7080
# Set config based on network argument
7181
if [ "${network}" = "testnet" ]; then
7282
config="https://ton-blockchain.github.io/testnet-global.config.json"
@@ -81,7 +91,7 @@ cpus=$(lscpu | grep "CPU(s)" | head -n 1 | awk '{print $2}')
8191
memory=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
8292

8393
echo "This machine has ${cpus} CPUs and ${memory}KB of Memory"
84-
if [ "$ignore" = false ] && ([ "${cpus}" -lt "${cpu_required}" ] || [ "${memory}" -lt "${mem_required}"]); then
94+
if [ "$ignore" = false ] && ([ "${cpus}" -lt "${cpu_required}" ] || [ "${memory}" -lt "${mem_required}" ]); then
8595
echo "Insufficient resources. Requires a minimum of "${cpu_required}" processors and "${mem_required}" RAM."
8696
exit 1
8797
fi
@@ -128,7 +138,7 @@ echo -e "${COLOR}[4/5]${ENDC} Running mytoninstaller"
128138

129139
parent_name=$(ps -p $PPID -o comm=)
130140
user=$(whoami)
131-
if [ "$parent_name" = "sudo" ] || [ "$parent_name" = "su" ]; then
141+
if [ "$parent_name" = "sudo" ] || [ "$parent_name" = "su" ] || [ "$parent_name" = "python3" ]; then
132142
user=$(logname)
133143
fi
134144
echo "User: $user"

0 commit comments

Comments
 (0)