Apache777 commited on
Commit
0d013a0
·
verified ·
1 Parent(s): ca9e1a3

Upload 7 files

Browse files
hf-space/.DS_Store ADDED
Binary file (6.15 kB). View file
 
hf-space/Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM alpine:3.21
2
+
3
+ ENV TZ=Asia/Shanghai
4
+ ENV PORT=8317
5
+ ENV RUNTIME_DIR=/tmp/runtime
6
+ ENV SUPERVISOR_CONF=/etc/supervisord.conf
7
+ ENV SUPERVISOR_CONF_TEMPLATE=/etc/supervisord.conf.template
8
+ ENV STOP_WAIT_SECS=60
9
+
10
+ RUN apk add --no-cache ca-certificates curl supervisor tzdata \
11
+ && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
12
+ && echo $TZ > /etc/timezone
13
+
14
+ COPY entrypoint.sh /usr/local/bin/entrypoint.sh
15
+ COPY scripts/release-common.sh /usr/local/bin/release-common.sh
16
+ COPY scripts/update-loop.sh /usr/local/bin/update-loop.sh
17
+ COPY supervisord.conf /etc/supervisord.conf.template
18
+ RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/release-common.sh /usr/local/bin/update-loop.sh
19
+
20
+ ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
hf-space/README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ccLoad
3
+ emoji: 💻
4
+ colorFrom: blue
5
+ colorTo: yellow
6
+ sdk: docker
7
+ pinned: false
8
+ app_port: 8317
9
+ ---
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
hf-space/entrypoint.sh ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ set -eu
3
+
4
+ : "${RELEASE_COMMON_SH:=/usr/local/bin/release-common.sh}"
5
+ : "${SUPERVISORD_BIN:=/usr/bin/supervisord}"
6
+ : "${SUPERVISOR_CONF_TEMPLATE:=/etc/supervisord.conf.template}"
7
+ : "${OPEN_FILE_LIMIT:=65535}"
8
+
9
+ . "$RELEASE_COMMON_SH"
10
+
11
+ export RUNTIME_DIR
12
+ export SUPERVISOR_CONF
13
+
14
+ shell_quote() {
15
+ printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\"'\"'/g")"
16
+ }
17
+
18
+ escape_sed_replacement() {
19
+ printf '%s' "$1" | sed 's/[\/&]/\\&/g'
20
+ }
21
+
22
+ write_supervisord_conf() {
23
+ mkdir -p "$(dirname "$SUPERVISOR_CONF")"
24
+ app_program_name=$(escape_sed_replacement "$SUPERVISOR_PROGRAM_NAME")
25
+ updater_program_name=$(escape_sed_replacement "$SUPERVISOR_UPDATER_PROGRAM_NAME")
26
+
27
+ sed \
28
+ -e "s/__APP_PROGRAM__/$app_program_name/g" \
29
+ -e "s/__UPDATER_PROGRAM__/$updater_program_name/g" \
30
+ "$SUPERVISOR_CONF_TEMPLATE" > "$SUPERVISOR_CONF"
31
+ }
32
+
33
+ write_run_app_script() {
34
+ run_app_script="$RUNTIME_DIR/run-app.sh"
35
+
36
+ {
37
+ printf '#!/bin/sh\n'
38
+ printf 'set -eu\n'
39
+ printf 'exec "%s"' "$(current_binary_path)"
40
+ for arg in "$@"; do
41
+ printf ' %s' "$(shell_quote "$arg")"
42
+ done
43
+ printf '\n'
44
+ } > "$run_app_script"
45
+
46
+ chmod +x "$run_app_script"
47
+ }
48
+
49
+ prepare_runtime_environment() {
50
+ ulimit -n "$OPEN_FILE_LIMIT"
51
+ ensure_runtime_dirs
52
+ rm -f "$RUNTIME_DIR/supervisor.sock" "$RUNTIME_DIR/supervisord.pid"
53
+ }
54
+
55
+ bootstrap_release_or_exit() {
56
+ if ! ensure_initial_release; then
57
+ echo "错误: 初次启动时无法准备可运行版本" >&2
58
+ exit 1
59
+ fi
60
+ }
61
+
62
+ start_supervisord() {
63
+ exec "$SUPERVISORD_BIN" -c "$SUPERVISOR_CONF"
64
+ }
65
+
66
+ main() {
67
+ prepare_runtime_environment
68
+ write_supervisord_conf
69
+ bootstrap_release_or_exit
70
+ write_run_app_script "$@"
71
+ start_supervisord
72
+ }
73
+
74
+ main "$@"
hf-space/scripts/release-common.sh ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ : "${REPO_RELEASES_URL:=https://github.com/caidaoli/ccLoad/releases}"
4
+ : "${RUNTIME_DIR:=/tmp/runtime}"
5
+ : "${RELEASES_DIR:=$RUNTIME_DIR/releases}"
6
+ : "${CURRENT_LINK:=$RUNTIME_DIR/current}"
7
+ : "${CURRENT_TAG_FILE:=$RUNTIME_DIR/current.tag}"
8
+ : "${DOWNLOADS_DIR:=$RUNTIME_DIR/downloads}"
9
+ : "${SUPERVISOR_CONF:=/etc/supervisord.conf}"
10
+ : "${SUPERVISOR_PROGRAM_NAME:=app}"
11
+ : "${SUPERVISOR_UPDATER_PROGRAM_NAME:=${SUPERVISOR_PROGRAM_NAME}-updater}"
12
+ : "${CURRENT_BINARY_NAME:=app}"
13
+ : "${APP_BINARY_NAMES:=}"
14
+
15
+ log() {
16
+ timestamp=$(date '+%Y-%m-%d %H:%M:%S %z')
17
+ printf '[%s] %s\n' "$timestamp" "$*" >&2
18
+ }
19
+
20
+ tag_to_dir_name() {
21
+ printf '%s\n' "$1" | tr '/:' '__'
22
+ }
23
+
24
+ release_dir_for_tag() {
25
+ printf '%s/%s\n' "$RELEASES_DIR" "$(tag_to_dir_name "$1")"
26
+ }
27
+
28
+ ensure_runtime_dirs() {
29
+ mkdir -p "$RUNTIME_DIR" "$RELEASES_DIR" "$DOWNLOADS_DIR"
30
+ }
31
+
32
+ current_tag() {
33
+ if [ -f "$CURRENT_TAG_FILE" ]; then
34
+ tr -d '\n' < "$CURRENT_TAG_FILE"
35
+ fi
36
+ }
37
+
38
+ current_binary_path() {
39
+ printf '%s/%s\n' "$CURRENT_LINK" "$CURRENT_BINARY_NAME"
40
+ }
41
+
42
+ repo_base_url() {
43
+ # Strip a trailing "/releases" (and any trailing slash) to get the repo root.
44
+ printf '%s\n' "$REPO_RELEASES_URL" | sed -E 's#/+$##; s#/releases$##'
45
+ }
46
+
47
+ resolve_latest_tag() {
48
+ # The Atom feed is ordered newest-first and includes prereleases.
49
+ releases_feed=$(curl -fsSL "$(repo_base_url)/releases.atom") || return 1
50
+
51
+ tag_name=$(
52
+ printf '%s\n' "$releases_feed" \
53
+ | sed -n 's#.*href="[^"]*/releases/tag/\([^"]*\)".*#\1#p' \
54
+ | head -n 1
55
+ )
56
+
57
+ [ -n "$tag_name" ] || return 1
58
+ printf '%s\n' "$tag_name"
59
+ }
60
+
61
+ fetch_latest_release_info() {
62
+ tag_name=$(resolve_latest_tag) || return 1
63
+
64
+ base_url=$(repo_base_url)
65
+
66
+ # The expanded_assets fragment lists every asset as an <a href="..."> link.
67
+ assets_html=$(curl -fsSL "$base_url/releases/expanded_assets/$tag_name") || return 1
68
+
69
+ asset_path=$(
70
+ printf '%s\n' "$assets_html" \
71
+ | grep -Eo 'href="[^"]*/releases/download/[^"]+"' \
72
+ | cut -d '"' -f 2 \
73
+ | grep -E '/[^/]*linux[-_]amd64(\.tar\.gz)?$' \
74
+ | head -n 1 \
75
+ || true
76
+ )
77
+
78
+ [ -n "$asset_path" ] || return 1
79
+
80
+ case "$asset_path" in
81
+ http://*|https://*) asset_url=$asset_path ;;
82
+ /*) asset_url="https://github.com$asset_path" ;;
83
+ *) asset_url="$base_url/$asset_path" ;;
84
+ esac
85
+
86
+ printf '%s|%s\n' "$tag_name" "$asset_url"
87
+ }
88
+
89
+ load_latest_release() {
90
+ release_info=$(fetch_latest_release_info) || return 1
91
+ LATEST_TAG=${release_info%%|*}
92
+ LATEST_ASSET_URL=${release_info#*|}
93
+ }
94
+
95
+ find_release_binary() {
96
+ release_dir=$1
97
+
98
+ for name in $APP_BINARY_NAMES; do
99
+ if [ -f "$release_dir/$name" ]; then
100
+ printf '%s\n' "$release_dir/$name"
101
+ return 0
102
+ fi
103
+ done
104
+
105
+ for name in $APP_BINARY_NAMES; do
106
+ binary_path=$(find "$release_dir" -maxdepth 3 -type f -name "$name" | head -n 1)
107
+ if [ -n "$binary_path" ]; then
108
+ printf '%s\n' "$binary_path"
109
+ return 0
110
+ fi
111
+ done
112
+
113
+ file_list=$(find "$release_dir" -maxdepth 3 -type f ! -name '._*' ! -path '*/__MACOSX/*' | sort)
114
+ first_file=$(printf '%s\n' "$file_list" | sed -n '1p')
115
+ second_file=$(printf '%s\n' "$file_list" | sed -n '2p')
116
+ if [ -n "$first_file" ] && [ -z "$second_file" ]; then
117
+ printf '%s\n' "$first_file"
118
+ return 0
119
+ fi
120
+
121
+ return 1
122
+ }
123
+
124
+ activate_release() {
125
+ tag_name=$1
126
+ release_dir=$2
127
+
128
+ rm -f "$CURRENT_LINK"
129
+ ln -s "$release_dir" "$CURRENT_LINK"
130
+ printf '%s\n' "$tag_name" > "$CURRENT_TAG_FILE"
131
+ }
132
+
133
+ extract_release_binary() {
134
+ source_dir=$1
135
+ target_path=$2
136
+ binary_path=$(find_release_binary "$source_dir" || true)
137
+
138
+ if [ -z "$binary_path" ]; then
139
+ return 1
140
+ fi
141
+
142
+ if [ "$binary_path" != "$target_path" ]; then
143
+ cp "$binary_path" "$target_path"
144
+ fi
145
+ chmod +x "$target_path"
146
+ }
147
+
148
+ cleanup_workspace() {
149
+ workspace=$1
150
+ if [ -n "$workspace" ] && [ -d "$workspace" ]; then
151
+ rm -rf "$workspace"
152
+ fi
153
+ }
154
+
155
+ install_release() {
156
+ tag_name=$1
157
+ asset_url=$2
158
+
159
+ ensure_runtime_dirs
160
+ release_dir=$(release_dir_for_tag "$tag_name")
161
+ release_binary="$release_dir/$CURRENT_BINARY_NAME"
162
+
163
+ if [ -x "$release_binary" ]; then
164
+ activate_release "$tag_name" "$release_dir"
165
+ return 0
166
+ fi
167
+
168
+ workspace=$(mktemp -d "$RUNTIME_DIR/install.XXXXXX") || return 1
169
+ download_name=$(basename "$asset_url")
170
+ if [ -z "$download_name" ] || [ "$download_name" = "/" ] || [ "$download_name" = "." ]; then
171
+ download_name="release.asset"
172
+ fi
173
+ download_path="$DOWNLOADS_DIR/$download_name"
174
+ stage_dir="$workspace/stage"
175
+
176
+ rm -f "$download_path" || {
177
+ cleanup_workspace "$workspace"
178
+ return 1
179
+ }
180
+ mkdir -p "$stage_dir" || {
181
+ cleanup_workspace "$workspace"
182
+ return 1
183
+ }
184
+ curl -fsSL "$asset_url" -o "$download_path" || {
185
+ cleanup_workspace "$workspace"
186
+ return 1
187
+ }
188
+
189
+ case "$download_path" in
190
+ *.tar.gz)
191
+ tar -xzf "$download_path" -C "$stage_dir" || {
192
+ cleanup_workspace "$workspace"
193
+ return 1
194
+ }
195
+ extract_release_binary "$stage_dir" "$stage_dir/$CURRENT_BINARY_NAME" || {
196
+ cleanup_workspace "$workspace"
197
+ return 1
198
+ }
199
+ ;;
200
+ *)
201
+ cp "$download_path" "$stage_dir/$CURRENT_BINARY_NAME" || {
202
+ cleanup_workspace "$workspace"
203
+ return 1
204
+ }
205
+ chmod +x "$stage_dir/$CURRENT_BINARY_NAME" || {
206
+ cleanup_workspace "$workspace"
207
+ return 1
208
+ }
209
+ ;;
210
+ esac
211
+
212
+ rm -rf "$release_dir" || {
213
+ cleanup_workspace "$workspace"
214
+ return 1
215
+ }
216
+ mv "$stage_dir" "$release_dir" || {
217
+ cleanup_workspace "$workspace"
218
+ return 1
219
+ }
220
+ cleanup_workspace "$workspace"
221
+
222
+ activate_release "$tag_name" "$release_dir"
223
+ }
224
+
225
+ ensure_initial_release() {
226
+ if load_latest_release; then
227
+ if install_release "$LATEST_TAG" "$LATEST_ASSET_URL"; then
228
+ return 0
229
+ fi
230
+ log "警告: 安装最新版本失败,尝试使用已有版本继续启动"
231
+ fi
232
+
233
+ [ -x "$(current_binary_path)" ]
234
+ }
235
+
236
+ restart_managed_service() {
237
+ supervisorctl -c "$SUPERVISOR_CONF" restart "$SUPERVISOR_PROGRAM_NAME"
238
+ }
239
+
240
+ update_if_needed() {
241
+ load_latest_release || {
242
+ log "检查最新版本失败"
243
+ return 1
244
+ }
245
+
246
+ installed_tag=$(current_tag)
247
+ if [ "$LATEST_TAG" = "$installed_tag" ]; then
248
+ log "当前已是最新版本: $LATEST_TAG"
249
+ return 0
250
+ fi
251
+
252
+ log "发现新版本: ${installed_tag:-<none>} -> $LATEST_TAG"
253
+ install_release "$LATEST_TAG" "$LATEST_ASSET_URL" || {
254
+ log "安装新版本失败: $LATEST_TAG"
255
+ return 1
256
+ }
257
+
258
+ if [ -n "$installed_tag" ]; then
259
+ restart_managed_service
260
+ fi
261
+ }
hf-space/scripts/update-loop.sh ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ set -eu
3
+
4
+ : "${UPDATE_INTERVAL_SECONDS:=3600}"
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
15
+ done
hf-space/supervisord.conf ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [unix_http_server]
2
+ file=%(ENV_RUNTIME_DIR)s/supervisor.sock
3
+ chmod=0700
4
+
5
+ [supervisord]
6
+ nodaemon=true
7
+ pidfile=%(ENV_RUNTIME_DIR)s/supervisord.pid
8
+ logfile=%(ENV_RUNTIME_DIR)s/supervisord.log
9
+ logfile_maxbytes=0
10
+ minfds=65535
11
+
12
+ [rpcinterface:supervisor]
13
+ supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
14
+
15
+ [supervisorctl]
16
+ serverurl=unix://%(ENV_RUNTIME_DIR)s/supervisor.sock
17
+
18
+ [program:__APP_PROGRAM__]
19
+ command=/bin/sh %(ENV_RUNTIME_DIR)s/run-app.sh
20
+ autostart=true
21
+ autorestart=true
22
+ startsecs=0
23
+ stopasgroup=true
24
+ killasgroup=true
25
+ stopwaitsecs=%(ENV_STOP_WAIT_SECS)s
26
+ stdout_logfile=/dev/fd/1
27
+ stdout_logfile_maxbytes=0
28
+ stderr_logfile=/dev/fd/2
29
+ stderr_logfile_maxbytes=0
30
+
31
+ [program:__UPDATER_PROGRAM__]
32
+ command=/usr/local/bin/update-loop.sh
33
+ autostart=true
34
+ autorestart=true
35
+ startsecs=0
36
+ stopasgroup=true
37
+ killasgroup=true
38
+ stdout_logfile=/dev/fd/1
39
+ stdout_logfile_maxbytes=0
40
+ stderr_logfile=/dev/fd/2
41
+ stderr_logfile_maxbytes=0