-
Notifications
You must be signed in to change notification settings - Fork 13
NO-JIRA: feat: add ssh-node target for quick SSH access to cluster nodes #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR=$(dirname "$0") | ||
| DEPLOY_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)" | ||
| INVENTORY_FILE="${DEPLOY_DIR}/openshift-clusters/inventory.ini" | ||
|
|
||
| usage() { | ||
| echo "Usage: $0 <node> [command]" | ||
| echo "" | ||
| echo "SSH into a cluster node via the hypervisor jump host." | ||
| echo "If a command is provided, execute it and return." | ||
| echo "" | ||
| echo "Node can be specified as:" | ||
| echo " master-0, master_0, node0, 0 -> first master node" | ||
| echo " master-1, master_1, node1, 1 -> second master node" | ||
| echo "" | ||
| echo "Examples:" | ||
| echo " $0 master-0 # Interactive SSH session" | ||
| echo " $0 0 uptime # Run 'uptime' on master-0" | ||
| echo " $0 1 'pcs status' # Run 'pcs status' on master-1" | ||
| exit 1 | ||
| } | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| usage | ||
| fi | ||
|
|
||
| NODE_ARG="$1" | ||
| shift | ||
|
|
||
| # Check if inventory file exists | ||
| if [[ ! -f "${INVENTORY_FILE}" ]]; then | ||
| echo "Error: Inventory file not found at ${INVENTORY_FILE}" | ||
| echo "Run 'make inventory' first." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Normalize node argument to inventory name | ||
| case "${NODE_ARG}" in | ||
| master-0|master_0|node0|0) | ||
| NODE_PATTERN="master_0" | ||
| ;; | ||
| master-1|master_1|node1|1) | ||
| NODE_PATTERN="master_1" | ||
| ;; | ||
| *) | ||
| echo "Error: Unknown node '${NODE_ARG}'" | ||
| usage | ||
| ;; | ||
| esac | ||
|
|
||
| # Extract node IP from inventory | ||
| # NOTE: Preventing this command to fail with `|| true`. The script setting at the top will make it exit immediately in case of failure. | ||
| NODE_IP=$(grep -W "master_0|master_1" "${INVENTORY_FILE}" | grep "${NODE_PATTERN}" | grep -oP "ansible_host='\\K[^']+" || true) | ||
| if [[ -z "${NODE_IP}" ]]; then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be unreachable: if line 55 grep doesn't find anything, it will have exit code 1 and script should exit. Adding "|| true" to the command on 55 should be enough, we're checking for content of grep output anyway |
||
| echo "Error: Could not find ${NODE_PATTERN} in inventory" | ||
| echo "Make sure the cluster is deployed and inventory is updated." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Extract hypervisor info from inventory | ||
| # NOTE: Preventing this command to fail with `|| true`. The script setting at the top will make it exit immediately in case of failure. | ||
| HYPERVISOR=$(awk '/^\[metal_machine\]/{found=1;next} /^\[/{found=0} found && /@/{print $1; exit}' "${INVENTORY_FILE}" || true) | ||
| if [[ -z "${HYPERVISOR}" ]]; then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be unreachable: if line 64 grep doesn't find anything, it will have exit code 1 and script should exit |
||
| echo "Error: Could not find hypervisor in inventory" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Common SSH options to avoid known_hosts issues after redeploys | ||
| SSH_OPTS=( | ||
| -o StrictHostKeyChecking=no | ||
| -o UserKnownHostsFile=/dev/null | ||
| -o LogLevel=WARN | ||
| ) | ||
|
|
||
| # ProxyJump with same options for the jump host | ||
| PROXY_CMD="ssh ${SSH_OPTS[*]} -W %h:%p ${HYPERVISOR}" | ||
|
|
||
| if [[ $# -gt 0 ]]; then | ||
| # Run command and return | ||
| ssh "${SSH_OPTS[@]}" \ | ||
| -o "ProxyCommand=${PROXY_CMD}" \ | ||
| "core@${NODE_IP}" "$@" | ||
| else | ||
| # Interactive session | ||
| echo "Connecting to ${NODE_PATTERN} (${NODE_IP}) via ${HYPERVISOR}..." | ||
| ssh "${SSH_OPTS[@]}" \ | ||
| -o "ProxyCommand=${PROXY_CMD}" \ | ||
| "core@${NODE_IP}" | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to make a few lines of helpful errors unreachable, I'll point them out below