#!/usr/bin/env bash set -euo pipefail if [[ $EUID -ne 0 ]]; then echo 'Run as root' >&2; exit 1; fi src=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) command -v apt-get >/dev/null || { echo 'Debian apt-get required' >&2; exit 1; } mode=${1:-install} [[ $# -le 1 && ( $mode == install || $mode == --dependencies-only || $mode == --check-dependencies ) ]] || { echo 'Usage: install.sh [--dependencies-only|--check-dependencies]' >&2; exit 2; } if [[ $mode == --check-dependencies ]]; then exec bash "$src/scripts/install-dependencies.sh" --check; fi build="" trap 'if [[ -n $build ]]; then rm -rf "$build"; fi' EXIT install -d -m 0700 /var/lib/debtik/update exec 9>/var/lib/debtik/update/lock flock -n 9 || { echo 'An update or installation is already running' >&2;exit 1; } bash "$src/scripts/install-dependencies.sh" if [[ $mode == --dependencies-only ]]; then exit; fi export HOME=${HOME:-/root} export GOPATH=${GOPATH:-/var/cache/debtik/go} export GOCACHE=${GOCACHE:-/var/cache/debtik/go-build} go_cmd=$(command -v go || :) if [[ -x /opt/debtik/go/bin/go ]]; then go_cmd=/opt/debtik/go/bin/go export PATH=/opt/debtik/go/bin:$PATH fi [[ -n $go_cmd ]] || { echo 'Go compiler not found' >&2; exit 1; } cd "$src" build=$(mktemp -d) commit=$(git rev-parse HEAD 2>/dev/null || printf unknown) echo "Building DebTik server for $(uname -m); the first build may take several minutes..." CGO_ENABLED=0 "$go_cmd" build -trimpath -ldflags "-X debtik/internal/api.BuildCommit=$commit" -o "$build/debtik" ./cmd/server echo "Building DebTik CLI..." CGO_ENABLED=0 "$go_cmd" build -trimpath -o "$build/debtik-cli" ./cmd/debtik-cli # Migrate legacy /etc/proxmox-router config directory to /etc/debtik if [[ -d /etc/proxmox-router && ! -L /etc/proxmox-router ]]; then install -d -m 0700 /etc/debtik cp -a /etc/proxmox-router/* /etc/debtik/ 2>/dev/null || true rm -rf /etc/proxmox-router ln -s /etc/debtik /etc/proxmox-router fi if [[ ! -e /etc/proxmox-router ]]; then install -d -m 0700 /etc/debtik ln -s /etc/debtik /etc/proxmox-router fi install -d -m 0700 /var/lib/debtik/update install -d -m 0755 /usr/local/libexec install -m 0750 scripts/debtik-update /usr/local/libexec/debtik-update install -m 0750 scripts/release-version.py /usr/local/libexec/debtik-release-version install -m 0750 scripts/install-dependencies.sh /usr/local/libexec/debtik-install-dependencies install -m 0755 scripts/debtik-ifnames /usr/local/libexec/debtik-ifnames mkdir -p /etc/network/if-pre-up.d ln -sf /usr/local/libexec/debtik-ifnames /etc/network/if-pre-up.d/debtik-ifnames install -d -m 0755 /etc/network/interfaces.d for name in debtik debtik-cli; do if [[ -f /usr/local/bin/$name ]]; then cp -p "/usr/local/bin/$name" "/usr/local/bin/$name.previous"; fi install -m 0755 "$build/$name" "/usr/local/bin/$name.next" mv -f "/usr/local/bin/$name.next" "/usr/local/bin/$name" done # Backward compatibility symlink ln -sf /usr/local/bin/debtik /usr/local/bin/proxmox-router 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 install -m 0644 systemd/debtik.service /etc/systemd/system/debtik.service if [[ -f systemd/debtik-ifnames.service ]]; then install -m 0644 systemd/debtik-ifnames.service /etc/systemd/system/debtik-ifnames.service fi if [[ -f /etc/systemd/system/proxmox-router.service.d/mstpd.conf && ! -f /etc/systemd/system/debtik.service.d/mstpd.conf ]]; then install -d -m 0755 /etc/systemd/system/debtik.service.d cp /etc/systemd/system/proxmox-router.service.d/mstpd.conf /etc/systemd/system/debtik.service.d/mstpd.conf fi if systemctl is-active --quiet proxmox-router.service; then systemctl stop proxmox-router.service || true systemctl disable proxmox-router.service || true fi systemctl daemon-reload systemctl enable debtik-ifnames.service || true systemctl enable debtik.service systemctl restart debtik.service systemctl is-active --quiet debtik.service if [[ $commit =~ ^[a-f0-9]{40}$ ]]; then printf '%s\n' "$commit" > /var/lib/debtik/update/installed_commit; fi install -m 0600 VERSION /var/lib/debtik/update/installed_version install -m 0644 go.mod /var/lib/debtik/update/go.mod echo 'DebTik installed: http:/// and debtik-cli'