-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·134 lines (118 loc) · 3.76 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·134 lines (118 loc) · 3.76 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
#!/usr/bin/env bash
set -Eeuo pipefail
ROOT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
BIN="$ROOT_DIR/amap"
INSTALL_DIR="${AMAP_INSTALL_DIR:-${HOME}/bin}"
INSTALL_DIR_EXPLICIT=0
ACTION="deploy"
usage() {
cat <<'EOF'
Usage: ./deploy.sh [--install-dir DIR] [--uninstall|--rollback] [-h|--help]
EOF
}
while (($# > 0)); do
case "$1" in
--install-dir)
[[ $# -ge 2 ]] || { echo "--install-dir requires a directory" >&2; exit 2; }
INSTALL_DIR="$2"; INSTALL_DIR_EXPLICIT=1; shift 2
;;
--uninstall)
ACTION="uninstall"; shift
;;
--rollback)
ACTION="rollback"; shift
;;
-h|--help)
usage; exit 0
;;
*)
echo "Unknown argument: $1" >&2; usage >&2; exit 2
;;
esac
done
# Detect active amap on PATH to avoid deploying to ~/.local/bin while PATH
# resolves to a different location (e.g. ~/bin).
ACTIVE_AMAP=$(type -P amap 2>/dev/null || true)
INSTALL_DIRS=("$INSTALL_DIR")
if ((INSTALL_DIR_EXPLICIT == 0)) && [[ -n "$ACTIVE_AMAP" && -x "$ACTIVE_AMAP" ]]; then
ACTIVE_DIR=$(dirname -- "$ACTIVE_AMAP")
if [[ "$ACTIVE_DIR" != "$INSTALL_DIR" ]]; then
INSTALL_DIRS+=("$ACTIVE_DIR")
echo "Detected active amap on PATH: $ACTIVE_AMAP; will also deploy to $ACTIVE_DIR"
fi
fi
is_alive() {
[[ "$1" =~ ^[0-9]+$ ]] && kill -0 "$1" 2>/dev/null
}
is_amap_process() {
local pid="$1" args
is_alive "$pid" || return 1
args=$(ps -p "$pid" -o args= 2>/dev/null || true)
[[ "$args" == *"amap serve"* || "$args" == *"amap dashboard"* || "$args" == *"amap mcp"* ]]
}
stop_pid() {
local pid="$1"
is_alive "$pid" || return 0
echo "Stopping amap process: $pid"
kill -TERM "$pid" 2>/dev/null || true
for _ in {1..20}; do
is_alive "$pid" || return 0
sleep 0.25
done
echo "Process did not exit gracefully, sending KILL: $pid" >&2
kill -KILL "$pid" 2>/dev/null || true
}
is_amap_service_command() {
local args="$1" path
for path in "${INSTALL_DIRS[@]}" "$ROOT_DIR"; do
[[ "$args" == "$path/amap serve"* || "$args" == "$path/amap dashboard"* || "$args" == "$path/amap mcp"* ]] && return 0
done
[[ "$args" == */amap\ serve* || "$args" == */amap\ dashboard* || "$args" == */amap\ mcp* ]]
}
stop_running_processes() {
while read -r pid args; do
[[ -n "${pid:-}" && "$pid" != "$$" ]] || continue
if is_amap_service_command "$args"; then
stop_pid "$pid"
fi
done < <(ps -eo pid=,args=)
}
echo "[1/3] Stopping running amap processes"
stop_running_processes
INSTALL_BIN="${INSTALL_DIR}/amap"
PREVIOUS_BIN="${INSTALL_DIR}/amap.previous"
if [[ "$ACTION" == "uninstall" ]]; then
for directory in "${INSTALL_DIRS[@]}"; do
rm -f -- "$directory/amap" "$directory/amap.previous"
done
echo "Uninstalled amap binary"
exit 0
fi
if [[ "$ACTION" == "rollback" ]]; then
[[ -f "$PREVIOUS_BIN" ]] || { echo "No previous version to rollback: $PREVIOUS_BIN" >&2; exit 1; }
if [[ -f "$INSTALL_BIN" ]]; then
mv -f -- "$INSTALL_BIN" "${INSTALL_BIN}.failed"
fi
mv -- "$PREVIOUS_BIN" "$INSTALL_BIN"
rm -f -- "${INSTALL_BIN}.failed"
echo "Rolled back to: $INSTALL_BIN"
exit 0
fi
echo "[2/3] Building amap"
make -C "$ROOT_DIR" build
echo "[3/3] Installing amap"
for directory in "${INSTALL_DIRS[@]}"; do
mkdir -p "$directory"
target="$directory/amap"
previous="$directory/amap.previous"
staging="${target}.new"
cp "$ROOT_DIR/amap" "$staging"
chmod 0755 "$staging"
if [[ -f "$target" ]]; then
mv -f -- "$target" "$previous"
fi
mv -- "$staging" "$target"
echo "Installed: $target"
done
echo "Deploy complete"
echo "Verify: $INSTALL_BIN --version"