Skip to content

Commit cd43a23

Browse files
dhensel-rhclaude
andcommitted
Fix shellcheck SC2086 and auto-discover hypervisor from inventory
- Convert SSH_OPTS from string to bash array, expand with "${SSH_OPTS[@]}" - Auto-discover hypervisor from TNT inventory (deploy/openshift-clusters/inventory.ini) with explicit argument as override - Add pre-flight check for dev-scripts on the hypervisor - Make missing config file non-fatal (Patch 0 skips gracefully) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7af8e43 commit cd43a23

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

helpers/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,19 @@ Also fixes `OPENSHIFT_CI` case sensitivity (`"TRUE"` → `"true"`) in the dev-sc
146146
**Usage:**
147147

148148
```bash
149-
# From helpers/ directory
149+
# From helpers/ directory — auto-discovers hypervisor from TNT inventory
150+
./patch-devscripts-mac-fencing.sh
151+
152+
# Or specify an explicit hypervisor
150153
./patch-devscripts-mac-fencing.sh ec2-user@<hypervisor-ip>
151154

152155
# Then on the hypervisor:
153156
cd /home/ec2-user/openshift-metal3/dev-scripts
154157
sudo make agent
155158
```
156159

160+
The hypervisor is auto-discovered from `deploy/openshift-clusters/inventory.ini` (the `[metal_machine]` group). An explicit argument overrides auto-discovery.
161+
157162
**Important:** TNT's Ansible role runs `git checkout --force` on dev-scripts before every deployment, wiping these patches. Either run this script after TNT's git checkout (before `make agent`), or deploy directly on the hypervisor.
158163

159164
### Resource Agents Patching

helpers/patch-devscripts-mac-fencing.sh

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ set -euo pipefail
1010
# - A running cluster is NOT required — this patches source files
1111
# before the cluster is deployed.
1212
#
13-
# Usage: ./patch-devscripts-mac-fencing.sh <hypervisor_host>
14-
# Example: ./patch-devscripts-mac-fencing.sh ec2-user@3.21.82.70
13+
# Usage: ./patch-devscripts-mac-fencing.sh [hypervisor_host]
14+
#
15+
# The hypervisor is auto-discovered from the TNT inventory file
16+
# (deploy/openshift-clusters/inventory.ini). An explicit argument
17+
# overrides auto-discovery.
1518
#
1619
# IMPORTANT: TNT's Ansible role runs `git checkout --force` on dev-scripts
1720
# before every deployment, wiping these patches. Either:
@@ -25,21 +28,45 @@ set -euo pipefail
2528
# rm -rf ocp/ostest
2629
# sudo make agent
2730

28-
HYPERVISOR="${1:?Usage: $0 <hypervisor_host> (e.g. ec2-user@3.21.82.70)}"
31+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
32+
INVENTORY="${SCRIPT_DIR}/../deploy/openshift-clusters/inventory.ini"
33+
34+
if [[ -n "${1:-}" ]]; then
35+
HYPERVISOR="$1"
36+
elif [[ -f "${INVENTORY}" ]]; then
37+
HYPERVISOR=$(awk '/^\[metal_machine\]/{found=1; next} found && /^[[:space:]]*$/{next} found && /^#/{next} found && /^\[/{exit} found{print $1; exit}' "${INVENTORY}")
38+
if [[ -z "${HYPERVISOR}" ]]; then
39+
echo "ERROR: Could not parse hypervisor from ${INVENTORY}"
40+
exit 1
41+
fi
42+
echo "Auto-discovered hypervisor from inventory: ${HYPERVISOR}"
43+
else
44+
echo "ERROR: No hypervisor specified and inventory not found at ${INVENTORY}"
45+
echo "Usage: $0 [hypervisor_host]"
46+
exit 1
47+
fi
48+
2949
DEV_SCRIPTS="/home/ec2-user/openshift-metal3/dev-scripts"
30-
SSH_OPTS="-o StrictHostKeyChecking=no -o ServerAliveInterval=30 -o ServerAliveCountMax=120"
50+
SSH_OPTS=(-o StrictHostKeyChecking=no -o ServerAliveInterval=30 -o ServerAliveCountMax=120)
3151

