-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaniel-runner.sh
More file actions
executable file
·309 lines (268 loc) · 8.89 KB
/
Copy pathhaniel-runner.sh
File metadata and controls
executable file
·309 lines (268 loc) · 8.89 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env bash
# haniel-runner.sh - outer loop wrapper for Haniel self-update on Linux/systemd.
#
# Register this script as the systemd service ExecStart, not `python -m haniel`
# directly. Exit code 10 from Haniel means "self-update approved"; this wrapper
# then fetches, resets, reinstalls, writes the self-update result marker, and
# launches Haniel again.
set -u
ROOT_DIR="$(pwd)"
CONF_PATH="$ROOT_DIR/haniel-runner.conf"
if [[ ! -f "$CONF_PATH" ]]; then
echo "Configuration file not found: $CONF_PATH" >&2
exit 1
fi
declare -A CONFIG
while IFS='=' read -r raw_key raw_value; do
key="$(printf '%s' "$raw_key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
value="$(printf '%s' "${raw_value:-}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
if [[ -z "$key" || "$key" == \#* ]]; then
continue
fi
CONFIG["$key"]="$value"
done < "$CONF_PATH"
WEBHOOK_URL="${CONFIG[WEBHOOK_URL]:-}"
HANIEL_REPO="${CONFIG[HANIEL_REPO]:-}"
CONFIG_FILE="${CONFIG[CONFIG]:-}"
MAX_GIT_FAILURES="${CONFIG[MAX_GIT_FAILURES]:-3}"
PYTHON_BIN_CONFIG="${CONFIG[PYTHON_BIN]:-}"
if [[ -z "$HANIEL_REPO" || -z "$CONFIG_FILE" ]]; then
echo "HANIEL_REPO and CONFIG must be set in haniel-runner.conf" >&2
exit 1
fi
resolve_path() {
local path="$1"
if [[ "$path" = /* ]]; then
printf '%s\n' "$path"
else
printf '%s/%s\n' "$ROOT_DIR" "$path"
fi
}
REPO_PATH="$(resolve_path "$HANIEL_REPO")"
CONFIG_PATH="$(resolve_path "$CONFIG_FILE")"
detect_python() {
if [[ -n "$PYTHON_BIN_CONFIG" ]]; then
resolve_path "$PYTHON_BIN_CONFIG"
return
fi
if [[ -x "$REPO_PATH/.venv/bin/python" ]]; then
printf '%s\n' "$REPO_PATH/.venv/bin/python"
return
fi
if command -v python3 >/dev/null 2>&1; then
command -v python3
return
fi
command -v python
}
PYTHON_BIN="$(detect_python)"
if [[ -z "$PYTHON_BIN" || ! -x "$PYTHON_BIN" ]]; then
echo "Python executable not found. Set PYTHON_BIN in haniel-runner.conf." >&2
exit 1
fi
send_webhook() {
local message="$1"
local level="${2:-info}"
if [[ -z "$WEBHOOK_URL" || "$WEBHOOK_URL" == "https://hooks.slack.com/services/YOUR/WEBHOOK/URL" ]]; then
return
fi
if ! command -v curl >/dev/null 2>&1; then
echo "curl not found; webhook skipped: $message" >&2
return
fi
"$PYTHON_BIN" - "$WEBHOOK_URL" "$level" "$message" <<'PY'
import json
import subprocess
import sys
url, level, message = sys.argv[1:4]
prefix = {
"error": ":rotating_light:",
"warning": ":warning:",
}.get(level, ":information_source:")
body = json.dumps({"text": f"{prefix} *haniel-runner*: {message}"})
try:
subprocess.run(
["curl", "-fsS", "-m", "10", "-H", "Content-Type: application/json", "-d", body, url],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass
PY
}
STEP_NAMES=()
STEP_OKS=()
STEP_ERRORS=()
LAST_UPDATE_ERROR=""
LAST_UPDATE_STARTED_AT=""
LAST_UPDATE_FINISHED_AT=""
add_step() {
local name="$1"
local ok="$2"
local error_message="${3:-}"
if [[ -z "$error_message" && "$ok" != "true" ]]; then
error_message="$name failed (no message)"
fi
STEP_NAMES+=("$name")
STEP_OKS+=("$ok")
STEP_ERRORS+=("$error_message")
if [[ "$ok" != "true" && -z "$LAST_UPDATE_ERROR" ]]; then
LAST_UPDATE_ERROR="$name failed: $error_message"
fi
}
join_unit_sep() {
local IFS=$'\x1f'
printf '%s' "$*"
}
write_self_update_marker() {
local ok="$1"
local marker_path="$ROOT_DIR/.local/self_update_result.json"
mkdir -p "$ROOT_DIR/.local"
HANIEL_STEP_NAMES="$(join_unit_sep "${STEP_NAMES[@]}")" \
HANIEL_STEP_OKS="$(join_unit_sep "${STEP_OKS[@]}")" \
HANIEL_STEP_ERRORS="$(join_unit_sep "${STEP_ERRORS[@]}")" \
HANIEL_LAST_UPDATE_ERROR="$LAST_UPDATE_ERROR" \
"$PYTHON_BIN" - "$marker_path" "$LAST_UPDATE_STARTED_AT" "$LAST_UPDATE_FINISHED_AT" "$ok" <<'PY'
import json
import os
import sys
marker_path, started_at, finished_at, ok_raw = sys.argv[1:5]
sep = "\x1f"
names = os.environ.get("HANIEL_STEP_NAMES", "").split(sep) if os.environ.get("HANIEL_STEP_NAMES") else []
oks = os.environ.get("HANIEL_STEP_OKS", "").split(sep) if os.environ.get("HANIEL_STEP_OKS") else []
errors = os.environ.get("HANIEL_STEP_ERRORS", "").split(sep) if os.environ.get("HANIEL_STEP_ERRORS") else []
steps = []
for index, name in enumerate(names):
steps.append({
"name": name,
"ok": index < len(oks) and oks[index] == "true",
"error": errors[index] if index < len(errors) and errors[index] else None,
})
payload = {
"version": 1,
"started_at": started_at,
"finished_at": finished_at,
"ok": ok_raw == "true",
"steps": steps,
"error": os.environ.get("HANIEL_LAST_UPDATE_ERROR") or None,
}
with open(marker_path, "w", encoding="utf-8") as handle:
json.dump(payload, handle, ensure_ascii=False, indent=2)
PY
}
update_haniel_repo() {
local git_failures=0
local output=""
while (( git_failures < MAX_GIT_FAILURES )); do
output="$(git -C "$REPO_PATH" fetch origin 2>&1)"
if [[ $? -eq 0 ]]; then
break
fi
git_failures=$((git_failures + 1))
echo "git fetch failed (attempt $git_failures/$MAX_GIT_FAILURES): $output" >&2
sleep 5
done
if (( git_failures >= MAX_GIT_FAILURES )); then
add_step "git_fetch" "false" "git fetch failed $MAX_GIT_FAILURES times"
send_webhook "git fetch failed $MAX_GIT_FAILURES times. Launching with current code." "error"
return 1
fi
add_step "git_fetch" "true"
local branch
branch="$(git -C "$REPO_PATH" rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [[ -z "$branch" ]]; then
branch="main"
fi
output="$(git -C "$REPO_PATH" reset --hard "origin/$branch" 2>&1)"
if [[ $? -ne 0 ]]; then
add_step "git_reset" "false" "${output:-git reset failed}"
send_webhook "git reset --hard failed. Launching with current code." "warning"
return 1
fi
add_step "git_reset" "true"
output="$("$PYTHON_BIN" -m pip install -e "$REPO_PATH" 2>&1)"
if [[ $? -ne 0 ]]; then
add_step "pip_install" "false" "${output:-pip install failed}"
send_webhook "pip install failed. Attempting to launch with previous code." "warning"
else
add_step "pip_install" "true"
fi
local dashboard_path="$REPO_PATH/dashboard"
if [[ -d "$dashboard_path" ]]; then
echo "[haniel-runner] Building dashboard..."
if ! command -v pnpm >/dev/null 2>&1; then
add_step "pnpm_install" "false" "pnpm not found"
send_webhook "Dashboard pnpm install failed. pnpm not found." "warning"
return 0
fi
output="$(pnpm --dir "$dashboard_path" install 2>&1)"
if [[ $? -ne 0 ]]; then
add_step "pnpm_install" "false" "${output:-pnpm install failed}"
send_webhook "Dashboard pnpm install failed. Launching with previous build." "warning"
return 0
fi
add_step "pnpm_install" "true"
output="$(pnpm --dir "$dashboard_path" build 2>&1)"
if [[ $? -ne 0 ]]; then
add_step "pnpm_build" "false" "${output:-pnpm build failed}"
send_webhook "Dashboard build failed. Launching with previous build." "warning"
else
add_step "pnpm_build" "true"
fi
fi
return 0
}
if [[ ! -d "$REPO_PATH" ]]; then
send_webhook "Haniel repo not found at $REPO_PATH" "error"
echo "Repo not found: $REPO_PATH" >&2
exit 1
fi
EXIT_SELF_UPDATE=10
EXIT_RESTART=11
skip_update=false
write_self_update_marker=false
while true; do
if [[ "$skip_update" != "true" ]]; then
echo "[haniel-runner] Updating haniel repository..."
STEP_NAMES=()
STEP_OKS=()
STEP_ERRORS=()
LAST_UPDATE_ERROR=""
LAST_UPDATE_STARTED_AT="$("$PYTHON_BIN" -c 'from datetime import datetime, timezone; print(datetime.now(timezone.utc).isoformat())')"
LAST_UPDATE_FINISHED_AT=""
update_haniel_repo || true
LAST_UPDATE_FINISHED_AT="$("$PYTHON_BIN" -c 'from datetime import datetime, timezone; print(datetime.now(timezone.utc).isoformat())')"
update_ok=true
if [[ -n "$LAST_UPDATE_ERROR" ]]; then
update_ok=false
fi
if [[ "$write_self_update_marker" == "true" ]]; then
write_self_update_marker "$update_ok"
fi
write_self_update_marker=false
fi
skip_update=false
echo "[haniel-runner] Launching haniel..."
"$PYTHON_BIN" -m haniel.cli run "$CONFIG_PATH"
exit_code=$?
echo "[haniel-runner] haniel exited with code: $exit_code"
if [[ "$exit_code" -eq 0 ]]; then
echo "[haniel-runner] Clean shutdown. Exiting wrapper."
exit 0
elif [[ "$exit_code" -eq "$EXIT_SELF_UPDATE" ]]; then
echo "[haniel-runner] Self-update requested. Looping..."
send_webhook "Self-update initiated. Updating and restarting..." "info"
write_self_update_marker=true
sleep 5
elif [[ "$exit_code" -eq "$EXIT_RESTART" ]]; then
echo "[haniel-runner] Restart requested. Skipping update..."
send_webhook "Restart initiated (no update)." "info"
skip_update=true
sleep 3
else
echo "[haniel-runner] Unexpected exit code $exit_code. Exiting wrapper."
send_webhook "haniel exited with unexpected code $exit_code." "error"
exit "$exit_code"
fi
done