debtik-update 6.3 KB

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