Scripted reboot of SpinupWP servers with kernel updates waiting

Scripted reboot of SpinupWP servers with kernel updates waiting

My list of servers is rapidly growing, and I got tired of rebooting each server one by one. Sure, I could have used spinupwp servers:reboot --all, but I didn’t necessarily want to reboot “all” servers, since several of them didn’t need it or were very recently created. Less disruption where possible, am I right?

I leveraged the SpinupWP CLI on my Mac with Zsh for this. It’s a Zsh function that I can run on the command line, but at its heart it’s a Bash script. So far I’ve only tested it on Zsh.

# Reboot servers managed by SpinupWP if a reboot is required.
#
# This function retrieves a list of servers managed by SpinupWP
# and checks if any of them require a reboot. If a server
# requires a reboot, it automatically triggers the reboot
# process for that server.
#
# Usage: spinup-reboots
#
function spinup-reboots() {
    # Set the SpinupWP profile name
    local profile_name="yourprofile"

    # Check for dependencies
    if ! command -v spinupwp &> /dev/null; then
        echo "Error: spinupwp command not found. Please make sure SpinupWP CLI is installed and accessible in your PATH."
        return 1
    fi

    if ! command -v jq &> /dev/null; then
        echo "Error: jq command not found. Please make sure jq is installed and accessible in your PATH."
        return 1
    fi

    # Run the spinupwp servers:list command and store the output as JSON
    json_output=$(spinupwp servers:list --fields=id,name,reboot_required --format=json --profile="$profile_name")

    # Flag to track if any servers require reboots
    local servers_requiring_reboot=false

    # Parse JSON output and reboot servers that require a reboot
    echo "$json_output" | jq -c '.[]' | while read -r server; do
        id=$(echo "$server" | jq -r '.id')
        name=$(echo "$server" | jq -r '.name')
        reboot_required=$(echo "$server" | jq -r '.reboot_required')

        if [[ "$reboot_required" == "Yes" ]]; then
            echo "Rebooting server $name (ID: $id)..."
            yes | spinupwp servers:reboot "$id" --profile="$profile_name"
            servers_requiring_reboot=true
        fi
    done

    # Inform the user if no servers require reboots
    if ! $servers_requiring_reboot; then
        echo "No servers require reboots at this time."
    fi
}

The use of this script presumes:

  • You already have the SpinupWP CLI installed and configured with a profile.

  • You have jq installed on the system for JSON handling.

  • You have configured the profile_name variable at the top of the function.

  • You are running this attended. Because precautions.

  • You have backups of your data like you normally would.

This isn’t really intended to be unattended. It’s merely an aid to help you keep your wits about you, while not necessarily having to reboot “all” servers within your SpinupWP account.

Hopefully this is helpful. Enjoy!