3252
echo "==> Patching dev-scripts on ${HYPERVISOR} for MAC-only fencing credentials"
3353

54+
# Pre-flight: verify dev-scripts is cloned on the hypervisor
55+
if ! ssh "${SSH_OPTS[@]}" "${HYPERVISOR}" test -d "${DEV_SCRIPTS}/agent"; then
56+
echo "ERROR: dev-scripts not found at ${DEV_SCRIPTS} on ${HYPERVISOR}"
57+
echo "Run 'make init' or 'make deploy' from deploy/ first."
58+
exit 1
59+
fi
60+
3461
# Patch 0: Ensure AGENT_E2E_TEST_SCENARIO is set in the config file
3562
echo "--- Patch 0/3: config file (AGENT_E2E_TEST_SCENARIO)"
3663
# shellcheck disable=SC2087
37-
ssh ${SSH_OPTS} "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH0_EOF'
64+
ssh "${SSH_OPTS[@]}" "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH0_EOF'
3865
DEV_SCRIPTS="$1"
3966
CONFIG=$(ls "${DEV_SCRIPTS}"/config_*.sh 2>/dev/null | head -1)
4067
if [[ -z "$CONFIG" ]]; then
41-
echo " ERROR: No config_*.sh found in ${DEV_SCRIPTS}"
42-
exit 1
68+
echo " SKIP: No config_*.sh found yet — will be created by 'make deploy'"
69+
exit 0
4370
fi
4471
echo " Config file: $(basename "$CONFIG")"
4572
@@ -67,7 +94,7 @@ PATCH0_EOF
6794
# - Export AGENT_MASTER_MACS_STR (after AGENT_MASTER_HOSTNAMES_STR)
6895
echo "--- Patch 1/3: agent/05_agent_configure.sh"
6996
# shellcheck disable=SC2087
70-
ssh ${SSH_OPTS} "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH1_EOF'
97+
ssh "${SSH_OPTS[@]}" "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH1_EOF'
7198
DEV_SCRIPTS="$1"
7299
FILE="${DEV_SCRIPTS}/agent/05_agent_configure.sh"
73100
@@ -92,7 +119,7 @@ PATCH1_EOF
92119
# - Add agent_master_macs variable
93120
echo "--- Patch 2/3: agent/roles/manifests/vars/main.yml"
94121
# shellcheck disable=SC2087
95-
ssh ${SSH_OPTS} "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH2_EOF'
122+
ssh "${SSH_OPTS[@]}" "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH2_EOF'
96123
DEV_SCRIPTS="$1"
97124
FILE="${DEV_SCRIPTS}/agent/roles/manifests/vars/main.yml"
98125
@@ -108,7 +135,7 @@ PATCH2_EOF
108135
# - Replace hostname-based fencing with macaddress-based
109136
echo "--- Patch 3/3: install-config_baremetal_yaml.j2 (MAC-only fencing)"
110137
# shellcheck disable=SC2087
111-
ssh ${SSH_OPTS} "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH3_EOF'
138+
ssh "${SSH_OPTS[@]}" "${HYPERVISOR}" bash -s "${DEV_SCRIPTS}" <<'PATCH3_EOF'
112139
DEV_SCRIPTS="$1"
113140
FILE="${DEV_SCRIPTS}/agent/roles/manifests/templates/install-config_baremetal_yaml.j2"
114141
@@ -127,7 +154,7 @@ echo ""
127154
echo "==> All patches applied successfully."
128155
echo ""
129156
echo "Next steps:"
130-
echo " 1. SSH to the hypervisor: ssh ${SSH_OPTS} ${HYPERVISOR}"
157+
echo " 1. SSH to the hypervisor: ssh ${HYPERVISOR}"
131158
echo " 2. cd ${DEV_SCRIPTS}"
132159
echo " 3. make clean (if cluster exists)"
133-
echo " 4. make agent (deploy with MAC-only fencing)"
160+
echo " 4. sudo make agent (deploy with MAC-only fencing)"

0 commit comments

Comments
 (0)