Created
April 16, 2026 23:48
-
-
Save Chinoman10/8d798b6e298da3dfe19dee5cd6209bfc to your computer and use it in GitHub Desktop.
Old update script for a Cosmos blockchain validator/node.
This file contains hidden or 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 | |
| CHAIN_DAEMON="terrad" | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -e | |
| # Define color-coded log functions with timestamps | |
| cyan() { printf '[%b] \e[1;36m%b\e[m\n' "$(date +%T)" "$1"; } | |
| ok() { printf '[%b] \e[32m%b\e[m\n' "$(date +%T)" "$1"; } | |
| warn() { printf '[%b] \e[1;33m%b\e[m\n' "$(date +%T)" "$1"; } | |
| err() { printf '[%b] \e[1;31m%b\e[m\n' "$(date +%T)" "$1" 1>&2; } | |
| # Visual enhancements | |
| divider() { printf '-%.0s' {1..30}; } # // https://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash | |
| header() { divider; cyan "πΉ $1 πΉ"; divider; } | |
| footer() { divider; cyan "πΉ End of $1 πΉ"; divider; echo; } | |
| # OLD - Accepting a git tag/version as an optional argument | |
| # OLD - VERSION=${1:-"v2.10.5"} | |
| # Default values | |
| DEFAULT_VERSION="v2.10.5" | |
| VERSION="$DEFAULT_VERSION" | |
| MONITOR=false | |
| UPDATE_DEFAULT=false | |
| # Argument parsing | |
| while getopts "v:muh" opt; do | |
| case ${opt} in | |
| v ) VERSION=$OPTARG ;; | |
| m ) MONITOR=true ;; | |
| u ) UPDATE_DEFAULT=true ;; | |
| h ) echo "Usage: $(basename "$0") [-v version] [-m] [-h]" | |
| echo " -v version : Specify the version to update to (default: $DEFAULT_VERSION)" | |
| echo " -m : Monitor the Terra Daemon's output after restarting" | |
| echo " -u : Update the script's default version without asking" | |
| echo " -h : Display this help message and exit" | |
| exit 0 ;; | |
| \? ) echo "Usage: $(basename "$0") [-v version] [-m] [-u] [-h]" | |
| exit 1 ;; | |
| esac | |
| done | |
| # Check if the specified version is different from the default and prompt for update | |
| if [ "$VERSION" != "$DEFAULT_VERSION" ]; then | |
| if [ "$UPDATE_DEFAULT" = false ]; then | |
| read -p "You have specified a different version ($VERSION). Do you want to update the default version in the script? [y/N]: " confirm_update | |
| fi | |
| if [[ $confirm_update =~ ^[Yy]$ || "$UPDATE_DEFAULT" = true ]]; then | |
| sed -i "s/DEFAULT_VERSION=\"[^\"]*\"/DEFAULT_VERSION=\"$VERSION\"/" "$0" | |
| ok "Default version updated in the script." | |
| fi | |
| fi | |
| # Stopping the Daemon with more detailed logging | |
| warn "Attempting to stop the Daemon..." | |
| sudo systemctl stop $CHAIN_DAEMON | |
| ok "-------- Daemon stopped successfully. π --------" | |
| # Changing directory with error handling | |
| warn "Changing directory to core..." | |
| cd core || { err "Failed to change directory to core."; exit 1; } | |
| ok "Changed directory to core. βοΈ" | |
| # Updating the repository and checking out the specified version with detailed logging | |
| warn "Fetching updates from the origin..." | |
| git fetch origin tag ${VERSION} | |
| ok "Fetched updates successfully. π©" | |
| warn "Checking out version ${VERSION}..." | |
| git checkout ${VERSION} | |
| ok "Checked out version ${VERSION} successfully. β " | |
| # Building and installing the new version with error handling | |
| warn "Building and installing the new version..." | |
| make install | |
| ok "-------- Finished installing! π ---------" | |
| # Verifying the updated version | |
| cyan "----- Verifying Daemon version: -----" | |
| CURRENT_VERSION=$($CHAIN_DAEMON version) | |
| if [ "$CURRENT_VERSION" = "$VERSION" ]; then | |
| ok "β Version verified: ${CURRENT_VERSION}" | |
| else | |
| err "β Version mismatch. Expected: ${VERSION}, but found: ${CURRENT_VERSION}" | |
| exit 1 | |
| fi | |
| # Custom error handling for starting the daemon | |
| set +e # Disable 'exit immediately' to manually handle errors | |
| cyan "--- Restarting the Daemon... ---" | |
| if ! sudo systemctl start $CHAIN_DAEMON; then | |
| err "Failed to start the daemon. Please check the system logs with the following command:" | |
| err "sudo journalctl -u ${CHAIN_DAEMON} -n 50 --no-pager" | |
| exit 1 | |
| fi | |
| set -e # Re-enable 'exit immediately' | |
| ok "Daemon started successfully. π" | |
| # Prompting for monitoring based on user input or argument | |
| if [ "$MONITOR" = false ]; then | |
| read -p "Do you want to monitor the Terra Daemon's output? [y/N]: " answer | |
| case ${answer:0:1} in | |
| y|Y ) | |
| MONITOR=true | |
| ;; | |
| * ) | |
| MONITOR=false | |
| ;; | |
| esac | |
| fi | |
| if [ "$MONITOR" = true ]; then | |
| ok "Monitoring the output π ..." | |
| sudo journalctl -fu $CHAIN_DAEMON --output cat | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment