Skip to content

Commit

Permalink
Merge pull request #54 from SierraSoftworks/feat/2.x-firmware-persist…
Browse files Browse the repository at this point in the history
…ence

feat: Add support for automatically installing Tailscale after a firmware upgrade
  • Loading branch information
notheotherben authored Apr 3, 2023
2 parents 154ff8e + 3106f73 commit 923ac33
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions build/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cp "${SOURCE}/package/on-boot.sh" "${WORKDIR}/on_boot.d/10-tailscaled.sh"
cp "${SOURCE}/package/manage.sh" "${WORKDIR}/tailscale/manage.sh"
cp "${SOURCE}/package/unios_"*".sh" "${WORKDIR}/tailscale/"
cp "${SOURCE}/package/tailscale-env" "${WORKDIR}/tailscale/tailscale-env"
cp "${SOURCE}/package/tailscale-install.service" "${WORKDIR}/tailscale/tailscale-install.service"
cp "${SOURCE}/LICENSE" "${WORKDIR}/tailscale/LICENSE"

echo "Building tailscale-udm package"
Expand Down
17 changes: 14 additions & 3 deletions package/manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ else
fi

tailscale_status() {
if _tailscale_is_running; then
if ! _tailscale_is_installed; then
echo "Tailscale is not installed"
exit 1
elif _tailscale_is_running; then
echo "Tailscaled is running"
$TAILSCALE --version
else
Expand Down Expand Up @@ -82,10 +85,14 @@ case $1 in
;;
"install")
if _tailscale_is_running; then
echo "Tailscale is already installed, if you wish to update it, run '$0 update'"
echo "Tailscale is already installed and running, if you wish to update it, run '$0 update'"
echo "If you wish to force a reinstall, run '$0 install!'"
exit 0
fi

tailscale_install "$2"
;;
"install!")
tailscale_install "$2"
;;
"uninstall")
Expand All @@ -112,8 +119,12 @@ case $1 in
fi
;;
"on-boot")
if ! _tailscale_is_installed; then
tailscale_install
fi

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

if [ "${TAILSCALE_AUTOUPDATE}" = "true" ]; then
tailscale_has_update && tailscale_update || echo "Not updated"
Expand Down
16 changes: 16 additions & 0 deletions package/tailscale-install.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Ensure that Tailscale is installed on your device
After=network.target

[Service]
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]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions package/unios_1.x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ _tailscale_is_running() {
fi
}

_tailscale_is_installed() {
if [ -e "${TAILSCALE}" ] && [ -e "${TAILSCALED}" ]; then
return 0
else
return 1
fi
}

