|
| 1 | +#!/bin/bash |
| 2 | +# docker-init.sh — GCU venv setup and health-check script. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# gcu-init.sh setup — create /opt/gcu-venv and install the sonic-gcu wheel |
| 6 | +# gcu-init.sh healthcheck — run lightweight smoke-tests against the installed GCU |
| 7 | +# |
| 8 | +# This script is invoked by supervisord as two separate one-shot programs so that |
| 9 | +# supervisord_dependent_startup can gate execution order: |
| 10 | +# 1. gcu-setup (priority=2): runs 'setup' — exits 0 on success |
| 11 | +# 2. gcu-healthcheck (priority=3): runs 'healthcheck' — exits 0 on success |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +VENV_DIR="/opt/gcu-venv" |
| 16 | +WHEEL_DIR="/python-wheels" |
| 17 | +WHEEL_GLOB="${WHEEL_DIR}/sonic_gcu-*.whl" |
| 18 | +SETUP_SENTINEL="${VENV_DIR}/.setup_complete" |
| 19 | + |
| 20 | +############################################################################### |
| 21 | +# setup: create venv and install wheel |
| 22 | +############################################################################### |
| 23 | +do_setup() { |
| 24 | + echo "[gcu-init] Starting GCU venv setup ..." |
| 25 | + |
| 26 | + # Create venv with --system-site-packages so C-extension packages |
| 27 | + # (swsscommon, sonic-py-common, libyang Python bindings, etc.) that are |
| 28 | + # installed system-wide are visible inside the venv without reinstalling |
| 29 | + # them. Pure-Python deps that are NOT on the system path must still be |
| 30 | + # declared in the wheel's install_requires and will be downloaded/installed |
| 31 | + # into the venv itself. |
| 32 | + if [ ! -d "${VENV_DIR}" ]; then |
| 33 | + python3 -m venv --system-site-packages "${VENV_DIR}" |
| 34 | + echo "[gcu-init] Created venv at ${VENV_DIR}" |
| 35 | + else |
| 36 | + echo "[gcu-init] Venv already exists at ${VENV_DIR}, skipping creation" |
| 37 | + fi |
| 38 | + |
| 39 | + # Install the sonic-gcu wheel. |
| 40 | + # shellcheck disable=SC2086 |
| 41 | + WHEEL_FILE=$(ls ${WHEEL_GLOB} 2>/dev/null | head -n 1) |
| 42 | + if [ -z "${WHEEL_FILE}" ]; then |
| 43 | + echo "[gcu-init] ERROR: No sonic_gcu wheel found in ${WHEEL_DIR}" >&2 |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + |
| 47 | + echo "[gcu-init] Installing ${WHEEL_FILE} into ${VENV_DIR} ..." |
| 48 | + "${VENV_DIR}/bin/pip" install --no-index --system-site-packages "${WHEEL_FILE}" |
| 49 | + |
| 50 | + # Write a sentinel file so the healthcheck can verify setup completed OK. |
| 51 | + touch "${SETUP_SENTINEL}" |
| 52 | + echo "[gcu-init] GCU venv setup complete." |
| 53 | +} |
| 54 | + |
| 55 | +############################################################################### |
| 56 | +# healthcheck: smoke-test venv GCU + host GCU |
| 57 | +############################################################################### |
| 58 | +do_healthcheck() { |
| 59 | + echo "[gcu-init] Running GCU health checks ..." |
| 60 | + |
| 61 | + # Gate on setup sentinel — if setup did not complete successfully, skip |
| 62 | + # the healthcheck to avoid misleading 'config apply-patch' errors. |
| 63 | + if [[ ! -f "${SETUP_SENTINEL}" ]]; then |
| 64 | + echo "[gcu-init] ERROR: Setup sentinel not found at ${SETUP_SENTINEL}." >&2 |
| 65 | + echo "[gcu-init] 'gcu-setup' may have failed. Skipping healthcheck." >&2 |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + |
| 69 | + # Health-check 1: venv GCU — apply an empty JSON patch via the venv binary. |
| 70 | + # An empty patch list '[]' is a no-op and must succeed without error. |
| 71 | + echo "[gcu-init] Checking venv GCU (${VENV_DIR}/bin/config apply-patch) ..." |
| 72 | + if echo '[]' | "${VENV_DIR}/bin/config" apply-patch /dev/stdin; then |
| 73 | + echo "[gcu-init] Venv GCU health check: PASS" |
| 74 | + else |
| 75 | + echo "[gcu-init] Venv GCU health check: FAIL" >&2 |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + |
| 79 | + # Health-check 2: host (system-installed) GCU — same empty patch via the |
| 80 | + # system-wide 'config' CLI. SONiC containers run as root so sudo is not |
| 81 | + # needed; running it directly avoids requiring a sudoers configuration |
| 82 | + # inside the container. |
| 83 | + echo "[gcu-init] Checking host GCU (config apply-patch) ..." |
| 84 | + if echo '[]' | config apply-patch /dev/stdin; then |
| 85 | + echo "[gcu-init] Host GCU health check: PASS" |
| 86 | + else |
| 87 | + echo "[gcu-init] Host GCU health check: FAIL" >&2 |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + |
| 91 | + echo "[gcu-init] All GCU health checks passed." |
| 92 | +} |
| 93 | + |
| 94 | +############################################################################### |
| 95 | +# Dispatch |
| 96 | +############################################################################### |
| 97 | +COMMAND="${1:-setup}" |
| 98 | + |
| 99 | +case "${COMMAND}" in |
| 100 | + setup) |
| 101 | + do_setup |
| 102 | + ;; |
| 103 | + healthcheck) |
| 104 | + do_healthcheck |
| 105 | + ;; |
| 106 | + *) |
| 107 | + echo "Usage: $0 {setup|healthcheck}" >&2 |
| 108 | + exit 1 |
| 109 | + ;; |
| 110 | +esac |
0 commit comments