#!/bin/sh # danio installer, served by a workspace's host (05-WORKSPACE §5) and by # danio.learnder.org (01-ARCHITECTURE §10), one file for both: # # curl -fsSL /install.sh | sh -s -- -e -t <设备令牌> [--from official] [--cache-dir <目录>] [--work-dir <目录>] # curl -fsSL https://danio.learnder.org/install.sh | sh # # With a host: downloads the host's danio for this machine from the host # (with --from official, from the official releases: the host gives the # address and the checksum) and joins the workspace with the token. # Without one (the site's script): the newest release, through the site. # Either way it checks the download against its SHA-256, puts it in /opt/danio (root) or # ~/.local/share/danio, sets the machine config it was given, starts it # with the computer (systemd; without systemd, as in a container, in the # background now). Run it again to update danio (to the host's version). set -eu endpoint="" token="${DANIO_JOIN_TOKEN:-}" service=1 from=host dir="" cache_dir="" work_dir="" shared_cache_dir="" while [ $# -gt 0 ]; do case "$1" in -e|--endpoint) endpoint="$2"; shift 2 ;; -t|--token) token="$2"; shift 2 ;; --from) from="$2"; shift 2 ;; --no-service) service=0; shift ;; --dir) dir="$2"; shift 2 ;; --cache-dir) cache_dir="$2"; shift 2 ;; --work-dir) work_dir="$2"; shift 2 ;; --shared-cache-dir) shared_cache_dir="$2"; shift 2 ;; *) printf 'danio: 不认识的参数 %s\n' "$1" >&2; exit 1 ;; esac done endpoint="${endpoint%/}" case "$from" in host) q="" ;; official) q="?from=official" ;; *) printf 'danio: --from 只能是 host 或 official\n' >&2; exit 1 ;; esac # no host: the newest release, from the site src="$endpoint" if [ -z "$endpoint" ]; then [ -z "$token" ] || { printf 'danio: 有令牌时要用 -e 给出主机地址\n' >&2; exit 1; } src=https://danio.learnder.org from=official q="" fi say() { printf ' %s\n' "$*"; } die() { printf 'danio: %s\n' "$*" >&2; exit 1; } [ "$(uname -s)" = Linux ] || die "这个脚本只用于 Linux;Windows 用 install.ps1" case "$(uname -m)" in x86_64|amd64) arch=amd64 ;; aarch64|arm64) arch=arm64 ;; *) die "不支持的 CPU:$(uname -m)" ;; esac file="danio-linux-$arch" command -v curl >/dev/null || die "需要 curl" if [ "$(id -u)" = 0 ]; then dir="${dir:-/opt/danio}"; bin=/usr/local/bin else dir="${dir:-${XDG_DATA_HOME:-$HOME/.local/share}/danio}"; bin="$HOME/.local/bin" fi tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT if [ "$from" = official ]; then say "从官方源下载 $file"; else say "从 $src 下载 $file"; fi sum=$(curl -fsSL "$src/download/$file.sha256$q" | cut -d' ' -f1) || die "取不到 $file 的校验值($src)" curl -fSL --progress-bar -o "$tmp/danio" "$src/download/$file$q" || die "下载失败" if command -v sha256sum >/dev/null; then got=$(sha256sum "$tmp/danio" | cut -d' ' -f1) else got=$(shasum -a 256 "$tmp/danio" | cut -d' ' -f1); fi [ "$got" = "$sum" ] || die "$file 与校验值不符" mkdir -p "$dir" "$bin" install -m 755 "$tmp/danio" "$dir/danio.new" mv -f "$dir/danio.new" "$dir/danio" ln -sf "$dir/danio" "$bin/danio" say "已安装 $dir/danio($("$dir/danio" version))" # the machine config before danio first starts: it reads it then if [ -n "$cache_dir" ]; then mkdir -p "$cache_dir" && "$dir/danio" config set cache_dir "$cache_dir" || die "设不了缓存目录 $cache_dir" fi if [ -n "$work_dir" ]; then mkdir -p "$work_dir" && "$dir/danio" config set work_dir "$work_dir" || die "设不了工作目录 $work_dir" fi if [ -n "$shared_cache_dir" ]; then mkdir -p "$shared_cache_dir" && "$dir/danio" config set shared_cache_dir "$shared_cache_dir" || die "设不了共享缓存目录 $shared_cache_dir" fi if [ "$service" = 1 ]; then if ! "$dir/danio" service install; then # no systemd (a container): run it now; after a reboot, run danio serve again nohup "$dir/danio" serve >"$dir/serve.log" 2>&1 & sleep 2 say "没有 systemd:已在后台运行 danio serve(日志 $dir/serve.log);机器重启后要再运行一次" fi fi if [ -n "$token" ]; then DANIO_JOIN_TOKEN="$token" "$dir/danio" workspace join "$endpoint" fi case ":$PATH:" in *":$bin:"*) ;; *) say "把 $bin 加进 PATH 后可以直接运行 danio" ;; esac