debtik-update 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. umask 077
  4. # Transient systemd units may omit HOME; Go needs explicit cache locations.
  5. export HOME=${HOME:-/root}
  6. export GOPATH=${GOPATH:-/var/cache/debtik/go}
  7. export GOCACHE=${GOCACHE:-/var/cache/debtik/go-build}
  8. mkdir -p "$GOPATH" "$GOCACHE"
  9. base=/var/lib/debtik/update
  10. mkdir -p "$base"
  11. exec 9>"$base/lock"
  12. flock -n 9 || { echo 'An update is already running' >&2; exit 1; }
  13. exec >>"$base/update.log" 2>&1
  14. state() { printf '%s\n' "$1" >"$base/status"; printf '[%s] %s\n' "$(date -u +%FT%TZ)" "$1"; }
  15. cleanup_veth() {
  16. python3 - <<'PY'
  17. import json,subprocess,pathlib
  18. p=pathlib.Path('/etc/proxmox-router/vpn/aerotun_instances.json')
  19. if p.exists():
  20. for tunnel in json.loads(p.read_text()):
  21. name=tunnel['name']
  22. result=subprocess.run(['ip','-j','-d','link','show','dev',name],capture_output=True,text=True)
  23. if result.returncode:
  24. continue
  25. links=json.loads(result.stdout)
  26. if len(links)!=1:raise RuntimeError('Ambiguous AeroTUN interface')
  27. link=links[0]
  28. if link.get('linkinfo',{}).get('info_kind')=='veth':
  29. if link.get('link')!=name+'_ep':raise RuntimeError('Unexpected veth peer')
  30. subprocess.run(['ip','link','delete',name],check=True)
  31. PY
  32. }
  33. restore() {
  34. state rolling_back
  35. systemctl stop proxmox-router.service || true
  36. cleanup_veth || true
  37. for name in proxmox-router debtik-cli; do
  38. test -f "$base/previous/$name"
  39. cp -p "$base/previous/$name" "/usr/local/bin/$name.rollback"
  40. mv -f "/usr/local/bin/$name.rollback" "/usr/local/bin/$name"
  41. done
  42. for name in debtik-update debtik-release-version; do
  43. 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
  44. done
  45. if [[ -f "$base/previous_commit" ]]; then cp -p "$base/previous_commit" "$base/installed_commit"; fi
  46. if [[ -f "$base/previous_version" ]]; then cp -p "$base/previous_version" "$base/installed_version"; fi
  47. systemctl start proxmox-router.service
  48. systemctl is-active --quiet proxmox-router.service
  49. if [[ -f "$base/previous/iptables.save" ]]; then iptables-restore -c < "$base/previous/iptables.save"; fi
  50. if [[ -s "$base/previous/ip6tables.save" ]]; then ip6tables-restore -c < "$base/previous/ip6tables.save"; fi
  51. state rolled_back
  52. }
  53. if [[ ${1:-} == rollback ]]; then restore; exit; fi
  54. [[ $# == 4 && $1 == install ]] || { echo 'Usage: debtik-update install URL BRANCH | rollback' >&2; exit 2; }
  55. repo=$2 branch=$3 pin=$4
  56. git_opts=()
  57. if [[ -n $pin ]]; then
  58. [[ $pin =~ ^sha256//[A-Za-z0-9+/]{43}=$ ]] || exit 2
  59. git_opts=(-c http.sslVerify=false -c "http.pinnedpubkey=$pin")
  60. fi
  61. [[ $repo == http://* || $repo == https://* || $repo == ssh://* ]] || exit 2
  62. [[ $branch =~ ^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$ && $branch != *..* ]] || exit 2
  63. work=$(mktemp -d "$base/work.XXXXXXXX")
  64. trap 'rm -rf "$work"' EXIT
  65. trap 'state failed' ERR
  66. state fetching
  67. version_tool=/usr/local/libexec/debtik-release-version
  68. version=$(GIT_TERMINAL_PROMPT=0 git "${git_opts[@]}" ls-remote --tags "$repo" 'refs/tags/v*' | "$version_tool" latest)
  69. installed_version=$(cat "$base/installed_version")
  70. if ! "$version_tool" greater "$version" "$installed_version"; then
  71. state failed
  72. echo "Update refused: available version $version must be greater than installed version $installed_version"
  73. exit 1
  74. fi
  75. GIT_TERMINAL_PROMPT=0 git -c protocol.file.allow=never "${git_opts[@]}" clone --single-branch --branch "v$version" -- "$repo" "$work/source"
  76. cd "$work/source"
  77. commit=$(git rev-parse HEAD)
  78. [[ $(cat VERSION) == "$version" ]] || { state failed; echo 'Release tag and VERSION do not match'; exit 1; }
  79. bash -n scripts/debtik-update
  80. python3 -c 'import pathlib; compile(pathlib.Path("scripts/release-version.py").read_text(),"release-version.py","exec")'
  81. state building
  82. go_cmd=$(command -v go || true)
  83. if [[ -x /opt/debtik/go/bin/go ]]; then go_cmd=/opt/debtik/go/bin/go; fi
  84. [[ -n $go_cmd ]] || { state failed; echo 'Go is not installed'; exit 1; }
  85. CGO_ENABLED=0 "$go_cmd" build -trimpath -ldflags "-X proxmox-router/internal/api.BuildCommit=$commit" -o "$work/proxmox-router" ./cmd/server
  86. CGO_ENABLED=0 "$go_cmd" build -trimpath -o "$work/debtik-cli" ./cmd/debtik-cli
  87. install -d -m 0700 "$base/previous"
  88. for name in proxmox-router debtik-cli; do cp -p "/usr/local/bin/$name" "$base/previous/$name"; done
  89. for name in debtik-update debtik-release-version; do cp -p "/usr/local/libexec/$name" "$base/previous/$name"; done
  90. cp -p "$base/installed_version" "$base/previous_version"
  91. if [[ -f "$base/installed_commit" ]]; then cp -p "$base/installed_commit" "$base/previous_commit"; fi
  92. printf '%s\n' "$commit" > "$base/candidate_commit"
  93. iptables-save -c > "$base/previous/iptables.save"
  94. ip6tables-save -c > "$base/previous/ip6tables.save"
  95. state installing
  96. rollback_on_error() { trap - ERR; restore || state failed; exit 1; }
  97. trap rollback_on_error ERR
  98. systemctl stop proxmox-router.service
  99. cleanup_veth
  100. for name in proxmox-router debtik-cli; do
  101. install -m 0755 "$work/$name" "/usr/local/bin/$name.next"
  102. mv -f "/usr/local/bin/$name.next" "/usr/local/bin/$name"
  103. done
  104. install -m 0750 scripts/debtik-update /usr/local/libexec/debtik-update.next
  105. install -m 0750 scripts/release-version.py /usr/local/libexec/debtik-release-version.next
  106. mv -f /usr/local/libexec/debtik-update.next /usr/local/libexec/debtik-update
  107. mv -f /usr/local/libexec/debtik-release-version.next /usr/local/libexec/debtik-release-version
  108. systemctl start proxmox-router.service
  109. sleep 3
  110. systemctl is-active --quiet proxmox-router.service
  111. printf '%s\n' "$commit" > "$base/installed_commit"
  112. printf '%s\n' "$version" > "$base/installed_version"
  113. state complete