Upload 5 files
Browse files- Dockerfile +0 -1
- entrypoint.sh +23 -12
- scripts/release-common.sh +84 -34
- scripts/update-loop.sh +3 -1
Dockerfile
CHANGED
|
@@ -5,7 +5,6 @@ ENV RUNTIME_DIR=/tmp/cliproxyapi
|
|
| 5 |
ENV SUPERVISOR_CONF=/etc/supervisord.conf
|
| 6 |
ENV UPDATE_INTERVAL_SECONDS=14400
|
| 7 |
ENV STOP_WAIT_SECS=60
|
| 8 |
-
ENV SUPERVISORD_MINFDS=4096
|
| 9 |
|
| 10 |
RUN apk add --no-cache ca-certificates curl supervisor tzdata \
|
| 11 |
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
|
|
|
|
| 5 |
ENV SUPERVISOR_CONF=/etc/supervisord.conf
|
| 6 |
ENV UPDATE_INTERVAL_SECONDS=14400
|
| 7 |
ENV STOP_WAIT_SECS=60
|
|
|
|
| 8 |
|
| 9 |
RUN apk add --no-cache ca-certificates curl supervisor tzdata \
|
| 10 |
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
|
entrypoint.sh
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
#!/bin/sh
|
| 2 |
set -eu
|
| 3 |
|
| 4 |
-
: "${RUNTIME_DIR:=/tmp/cliproxyapi}"
|
| 5 |
-
: "${SUPERVISOR_CONF:=/etc/supervisord.conf}"
|
| 6 |
: "${RELEASE_COMMON_SH:=/usr/local/bin/release-common.sh}"
|
| 7 |
: "${SUPERVISORD_BIN:=/usr/bin/supervisord}"
|
|
|
|
| 8 |
|
| 9 |
. "$RELEASE_COMMON_SH"
|
| 10 |
|
| 11 |
export RUNTIME_DIR
|
| 12 |
export SUPERVISOR_CONF
|
| 13 |
|
| 14 |
-
ulimit -n 65535
|
| 15 |
-
|
| 16 |
shell_quote() {
|
| 17 |
printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\"'\"'/g")"
|
| 18 |
}
|
|
@@ -33,14 +30,28 @@ write_run_app_script() {
|
|
| 33 |
chmod +x "$run_app_script"
|
| 34 |
}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
fi
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
| 1 |
#!/bin/sh
|
| 2 |
set -eu
|
| 3 |
|
|
|
|
|
|
|
| 4 |
: "${RELEASE_COMMON_SH:=/usr/local/bin/release-common.sh}"
|
| 5 |
: "${SUPERVISORD_BIN:=/usr/bin/supervisord}"
|
| 6 |
+
: "${OPEN_FILE_LIMIT:=65535}"
|
| 7 |
|
| 8 |
. "$RELEASE_COMMON_SH"
|
| 9 |
|
| 10 |
export RUNTIME_DIR
|
| 11 |
export SUPERVISOR_CONF
|
| 12 |
|
|
|
|
|
|
|
| 13 |
shell_quote() {
|
| 14 |
printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\"'\"'/g")"
|
| 15 |
}
|
|
|
|
| 30 |
chmod +x "$run_app_script"
|
| 31 |
}
|
| 32 |
|
| 33 |
+
prepare_runtime_environment() {
|
| 34 |
+
ulimit -n "$OPEN_FILE_LIMIT"
|
| 35 |
+
ensure_runtime_dirs
|
| 36 |
+
rm -f "$RUNTIME_DIR/supervisor.sock" "$RUNTIME_DIR/supervisord.pid"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
bootstrap_release_or_exit() {
|
| 40 |
+
if ! ensure_initial_release; then
|
| 41 |
+
echo "错误: 初次启动时无法准备可运行版本" >&2
|
| 42 |
+
exit 1
|
| 43 |
+
fi
|
| 44 |
+
}
|
| 45 |
|
| 46 |
+
start_supervisord() {
|
| 47 |
+
exec "$SUPERVISORD_BIN" -c "$SUPERVISOR_CONF"
|
| 48 |
+
}
|
|
|
|
| 49 |
|
| 50 |
+
main() {
|
| 51 |
+
prepare_runtime_environment
|
| 52 |
+
bootstrap_release_or_exit
|
| 53 |
+
write_run_app_script "$@"
|
| 54 |
+
start_supervisord
|
| 55 |
+
}
|
| 56 |
|
| 57 |
+
main "$@"
|
scripts/release-common.sh
CHANGED
|
@@ -12,7 +12,16 @@
|
|
| 12 |
: "${APP_BINARY_NAMES:=cli-proxy-api CLIProxyAPI}"
|
| 13 |
|
| 14 |
log() {
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
ensure_runtime_dirs() {
|
|
@@ -30,19 +39,22 @@ current_binary_path() {
|
|
| 30 |
}
|
| 31 |
|
| 32 |
fetch_latest_release_info() {
|
| 33 |
-
response=$(curl -fsSL "$REPO_API_URL")
|
|
|
|
| 34 |
tag_name=$(
|
| 35 |
printf '%s\n' "$response" \
|
| 36 |
| grep -Eo '"tag_name"[[:space:]]*:[[:space:]]*"[^"]+"' \
|
| 37 |
| head -n 1 \
|
| 38 |
-
| cut -d '"' -f 4
|
|
|
|
| 39 |
)
|
| 40 |
asset_url=$(
|
| 41 |
printf '%s\n' "$response" \
|
| 42 |
| grep -Eo '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]*"' \
|
| 43 |
| cut -d '"' -f 4 \
|
| 44 |
| grep -E '/[^/]*linux[-_]amd64(\.tar\.gz)?$' \
|
| 45 |
-
| head -n 1
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
if [ -z "$tag_name" ] || [ -z "$asset_url" ]; then
|
|
@@ -52,6 +64,12 @@ fetch_latest_release_info() {
|
|
| 52 |
printf '%s|%s\n' "$tag_name" "$asset_url"
|
| 53 |
}
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
find_release_binary() {
|
| 56 |
release_dir=$1
|
| 57 |
|
|
@@ -89,12 +107,19 @@ extract_release_binary() {
|
|
| 89 |
chmod +x "$target_path"
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
install_release() {
|
| 93 |
tag_name=$1
|
| 94 |
asset_url=$2
|
| 95 |
|
| 96 |
ensure_runtime_dirs
|
| 97 |
-
release_dir="$
|
| 98 |
release_binary="$release_dir/$CURRENT_BINARY_NAME"
|
| 99 |
|
| 100 |
if [ -x "$release_binary" ]; then
|
|
@@ -102,42 +127,68 @@ install_release() {
|
|
| 102 |
return 0
|
| 103 |
fi
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
rm -f "$download_path"
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
case "$download_path" in
|
| 114 |
*.tar.gz)
|
| 115 |
-
tar -xzf "$download_path" -C "$
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
| 118 |
return 1
|
| 119 |
}
|
| 120 |
;;
|
| 121 |
*)
|
| 122 |
-
cp "$download_path" "$
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
;;
|
| 125 |
esac
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
activate_release "$tag_name" "$release_dir"
|
| 132 |
}
|
| 133 |
|
| 134 |
ensure_initial_release() {
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
asset_url=${release_info#*|}
|
| 140 |
-
install_release "$latest_tag" "$asset_url" && return 0
|
| 141 |
log "警告: 安装最新版本失败,尝试使用已有版本继续启动"
|
| 142 |
fi
|
| 143 |
|
|
@@ -149,21 +200,20 @@ restart_managed_service() {
|
|
| 149 |
}
|
| 150 |
|
| 151 |
update_if_needed() {
|
| 152 |
-
|
| 153 |
log "检查最新版本失败"
|
| 154 |
return 1
|
| 155 |
}
|
| 156 |
-
latest_tag=${release_info%%|*}
|
| 157 |
-
asset_url=${release_info#*|}
|
| 158 |
-
installed_tag=$(current_tag)
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
| 162 |
return 0
|
| 163 |
fi
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
|
|
|
| 167 |
return 1
|
| 168 |
}
|
| 169 |
|
|
|
|
| 12 |
: "${APP_BINARY_NAMES:=cli-proxy-api CLIProxyAPI}"
|
| 13 |
|
| 14 |
log() {
|
| 15 |
+
timestamp=$(date '+%Y-%m-%d %H:%M:%S %z')
|
| 16 |
+
printf '[%s] %s\n' "$timestamp" "$*" >&2
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
tag_to_dir_name() {
|
| 20 |
+
printf '%s\n' "$1" | tr '/:' '__'
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
release_dir_for_tag() {
|
| 24 |
+
printf '%s/%s\n' "$RELEASES_DIR" "$(tag_to_dir_name "$1")"
|
| 25 |
}
|
| 26 |
|
| 27 |
ensure_runtime_dirs() {
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
fetch_latest_release_info() {
|
| 42 |
+
response=$(curl -fsSL "$REPO_API_URL") || return 1
|
| 43 |
+
|
| 44 |
tag_name=$(
|
| 45 |
printf '%s\n' "$response" \
|
| 46 |
| grep -Eo '"tag_name"[[:space:]]*:[[:space:]]*"[^"]+"' \
|
| 47 |
| head -n 1 \
|
| 48 |
+
| cut -d '"' -f 4 \
|
| 49 |
+
|| true
|
| 50 |
)
|
| 51 |
asset_url=$(
|
| 52 |
printf '%s\n' "$response" \
|
| 53 |
| grep -Eo '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]*"' \
|
| 54 |
| cut -d '"' -f 4 \
|
| 55 |
| grep -E '/[^/]*linux[-_]amd64(\.tar\.gz)?$' \
|
| 56 |
+
| head -n 1 \
|
| 57 |
+
|| true
|
| 58 |
)
|
| 59 |
|
| 60 |
if [ -z "$tag_name" ] || [ -z "$asset_url" ]; then
|
|
|
|
| 64 |
printf '%s|%s\n' "$tag_name" "$asset_url"
|
| 65 |
}
|
| 66 |
|
| 67 |
+
load_latest_release() {
|
| 68 |
+
release_info=$(fetch_latest_release_info) || return 1
|
| 69 |
+
LATEST_TAG=${release_info%%|*}
|
| 70 |
+
LATEST_ASSET_URL=${release_info#*|}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
find_release_binary() {
|
| 74 |
release_dir=$1
|
| 75 |
|
|
|
|
| 107 |
chmod +x "$target_path"
|
| 108 |
}
|
| 109 |
|
| 110 |
+
cleanup_workspace() {
|
| 111 |
+
workspace=$1
|
| 112 |
+
if [ -n "$workspace" ] && [ -d "$workspace" ]; then
|
| 113 |
+
rm -rf "$workspace"
|
| 114 |
+
fi
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
install_release() {
|
| 118 |
tag_name=$1
|
| 119 |
asset_url=$2
|
| 120 |
|
| 121 |
ensure_runtime_dirs
|
| 122 |
+
release_dir=$(release_dir_for_tag "$tag_name")
|
| 123 |
release_binary="$release_dir/$CURRENT_BINARY_NAME"
|
| 124 |
|
| 125 |
if [ -x "$release_binary" ]; then
|
|
|
|
| 127 |
return 0
|
| 128 |
fi
|
| 129 |
|
| 130 |
+
workspace=$(mktemp -d "$RUNTIME_DIR/install.XXXXXX") || return 1
|
| 131 |
+
download_name=$(basename "$asset_url")
|
| 132 |
+
if [ -z "$download_name" ] || [ "$download_name" = "/" ] || [ "$download_name" = "." ]; then
|
| 133 |
+
download_name="release.asset"
|
| 134 |
+
fi
|
| 135 |
+
download_path="$DOWNLOADS_DIR/$download_name"
|
| 136 |
+
stage_dir="$workspace/stage"
|
| 137 |
|
| 138 |
+
rm -f "$download_path" || {
|
| 139 |
+
cleanup_workspace "$workspace"
|
| 140 |
+
return 1
|
| 141 |
+
}
|
| 142 |
+
mkdir -p "$stage_dir" || {
|
| 143 |
+
cleanup_workspace "$workspace"
|
| 144 |
+
return 1
|
| 145 |
+
}
|
| 146 |
+
curl -fsSL "$asset_url" -o "$download_path" || {
|
| 147 |
+
cleanup_workspace "$workspace"
|
| 148 |
+
return 1
|
| 149 |
+
}
|
| 150 |
|
| 151 |
case "$download_path" in
|
| 152 |
*.tar.gz)
|
| 153 |
+
tar -xzf "$download_path" -C "$stage_dir" || {
|
| 154 |
+
cleanup_workspace "$workspace"
|
| 155 |
+
return 1
|
| 156 |
+
}
|
| 157 |
+
extract_release_binary "$stage_dir" "$stage_dir/$CURRENT_BINARY_NAME" || {
|
| 158 |
+
cleanup_workspace "$workspace"
|
| 159 |
return 1
|
| 160 |
}
|
| 161 |
;;
|
| 162 |
*)
|
| 163 |
+
cp "$download_path" "$stage_dir/$CURRENT_BINARY_NAME" || {
|
| 164 |
+
cleanup_workspace "$workspace"
|
| 165 |
+
return 1
|
| 166 |
+
}
|
| 167 |
+
chmod +x "$stage_dir/$CURRENT_BINARY_NAME" || {
|
| 168 |
+
cleanup_workspace "$workspace"
|
| 169 |
+
return 1
|
| 170 |
+
}
|
| 171 |
;;
|
| 172 |
esac
|
| 173 |
|
| 174 |
+
rm -rf "$release_dir" || {
|
| 175 |
+
cleanup_workspace "$workspace"
|
| 176 |
+
return 1
|
| 177 |
+
}
|
| 178 |
+
mv "$stage_dir" "$release_dir" || {
|
| 179 |
+
cleanup_workspace "$workspace"
|
| 180 |
+
return 1
|
| 181 |
+
}
|
| 182 |
+
cleanup_workspace "$workspace"
|
| 183 |
+
|
| 184 |
activate_release "$tag_name" "$release_dir"
|
| 185 |
}
|
| 186 |
|
| 187 |
ensure_initial_release() {
|
| 188 |
+
if load_latest_release; then
|
| 189 |
+
if install_release "$LATEST_TAG" "$LATEST_ASSET_URL"; then
|
| 190 |
+
return 0
|
| 191 |
+
fi
|
|
|
|
|
|
|
| 192 |
log "警告: 安装最新版本失败,尝试使用已有版本继续启动"
|
| 193 |
fi
|
| 194 |
|
|
|
|
| 200 |
}
|
| 201 |
|
| 202 |
update_if_needed() {
|
| 203 |
+
load_latest_release || {
|
| 204 |
log "检查最新版本失败"
|
| 205 |
return 1
|
| 206 |
}
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
installed_tag=$(current_tag)
|
| 209 |
+
if [ "$LATEST_TAG" = "$installed_tag" ]; then
|
| 210 |
+
log "当前已是最新版本: $LATEST_TAG"
|
| 211 |
return 0
|
| 212 |
fi
|
| 213 |
|
| 214 |
+
log "发现新版本: ${installed_tag:-<none>} -> $LATEST_TAG"
|
| 215 |
+
install_release "$LATEST_TAG" "$LATEST_ASSET_URL" || {
|
| 216 |
+
log "安装新版本失败: $LATEST_TAG"
|
| 217 |
return 1
|
| 218 |
}
|
| 219 |
|
scripts/update-loop.sh
CHANGED
|
@@ -2,11 +2,13 @@
|
|
| 2 |
set -eu
|
| 3 |
|
| 4 |
: "${UPDATE_INTERVAL_SECONDS:=14400}"
|
|
|
|
| 5 |
|
| 6 |
-
.
|
| 7 |
|
| 8 |
while true; do
|
| 9 |
sleep "$UPDATE_INTERVAL_SECONDS"
|
|
|
|
| 10 |
if ! update_if_needed; then
|
| 11 |
log "警告: 自动更新检查失败,继续运行当前版本"
|
| 12 |
fi
|
|
|
|
| 2 |
set -eu
|
| 3 |
|
| 4 |
: "${UPDATE_INTERVAL_SECONDS:=14400}"
|
| 5 |
+
: "${RELEASE_COMMON_SH:=/usr/local/bin/release-common.sh}"
|
| 6 |
|
| 7 |
+
. "$RELEASE_COMMON_SH"
|
| 8 |
|
| 9 |
while true; do
|
| 10 |
sleep "$UPDATE_INTERVAL_SECONDS"
|
| 11 |
+
log "开始定时版本检查"
|
| 12 |
if ! update_if_needed; then
|
| 13 |
log "警告: 自动更新检查失败,继续运行当前版本"
|
| 14 |
fi
|