Skip to content

Commit ae2fc89

Browse files
authored
Add auto-update (#120)
* Add auto-update
1 parent b7369e8 commit ae2fc89

12 files changed

+576
-9
lines changed

scripts/setup_fullnode.sh

+48-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/bin/bash
22
##Setup & Upgrade Iotex MainNet / TestNet
33
## User local: source/bash/sh $0 [$1=testnet]
4+
## User local: source/bash/sh $0 [$1=testnet $2=plugin=gateway]
5+
## User local: source/bash/sh $0 [$1=testnet $2=plugin=gateway $3=auto-update=on]
46
## If remote: source/bash/sh <(curl -s https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/scripts/setup_fullnode.sh) [$1=testnet]
57
## If remote: source/bash/sh <(curl -s https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/scripts/setup_fullnode.sh) [$1=testnet $2=plugin=gateway]
8+
## If remote: source/bash/sh <(curl -s https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/scripts/setup_fullnode.sh) [$1=testnet $2=plugin=gateway $3=auto-update=on]
69

710
# Colour codes
811
YELLOW='\033[0;33m'
@@ -49,6 +52,7 @@ function setVar() {
4952

5053
function processParam() {
5154
_ENV_=mainnet
55+
_GREP_STRING_=MainNet
5256
if [ $# -gt 0 ];then
5357
if [ "$1"X = "testnet"X ];then
5458
_ENV_=testnet
@@ -66,6 +70,9 @@ function processParam() {
6670
fi
6771
fi
6872
env=$_ENV_
73+
74+
# Use for auto-update
75+
export _GREP_STRING_ _ENV_
6976
}
7077

7178
function determinePluginIsChanged() {
@@ -113,7 +120,7 @@ function preDockerCompose() {
113120
export IOTEX_HOME IOTEX_MONITOR_HOME IOTEX_IMAGE
114121

115122
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/monitor/docker-compose.yml.gateway > $IOTEX_MONITOR_HOME/docker-compose.yml.gateway
116-
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/monitor/docker-compose.yml > $IOTEX_MONITOR_HOME/docker-compose.yml
123+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/monitor/docker-compose.yml > $IOTEX_MONITOR_HOME/docker-compose.yml.default
117124
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/monitor/.env > $IOTEX_MONITOR_HOME/.env
118125
}
119126

@@ -216,18 +223,23 @@ function downloadConfig() {
216223

217224
function enableGateway() {
218225
cd $IOTEX_MONITOR_HOME
219-
\cp docker-compose.yml docker-compose.yml.bak
220226
\cp docker-compose.yml.gateway docker-compose.yml
221227
cd $CUR_PWD
222228
}
223229

230+
function disableGateway() {
231+
cd $IOTEX_MONITOR_HOME
232+
\cp docker-compose.yml.default docker-compose.yml
233+
cd $CUR_PWD
234+
}
235+
224236
function startupWithMonitor() {
225237
echo -e "Start iotex-server and monitor."
226238
cd $IOTEX_MONITOR_HOME
227239
docker-compose up -d --no-recreate
228240
if [ $? -eq 0 ];then
229241
echo -e "${YELLOW} If you first create monitor, you can access 'localhost:3000' to view node monitoring ${NC}"
230-
echo -e "${YELLOW} Default User/Pass: admin/admin."
242+
echo -e "${YELLOW} Default User/Pass: admin/admin. ${NC}"
231243
fi
232244
cd $CUR_PWD
233245
}
@@ -264,11 +276,27 @@ function startupNode() {
264276

265277
}
266278

279+
function startAutoUpdate() {
280+
# Download auto-update command
281+
mkdir -p $IOTEX_HOME/bin
282+
if [ "$(uname)"X = "Darwin"X ];then
283+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/tools/auto-update/auto-update_darwin-amd64 > $IOTEX_HOME/bin/auto-update || exit 1
284+
else
285+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/tools/auto-update/auto-update_linux-amd64 > $IOTEX_HOME/bin/auto-update || exit 1
286+
fi
287+
288+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/scripts/update_silence.sh > $IOTEX_HOME/bin/update_silence.sh || exit 1
289+
chmod +x $IOTEX_HOME/bin/auto-update $IOTEX_HOME/bin/update_silence.sh
290+
291+
# Run background
292+
nohup $IOTEX_HOME/bin/auto-update > $IOTEX_HOME/log/update.log 2>&1 &
293+
echo -e "${YELLOW} Auto-update is on. it will auto update every 3 days. ${NC}"
294+
}
295+
267296
function main() {
268297
checkDockerPermissions
269298
checkDockerCompose
270299

271-
_GREP_STRING_=MainNet
272300
setVar $@
273301
lastversion=$(curl -sS https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/README.md|grep "^- $_GREP_STRING_:"|awk '{print $3}')
274302

@@ -287,10 +315,15 @@ function main() {
287315
determinIotexHome
288316
confirmEnvironmentVariable
289317

318+
read -p "Do you want to auto update the node [Y/N] (Default: Y)? " _AUTO_UPDATE_
319+
290320
deleteOldFile
291321

292322
if [ "$version"X = "$runversion"X ] && [ $_PLUGINS_CHANGE_ -eq 0 ];then
293323
procssNotUpdate
324+
if [ "$_AUTO_UPDATE_"X = "on"X ];then
325+
startAutoUpdate
326+
fi
294327
exit 0
295328
fi
296329

@@ -315,15 +348,21 @@ function main() {
315348
preDockerCompose
316349
if [ $_PLUGINS_ ] && [ "$_PLUGINS_"X = "gateway"X ];then
317350
enableGateway
351+
else
352+
disableGateway
318353
fi
319354

320355
startupNode
321356

322-
# restore file docker-compose.yml
323-
if [ $_PLUGINS_ ] && [ "$_PLUGINS_"X = "gateway"X ];then
324-
cd $IOTEX_MONITOR_HOME
325-
mv docker-compose.yml.bak docker-compose.yml
326-
cd ${CUR_PWD}
357+
# stop auto-updatem, if it is running.
358+
ps -ef | grep "$IOTEX_HOME/bin/bauto-update" > /dev/null
359+
if [ $? -eq 0 ];then
360+
pid=$(ps -ef | grep "$IOTEX_HOME/bin/auto-update" | grep -v grep | awk '{print $2}')
361+
kill -9 $pid
362+
fi
363+
364+
if [ "$_AUTO_UPDATE_"X != "N"X ];then
365+
startAutoUpdate
327366
fi
328367
}
329368

scripts/stop_fullnode.sh

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
## User local: source/bash/sh [$0]
3+
## If remote: source/bash/sh <(curl -s https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/scripts/stop_fullnode.sh)
4+
5+
# Colour codes
6+
YELLOW='\033[0;33m'
7+
RED='\033[0;31m'
8+
NC='\033[0m' # No Color
9+
10+
pushd () {
11+
command pushd "$@" > /dev/null
12+
}
13+
14+
popd () {
15+
command popd "$@" > /dev/null
16+
}
17+
18+
function checkDockerPermissions() {
19+
docker ps > /dev/null
20+
if [ $? = 1 ];then
21+
echo -e "your $RED [$USER] $NC not privilege docker"
22+
echo -e "please run $RED [sudo bash] $NC first"
23+
echo -e "Or docker not install "
24+
exit 1
25+
fi
26+
}
27+
28+
function checkDockerCompose() {
29+
docker-compose --version > /dev/null 2>&1
30+
if [ $? -eq 127 ];then
31+
echo -e "$RED docker-compose command not found $NC"
32+
echo -e "Please install it first"
33+
exit 1
34+
fi
35+
}
36+
37+
function setVar() {
38+
defaultdatadir="$HOME/iotex-var"
39+
}
40+
41+
42+
function determinIotexHome() {
43+
##Input Data Dir
44+
echo "The current user of the input directory must have write permission!!!"
45+
echo -e "${RED} Input your directory \$IOTEX_HOME !!! ${NC}"
46+
47+
#while True: do
48+
read -p "Input your \$IOTEX_HOME [e.g., $defaultdatadir]: " inputdir
49+
IOTEX_HOME=${inputdir:-"$defaultdatadir"}
50+
}
51+
52+
function confirmEnvironmentVariable() {
53+
echo -e "Confirm version: ${RED} ${version} ${NC}"
54+
echo -e "Confirm IOTEX_HOME directory: ${RED} ${IOTEX_HOME} ${NC}"
55+
read -p "Press any key to continue ... [Ctrl + c exit!] " key1
56+
}
57+
58+
function stopNode() {
59+
echo -e "${YELLOW}Stopping iotex-server.${NC}"
60+
pushd $IOTEX_HOME/monitor
61+
docker-compose stop
62+
63+
# stop auto-updatem, if it is running.
64+
ps -ef | grep "$IOTEX_HOME/bin/auto-update" > /dev/null
65+
if [ $? -eq 0 ];then
66+
echo -e "${YELLOW} stopping auto-update service... ${NC}"
67+
pid=$(ps -ef | grep "$IOTEX_HOME/bin/auto-update" | grep -v grep | awk '{print $2}')
68+
kill -9 $pid
69+
echo -e "${YELLOW} auto-update stopped.${NC}"
70+
fi
71+
popd
72+
echo -e "${YELLOW}Iotex-server. stopped.${NC}"
73+
}
74+
75+
76+
function main() {
77+
checkDockerPermissions
78+
checkDockerCompose
79+
80+
setVar
81+
determinIotexHome
82+
confirmEnvironmentVariable
83+
84+
stopNode
85+
}
86+
87+
main

scripts/update_silence.sh

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/bin/bash
2+
## Upgrade Iotex MainNet / TestNet Silence none params
3+
4+
# Colour codes
5+
YELLOW='\033[0;33m'
6+
RED='\033[0;31m'
7+
NC='\033[0m' # No Color
8+
9+
10+
# set it in run auto-update
11+
#echo -e "ENV IOTEX_HOME must be set."
12+
[ $IOTEX_HOME ] || exit 2
13+
#echo -e "ENV _ENV_ must be set."
14+
[ $_ENV_ ] || exit 2
15+
#echo -e "ENV _GREP_STRING_ must be set."
16+
[ $_GREP_STRING_ ] || exit 2
17+
18+
pushd () {
19+
command pushd "$@" > /dev/null
20+
}
21+
22+
popd () {
23+
command popd "$@" > /dev/null
24+
}
25+
26+
function sedCheck() {
27+
sed --version > /dev/null 2>&1
28+
if [ $? -eq 0 ];then
29+
sed --version|grep 'GNU sed' > /dev/null 2>&1
30+
if [ $? -eq 0 ];then
31+
SED_IS_GNU=1
32+
fi
33+
fi
34+
}
35+
36+
function checkDockerPermissions() {
37+
docker ps > /dev/null
38+
if [ $? = 1 ];then
39+
echo -e "your $RED [$USER] $NC not privilege docker"
40+
echo -e "please run $RED [sudo bash] $NC first"
41+
echo -e "Or docker not install "
42+
exit 1
43+
fi
44+
}
45+
46+
function checkDockerCompose() {
47+
docker-compose --version > /dev/null 2>&1
48+
if [ $? -eq 127 ];then
49+
echo -e "$RED docker-compose command not found $NC"
50+
echo -e "Please install it first"
51+
exit 1
52+
fi
53+
}
54+
55+
function setVar() {
56+
IOTEX_MONITOR_HOME=$IOTEX_HOME/monitor
57+
58+
producerPrivKey=""
59+
externalHost=""
60+
}
61+
62+
function isNotRunning() {
63+
pushd $IOTEX_MONITOR_HOME
64+
docker-compose ps iotex
65+
if [ $? -eq 0 ];then
66+
return 0
67+
fi
68+
popd
69+
return 1
70+
}
71+
72+
function cleanOldVersion() {
73+
echo -e "${YELLOW} ****** Upgrade Iotex Node ******* ${NC}"
74+
echo -e "${YELLOW} *** Will stop, delete old iotex container; ${NC}"
75+
echo -e "${YELLOW} *** download new config and recover your externalHost producerPrivKey ${NC}"
76+
read -p "******* Press any key to continue ... [Ctrl + c exit!] " upgreadekey
77+
echo "Stop old iotex-core"
78+
docker stop iotex
79+
echo "delete old iotex docker container"
80+
docker rm iotex
81+
producerPrivKey=$(grep '^ producerPrivKey:' ${IOTEX_HOME}/etc/config.yaml|sed 's/^ //g')
82+
externalHost=$(grep '^ externalHost:' ${IOTEX_HOME}/etc/config.yaml|sed 's/^ //g')
83+
}
84+
85+
function downloadConfig() {
86+
#(Optional) If you prefer to start from a snapshot, run the following commands:
87+
#curl -LSs https://t.iotex.me/${env}-data-latest > $IOTEX_HOME/data.tar.gz
88+
#cd ${IOTEX_HOME} && tar -xzf data.tar.gz
89+
echo "download new config"
90+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/${version}/config_${_ENV_}.yaml > $IOTEX_HOME/etc/config.yaml
91+
curl -Ss https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/${version}/genesis_${_ENV_}.yaml > $IOTEX_HOME/etc/genesis.yaml
92+
# https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/v0.9.2/config_mainnet.yaml
93+
94+
SED_IS_GNU=0
95+
sedCheck
96+
echo "Update your externalHost,producerPrivKey to config.yaml"
97+
if [ $SED_IS_GNU -eq 1 ];then
98+
sed -i "/^network:/a\ \ $externalHost" $IOTEX_HOME/etc/config.yaml
99+
sed -i "/^chain:/a\ \ $producerPrivKey" $IOTEX_HOME/etc/config.yaml
100+
else
101+
sed -i '' "/^network:/a\
102+
\ \ $externalHost
103+
" $IOTEX_HOME/etc/config.yaml
104+
sed -i '' "/^chain:/a\
105+
\ \ $producerPrivKey
106+
" $IOTEX_HOME/etc/config.yaml
107+
fi
108+
}
109+
110+
function startup() {
111+
echo -e "Start iotex-server."
112+
pushd $IOTEX_MONITOR_HOME
113+
docker-compose up -d iotex
114+
popd
115+
}
116+
117+
function startupNode() {
118+
startup
119+
#check node running
120+
sleep 5
121+
pushd $IOTEX_MONITOR_HOME
122+
docker-compose ps iotex
123+
popd
124+
}
125+
126+
function main() {
127+
checkDockerPermissions
128+
checkDockerCompose
129+
130+
setVar
131+
132+
# Not running, not update.
133+
isNotRunning || exit 0
134+
135+
version=$(curl -sS https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/master/README.md|grep "^- $_GREP_STRING_:"|awk '{print $3}')
136+
137+
echo -e "Current operating environment: ${YELLOW} $env ${NC}"
138+
139+
runversion=$(docker ps -a |grep "iotex/iotex-core:v"|awk '{print$2}'|awk -F'[:]' '{print$2}')
140+
141+
if [ "$version"X = "$runversion"X ];then
142+
echo -e "${YELLOW} Current running version is latest. Don't need update. ${NC}"
143+
exit 0
144+
fi
145+
146+
echo -e "${YELLOW} Current runing version ( $runversion ) need update ( latest $version ), ${NC}"
147+
148+
IOTEX_IMAGE=iotex/iotex-core:${version}
149+
export IOTEX_IMAGE
150+
151+
echo "docker pull iotex-core ${version}"
152+
docker pull $IOTEX_IMAGE
153+
154+
downloadConfig
155+
156+
startupNode
157+
}
158+
159+
main

0 commit comments

Comments
 (0)