|
| 1 | +#!/usr/bin/env bash |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | +set -o errexit |
| 4 | +set -o pipefail |
| 5 | +set -o nounset |
| 6 | + |
| 7 | +# Check if required tools are available |
| 8 | +command -v oc >/dev/null 2>&1 || { echo "Error: 'oc' command not found. Please ensure you're logged into an OpenShift cluster." >&2; exit 1; } |
| 9 | +command -v jq >/dev/null 2>&1 || { echo "Error: 'jq' command not found. Please install jq." >&2; exit 1; } |
| 10 | + |
| 11 | +RPM_FULL_PATH="$1" |
| 12 | +PACKAGE=$(basename "$RPM_FULL_PATH") |
| 13 | + |
| 14 | +# Function to show usage |
| 15 | +usage() { |
| 16 | + echo "Usage: $0 <rpm-file-path>" |
| 17 | + echo -e "\nThis script installs an RPM package on all OpenShift cluster nodes using rpm-ostree.\n" |
| 18 | + echo "Arguments:" |
| 19 | + echo -e " rpm-file-path Path to the RPM file to install\n" |
| 20 | + echo "Example:" |
| 21 | + echo " $0 /path/to/package.rpm" |
| 22 | + exit 1 |
| 23 | +} |
| 24 | + |
| 25 | +# Check arguments |
| 26 | +if [[ $# -ne 1 ]]; then |
| 27 | + usage |
| 28 | +fi |
| 29 | + |
| 30 | +if [[ ! -f "$RPM_FULL_PATH" ]]; then |
| 31 | + echo "Error: RPM file '$RPM_FULL_PATH' not found." >&2 |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +# Get the list of all node IPs |
| 36 | +echo "Discovering OpenShift cluster nodes..." |
| 37 | +mapfile -t NODE_IPS< <(oc get nodes -o json | jq -r '.items[].status.addresses[0].address') |
| 38 | + |
| 39 | +if [[ ${#NODE_IPS[@]} -eq 0 ]]; then |
| 40 | + echo "Error: No nodes found in the cluster." >&2 |
| 41 | + echo "Make sure you're connected to an OpenShift cluster." >&2 |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +echo "Found ${#NODE_IPS[@]} node(s):" |
| 46 | +for ip in "${NODE_IPS[@]}"; do |
| 47 | + echo " - $ip" |
| 48 | +done |
| 49 | + |
| 50 | +echo -e "\nInstalling RPM package '$PACKAGE' on ${#NODE_IPS[@]} node(s)...\n" |
| 51 | + |
| 52 | +set -x |
| 53 | +for IP in "${NODE_IPS[@]}"; do |
| 54 | + echo "Processing node: $IP" |
| 55 | + scp "$RPM_FULL_PATH" "core@$IP":/var/home/core |
| 56 | + ssh -t "core@$IP" -- sudo rpm-ostree -C override replace /var/home/core/"$PACKAGE" |
| 57 | + sleep 2 |
| 58 | +done |
| 59 | + |
| 60 | +echo -e "\nRPM installation completed on all nodes." |
| 61 | +echo -e "\nIMPORTANT: The nodes need to be rebooted to apply the rpm-ostree changes." |
| 62 | +echo "Plan the reboots carefully to maintain cluster availability:" |
| 63 | +echo "- Consider rebooting nodes one at a time" |
| 64 | +echo "- Wait for each node to become Ready before rebooting the next" |
| 65 | +echo "- Monitor cluster health during the process" |
| 66 | +echo -e "\nReboot commands (run manually when ready):" |
| 67 | +for IP in "${NODE_IPS[@]}"; do |
| 68 | + echo " ssh core@$IP sudo systemctl reboot" |
| 69 | +done |
| 70 | + |
0 commit comments