Skip to content

Instantly share code, notes, and snippets.

@majal
Last active September 16, 2020 05:22
Show Gist options
  • Save majal/ce091bf598ab77f69b7312791eba9af2 to your computer and use it in GitHub Desktop.
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)
#!/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')"
@majal
Copy link
Author

majal commented Sep 16, 2020

Normal nearup update usually follows this process:

  1. Stop running near node.
  2. Check if there is a newer version of near.
  3. If there is a new version, download it. <-- This could take some time depending on traffic
  4. Start the near node again.

The steps above could introduce downtime on the system. To minimize down time, this script does the following:

  1. Compare if the local node is of the same version as what is running in the cloud.
  2. If there is a difference, download the source code from GitHub of the version from the cloud.
  3. Build in the background the new near binaries locally. <-- This takes time, but the node is running while this takes place
  4. Once the new version is built, the node is restarted with the new version.

With this script, downtime is minimized. It can be edited to be used on any network. Sample here is for betanet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment