-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreflight.sh
More file actions
executable file
·113 lines (98 loc) · 3.08 KB
/
preflight.sh
File metadata and controls
executable file
·113 lines (98 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2026 Marcus Krueger
# Pre-flight check: validates a container runtime is available on the host.
# Runs via initializeCommand BEFORE any container build/pull/start.
set -euo pipefail
# --- OS detection ---
detect_os() {
if [[ -f /proc/version ]] && grep -qi 'microsoft\|wsl' /proc/version 2>/dev/null; then
echo "wsl"
elif [[ "$(uname -s)" == "Darwin" ]]; then
echo "macos"
else
echo "linux"
fi
}
# --- Timeout wrapper (macOS lacks coreutils timeout) ---
run_with_timeout() {
local seconds="$1"
shift
if command -v timeout &>/dev/null; then
timeout "$seconds" "$@" &>/dev/null 2>&1
else
# Fallback for macOS: background + kill
"$@" &>/dev/null 2>&1 &
local pid=$!
(sleep "$seconds" && kill "$pid" 2>/dev/null) &
local watchdog=$!
if wait "$pid" 2>/dev/null; then
kill "$watchdog" 2>/dev/null
wait "$watchdog" 2>/dev/null
return 0
else
kill "$watchdog" 2>/dev/null
wait "$watchdog" 2>/dev/null
return 1
fi
fi
}
# --- Runtime detection ---
check_runtime() {
local runtime="$1"
if ! command -v "$runtime" &>/dev/null; then
return 1
fi
if run_with_timeout 5 "$runtime" info; then
return 0
fi
return 1
}
# --- Main ---
for runtime in docker podman; do
if check_runtime "$runtime"; then
exit 0
fi
done
# No working runtime found — determine why and advise
found_binary=""
for runtime in docker podman; do
if command -v "$runtime" &>/dev/null; then
found_binary="$runtime"
break
fi
done
HOST_OS="$(detect_os)"
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ CodeForge: Container runtime not available ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
if [[ -n "$found_binary" ]]; then
echo " Found '$found_binary' but the daemon is not responding."
echo ""
case "$HOST_OS" in
wsl)
echo " Fix: Start Docker Desktop and enable WSL 2 integration:"
echo " Settings → Resources → WSL Integration"
;;
macos)
echo " Fix: Start Docker Desktop:"
echo " open -a Docker"
;;
linux)
echo " Fix: Start the Docker daemon:"
echo " sudo systemctl start docker"
;;
esac
else
echo " No container runtime (docker or podman) found in PATH."
echo ""
echo " Install Docker Desktop:"
echo " https://www.docker.com/products/docker-desktop/"
echo ""
echo " Or install Podman:"
echo " https://podman.io/getting-started/installation"
fi
echo ""
exit 1