GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vm2api/scripts/docker-entrypoint.sh at dev · dofastted/vm2api

Expand file tree

/

docker-entrypoint.sh

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
277 lines (261 loc) · 10.1 KB
/

docker-entrypoint.sh

Copy path

File metadata and controls

executable file
·
277 lines (261 loc) · 10.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/bin/sh
set -eu
ROOT="${KIN_PROJECT_ROOT:-/opt/vm2api}"
IMAGE_BIN="/opt/vm2api/image-bin"
mkdir -p "$ROOT/data"
# Modes:
# (default) leader boot: take the data-dir lock, refresh shared binaries, exec the server.
# standby KIN_STANDBY=1 without its activation marker: a blue/green standby boots next to the
# leader. It neither locks nor touches shared files; the server takes the lock when it
# is activated and calls `vm2api-entrypoint sync` once the old controller has exited.
# sync refresh shared binaries only (caller already holds the lock); prints WRAP_CHANGED=0|1.
MODE="${1:-run}"
STANDBY=0
if [ "$MODE" = run ] && [ "${KIN_STANDBY:-}" = 1 ]; then
if [ -z "${KIN_STANDBY_ACTIVE_FILE:-}" ] || [ ! -f "$KIN_STANDBY_ACTIVE_FILE" ]; then
STANDBY=1
fi
fi
# A blue/green install (an activation marker exists) runs only through deploy/bluegreen/bg-deploy.sh.
# A container started the single-container way (plain `docker compose up -d`) would contend with the
# active colour for the lock and the shared files, so it refuses to start.
if [ "$MODE" = run ] && [ "${KIN_STANDBY:-}" != 1 ]; then
for marker in "$ROOT"/data/run/bg-active-*; do
[ -e "$marker" ] || continue
echo "vm2api_bluegreen_install: this install runs blue/green ($(basename "$marker")); deploy with deploy/bluegreen/bg-deploy.sh, not docker compose up." >&2
echo "vm2api_bluegreen_install: to go back to a single container, see docs/BLUEGREEN.md (rollback)." >&2
exit 78
done
fi
if [ "$MODE" = run ] && [ "$STANDBY" = 0 ]; then
# Match the Node startup lock before replacing any shared binaries or configuration.
exec 9<"$ROOT/data"
flock -n -E 75 9 || {
rc=$?
if [ "$rc" = 75 ]; then
echo 'vm2api_control_plane_already_running: stop the other controller first' >&2
fi
exit "$rc"
}
fi
mkdir -p "$ROOT/vms" "$ROOT/bin"
# 镜像安装没有仓库 checkout:把镜像内默认配置补进被 bind-mount 覆盖的 src/config。
mkdir -p "$ROOT/src/config"
if [ -d /opt/vm2api/image-config ]; then
for f in /opt/vm2api/image-config/*; do
[ -f "$f" ] || continue
dest="$ROOT/src/config/$(basename "$f")"
[ -f "$dest" ] || cp "$f" "$dest"
done
fi
# ARM64 主机上 slot 是 amd64,依赖宿主 qemu-x86_64 binfmt handler(重启后可能丢失)。
# 静态 amd64 kin-worker 无参数时以 2 退出;shell 返回 126/127 表示无法执行 x86 程序。
# 只提示不阻塞:面板仍要能打开。
X86_PROBE="${KIN_X86_PROBE_BIN:-/opt/vm2api/cluster-bin-amd64/kin-worker}"
if [ "$(uname -m)" = aarch64 ] && [ -f "$X86_PROBE" ]; then
probe_rc=0
"$X86_PROBE" >/dev/null 2>&1 || probe_rc=$?
if [ "$probe_rc" -eq 126 ] || [ "$probe_rc" -eq 127 ]; then
echo "vm2api: 宿主缺少 qemu-x86_64 binfmt handler,amd64 slot 将无法启动。" >&2
echo "vm2api: 在宿主执行 python3 deploy/prepare-arm64.py(详见 docs/ARM64.md)。" >&2
fi
fi
# No seeded vm-01. A fresh fleet is empty. Seeding an active placeholder made
# the only slot undeletable: DELETE refuses the active VM, and the next start
# would recreate the files.
ensure_bin() {
name="$1"
dest="$ROOT/bin/$name"
src="$IMAGE_BIN/$name"
if [ ! -f "$src" ]; then
if [ -f "$dest" ]; then
chmod 755 "$dest" || true
fi
return 0
fi
tmp="$dest.new"
cp "$src" "$tmp"
chmod 755 "$tmp"
mv -f "$tmp" "$dest"
}
sync_shared() {
ensure_bin kin-kernel
ensure_bin kin-egress
ensure_bin kin-slot-gate
ensure_bin kin-worker
ensure_bin kin-api-kernel
ensure_bin kin-redact
ensure_bin kin-scheduler
ensure_bin kin-codex-kernel
ensure_bin kin-oauth-auth
ensure_bin kin-computer
KERNEL="${KIN_KERNEL_BIN:-$ROOT/bin/kin-kernel}"
if [ ! -x "$KERNEL" ]; then
echo "vm2api: $KERNEL missing or not executable after image-bin copy. Rebuild with docker compose build, or put linux amd64 Release files in $ROOT/bin." >&2
exit 1
fi
API_KERNEL="${KIN_API_KERNEL_BIN:-$ROOT/bin/kin-api-kernel}"
if [ ! -x "$API_KERNEL" ]; then
echo "vm2api: $API_KERNEL missing or not executable after image-bin copy. Rebuild the control-plane image so kin-api-kernel is staged in image-bin." >&2
exit 1
fi
SCHEDULER="${KIN_SCHEDULER_BIN:-$ROOT/bin/kin-scheduler}"
if [ ! -x "$SCHEDULER" ]; then
echo "vm2api: $SCHEDULER missing or not executable after image-bin copy. Rebuild the control-plane image so kin-scheduler is staged in image-bin." >&2
exit 1
fi
mkdir -p "$ROOT/share/wrap-cli"
WRAP_CHANGED=0
if [ -d /opt/vm2api/image-wrap-cli ]; then
# 首装先整目录落地(glibc239 / SAMPLE.json),再由下面的循环按内容覆盖可执行文件。
if [ ! -f "$ROOT/share/wrap-cli/cli-node" ]; then
cp -a /opt/vm2api/image-wrap-cli/. "$ROOT/share/wrap-cli/"
WRAP_CHANGED=1
fi
# cli-node 也随镜像覆盖:只在缺失时复制会让升级用户一直留着旧 ELF(#120 baseline 换不进去)。
for name in kin-kernel.bin kin-kernel cli-node cc-node; do
src="/opt/vm2api/image-wrap-cli/$name"
dest="$ROOT/share/wrap-cli/$name"
if [ -f "$src" ]; then
if [ ! -f "$dest" ] || ! cmp -s "$src" "$dest"; then
WRAP_CHANGED=1
fi
cp "$src" "$dest.new"
chmod 755 "$dest.new"
mv -f "$dest.new" "$dest"
fi
done
fi
# The crag kernel no longer comes from a repo checkout; refresh it from the image like the wrap ELFs.
if [ -f /opt/vm2api/image-crag/kin-kernel ]; then
mkdir -p "$ROOT/share/crag"
dest="$ROOT/share/crag/kin-kernel"
if [ ! -f "$dest" ] || ! cmp -s /opt/vm2api/image-crag/kin-kernel "$dest"; then
cp /opt/vm2api/image-crag/kin-kernel "$dest.new"
chmod 755 "$dest.new"
mv -f "$dest.new" "$dest"
WRAP_CHANGED=1
fi
fi
# Bind-mounted share starts empty; seed the image's signed native package once.
node /opt/vm2api/scripts/seed-crag.mjs "$ROOT" /opt/vm2api/image-crag
}
WRAP_CHANGED=0
if [ "$STANDBY" = 0 ]; then
sync_shared
fi
if [ "$MODE" = sync ]; then
echo "WRAP_CHANGED=$WRAP_CHANGED"
exit 0
fi
if [ ! -S /var/run/docker.sock ]; then
echo "vm2api: /var/run/docker.sock not mounted; slot create/start will fail." >&2
elif [ -f /opt/vm2api/docker/kin-os/build.mjs ]; then
# 只拉预构建客体镜像;拉不到不阻塞启动,建槽时再兜底构建。
node /opt/vm2api/docker/kin-os/build.mjs --pull-only ubuntu || true
fi
env_get_file() {
name="$1"
file="$2"
[ -f "$file" ] || return 0
grep -E "^${name}=" "$file" 2>/dev/null | tail -n1 | cut -d= -f2- | tr -d "'" | tr -d '"' | tr -d '\r'
}
env_upsert_if_empty() {
name="$1"
value="$2"
file="$3"
[ -n "$file" ] || return 0
if [ -f "$file" ]; then
current="$(env_get_file "$name" "$file")"
if [ -n "$current" ]; then
return 0
fi
if grep -qE "^${name}=" "$file"; then
sed -i "s|^${name}=.*|${name}=${value}|" "$file"
else
printf '%s=%s\n' "$name" "$value" >>"$file"
fi
fi
}
rand_hex() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -hex 32
else
python3 -c 'import secrets; print(secrets.token_hex(32))'
fi
}
ENVF="${ROOT}/.env"
if [ -z "${VM2API_ADMIN_USER:-}" ]; then
VM2API_ADMIN_USER=admin
export VM2API_ADMIN_USER
env_upsert_if_empty VM2API_ADMIN_USER admin "$ENVF"
fi
if [ -z "${VM2API_ADMIN_PASSWORD:-}" ]; then
VM2API_ADMIN_PASSWORD=123456
export VM2API_ADMIN_PASSWORD
env_upsert_if_empty VM2API_ADMIN_PASSWORD 123456 "$ENVF"
echo "vm2api: VM2API_ADMIN_PASSWORD was empty; using default admin / 123456 (change after login)." >&2
fi
if [ -z "${VM2API_API_KEY:-}" ] && [ -z "${KIN_API_KEY:-}" ]; then
VM2API_API_KEY="$(rand_hex)"
export VM2API_API_KEY
env_upsert_if_empty VM2API_API_KEY "$VM2API_API_KEY" "$ENVF"
echo "vm2api: generated VM2API_API_KEY (empty in env)." >&2
fi
if [ -z "${VM2API_DB_SECRET:-}" ] && [ -z "${KIN_DB_SECRET:-}" ]; then
VM2API_DB_SECRET="$(rand_hex)"
export VM2API_DB_SECRET
env_upsert_if_empty VM2API_DB_SECRET "$VM2API_DB_SECRET" "$ENVF"
fi
if [ "$STANDBY" = 0 ]; then
export VM2API_BOOTSTRAP_LOCK_FD=9 VM2API_BOOTSTRAP_WRAP_CHANGED="$WRAP_CHANGED"
fi
# Upgrade guard: if an existing SQLite installation (data/kin.db) upgraded its Compose file
# without setting VM2API_DB_DRIVER or DATABASE_URL in .env, keep SQLite instead of letting
# docker-compose.yml's default DATABASE_HOST=127.0.0.1 accidentally boot an empty/unreachable PG.
if [ "${VM2API_DB_DRIVER:-auto}" = "auto" ] && [ -f "${ROOT}/data/kin.db" ]; then
if [ -z "${DATABASE_URL:-}" ] && [ -z "${VM2API_DATABASE_URL:-}" ] \
&& [ -z "$(env_get_file VM2API_DB_DRIVER "$ENVF")" ] \
&& [ -z "$(env_get_file DATABASE_URL "$ENVF")" ] \
&& [ -z "$(env_get_file VM2API_DATABASE_URL "$ENVF")" ]; then
VM2API_DB_DRIVER=sqlite
export VM2API_DB_DRIVER
echo "vm2api: existing SQLite detected (${ROOT}/data/kin.db) with no explicit PG config in .env; keeping SQLite. Run deploy/replace-db-to-pg.sh to migrate to PostgreSQL." >&2
fi
fi
# Wait briefly for PostgreSQL readiness when configured so cold container boots never race PG init
if [ "${VM2API_DB_DRIVER:-auto}" != "sqlite" ] && { [ "${VM2API_DB_DRIVER:-auto}" = "postgres" ] || [ -n "${DATABASE_URL:-}" ] || [ -n "${VM2API_DATABASE_URL:-}" ] || [ -n "${DATABASE_HOST:-}" ] || [ -n "${PGHOST:-}" ]; }; then
if command -v pg_isready >/dev/null 2>&1; then
pg_url="${VM2API_DATABASE_URL:-${DATABASE_URL:-}}"
pg_ready=0
if [ -n "$pg_url" ]; then
i=0
while [ "$i" -lt 25 ]; do
if pg_isready -d "$pg_url" >/dev/null 2>&1; then
pg_ready=1
break
fi
i=$((i + 1))
sleep 1
done
else
pg_host="${DATABASE_HOST:-${PGHOST:-127.0.0.1}}"
pg_port="${DATABASE_PORT:-${PGPORT:-5432}}"
pg_user="${DATABASE_USER:-${PGUSER:-vm2api}}"
i=0
while [ "$i" -lt 25 ]; do
if pg_isready -h "$pg_host" -p "$pg_port" -U "$pg_user" >/dev/null 2>&1; then
pg_ready=1
break
fi
i=$((i + 1))
sleep 1
done
fi
if [ "$pg_ready" != 1 ]; then
echo "vm2api: PostgreSQL not ready after 25s pre-wait; handing off to Node pool retry." >&2
fi
fi
fi
cd /opt/vm2api
exec node src/server.mjs
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.