-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.sh
More file actions
158 lines (149 loc) · 6.24 KB
/
launch.sh
File metadata and controls
158 lines (149 loc) · 6.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env bash
# Author: Nima Shafie
# =============================================================================
# launch.sh -- airgap-cpp-devkit primary entry point
#
# Preferred way to install and manage devkit tools.
# Finds Python 3.8+, launches the DevKit Manager web UI, and opens the browser.
# Falls back to the interactive CLI installer (install-cli.sh) if Python is absent.
#
# USAGE:
# bash launch.sh # auto-detect; launch UI or CLI fallback
# bash launch.sh --port 9090 # custom port for the web UI
# bash launch.sh --host 0.0.0.0 # bind to all interfaces (LAN access)
# bash launch.sh --no-browser # start server but don't open browser
# bash launch.sh --cli # skip UI, run install-cli.sh directly
#
# WHAT HAPPENS:
# 1. Script searches for Python 3.8+ (python3 / python).
# 2. If found -> launches manager/devkit.py and opens
# http://127.0.0.1:8080 (or --port value).
# From there: pick a profile or install tools individually.
# 3. If not found -> falls back to bash install-cli.sh (interactive CLI wizard).
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEVKIT_UI="${SCRIPT_DIR}/manager/devkit.py"
INSTALL_SH="${SCRIPT_DIR}/install-cli.sh"
# ---------------------------------------------------------------------------
# Plain ASCII display helpers (match install-cli.sh style)
# ---------------------------------------------------------------------------
_sep2() { printf '%s\n' "================================================================================"; }
# ---------------------------------------------------------------------------
# Parse flags: consume --cli; pass everything else through to devkit.py
# ---------------------------------------------------------------------------
FORCE_CLI=false
UI_ARGS=()
UI_PORT=8080
UI_HOST="127.0.0.1"
while [[ $# -gt 0 ]]; do
case "$1" in
--cli) FORCE_CLI=true; shift ;;
--port) UI_PORT="$2"; UI_ARGS+=("$1" "$2"); shift 2 ;;
--host) UI_HOST="$2"; UI_ARGS+=("$1" "$2"); shift 2 ;;
*) UI_ARGS+=("$1"); shift ;;
esac
done
# ---------------------------------------------------------------------------
# --cli: skip Python detection, go straight to install-cli.sh
# ---------------------------------------------------------------------------
if [[ "${FORCE_CLI}" == "true" ]]; then
echo ""
_sep2
echo " airgap-cpp-devkit -- CLI Installer"
_sep2
echo ""
echo " [--cli] Skipping DevKit Manager. Launching install-cli.sh..."
echo ""
exec bash "${INSTALL_SH}" "${UI_ARGS[@]+"${UI_ARGS[@]}"}"
fi
# ---------------------------------------------------------------------------
# Find Python 3.8+
# ---------------------------------------------------------------------------
_find_python() {
local candidates=(python3 python python3.13 python3.12 python3.11 python3.10 python3.9 python3.8)
for py in "${candidates[@]}"; do
if command -v "${py}" &>/dev/null; then
local ok
ok="$("${py}" -c 'import sys; print(sys.version_info >= (3,8))' 2>/dev/null || echo "False")"
if [[ "${ok}" == "True" ]]; then
echo "${py}"
return 0
fi
fi
done
return 1
}
# ---------------------------------------------------------------------------
# Free the target port if anything is already bound to it
# ---------------------------------------------------------------------------
_free_port() {
local port="$1"
local pids
# netstat -ano works on Windows (MINGW/MSYS) and Linux
# || true prevents grep's exit-1-on-no-match from killing the script under set -e
pids="$(netstat -ano 2>/dev/null \
| grep -E "[:.]${port}[[:space:]].*LISTEN" \
| awk '{print $NF}' \
| sort -u)" || true
if [[ -z "$pids" ]]; then
return 0
fi
echo " [!!] Port ${port} is in use — killing existing process(es): ${pids}"
for pid in $pids; do
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "${OS:-}" == "Windows_NT" ]]; then
taskkill.exe //PID "$pid" //F 2>/dev/null || true
else
kill -9 "$pid" 2>/dev/null || true
fi
done
sleep 1
}
echo ""
_sep2
echo " airgap-cpp-devkit -- Launcher"
_sep2
echo ""
echo " Checking for Python 3.8+..."
PYTHON_BIN=""
if PYTHON_BIN="$(_find_python 2>/dev/null)"; then
PY_VER="$(${PYTHON_BIN} --version 2>&1 | awk '{print $2}')"
echo " [OK] Python ${PY_VER} found (${PYTHON_BIN})"
echo ""
_free_port "${UI_PORT}"
echo " Starting DevKit Manager..."
echo " Open your browser at http://${UI_HOST}:${UI_PORT} if it does not open automatically."
echo " Press Ctrl+C to stop the server."
echo ""
_sep2
echo ""
# Standalone Python ships with a ._pth file that overrides sys.path entirely,
# ignoring both PYTHONPATH env var and cwd. The only reliable path injection
# is via -c code: sys.path.insert runs before any import, always wins.
# Use forward slashes in the Windows path — Python accepts them and it avoids
# backslash-escaping inside the shell double-quoted -c string.
TOOLS_DIR="${SCRIPT_DIR}/tools"
PY_SRC="${SCRIPT_DIR}/manager/src"
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "${OS:-}" == "Windows_NT" ]]; then
PY_SRC="$(cygpath -w "${PY_SRC}")"
PY_SRC="${PY_SRC//\\/\/}"
fi
# Do NOT use exec here — exec replaces bash with Python, causing MINGW64 to
# show a shell prompt while the server is still running, which leads users to
# close the terminal and kill the server unexpectedly.
"${PYTHON_BIN}" -c \
"import sys; sys.path.insert(0, '${PY_SRC}'); from airgap_devkit.launcher import main; main()" \
--tools "${TOOLS_DIR}" \
"${UI_ARGS[@]+"${UI_ARGS[@]}"}"
else
echo " [!!] Python 3.8+ not found on PATH."
echo ""
echo " The DevKit Manager (web UI) requires Python 3.8+."
echo ""
echo " Falling back to the interactive CLI installer..."
echo ""
printf " Press Enter to continue with install-cli.sh, or Ctrl+C to cancel..."
read -r
echo ""
exec bash "${INSTALL_SH}"
fi