#!/bin/sh dnn_start() { exec /bin/bash -s -- "$@" <<'DNN_LAUNCHER_END' #!/usr/bin/env bash set -euo pipefail umask 077 LAUNCHER_FORMAT=3 AMD64_SHA256='f2f691f7fca7f2376ba40a1fffa4ff1653fb9a8047ff69cfd0da98fa413c8b8a' ARM64_SHA256='804554874148f4db1bcd0a19bfcbe470618d2ec22008ce1c27e04cfa7b7404ce' AMD64_SIZE='16232632' ARM64_SIZE='15532216' BASE_URL='https://get.njdnn.cn' valid_base() { local value=$1 authority host port local forbidden="[[:space:][:cntrl:]'\"\\\\?#]" local address='^([[:alnum:].-]+|\[[0-9A-Fa-f:]+\])(:([0-9]{1,5}))?$' [[ ! $value =~ $forbidden ]] || return 1 authority=${value#*://} authority=${authority%%/*} [[ $authority =~ $address ]] || return 1 host=${BASH_REMATCH[1]} port=${BASH_REMATCH[3]:-} [[ -z $port ]] || (( 10#$port > 0 && 10#$port <= 65535 )) || return 1 case "$value" in https://*) return 0 ;; http://*) [[ $host == localhost || $host == 127.0.0.1 || $host == '[::1]' ]] ;; *) return 1 ;; esac } emit_launcher() { printf '%s\n' '#!/usr/bin/env bash' 'set -euo pipefail' 'umask 077' "LAUNCHER_FORMAT=$LAUNCHER_FORMAT" printf "AMD64_SHA256='%s'\nARM64_SHA256='%s'\nBASE_URL='%s'\n" "$AMD64_SHA256" "$ARM64_SHA256" "$1" printf "AMD64_SIZE='%s'\nARM64_SIZE='%s'\n" "$AMD64_SIZE" "$ARM64_SIZE" declare -f valid_base emit_launcher write_launcher install_short_command remember_launcher download_field download_manager manager_progress download_with_progress launch_manager printf '\n%s\n' '{ launch_manager "$@"; }' } write_launcher() ( local target=$1 base=$2 mode=$3 temp temp=$(mktemp -- "$(dirname -- "$target")/.dnn-launcher.XXXXXXXX") trap 'rm -f -- "$temp"' EXIT emit_launcher "$base" > "$temp" chmod 700 -- "$temp" if [[ $mode == export ]]; then if ! ln -T -- "$temp" "$target" 2>/dev/null; then printf '无法新建现场脚本,请指定可写目录中尚未使用的文件名。\n' >&2; return 1 fi elif [[ ! -f $target ]] || ! cmp -s -- "$target" "$temp"; then mv -fT -- "$temp" "$target" fi ) install_short_command() ( local directory=$1 installed=$2 entry=$1/dnn existing temp existing=$(type -P dnn || true) if [[ -n $existing && $existing != "$entry" ]]; then printf 'dnn 已被其他程序使用,可运行 sudo dnn-deploy。\n' >&2 return 0 fi temp=$(mktemp -- "$directory/.dnn-command.XXXXXXXX") || return 1 trap 'rm -f -- "$temp"' EXIT { printf '%s\n' '#!/bin/bash' printf 'if (( EUID )); then exec /usr/bin/sudo -- /bin/bash %q "$@"; fi\n' "$installed" printf 'exec /bin/bash %q "$@"\n' "$installed" } > "$temp" chmod 755 -- "$temp" if [[ -f $entry && ! -L $entry ]] && cmp -s -- "$entry" "$temp"; then return 0; fi if ! ln -T -- "$temp" "$entry" 2>/dev/null; then printf '未创建快捷命令 dnn,可运行 sudo dnn-deploy。\n' >&2 fi ) remember_launcher() { local installed=$1 base=$2 source_file=$3 entry=/usr/local/bin/dnn-deploy if [[ ! $source_file -ef $installed || $base != "$BASE_URL" ]]; then mkdir -p -- "$(dirname -- "$installed")" chmod 700 -- "$(dirname -- "$installed")" write_launcher "$installed" "$base" installed fi local directory directory=$(dirname -- "$entry") if ! mkdir -p -m 755 -- "$directory"; then printf '请使用 sudo bash %s 启动。\n' "$installed" >&2 return 0 fi if [[ ! -L $entry || $(readlink -- "$entry") != "$installed" ]] && ! ln -sT -- "$installed" "$entry" 2>/dev/null; then printf '未创建 %s,请使用 sudo bash %s 启动。\n' "$entry" "$installed" >&2 fi install_short_command "$directory" "$installed" || printf '未创建快捷命令 dnn。\n' >&2 } download_field() ( local port=$1 url=$2 temp=$3 path offset=0 status header blank length cr printf -v cr '\r' local length_pattern="^Content-Length: ([1-9][0-9]{0,11})$cr$" [[ $port =~ ^[0-9]{1,5}$ ]] && (( 10#$port > 0 && 10#$port <= 65535 )) || return 1 [[ $url =~ /tools/[0-9a-f]{64}/manager-linux-(amd64|arm64)$ ]] || return 1 path=${BASH_REMATCH[0]} if [[ -s $temp ]]; then offset=$(stat -c %s -- "$temp") || return 1; fi exec 8<>"/dev/tcp/127.0.0.1/$((10#$port))" || return 1 printf 'GET %s HTTP/1.0\r\n' "$path" >&8 || return 1 if (( offset )); then printf 'Range: bytes=%s-\r\n' "$offset" >&8 || return 1; fi printf '\r\n' >&8 || return 1 IFS= read -r -t 90 -u 8 status || return 1 IFS= read -r -t 90 -u 8 header || return 1 IFS= read -r -t 90 -u 8 blank || return 1 [[ $blank == "$cr" && $header =~ $length_pattern ]] || return 1 length=${BASH_REMATCH[1]} case "$status" in "HTTP/1.0 200 OK$cr") offset=0 head -c "$length" <&8 > "$temp" || return 1 ;; "HTTP/1.0 206 Partial Content$cr") (( offset > 0 )) || return 1 head -c "$length" <&8 >> "$temp" || return 1 ;; *) return 1 ;; esac [[ $(stat -c %s -- "$temp") == "$((offset + length))" ]] ) download_manager() { local url=$1 protocols=$2 temp=$3 status=0 response resume=0 if [[ $url =~ [[:cntrl:]] ]]; then printf '发布源地址无效。\n' >&2; return 1; fi if [[ -n ${DNN_FIELD_PORT:-} ]]; then if ! download_field "$DNN_FIELD_PORT" "$url" "$temp"; then printf '管理器下载未完成,请重新连接 Windows 安装窗口继续下载。\n' >&2 return 1 fi return 0 fi if ! command -v curl >/dev/null; then if ! command -v wget >/dev/null; then printf '缺少 curl 或 wget,请使用 Windows 现场安装或离线包。\n' >&2 return 1 fi local wget_options=(--no-config -q --tries=1 --connect-timeout=12 --read-timeout=90 --output-document="$temp" --input-file=-) if [[ $protocols == '=https' ]]; then wget_options+=(--https-only); fi if [[ -s $temp ]]; then resume=$(stat -c %s -- "$temp"); fi wget "${wget_options[@]}" --continue <<< "$url" 2>/dev/null || status=$? if (( !status && resume )) && [[ $(stat -c %s -- "$temp") == "$resume" ]]; then wget "${wget_options[@]}" <<< "$url" 2>/dev/null || status=$? fi if (( status )); then printf '管理器下载未完成(wget %d),请重新运行安装命令继续下载。\n' "$status" >&2 fi return "$status" fi url=${url//\\/\\\\} url=${url//\"/\\\"} local options=(--disable --silent --fail --location --globoff --proto "$protocols" --proto-redir "$protocols" --connect-timeout 12 --speed-limit 1 --speed-time 90 --output "$temp" --config -) if [[ -s $temp ]]; then resume=1; fi response=$(curl "${options[@]}" --continue-at - --write-out '%{http_code}' <<< "url = \"$url\"" 2>/dev/null) || status=$? if (( resume )) && [[ $response == 416 || ( $response == 200 && ( $status == 33 || $status == 0 ) ) ]]; then status=0 curl "${options[@]}" <<< "url = \"$url\"" 2>/dev/null || status=$? fi if (( status )); then printf '管理器下载未完成(curl %d),请重新运行安装命令继续下载。\n' "$status" >&2 return "$status" fi } manager_progress() { local done=$1 total=$2 if (( done > total )); then done=0; fi printf '\r下载管理器 %3d%% %d.%d / %d.%d MB ' \ "$((done * 100 / total))" "$((done / 1000000))" "$((done / 100000 % 10))" \ "$((total / 1000000))" "$((total / 100000 % 10))" >&2 } download_with_progress() ( local temp=$3 total=$4 parent=$BASHPID monitor size status=0 if [[ ! -t 2 || ! $total =~ ^[1-9][0-9]{0,11}$ ]]; then download_manager "$1" "$2" "$temp" return fi ( while kill -0 "$parent" 2>/dev/null; do size=$(stat -c %s -- "$temp" 2>/dev/null) || size=0 if (( size == total )); then size=$((total - 1)); fi manager_progress "$size" "$total" sleep 0.4 done ) 9>&- & monitor=$! trap 'kill "$monitor" 2>/dev/null || :; wait "$monitor" 2>/dev/null || :' EXIT download_manager "$1" "$2" "$temp" || status=$? kill "$monitor" 2>/dev/null || : wait "$monitor" 2>/dev/null || : trap - EXIT if (( !status )); then manager_progress "$(stat -c %s -- "$temp")" "$total" fi printf '\n' >&2 return "$status" ) launch_manager() { local source_file=${BASH_SOURCE[0]:-} source_dir arch expected size cache binary relative temp local installed=/docker/app-manager/launcher/manager.sh base=${DNN_DEPLOY_BASE_URL:-$BASE_URL} save_path= local manager_args=() headless=0 source_dir=$(pwd -P) case "$source_file" in /dev/fd/*|/proc/*/fd/*|/dev/stdin) source_file= ;; esac if [[ -n $source_file && -f $source_file ]]; then source_file=$(readlink -f -- "$source_file") if [[ ! $source_file -ef $installed ]]; then source_dir=$(dirname -- "$source_file"); fi else source_file= fi while (( $# )); do case "$1" in -h|--help) printf '%s\n' 'DNN Deploy 现场部署管理器' \ '用法:sudo bash manager.sh [--base-url <发布根地址>] [--version|--licenses]' \ '在线运行:curl -f https://get.njdnn.cn|sh' \ '离线使用:解开交付包后运行其中的 manager.sh。' \ '再次打开:dnn' \ '安装组件:sudo dnn-deploy --install <组件ID> [--image-version <版本>]' \ '导出现场脚本:sudo dnn-deploy --save-script <新文件>' return ;; --base-url) if [[ -z ${2:-} ]]; then printf '请提供发布根地址。\n' >&2; return 1; fi base=$2; shift 2 ;; --save-script) if [[ -z ${2:-} ]]; then printf '请提供新脚本的保存路径。\n' >&2; return 1; fi save_path=$2; shift 2 ;; *) case "$1" in --version|--licenses|--install|--install=*) headless=1 ;; esac manager_args+=("$1"); shift ;; esac done if [[ $(uname -s) != Linux ]]; then printf '请在 Linux 主机运行。\n' >&2; return 1; fi if (( EUID != 0 )); then if command -v sudo >/dev/null; then local launcher_args=() if [[ -n $save_path ]]; then launcher_args+=(--save-script "$save_path"); fi launcher_args+=(${manager_args[@]+"${manager_args[@]}"}) if [[ -n $base ]] && ! valid_base "$base"; then printf '发布根地址无效。\n' >&2; return 1; fi if [[ -z $source_file ]]; then emit_launcher "$base" | sudo -- bash -s -- ${launcher_args[@]+"${launcher_args[@]}"} return fi if [[ -n $base && $base != "$BASE_URL" ]]; then exec sudo -- bash -c 'IFS= read -r DNN_DEPLOY_BASE_URL && export DNN_DEPLOY_BASE_URL && exec bash "$@"' \ bash "$source_file" ${launcher_args[@]+"${launcher_args[@]}"} <<< "$base" fi exec sudo -- bash "$source_file" ${launcher_args[@]+"${launcher_args[@]}"} fi printf '请以 root 身份启动,在线安装可执行发布工具生成的命令。\n' >&2 return 1 fi case $(uname -m) in x86_64|amd64) arch=amd64; expected=$AMD64_SHA256; size=$AMD64_SIZE ;; aarch64|arm64) arch=arm64; expected=$ARM64_SHA256; size=$ARM64_SIZE ;; *) printf '不支持的主机架构:%s\n' "$(uname -m)" >&2; return 1 ;; esac if [[ ! $expected =~ ^[0-9a-f]{64}$ ]]; then printf '启动脚本尚未构建。\n' >&2; return 1; fi mkdir -p /run/lock exec 9>/run/lock/dnn-app-manager.lock if ! flock -n 9; then printf '已有管理器正在运行。\n' >&2; return 1; fi if [[ -z $base && -f $installed ]]; then base=$(sed -n "s/^BASE_URL='\([^']*\)'$/\1/p" "$installed") fi if [[ -n $base ]] && ! valid_base "$base"; then printf '发布根地址无效。\n' >&2; return 1; fi while [[ $base == */ ]]; do base=${base%/}; done export DNN_DEPLOY_BASE_URL=$base if [[ -n $save_path ]]; then if [[ -z $base ]]; then printf '请用 --base-url 指定现场脚本使用的发布根地址。\n' >&2; return 1; fi write_launcher "$save_path" "$base" export printf '现场脚本已保存。\n' return fi relative=tools/$expected/manager-linux-$arch cache=/docker/app-manager/tools/$expected binary=$cache/manager-linux-$arch if [[ ! -x $binary ]] || ! printf '%s %s\n' "$expected" "$binary" | sha256sum --check --status; then mkdir -p -- "$cache" chmod 700 -- "$cache" temp=$binary.part if [[ -f $source_dir/$relative ]]; then cp -- "$source_dir/$relative" "$temp" else if [[ -z $DNN_DEPLOY_BASE_URL ]]; then printf '离线包缺少 %s,也未设置在线发布地址。\n' "$relative" >&2; return 1 fi printf '正在下载 %s 管理器…\n' "$arch" >&2 local protocols='=https' if [[ $base == http://* ]]; then protocols='=http,https'; fi local url="${DNN_DEPLOY_BASE_URL%/}/$relative" if [[ ! -s $temp ]] || ! printf '%s %s\n' "$expected" "$temp" | sha256sum --check --status; then if [[ -s $temp && $size =~ ^[1-9][0-9]{0,11}$ ]] && (( $(stat -c %s -- "$temp") >= size )); then rm -f -- "$temp" fi download_with_progress "$url" "$protocols" "$temp" "$size" fi fi if ! printf '%s %s\n' "$expected" "$temp" | sha256sum --check --status; then rm -f -- "$temp"; printf '管理器 SHA-256 校验失败。\n' >&2; return 1 fi chmod 700 -- "$temp" mv -fT -- "$temp" "$binary" fi remember_launcher "$installed" "$base" "$source_file" if (( ! headless )) && [[ ! -t 0 ]]; then if ! { exec /dev/null; then printf '交互菜单需要终端,请在现场终端运行。\n' >&2; return 1 fi fi exec "$binary" --dir "$source_dir" ${manager_args[@]+"${manager_args[@]}"} } { launch_manager "$@"; } DNN_LAUNCHER_END } dnn_start "$@"