-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathinstall-qwen-standalone.sh
More file actions
executable file
·1493 lines (1290 loc) · 45.1 KB
/
Copy pathinstall-qwen-standalone.sh
File metadata and controls
executable file
·1493 lines (1290 loc) · 45.1 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Qwen Code Installation Script
# Installs Qwen Code from a standalone archive when available, with npm fallback.
# This script intentionally does not install Node.js or change npm config.
#
# Usage:
# install-qwen-standalone.sh --source [github|npm|internal|local-build]
# install-qwen-standalone.sh --method [detect|standalone|npm]
if [ -z "${BASH_VERSION}" ] && [ -z "${__QWEN_INSTALL_REEXEC:-}" ]; then
if command -v bash >/dev/null 2>&1; then
if [ -f "${0}" ]; then
export __QWEN_INSTALL_REEXEC=1
exec bash -- "${0}" "$@"
fi
echo "Error: This script requires bash. Run the installer with: curl ... | bash"
exit 1
fi
echo "Error: This script requires bash. Please install bash first."
exit 1
fi
set -eo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() {
printf '%bINFO:%b %s\n' "${BLUE}" "${NC}" "$1"
}
log_success() {
printf '%bSUCCESS:%b %s\n' "${GREEN}" "${NC}" "$1"
}
log_warning() {
printf '%bWARNING:%b %s\n' "${YELLOW}" "${NC}" "$1"
}
log_error() {
printf '%bERROR:%b %s\n' "${RED}" "${NC}" "$1" >&2
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
TEMP_DIRS=()
cleanup_temp_dirs() {
local temp_dir
for temp_dir in "${TEMP_DIRS[@]}"; do
if [[ -n "${temp_dir}" ]]; then
rm -rf "${temp_dir}"
fi
done
}
register_temp_dir() {
local temp_dir="$1"
TEMP_DIRS+=("${temp_dir}")
}
shell_quote() {
printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"
}
display_install_version() {
if [[ "${VERSION}" == "latest" ]]; then
echo "latest"
return 0
fi
echo "${VERSION#v}"
}
trap cleanup_temp_dirs EXIT
trap 'cleanup_temp_dirs; exit 130' INT
trap 'cleanup_temp_dirs; exit 143' TERM
print_usage() {
cat <<EOF
Qwen Code Installer
Usage: $0 [OPTIONS]
Options:
-s, --source SOURCE Record the installation source.
--method METHOD Install method: detect, standalone, or npm.
Defaults to QWEN_INSTALL_METHOD or detect.
--mirror MIRROR Standalone archive mirror: auto, github, or aliyun.
Defaults to QWEN_INSTALL_MIRROR or auto, which picks
whichever responds first via a HEAD probe.
--base-url URL Override standalone archive base URL.
--archive PATH Install from a local standalone archive.
--version VERSION Standalone release version. Defaults to latest.
--registry REGISTRY npm registry to use for npm fallback.
Defaults to QWEN_NPM_REGISTRY or https://registry.npmmirror.com
--no-modify-path Do not append PATH to the user's shell rc file even
when a shadowing 'qwen' is detected.
-h, --help Show this help message.
Examples:
curl -fsSL https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen-standalone.sh | bash
curl -fsSL https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen-standalone.sh | bash -s -- --source github
curl -fsSL https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen-standalone.sh | bash -s -- --method standalone
./install-qwen-standalone.sh --archive ./qwen-code-linux-x64.tar.gz
EOF
}
SOURCE="unknown"
METHOD="${QWEN_INSTALL_METHOD:-}"
MIRROR="${QWEN_INSTALL_MIRROR:-auto}"
BASE_URL="${QWEN_INSTALL_BASE_URL:-}"
ARCHIVE_PATH="${QWEN_INSTALL_ARCHIVE:-}"
VERSION="${QWEN_INSTALL_VERSION:-latest}"
NO_MODIFY_PATH="${QWEN_NO_MODIFY_PATH:-0}"
NPM_REGISTRY="${QWEN_NPM_REGISTRY:-https://registry.npmmirror.com}"
INSTALL_ROOT="${QWEN_INSTALL_ROOT:-${HOME:-}/.local}"
if [[ -n "${QWEN_INSTALL_LIB_DIR:-}" ]]; then
INSTALL_LIB_DIR="${QWEN_INSTALL_LIB_DIR}"
INSTALL_LIB_PARENT="$(dirname "${INSTALL_LIB_DIR}")"
else
INSTALL_LIB_PARENT="${QWEN_INSTALL_LIB_PARENT:-${INSTALL_ROOT}/lib}"
INSTALL_LIB_DIR="${INSTALL_LIB_PARENT}/qwen-code"
fi
INSTALL_BIN_DIR="${QWEN_INSTALL_BIN_DIR:-${INSTALL_ROOT}/bin}"
validate_source() {
if [[ "${SOURCE}" == "unknown" ]]; then
return 0
fi
if [[ "${SOURCE}" =~ ^[A-Za-z0-9._-]+$ ]]; then
return 0
fi
log_error "--source may only contain letters, numbers, dot, underscore, or dash."
exit 1
}
validate_https_url() {
local value="$1"
local option_name="$2"
if [[ -z "${value}" ]]; then
return 0
fi
if [[ "${value}" == https://* ]]; then
return 0
fi
log_error "${option_name} must start with https://"
exit 1
}
validate_version() {
if [[ "${VERSION}" == "latest" ]]; then
return 0
fi
if [[ "${VERSION}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9]+)*$ ]]; then
return 0
fi
log_error "--version must be 'latest' or a semver string."
exit 1
}
validate_github_repo() {
local github_repo="${QWEN_INSTALL_GITHUB_REPO:-QwenLM/qwen-code}"
if [[ "${github_repo}" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]]; then
return 0
fi
log_error "QWEN_INSTALL_GITHUB_REPO must be in owner/repo format."
exit 1
}
validate_install_path() {
local value="$1"
local option_name="$2"
if [[ -z "${value}" ]]; then
log_error "${option_name} must not be empty."
exit 1
fi
case "${value}" in
*$'\n'*|*$'\r'*)
log_error "${option_name} must not contain newlines."
exit 1
;;
esac
if [[ "${value}" != /* ]]; then
log_error "${option_name} must be an absolute path."
exit 1
fi
}
validate_options() {
METHOD="${METHOD:-detect}"
case "${METHOD}" in
detect|standalone|npm)
;;
*)
log_error "--method must be detect, standalone, or npm."
exit 1
;;
esac
case "${MIRROR}" in
auto|github|aliyun)
;;
*)
log_error "--mirror must be auto, github, or aliyun."
exit 1
;;
esac
validate_https_url "${BASE_URL}" "--base-url"
validate_https_url "${NPM_REGISTRY}" "--registry"
validate_version
validate_github_repo
validate_install_path "${INSTALL_ROOT}" "QWEN_INSTALL_ROOT"
validate_install_path "${INSTALL_LIB_PARENT}" "QWEN_INSTALL_LIB_PARENT"
validate_install_path "${INSTALL_LIB_DIR}" "QWEN_INSTALL_LIB_DIR"
validate_install_path "${INSTALL_BIN_DIR}" "QWEN_INSTALL_BIN_DIR"
validate_source
}
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--source)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--source requires a value"
exit 1
fi
SOURCE="$2"
shift 2
;;
--method)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--method requires a value"
exit 1
fi
METHOD="$2"
shift 2
;;
--mirror)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--mirror requires a value"
exit 1
fi
MIRROR="$2"
shift 2
;;
--base-url)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--base-url requires a value"
exit 1
fi
validate_https_url "$2" "--base-url"
BASE_URL="$2"
shift 2
;;
--archive)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--archive requires a value"
exit 1
fi
ARCHIVE_PATH="$2"
shift 2
;;
--version)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--version requires a value"
exit 1
fi
VERSION="$2"
shift 2
;;
--registry)
if [[ -z "${2:-}" ]] || [[ "${2:-}" == -* ]]; then
log_error "--registry requires a value"
exit 1
fi
NPM_REGISTRY="$2"
shift 2
;;
--no-modify-path)
NO_MODIFY_PATH=1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
log_error "Unknown option: $1"
echo ""
print_usage
exit 1
;;
esac
done
# Validate all user-supplied options before doing network or filesystem work.
validate_options
print_header() {
echo "Installing Qwen Code version: $(display_install_version)"
}
print_node_help() {
echo ""
echo "Node.js 22 or newer is required before installing Qwen Code with npm."
echo ""
echo "Install Node.js, then rerun this installer:"
case "$(uname -s 2>/dev/null || echo unknown)" in
Darwin)
echo " brew install node"
echo " # or download from https://nodejs.org/"
;;
Linux)
echo " # Use your distribution package manager or:"
echo " https://nodejs.org/en/download/package-manager"
;;
*)
echo " https://nodejs.org/"
;;
esac
echo ""
echo "If you already use a Node version manager, activate Node.js 22+"
echo "in this shell before rerunning the installer."
}
require_node() {
if ! command_exists node; then
log_error "Node.js was not found."
print_node_help
return 1
fi
local node_version
node_version=$(node -p "process.versions.node" 2>/dev/null || true)
local node_major
node_major=$(node -p "Number(process.versions.node.split('.')[0])" 2>/dev/null || true)
if [[ -z "${node_major}" ]] || ! [[ "${node_major}" =~ ^[0-9]+$ ]]; then
log_error "Unable to determine Node.js version."
print_node_help
return 1
fi
if [[ "${node_major}" -lt 22 ]]; then
log_error "Node.js ${node_version:-unknown} is installed, but Node.js 22 or newer is required."
print_node_help
return 1
fi
log_success "Node.js ${node_version} detected."
}
require_npm() {
if command_exists npm; then
log_success "npm $(npm -v 2>/dev/null || echo unknown) detected."
return 0
fi
log_error "npm was not found."
echo ""
echo "Please install Node.js with npm included, then rerun this installer."
echo "Download Node.js from https://nodejs.org/ if your package manager"
echo "installed Node without npm."
return 1
}
get_npm_global_bin() {
local prefix
prefix=$(npm prefix -g 2>/dev/null || true)
if [[ -z "${prefix}" ]]; then
return 0
fi
case "$(uname -s 2>/dev/null || echo unknown)" in
MINGW*|MSYS*|CYGWIN*)
echo "${prefix}"
;;
*)
echo "${prefix}/bin"
;;
esac
}
get_npm_global_root() {
npm root -g 2>/dev/null || true
}
create_source_json() {
if [[ "${SOURCE}" == "unknown" ]]; then
return 0
fi
local qwen_dir="${HOME}/.qwen"
mkdir -p "${qwen_dir}"
local escaped_source
escaped_source=$(printf '%s' "${SOURCE}" | sed 's/\\/\\\\/g; s/"/\\"/g')
cat > "${qwen_dir}/source.json" <<EOF
{
"source": "${escaped_source}"
}
EOF
log_success "Installation source saved to ~/.qwen/source.json"
}
detect_target() {
local os
os=$(uname -s 2>/dev/null || echo unknown)
local arch
arch=$(uname -m 2>/dev/null || echo unknown)
case "${os}" in
Darwin)
os="darwin"
;;
Linux)
os="linux"
;;
*)
return 1
;;
esac
case "${arch}" in
x86_64|amd64)
arch="x64"
;;
arm64|aarch64)
arch="arm64"
;;
*)
return 1
;;
esac
echo "${os}-${arch}"
}
archive_extension_for_target() {
case "$1" in
darwin-*|linux-*)
echo "tar.gz"
;;
*)
return 1
;;
esac
}
release_version_path() {
if [[ "${VERSION}" == "latest" ]]; then
echo "latest"
return 0
fi
case "${VERSION}" in
v*)
echo "${VERSION}"
;;
*)
echo "v${VERSION}"
;;
esac
}
# When a shadowing 'qwen' is detected, append a PATH prepend to the user's
# shell rc file at the very end. Putting it at the END means our prepend runs
# AFTER any earlier PATH munging in the rc file (e.g., other tools' shell
# init), so our installed_bin wins. Idempotent via a marker comment.
maybe_update_shell_path() {
local install_bin_dir="$1"
[[ "${NO_MODIFY_PATH:-0}" == "1" ]] && return 0
[[ -z "${install_bin_dir}" ]] && return 0
[[ -z "${HOME:-}" ]] && return 0
local rc_file=""
case "${SHELL:-}" in
*/zsh) rc_file="${HOME}/.zshrc" ;;
*/bash)
if [[ -f "${HOME}/.bashrc" ]]; then
rc_file="${HOME}/.bashrc"
elif [[ -f "${HOME}/.bash_profile" ]]; then
rc_file="${HOME}/.bash_profile"
else
rc_file="${HOME}/.bashrc"
fi
;;
*/fish) rc_file="${HOME}/.config/fish/config.fish" ;;
*)
log_warning "Unsupported shell for automatic PATH update: ${SHELL:-unknown}. Add ${install_bin_dir} to PATH manually."
return 0
;;
esac
[[ -z "${rc_file}" ]] && return 0
local begin_marker="# Qwen Code PATH block begin"
local end_marker="# Qwen Code PATH block end"
local quoted_install_bin_dir
quoted_install_bin_dir=$(shell_quote "${install_bin_dir}")
local export_line
if [[ "${rc_file}" == *config.fish ]]; then
export_line="set -gx PATH ${quoted_install_bin_dir} \$PATH"
else
export_line="export PATH=${quoted_install_bin_dir}:\$PATH"
fi
if [[ -f "${rc_file}" ]] && grep -qxF "${export_line}" "${rc_file}" 2>/dev/null; then
log_info "PATH update for ${install_bin_dir} already present in ${rc_file} (skipping)."
return 0
fi
mkdir -p "$(dirname "${rc_file}")" 2>/dev/null || true
{
echo ""
echo "${begin_marker}"
echo "${export_line}"
echo "${end_marker}"
} >> "${rc_file}" || {
log_warning "Could not write PATH update to ${rc_file}."
return 0
}
log_success "Appended PATH prepend to ${rc_file}"
log_info "Open a new terminal, or run: source ${rc_file}"
}
github_base_url_for_version() {
local version_path="$1"
local github_repo="${QWEN_INSTALL_GITHUB_REPO:-QwenLM/qwen-code}"
if [[ "${version_path}" == "latest" ]]; then
echo "https://github.com/${github_repo}/releases/latest/download"
else
echo "https://github.com/${github_repo}/releases/download/${version_path}"
fi
}
aliyun_base_url_for_version() {
local version_path="$1"
echo "https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/releases/qwen-code/${version_path}"
}
aliyun_latest_version_url() {
echo "https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/releases/qwen-code/latest/VERSION"
}
normalize_version_path_value() {
local raw_version="$1"
local version_path
raw_version=$(printf '%s' "${raw_version}" | tr -d '\r' | awk 'NF { print $1; exit }')
if [[ -z "${raw_version}" ]]; then
return 1
fi
case "${raw_version}" in
v*)
version_path="${raw_version}"
;;
*)
version_path="v${raw_version}"
;;
esac
if [[ "${version_path}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9]+)*$ ]]; then
echo "${version_path}"
return 0
fi
return 1
}
download_text() {
local url="$1"
if command_exists curl; then
curl -fsSL --retry 2 --connect-timeout 10 --max-time 30 "${url}"
return $?
fi
if command_exists wget; then
local wget_args=(--tries=3 --timeout=10)
if wget --help 2>&1 | grep -q -- '--read-timeout'; then
wget_args+=(--read-timeout=30)
fi
wget -q "${wget_args[@]}" -O - "${url}"
return $?
fi
log_error "curl or wget is required to resolve the standalone release version."
return 1
}
resolve_aliyun_version_path() {
local version_path="$1"
if [[ "${version_path}" != "latest" ]]; then
echo "${version_path}"
return 0
fi
local latest_url
latest_url=$(aliyun_latest_version_url)
local latest_version
if ! latest_version=$(download_text "${latest_url}"); then
log_warning "Failed to resolve Aliyun latest VERSION pointer." >&2
return 1
fi
local resolved_version_path
if ! resolved_version_path=$(normalize_version_path_value "${latest_version}"); then
log_error "Aliyun latest VERSION pointer is not a valid semver value."
return 1
fi
log_info "Resolved Aliyun latest to ${resolved_version_path}." >&2
echo "${resolved_version_path}"
}
# Probe a URL with a HEAD request first, then fall back to a 1-byte ranged GET
# for object stores or CDNs that reject HEAD while still serving the object.
probe_url_available() {
local url="$1"
local timeout="${2:-30}"
if command_exists curl; then
if curl -fsIL --retry 1 --connect-timeout 10 --max-time "${timeout}" "${url}" >/dev/null 2>&1; then
return 0
fi
curl -fsL --retry 1 --connect-timeout 10 --max-time "${timeout}" \
--range 0-0 -o /dev/null "${url}" >/dev/null 2>&1
return $?
fi
if command_exists wget; then
local wget_args=(--tries=2 --timeout=10)
if wget --help 2>&1 | grep -q -- '--read-timeout'; then
wget_args+=(--read-timeout="${timeout}")
fi
if wget -q --spider "${wget_args[@]}" "${url}" >/dev/null 2>&1; then
return 0
fi
wget -q "${wget_args[@]}" --header='Range: bytes=0-0' -O /dev/null "${url}" >/dev/null 2>&1
return $?
fi
return 1
}
# Race two availability probes; print "aliyun" or "github" based on which
# mirror's SHA256SUMS responds first, or "timeout" if neither responds before
# the deadline. Caller decides what to do with "timeout" (currently: log it and
# fall back to github).
race_mirror_head() {
local timeout="${1:-2}"
local gh_url="$2"
local oss_url="$3"
local tmpdir
if ! tmpdir=$(mktemp -d -t qwen-mirror.XXXXXX 2>/dev/null); then
# Refuse to fall back to a predictable PID-based path; a local attacker
# could pre-create it to influence mirror selection.
echo "mirror probe: mktemp failed" >&2
echo "github"
return 0
fi
register_temp_dir "${tmpdir}"
(probe_url_available "${oss_url}" "${timeout}" && : > "${tmpdir}/aliyun") &
local oss_pid=$!
(probe_url_available "${gh_url}" "${timeout}" && : > "${tmpdir}/github") &
local gh_pid=$!
local winner=""
local elapsed=0
local max=$((timeout * 10 + 5))
while [[ -z "${winner}" && "${elapsed}" -lt "${max}" ]]; do
# Probe OSS first to break ties in favor of the closer mirror for CN users.
[[ -e "${tmpdir}/aliyun" ]] && winner="aliyun" && break
[[ -e "${tmpdir}/github" ]] && winner="github" && break
sleep 0.1
elapsed=$((elapsed + 1))
done
kill "${oss_pid}" "${gh_pid}" 2>/dev/null || true
wait "${oss_pid}" "${gh_pid}" 2>/dev/null || true
rm -rf "${tmpdir}" 2>/dev/null || true
echo "${winner:-timeout}"
}
standalone_base_url() {
if [[ -n "${BASE_URL}" ]]; then
echo "${BASE_URL%/}"
return 0
fi
local version_path
version_path=$(release_version_path)
if [[ "${MIRROR}" == "auto" ]]; then
local gh_head oss_head selected
gh_head="$(github_base_url_for_version "${version_path}")/SHA256SUMS"
if [[ "${version_path}" == "latest" ]]; then
oss_head="$(aliyun_latest_version_url)"
else
oss_head="$(aliyun_base_url_for_version "${version_path}")/SHA256SUMS"
fi
selected=$(race_mirror_head 2 "${gh_head}" "${oss_head}")
if [[ "${selected}" == "timeout" ]]; then
log_info "Mirror auto-selection timed out; defaulting to github." >&2
selected="github"
else
log_info "Mirror auto-selected via HEAD probe: ${selected}" >&2
fi
MIRROR="${selected}"
fi
if [[ "${MIRROR}" == "aliyun" ]]; then
if ! version_path=$(resolve_aliyun_version_path "${version_path}"); then
return 1
fi
aliyun_base_url_for_version "${version_path}"
return 0
fi
github_base_url_for_version "${version_path}"
}
download_file() {
local url="$1"
local destination="$2"
if command_exists curl; then
curl -fL --retry 2 --connect-timeout 15 --max-time 300 --progress-bar "${url}" -o "${destination}"
return $?
fi
if command_exists wget; then
local wget_args=(--tries=3 --timeout=15)
if wget --help 2>&1 | grep -q -- '--read-timeout'; then
wget_args+=(--read-timeout=300)
fi
if wget --help 2>&1 | grep -q -- '--progress'; then
wget --progress=bar:force:noscroll "${wget_args[@]}" "${url}" -O "${destination}" || return 1
else
wget "${wget_args[@]}" "${url}" -O "${destination}" || return 1
fi
return $?
fi
log_error "curl or wget is required to download the standalone archive."
return 1
}
url_exists() {
local url="$1"
probe_url_available "${url}" 30
}
sha256_file() {
local file_path="$1"
if command_exists sha256sum; then
sha256sum "${file_path}" | awk '{print $1}'
return 0
fi
if command_exists shasum; then
shasum -a 256 "${file_path}" | awk '{print $1}'
return 0
fi
return 1
}
verify_checksum() {
local archive_path="$1"
local checksum_source="$2"
local archive_name="$3"
local checksum_file="${checksum_source}"
local temp_checksum=""
if [[ -z "${checksum_file}" ]]; then
checksum_file="$(dirname "${archive_path}")/SHA256SUMS"
elif [[ "${checksum_file}" == http://* || "${checksum_file}" == https://* ]]; then
temp_checksum="$(mktemp)"
if ! download_file "${checksum_file}" "${temp_checksum}"; then
rm -f "${temp_checksum}"
log_error "Could not download SHA256SUMS for checksum verification."
return 1
fi
checksum_file="${temp_checksum}"
fi
if [[ ! -f "${checksum_file}" ]]; then
rm -f "${temp_checksum}"
log_error "SHA256SUMS not found at ${checksum_file}; cannot verify archive."
return 1
fi
local expected
expected=$(awk -v archive_name="${archive_name}" '
{
name = $2
sub(/^\*/, "", name)
if (name == archive_name) {
print $1
exit
}
}
' "${checksum_file}")
if [[ -z "${expected}" ]]; then
rm -f "${temp_checksum}"
log_error "Checksum entry for ${archive_name} not found."
return 1
fi
local actual
if ! actual=$(sha256_file "${archive_path}"); then
rm -f "${temp_checksum}"
log_error "No SHA-256 utility found; cannot verify archive."
return 1
fi
rm -f "${temp_checksum}"
if [[ "${expected}" != "${actual}" ]]; then
log_error "Checksum mismatch for ${archive_name}: expected ${expected}, got ${actual}."
return 1
fi
log_success "Checksum verified for ${archive_name}."
}
validate_archive_entry_path() {
local entry="$1"
entry="${entry//\\//}"
while [[ "${entry}" == ./* ]]; do
entry="${entry#./}"
done
# Reject entries containing CR/LF so a `..\r` or `..\n` entry cannot
# bypass the literal `..` glob below.
case "${entry}" in
*$'\r'*|*$'\n'*)
log_error "Archive contains unsafe path with control character: ${entry}"
return 1
;;
esac
case "${entry}" in
""|/*|..|../*|*/..|*/../*)
log_error "Archive contains unsafe path: ${entry:-<empty>}"
return 1
;;
esac
}
archive_contains_symlinks() {
local archive_path="$1"
case "${archive_path}" in
*.zip)
unzip -Z -v "${archive_path}" 2>/dev/null | grep -E 'Unix file attributes \(12[0-7]{4} octal\)' >/dev/null
;;
*.tar.gz|*.tgz|*.tar.xz)
tar -tvf "${archive_path}" 2>/dev/null | awk '$1 ~ /^l/ { found=1 } END { exit found ? 0 : 1 }'
;;
*)
return 1
;;
esac
}
validate_archive_contents() {
local archive_path="$1"
local entries
local entry
case "${archive_path}" in
*.zip)
if ! command_exists unzip; then
log_error "unzip is required to inspect ${archive_path}."
return 1
fi
if ! entries=$(unzip -Z1 "${archive_path}"); then
log_error "Failed to inspect archive entries: ${archive_path}"
return 1
fi
;;
*.tar.gz|*.tgz|*.tar.xz)
if ! entries=$(tar -tf "${archive_path}"); then
log_error "Failed to inspect archive entries: ${archive_path}"
return 1
fi
;;
*)
log_error "Unsupported archive format: ${archive_path}"
return 1
;;
esac
if [[ -z "${entries}" ]]; then
log_error "Archive is empty: ${archive_path}"
return 1
fi
if archive_contains_symlinks "${archive_path}"; then
log_error "Archive contains symlinks; refusing to install."
return 1
fi
while IFS= read -r entry; do
validate_archive_entry_path "${entry}" || return 1
done <<< "${entries}"
}
extract_archive() {
local archive_path="$1"
local destination="$2"
mkdir -p "${destination}" || return 1
validate_archive_contents "${archive_path}" || return 1
case "${archive_path}" in
*.zip)
if ! command_exists unzip; then
log_error "unzip is required to extract ${archive_path}."
return 1
fi
unzip -q "${archive_path}" -d "${destination}" || return 1
;;
*.tar.gz|*.tgz)
tar -xzf "${archive_path}" -C "${destination}" || return 1
;;
*.tar.xz)
tar -xf "${archive_path}" -C "${destination}" || return 1
;;
*)
log_error "Unsupported archive format: ${archive_path}"
return 1
;;
esac
local symlink_entry
symlink_entry=$(find "${destination}" -type l -print | sed -n '1p')
if [[ -n "${symlink_entry}" ]]; then
log_error "Archive contains symlinks; refusing to install."
return 1
fi
}
ensure_managed_install_dir() {
local install_dir="$1"
if [[ ! -e "${install_dir}" ]]; then
return 0
fi
if is_qwen_standalone_install_dir "${install_dir}"; then
return 0
fi
local backup="${install_dir}.backup.$(date +%Y%m%dT%H%M%S 2>/dev/null || date +%Y%m%d%H%M%S)"
log_warning "${install_dir} exists but is not a Qwen Code standalone install."
log_warning "Backing up to: ${backup}"
if mv "${install_dir}" "${backup}"; then
return 0
fi
log_error "Failed to back up ${install_dir}. Move or remove it manually, then rerun the installer."
return 1