_tailscale_start() {
# shellcheck source=package/tailscale-env
. "${TAILSCALE_ROOT}/tailscale-env"
Expand Down
25 changes: 20 additions & 5 deletions package/unios_2.x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ _tailscale_is_running() {
systemctl is-active --quiet tailscaled
}

_tailscale_is_installed() {
command -v tailscale >/dev/null 2>&1
}

_tailscale_start() {
systemctl start tailscaled

# Wait a few seconds for the daemon to start
sleep 5

Expand Down Expand Up @@ -55,23 +59,34 @@ _tailscale_install() {
}

echo "Restarting Tailscale daemon to detect new configuration..."
systemctl restart tailscaled || {
systemctl restart tailscaled.service || {
echo "Failed to restart Tailscale daemon"
echo "The daemon might not be running with userspace networking enabled, you can restart it manually using 'systemctl restart tailscaled'."
exit 1
}

echo "Enabling Tailscale to start on boot..."
systemctl enable tailscaled || {
systemctl enable tailscaled.service || {
echo "Failed to enable Tailscale to start on boot"
echo "You can enable it manually using 'systemctl enable tailscaled'."
exit 1
}


if [ ! -e "/etc/systemd/system/tailscale-install.service" ]; then
echo "Installing pre-start script to install Tailscale on firmware updates."
ln -s "${TAILSCALE_ROOT}/tailscale-install.service" /etc/systemd/system/tailscale-install.service

systemctl daemon-reload
systemctl enable tailscale-install.service
fi

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

_tailscale_uninstall() {
apt remove tailscale
apt remove -y tailscale
rm -f /etc/apt/sources.list.d/tailscale.list || true

systemctl disable tailscale-install.service || true
rm -f /lib/systemd/system/tailscale-install.service || true
}
Empty file modified tests/install.1.x.test.sh
100644 → 100755
Empty file.
13 changes: 10 additions & 3 deletions tests/install.2.x.test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mock "${WORKDIR}/apt-key" "--## apt-key mock: \$* ##--"
mock "${WORKDIR}/tee" "--## tee mock: \$* ##--"
mock "${WORKDIR}/apt" "--## apt mock: \$* ##--"
mock "${WORKDIR}/sed" "--## sed mock: \$* ##--"
mock "${WORKDIR}/ln" "--## ln mock: \$* ##--"
mock "${WORKDIR}/ubnt-device-info" "2.0.0"

# systemctl mock, used to ensure the installer doesn't block thinking that tailscale is running
Expand All @@ -34,8 +35,12 @@ case "\$1" in
exit 1
;;
"enable")
echo "--## systemctl enable ##--"
touch "${WORKDIR}/tailscaled.enabled"
echo "--## systemctl enable \$2 ##--"
touch "${WORKDIR}/\$2.enabled"
;;
"daemon-reload")
echo "--## systemctl daemon-reload ##--"
touch "${WORKDIR}/systemctl.daemon-reload"
;;
"restart")
echo "--## systemctl restart ##--"
Expand All @@ -57,4 +62,6 @@ assert_contains "$(head -n 1 "${WORKDIR}/apt.args")" "update" "The apt command s
assert_contains "$(head -n 2 "${WORKDIR}/apt.args" | tail -n 1)" "install -y tailscale" "The apt command should be called with the command to install tailscale file"
assert_contains "$(cat "${WORKDIR}/sed.args")" "--tun userspace-networking" "The defaults should be updated with userspace networking"
[[ -f "${WORKDIR}/tailscaled.restarted" ]]; assert "tailscaled should have been restarted"
[[ -f "${WORKDIR}/tailscaled.enabled" ]]; assert "tailscaled unit should be enabled"
[[ -f "${WORKDIR}/tailscaled.service.enabled" ]]; assert "tailscaled unit should be enabled"
[[ -f "${WORKDIR}/systemctl.daemon-reload" ]]; assert "systemctl should have been reloaded"
[[ -f "${WORKDIR}/tailscale-install.service.enabled" ]]; assert "tailscale-install unit should be enabled"
5 changes: 4 additions & 1 deletion tests/status.1.x.test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ export TAILSCALED_SOCK="${WORKDIR}/tailscaled.sock"

export PATH="${WORKDIR}:${PATH}"
mock "${WORKDIR}/ubnt-device-info" "1.0.0"
mock "${WORKDIR}/tailscale" "0.0.0"

assert_eq "$("${ROOT}/package/manage.sh" status)" "Tailscale is not installed" "Tailscaled should be reported as not installed"

mock "${WORKDIR}/tailscale" "0.0.0"
mock "${WORKDIR}/tailscaled" "0.0.0"
assert_eq "$("${ROOT}/package/manage.sh" status)" "Tailscaled is not running" "Tailscaled should be reported as not running"

touch "${TAILSCALED_SOCK}"; assert "The tailscale socket should be created"
Expand Down
3 changes: 3 additions & 0 deletions tests/status.2.x.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export TAILSCALED_SOCK="${WORKDIR}/tailscaled.sock"

export PATH="${WORKDIR}:${PATH}"
mock "${WORKDIR}/ubnt-device-info" "2.0.0"

assert_eq "$("${ROOT}/package/manage.sh" status)" "Tailscale is not installed" "Tailscaled should be reported as not installed"

mock "${WORKDIR}/tailscale" "0.0.0"

mock "${WORKDIR}/systemctl" "" 1
Expand Down
Empty file modified tests/update.1.x.test.sh
100644 → 100755
Empty file.

0 comments on commit 923ac33

Please sign in to comment.