Last active
September 16, 2020 05:22
-
-
Save majal/ce091bf598ab77f69b7312791eba9af2 to your computer and use it in GitHub Desktop.
Upgrade and monitoring Bash script for running a NEAR Node (NEAR Stake Wars II Challenge 005)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://www.majlovesreg.one/tag/code/ | |
# Made for betanet, can be edited for other networks | |
instances=$(pgrep -xc $(basename "$0")) | |
[ "${instances}" ] && [ ${instances} -gt 1 ] && { echo "Multiple instances found, exiting..."; exit 0; } | |
source $HOME/.profile | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion | |
dir=${HOME} | |
nearupdir="${dir}/.local/bin" | |
branchlocal='http://127.0.0.1:3030/' | |
branchcloud='https://rpc.betanet.near.org/' | |
staking_pool_id="<stakepool.network>" | |
wallet_acct_id="<wallet.network>" | |
callparams='--nodeUrl http://127.0.0.1:3030 --helperUrl http://127.0.0.1:3030' | |
callcloud='--nodeUrl https://rpc.betanet.near.org --helperUrl https://rpc.betanet.near.org' | |
pip3 install -U --user nearup | |
[ "$(pgrep -x near)" ] || { | |
${nearupdir}/nearup stop | |
nice -16 ${nearupdir}/nearup run betanet --binary-path ${dir}/nearcore/target/release && echo "nearup successfully started" | |
sleep 15 | |
near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} | |
} | |
verlocal=$(curl -s ${branchlocal}status | jq -r '.version.version') | |
vercloud=$(curl -s ${branchcloud}status | jq -r '.version.version') | |
[ "${verlocal}" ] || { echo "Returned null version for ${branchlocal}status"; exit 1; } | |
[ "${vercloud}" ] || { echo "Returned null version for ${branchcloud}status"; exit 2; } | |
[ "${verlocal}" = "${vercloud}" ] && { echo "Yay! No update required. :-) Cloud and local versions are the same (${vercloud})."; exit 0; } | |
cd ${dir} | |
git clone --branch "${vercloud}" https://github.com/nearprotocol/nearcore.git "nearcore-${vercloud}" || { echo "git clone of ${vercloud} branch failed!"; exit 3; } | |
cd "nearcore-${vercloud}" | |
nice -19 make release && cd .. || { echo "make release of ${vercloud} branch failed!"; exit 4; } | |
nearup stop && sleep 5 || { echo "Error stopping near!"; exit 5; } | |
[ -d "nearcore-${verlocal}" ] && rm -rf "nearcore-${verlocal}" | |
ls -1 | grep 'nearcore-.*-prev' | xargs rm -rf | |
mv nearcore "nearcore-${verlocal}-prev" && mv "nearcore-${vercloud}" nearcore | |
nice -16 ${nearupdir}/nearup run betanet --binary-path ${dir}/nearcore/target/release || { echo "running new version (${vercloud}) failed!"; exit 4; } | |
sleep 15; | |
near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} | |
echo "nearup successfully restarted with new version: $(curl -s ${branchlocal}status | jq -r '.version.version')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Normal
nearup
update usually follows this process:near
node.near
.<-- This could take some time depending on traffic
near
node again.The steps above could introduce downtime on the system. To minimize down time, this script does the following:
near
binaries locally.<-- This takes time, but the node is running while this takes place
With this script, downtime is minimized. It can be edited to be used on any network. Sample here is for
betanet
.