debtik-update 5.3 KB

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