Skip to content

Commit bb23774

Browse files
committed
NO-JIRA: feat: add ssh-node target for quick SSH access to cluster nodes
Add 'make ssh-node' command to simplify SSH access to cluster nodes. Usage: make ssh-node node=master-0 # Interactive SSH session make ssh-node node=1 # SSH to master-1 make ssh-node node=0 cmd='pcs status' # Run command and return Features: - Supports multiple node name formats (master-0, master_0, node0, 0) - Handles known_hosts issues from cluster redeploys - Uses ProxyJump through the hypervisor automatically
1 parent 684e375 commit bb23774

2 files changed

Lines changed: 107 additions & 2 deletions

File tree

deploy/Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ fencing-kcli:
7878

7979
patch-nodes:
8080
@./openshift-clusters/scripts/patch-nodes.sh
81+
82+
ssh-node:
83+
ifndef node
84+
@echo "Usage: make ssh-node node=<0|1> [cmd=\"command\"]"
85+
@echo "Examples:"
86+
@echo " make ssh-node node=0"
87+
@echo " make ssh-node node=1 cmd=\"sudo pcs status\""
88+
@exit 1
89+
endif
90+
@./openshift-clusters/scripts/ssh-node.sh $(node) $(if $(cmd),$(cmd))
91+
8192
get-tnf-logs:
82-
@./openshift-clusters/scripts/get-tnf-logs.sh
93+
@./openshift-clusters/scripts/get-tnf-logs.sh
8394

8495
help:
8596
@echo "Available commands:"
@@ -117,5 +128,7 @@ help:
117128
@echo " patch-nodes - Build resource-agents RPM and patch cluster nodes (default version: 4.11)"
118129
@echo ""
119130
@echo "Cluster Utilities:"
120-
@echo " get-tnf-logs - Collect pacemaker and etcd logs from cluster nodes"
131+
@echo " ssh-node node=<0|1> [cmd=\"..\"] - SSH into cluster node"
132+
@echo " e.g. make ssh-node node=0 cmd=\"sudo pcs status\""
133+
@echo " get-tnf-logs - Collect pacemaker and etcd logs from cluster nodes"
121134

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)