|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR=$(dirname "$0") |
| 6 | +DEPLOY_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)" |
| 7 | +INVENTORY_FILE="${DEPLOY_DIR}/openshift-clusters/inventory.ini" |
| 8 | + |
| 9 | +usage() { |
| 10 | + echo "Usage: $0 <node> [command]" |
| 11 | + echo "" |
| 12 | + echo "SSH into a cluster node via the hypervisor jump host." |
| 13 | + echo "If a command is provided, execute it and return." |
| 14 | + echo "" |
| 15 | + echo "Node can be specified as:" |
| 16 | + echo " master-0, master_0, node0, 0 -> first master node" |
| 17 | + echo " master-1, master_1, node1, 1 -> second master node" |
| 18 | + echo "" |
| 19 | + echo "Examples:" |
| 20 | + echo " $0 master-0 # Interactive SSH session" |
| 21 | + echo " $0 0 uptime # Run 'uptime' on master-0" |
| 22 | + echo " $0 1 'pcs status' # Run 'pcs status' on master-1" |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +if [[ $# -lt 1 ]]; then |
| 27 | + usage |
| 28 | +fi |
| 29 | + |
| 30 | +NODE_ARG="$1" |
| 31 | +shift |
| 32 | + |
| 33 | +# Check if inventory file exists |
| 34 | +if [[ ! -f "${INVENTORY_FILE}" ]]; then |
| 35 | + echo "Error: Inventory file not found at ${INVENTORY_FILE}" |
| 36 | + echo "Run 'make inventory' first." |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Normalize node argument to inventory name |
| 41 | +case "${NODE_ARG}" in |
| 42 | + master-0|master_0|node0|0) |
| 43 | + NODE_PATTERN="master_0" |
| 44 | + ;; |
| 45 | + master-1|master_1|node1|1) |
| 46 | + NODE_PATTERN="master_1" |
| 47 | + ;; |
| 48 | + *) |
| 49 | + echo "Error: Unknown node '${NODE_ARG}'" |
| 50 | + usage |
| 51 | + ;; |
| 52 | +esac |
| 53 | + |
| 54 | +# Extract node IP from inventory |
| 55 | +# NOTE: Preventing this command to fail with `|| true`. The script setting at the top will make it exit immediately in case of failure. |
| 56 | +NODE_IP=$(grep -W "master_0|master_1" "${INVENTORY_FILE}" | grep "${NODE_PATTERN}" | grep -oP "ansible_host='\\K[^']+" || true) |
| 57 | +if [[ -z "${NODE_IP}" ]]; then |
| 58 | + echo "Error: Could not find ${NODE_PATTERN} in inventory" |
| 59 | + echo "Make sure the cluster is deployed and inventory is updated." |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +# Extract hypervisor info from inventory |
| 64 | +# NOTE: Preventing this command to fail with `|| true`. The script setting at the top will make it exit immediately in case of failure. |
| 65 | +HYPERVISOR=$(awk '/^\[metal_machine\]/{found=1;next} /^\[/{found=0} found && /@/{print $1; exit}' "${INVENTORY_FILE}" || true) |
| 66 | +if [[ -z "${HYPERVISOR}" ]]; then |
| 67 | + echo "Error: Could not find hypervisor in inventory" |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +# Common SSH options to avoid known_hosts issues after redeploys |
| 72 | +SSH_OPTS=( |
| 73 | + -o StrictHostKeyChecking=no |
| 74 | + -o UserKnownHostsFile=/dev/null |
| 75 | + -o LogLevel=WARN |
| 76 | +) |
| 77 | + |
| 78 | +# ProxyJump with same options for the jump host |
| 79 | +PROXY_CMD="ssh ${SSH_OPTS[*]} -W %h:%p ${HYPERVISOR}" |
| 80 | + |
| 81 | +if [[ $# -gt 0 ]]; then |
| 82 | + # Run command and return |
| 83 | + ssh "${SSH_OPTS[@]}" \ |
| 84 | + -o "ProxyCommand=${PROXY_CMD}" \ |
| 85 | + "core@${NODE_IP}" "$@" |
| 86 | +else |
| 87 | + # Interactive session |
| 88 | + echo "Connecting to ${NODE_PATTERN} (${NODE_IP}) via ${HYPERVISOR}..." |
| 89 | + ssh "${SSH_OPTS[@]}" \ |
| 90 | + -o "ProxyCommand=${PROXY_CMD}" \ |
| 91 | + "core@${NODE_IP}" |
| 92 | +fi |
0 commit comments