Skip to content

Commit

Permalink
Merge pull request #79 from SierraSoftworks/feat/autoupdate
Browse files Browse the repository at this point in the history
feat: Add support for automatically updating Tailscale without a system reboot
  • Loading branch information
notheotherben authored Jan 8, 2024
2 parents f3e196c + c311aec commit 8ea58a4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 23 deletions.
6 changes: 3 additions & 3 deletions package/manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ case $1 in
fi
;;
"on-boot")
# shellcheck source=package/tailscale-env
. "${PACKAGE_ROOT}/tailscale-env"

if ! _tailscale_is_installed; then
tailscale_install
fi

# shellcheck source=package/tailscale-env
. "${PACKAGE_ROOT}/tailscale-env"

if [ "${TAILSCALE_AUTOUPDATE}" = "true" ]; then
tailscale_has_update && tailscale_update || echo "Not updated"
fi
Expand Down
3 changes: 2 additions & 1 deletion package/tailscale-env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PORT="41641"
TAILSCALED_FLAGS="--tun userspace-networking"
TAILSCALE_FLAGS=""
TAILSCALE_AUTOUPDATE="true"
TAILSCALE_AUTOUPDATE="true"
TAILSCALE_CHANNEL="stable"
3 changes: 0 additions & 3 deletions package/tailscale-install.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ Type=oneshot
RemainAfterExit=yes
Restart=no
Environment=DEBIAN_FRONTEND=noninteractive

# Wait 30 seconds after booting before attempting to install Tailscale
ExecStartPre=/bin/bash -c 'sleep 30'
ExecStart=/bin/bash /data/tailscale/manage.sh on-boot

[Install]
Expand Down
11 changes: 11 additions & 0 deletions package/tailscale-install.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Ensure that Tailscale is updated automatically on your device.
Needs=network.target

[Timer]
OnBootSec=5m
OnUnitActiveSec=24h
Unit=tailscale-install.service

[Install]
WantedBy=multi-user.target
10 changes: 7 additions & 3 deletions package/unios_1.x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ _tailscale_stop() {
}

