preflight.sh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. # shellcheck source=wp13-common.sh
  5. source "${SCRIPT_DIR}/wp13-common.sh"
  6. usage() {
  7. cat <<'EOF'
  8. Usage: preflight.sh [options]
  9. Validate an offline bundle or an existing Compose project before install/upgrade.
  10. Options:
  11. --bundle-root PATH Bundle or repository root.
  12. --compose-file PATH Compose file (default: <root>/deploy/docker/docker-compose.yml).
  13. --env-file PATH Optional Compose environment file.
  14. --project-name NAME Compose project (default: dataops-test).
  15. --checksums-only Verify checksums and exit before Docker access.
  16. --skip-checksums Repository development mode only.
  17. --help Show this help.
  18. EOF
  19. }
  20. BUNDLE_ROOT="$(wp13_default_root)"
  21. COMPOSE_FILE=""
  22. ENV_FILE=""
  23. PROJECT_NAME="dataops-test"
  24. CHECKSUMS_ONLY=0
  25. SKIP_CHECKSUMS=0
  26. while [[ $# -gt 0 ]]; do
  27. case "$1" in
  28. --bundle-root) BUNDLE_ROOT="$2"; shift 2 ;;
  29. --compose-file) COMPOSE_FILE="$2"; shift 2 ;;
  30. --env-file) ENV_FILE="$2"; shift 2 ;;
  31. --project-name) PROJECT_NAME="$2"; shift 2 ;;
  32. --checksums-only) CHECKSUMS_ONLY=1; shift ;;
  33. --skip-checksums) SKIP_CHECKSUMS=1; shift ;;
  34. --help|-h) usage; exit 0 ;;
  35. *) wp13_die "unknown argument: $1" ;;
  36. esac
  37. done
  38. BUNDLE_ROOT="$(cd "${BUNDLE_ROOT}" && pwd)"
  39. COMPOSE_FILE="${COMPOSE_FILE:-${BUNDLE_ROOT}/deploy/docker/docker-compose.yml}"
  40. if [[ "${SKIP_CHECKSUMS}" -eq 0 ]]; then
  41. wp13_verify_checksums "${BUNDLE_ROOT}"
  42. fi
  43. if [[ "${CHECKSUMS_ONLY}" -eq 1 ]]; then
  44. exit 0
  45. fi
  46. wp13_require_command docker
  47. docker version >/dev/null
  48. docker compose version >/dev/null
  49. [[ -f "${COMPOSE_FILE}" ]] || wp13_die "compose file not found: ${COMPOSE_FILE}"
  50. if [[ -n "${ENV_FILE}" ]]; then
  51. [[ -r "${ENV_FILE}" ]] || wp13_die "environment file is not readable: ${ENV_FILE}"
  52. fi
  53. wp13_compose "${COMPOSE_FILE}" "${PROJECT_NAME}" "${ENV_FILE}" config --quiet
  54. wp13_log "Compose configuration resolved"
  55. while IFS= read -r image; do
  56. [[ -n "${image}" ]] || continue
  57. docker image inspect "${image}" >/dev/null 2>&1 \
  58. || wp13_die "required offline image is missing: ${image}"
  59. done < <(
  60. wp13_compose "${COMPOSE_FILE}" "${PROJECT_NAME}" "${ENV_FILE}" \
  61. config --images | LC_ALL=C sort -u
  62. )
  63. wp13_log "all default-profile images are available locally"
  64. if [[ -n "$(wp13_project_running_ids "${PROJECT_NAME}")" ]]; then
  65. current="$(
  66. wp13_compose "${COMPOSE_FILE}" "${PROJECT_NAME}" "${ENV_FILE}" \
  67. exec -T backend alembic -c alembic.ini current \
  68. | awk 'NF {print $1; exit}'
  69. )"
  70. head="$(
  71. wp13_compose "${COMPOSE_FILE}" "${PROJECT_NAME}" "${ENV_FILE}" \
  72. exec -T backend alembic -c alembic.ini heads \
  73. | awk 'NF {print $1; exit}'
  74. )"
  75. [[ -n "${current}" && "${current}" == "${head}" ]] \
  76. || wp13_die "database migration is not at head: current=${current:-unknown}, head=${head:-unknown}"
  77. wp13_log "database migration is at head: ${current}"
  78. else
  79. wp13_log "project is not running; migration will be checked after first start"
  80. fi
  81. available_kb="$(df -Pk "${BUNDLE_ROOT}" | awk 'NR==2 {print $4}')"
  82. [[ "${available_kb:-0}" -ge 10485760 ]] \
  83. || wp13_die "less than 10 GiB free space is available"
  84. wp13_log "preflight passed for project ${PROJECT_NAME}"