| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #!/usr/bin/env bash
- set -Eeuo pipefail
- umask 077
- export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- if [[ -d /opt/debtik/go/bin ]]; then export PATH=/opt/debtik/go/bin:$PATH; fi
- # Transient systemd units may omit HOME; Go needs explicit cache locations.
- export HOME=${HOME:-/root}
- export GOPATH=${GOPATH:-/var/cache/debtik/go}
- export GOCACHE=${GOCACHE:-/var/cache/debtik/go-build}
- mkdir -p "$GOPATH" "$GOCACHE"
- base=/var/lib/debtik/update
- mkdir -p "$base"
- exec 9>"$base/lock"
- flock -n 9 || { echo 'An update is already running' >&2; exit 1; }
- exec >>"$base/update.log" 2>&1
- state() { printf '%s\n' "$1" >"$base/status"; printf '[%s] %s\n' "$(date -u +%FT%TZ)" "$1"; }
- cleanup_veth() {
- python3 - <<'PY'
- import json,subprocess,pathlib
- p=pathlib.Path('/etc/proxmox-router/vpn/aerotun_instances.json')
- if p.exists():
- for tunnel in json.loads(p.read_text()):
- name=tunnel['name']
- result=subprocess.run(['ip','-j','-d','link','show','dev',name],capture_output=True,text=True)
- if result.returncode:
- continue
- links=json.loads(result.stdout)
- if len(links)!=1:raise RuntimeError('Ambiguous AeroTUN interface')
- link=links[0]
- if link.get('linkinfo',{}).get('info_kind')=='veth':
- if link.get('link')!=name+'_ep':raise RuntimeError('Unexpected veth peer')
- subprocess.run(['ip','link','delete',name],check=True)
- PY
- }
- restore() {
- state rolling_back
- systemctl stop proxmox-router.service || true
- cleanup_veth || true
- for name in proxmox-router debtik-cli; do
- test -f "$base/previous/$name"
- cp -p "$base/previous/$name" "/usr/local/bin/$name.rollback"
- mv -f "/usr/local/bin/$name.rollback" "/usr/local/bin/$name"
- done
- for name in debtik-update debtik-release-version debtik-install-dependencies; do
- if [[ -f "$base/previous/$name" ]]; then install -m 0750 "$base/previous/$name" "/usr/local/libexec/$name.rollback"; mv -f "/usr/local/libexec/$name.rollback" "/usr/local/libexec/$name"; fi
- done
- if [[ -f "$base/previous_commit" ]]; then cp -p "$base/previous_commit" "$base/installed_commit"; fi
- if [[ -f "$base/previous_version" ]]; then cp -p "$base/previous_version" "$base/installed_version"; fi
- if [[ -f "$base/previous/go.mod" ]]; then cp -p "$base/previous/go.mod" "$base/go.mod"; fi
- systemctl start proxmox-router.service
- systemctl is-active --quiet proxmox-router.service
- if [[ -f "$base/previous/iptables.save" ]]; then iptables-restore -c < "$base/previous/iptables.save"; fi
- if [[ -s "$base/previous/ip6tables.save" ]]; then ip6tables-restore -c < "$base/previous/ip6tables.save"; fi
- state rolled_back
- }
- if [[ ${1:-} == rollback ]]; then restore; exit; fi
- [[ $# == 4 && $1 == install ]] || { echo 'Usage: debtik-update install URL BRANCH | rollback' >&2; exit 2; }
- repo=$2 branch=$3 pin=$4
- git_opts=()
- if [[ -n $pin ]]; then
- [[ $pin =~ ^sha256//[A-Za-z0-9+/]{43}=$ ]] || exit 2
- git_opts=(-c http.sslVerify=false -c "http.pinnedpubkey=$pin")
- fi
- [[ $repo == http://* || $repo == https://* || $repo == ssh://* ]] || exit 2
- [[ $branch =~ ^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$ && $branch != *..* ]] || exit 2
- work=$(mktemp -d "$base/work.XXXXXXXX")
- trap 'rm -rf "$work"' EXIT
- trap 'state failed' ERR
- state fetching
- version_tool=/usr/local/libexec/debtik-release-version
- version=$(GIT_TERMINAL_PROMPT=0 git "${git_opts[@]}" ls-remote --tags "$repo" 'refs/tags/v*' | "$version_tool" latest)
- installed_version=$(cat "$base/installed_version")
- if ! "$version_tool" greater "$version" "$installed_version"; then
- state failed
- echo "Update refused: available version $version must be greater than installed version $installed_version"
- exit 1
- fi
- GIT_TERMINAL_PROMPT=0 git -c protocol.file.allow=never "${git_opts[@]}" clone --single-branch --branch "v$version" -- "$repo" "$work/source"
- cd "$work/source"
- commit=$(git rev-parse HEAD)
- [[ $(cat VERSION) == "$version" ]] || { state failed; echo 'Release tag and VERSION do not match'; exit 1; }
- bash -n scripts/debtik-update
- bash -n scripts/install-dependencies.sh
- bash -n install.sh
- python3 -c 'import pathlib; compile(pathlib.Path("scripts/release-version.py").read_text(),"release-version.py","exec")'
- state dependencies
- bash scripts/install-dependencies.sh
- state building
- go_cmd=$(command -v go || true)
- if [[ -x /opt/debtik/go/bin/go ]]; then go_cmd=/opt/debtik/go/bin/go; fi
- [[ -n $go_cmd ]] || { state failed; echo 'Go is not installed'; exit 1; }
- CGO_ENABLED=0 "$go_cmd" build -trimpath -ldflags "-X proxmox-router/internal/api.BuildCommit=$commit" -o "$work/proxmox-router" ./cmd/server
- CGO_ENABLED=0 "$go_cmd" build -trimpath -o "$work/debtik-cli" ./cmd/debtik-cli
- install -d -m 0700 "$base/previous"
- for name in proxmox-router debtik-cli; do cp -p "/usr/local/bin/$name" "$base/previous/$name"; done
- for name in debtik-update debtik-release-version debtik-install-dependencies; do
- if [[ -f "/usr/local/libexec/$name" ]]; then cp -p "/usr/local/libexec/$name" "$base/previous/$name"; fi
- done
- cp -p "$base/installed_version" "$base/previous_version"
- if [[ -f "$base/installed_commit" ]]; then cp -p "$base/installed_commit" "$base/previous_commit"; fi
- if [[ -f "$base/go.mod" ]]; then cp -p "$base/go.mod" "$base/previous/go.mod"; fi
- install -m 0644 go.mod "$base/go.mod"
- printf '%s\n' "$commit" > "$base/candidate_commit"
- iptables-save -c > "$base/previous/iptables.save"
- ip6tables-save -c > "$base/previous/ip6tables.save"
- state installing
- rollback_on_error() { trap - ERR; restore || state failed; exit 1; }
- trap rollback_on_error ERR
- systemctl stop proxmox-router.service
- cleanup_veth
- for name in proxmox-router debtik-cli; do
- install -m 0755 "$work/$name" "/usr/local/bin/$name.next"
- mv -f "/usr/local/bin/$name.next" "/usr/local/bin/$name"
- done
- install -m 0750 scripts/debtik-update /usr/local/libexec/debtik-update.next
- install -m 0750 scripts/release-version.py /usr/local/libexec/debtik-release-version.next
- install -m 0750 scripts/install-dependencies.sh /usr/local/libexec/debtik-install-dependencies.next
- mv -f /usr/local/libexec/debtik-update.next /usr/local/libexec/debtik-update
- mv -f /usr/local/libexec/debtik-release-version.next /usr/local/libexec/debtik-release-version
- mv -f /usr/local/libexec/debtik-install-dependencies.next /usr/local/libexec/debtik-install-dependencies
- systemctl start proxmox-router.service
- sleep 3
- systemctl is-active --quiet proxmox-router.service
- printf '%s\n' "$commit" > "$base/installed_commit"
- printf '%s\n' "$version" > "$base/installed_version"
- state complete
|