_tailscale_install() {
VERSION="${1:-$(curl -sSLq --ipv4 'https://pkgs.tailscale.com/stable/?mode=json' | jq -r '.Tarballs.arm64 | capture("tailscale_(?<version>[^_]+)_").version')}"
# Load the tailscale-env file to discover the flags which are required to be set
# shellcheck source=package/tailscale-env
. "${TAILSCALE_ROOT}/tailscale-env"

VERSION="${1:-$(curl -sSLq --ipv4 "https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/?mode=json" | jq -r '.Tarballs.arm64 | capture("tailscale_(?<version>[^_]+)_").version')}"
WORKDIR="$(mktemp -d || exit 1)"
trap 'rm -rf ${WORKDIR}' EXIT
TAILSCALE_TGZ="${WORKDIR}/tailscale.tgz"

echo "Installing Tailscale v${VERSION} in ${TAILSCALE_ROOT}..."
curl -sSLf --ipv4 -o "${TAILSCALE_TGZ}" "https://pkgs.tailscale.com/stable/tailscale_${VERSION}_arm64.tgz" || {
echo "Failed to download Tailscale v${VERSION} from https://pkgs.tailscale.com/stable/tailscale_${VERSION}_arm64.tgz"
curl -sSLf --ipv4 -o "${TAILSCALE_TGZ}" "https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/tailscale_${VERSION}_arm64.tgz" || {
echo "Failed to download Tailscale v${VERSION} from https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/tailscale_${VERSION}_arm64.tgz"
echo "Please make sure that you're using a valid version number and try again."
exit 1
}
Expand Down
30 changes: 17 additions & 13 deletions package/unios_2.x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ _tailscale_start() {
exit 1
fi

# Run tailscale up to configure
echo "Running tailscale up to configure interface..."

# shellcheck disable=SC2086
timeout 30 tailscale up || echo "Interface configuration timed out, you may need to run this manually."
echo "Run tailscale up to configure the interface."
}

_tailscale_stop() {
systemctl stop tailscaled
}

_tailscale_install() {
tailscale_version="${1:-$(curl -sSLq --ipv4 'https://pkgs.tailscale.com/stable/?mode=json' | jq -r '.Tarballs.arm64 | capture("tailscale_(?<version>[^_]+)_").version')}"

# shellcheck source=tests/os-release
. /etc/os-release

# Load the tailscale-env file to discover the flags which are required to be set
# shellcheck source=package/tailscale-env
. "${TAILSCALE_ROOT}/tailscale-env"

tailscale_version="${1:-$(curl -sSLq --ipv4 "https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/?mode=json" | jq -r '.Tarballs.arm64 | capture("tailscale_(?<version>[^_]+)_").version')}"

echo "Installing latest Tailscale package repository key..."
curl -fsSL --ipv4 "https://pkgs.tailscale.com/stable/${ID}/${VERSION_CODENAME}.gpg" | apt-key add -
curl -fsSL --ipv4 "https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/${ID}/${VERSION_CODENAME}.gpg" | apt-key add -

if [ ! -f "/etc/apt/sources.list.d/tailscale.list" ]; then
echo "Installing Tailscale package repository..."
curl -fsSL --ipv4 "https://pkgs.tailscale.com/stable/${ID}/${VERSION_CODENAME}.list" | tee /etc/apt/sources.list.d/tailscale.list
curl -fsSL --ipv4 "https://pkgs.tailscale.com/${TAILSCALE_CHANNEL}/${ID}/${VERSION_CODENAME}.list" | tee /etc/apt/sources.list.d/tailscale.list
fi

echo "Updating package lists..."
Expand All @@ -54,10 +54,6 @@ _tailscale_install() {
echo "Installing Tailscale ${tailscale_version}..."
apt install -y tailscale="${tailscale_version}"

# Load the tailscale-env file to discover the flags which are required to be set
# shellcheck source=package/tailscale-env
. "${TAILSCALE_ROOT}/tailscale-env"

echo "Configuring Tailscale port..."
sed -i "s/PORT=\"[^\"]*\"/PORT=\"${PORT:-41641}\"/" /etc/default/tailscaled || {
echo "Failed to configure Tailscale port"
Expand Down Expand Up @@ -94,6 +90,14 @@ _tailscale_install() {
systemctl enable tailscale-install.service
fi

if [ ! -e "/etc/systemd/system/tailscale-install.timer" ]; then
echo "Installing auto-update timer to ensure that Tailscale is kept installed and up to date."
ln -s "${TAILSCALE_ROOT}/tailscale-install.timer" /etc/systemd/system/tailscale-install.timer

systemctl daemon-reload
systemctl enable --now tailscale-install.timer
fi

echo "Installation complete, run '$0 start' to start Tailscale"
}

Expand Down
3 changes: 3 additions & 0 deletions tests/install.1.x.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export TAILSCALED_SOCK="${WORKDIR}/tailscaled.sock"
export PATH="${WORKDIR}:${PATH}"
mock "${WORKDIR}/ubnt-device-info" "1.0.0"

cp "${ROOT}/tests/os-release" "${WORKDIR}/os-release"
cp "${PACKAGE_ROOT}/tailscale-env" "${WORKDIR}/tailscale-env"

"${ROOT}/package/manage.sh" install; assert "Tailscale installer should run successfully"

[[ -f "${TAILSCALE_ROOT}/tailscale" ]]; assert "Tailscale should be installed"
Expand Down
1 change: 1 addition & 0 deletions tests/install.2.x.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ esac
EOF
chmod +x "${WORKDIR}/systemctl"

cp "${ROOT}/tests/os-release" "${WORKDIR}/os-release"
cp "${PACKAGE_ROOT}/tailscale-env" "${WORKDIR}/tailscale-env"

"${ROOT}/package/manage.sh" install; assert "Tailscale installer should run successfully"
Expand Down
2 changes: 2 additions & 0 deletions tests/update.1.x.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ chmod +x "${WORKDIR}/tailscale"

mock "${WORKDIR}/tailscaled" "tailscaled \$1"

cp "${PACKAGE_ROOT}/tailscale-env" "${WORKDIR}/tailscale-env"

touch "${TAILSCALED_SOCK}"; assert "The tailscale socket should be created"
assert_eq "$("${ROOT}/package/manage.sh" update)" "Tailscaled is running, please stop it before updating" "The update command should exit with an error when Tailscale is running"

Expand Down

0 comments on commit 8ea58a4

Please sign in to comment.