install.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. if [[ $EUID -ne 0 ]]; then echo 'Run as root' >&2; exit 1; fi
  4. src=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
  5. command -v apt-get >/dev/null || { echo 'Debian apt-get required' >&2; exit 1; }
  6. mode=${1:-install}
  7. [[ $# -le 1 && ( $mode == install || $mode == --dependencies-only || $mode == --check-dependencies ) ]] || { echo 'Usage: install.sh [--dependencies-only|--check-dependencies]' >&2; exit 2; }
  8. if [[ $mode == --check-dependencies ]]; then exec bash "$src/scripts/install-dependencies.sh" --check; fi
  9. build=""
  10. trap 'if [[ -n $build ]]; then rm -rf "$build"; fi' EXIT
  11. install -d -m 0700 /var/lib/debtik/update
  12. exec 9>/var/lib/debtik/update/lock
  13. flock -n 9 || { echo 'An update or installation is already running' >&2;exit 1; }
  14. bash "$src/scripts/install-dependencies.sh"
  15. if [[ $mode == --dependencies-only ]]; then exit; fi
  16. export HOME=${HOME:-/root}
  17. export GOPATH=${GOPATH:-/var/cache/debtik/go}
  18. export GOCACHE=${GOCACHE:-/var/cache/debtik/go-build}
  19. go_cmd=$(command -v go || :)
  20. if [[ -x /opt/debtik/go/bin/go ]]; then
  21. go_cmd=/opt/debtik/go/bin/go
  22. export PATH=/opt/debtik/go/bin:$PATH
  23. fi
  24. [[ -n $go_cmd ]] || { echo 'Go compiler not found' >&2; exit 1; }
  25. cd "$src"
  26. build=$(mktemp -d)
  27. commit=$(git rev-parse HEAD 2>/dev/null || printf unknown)
  28. echo "Building DebTik server for $(uname -m); the first build may take several minutes..."
  29. CGO_ENABLED=0 "$go_cmd" build -trimpath -ldflags "-X proxmox-router/internal/api.BuildCommit=$commit" -o "$build/proxmox-router" ./cmd/server
  30. echo "Building DebTik CLI..."
  31. CGO_ENABLED=0 "$go_cmd" build -trimpath -o "$build/debtik-cli" ./cmd/debtik-cli
  32. install -d -m 0700 /etc/proxmox-router
  33. install -d -m 0700 /var/lib/debtik/update
  34. install -d -m 0755 /usr/local/libexec
  35. install -m 0750 scripts/debtik-update /usr/local/libexec/debtik-update
  36. install -m 0750 scripts/release-version.py /usr/local/libexec/debtik-release-version
  37. install -m 0750 scripts/install-dependencies.sh /usr/local/libexec/debtik-install-dependencies
  38. install -d -m 0755 /etc/network/interfaces.d
  39. for name in proxmox-router debtik-cli; do
  40. if [[ -f /usr/local/bin/$name ]]; then cp -p "/usr/local/bin/$name" "/usr/local/bin/$name.previous"; fi
  41. install -m 0755 "$build/$name" "/usr/local/bin/$name.next"
  42. mv -f "/usr/local/bin/$name.next" "/usr/local/bin/$name"
  43. done
  44. if [[ -f /etc/network/interfaces ]] && ! grep -Eq '^source[[:space:]]+/etc/network/interfaces\.d/\*' /etc/network/interfaces; then printf '\nsource /etc/network/interfaces.d/*\n' >> /etc/network/interfaces; fi
  45. install -m 0644 systemd/proxmox-router.service /etc/systemd/system/proxmox-router.service
  46. systemctl daemon-reload
  47. systemctl enable proxmox-router.service
  48. systemctl restart proxmox-router.service
  49. systemctl is-active --quiet proxmox-router.service
  50. if [[ $commit =~ ^[a-f0-9]{40}$ ]]; then printf '%s\n' "$commit" > /var/lib/debtik/update/installed_commit; fi
  51. install -m 0600 VERSION /var/lib/debtik/update/installed_version
  52. install -m 0644 go.mod /var/lib/debtik/update/go.mod
  53. echo 'DebTik installed: http://<host>/ and debtik-cli'