2023-03-19 23:38:30 -07:00
|
|
|
{{- if or (and (ne .host.distro.family "windows") (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "TAILSCALE_AUTH_KEY")) (env "TAILSCALE_AUTH_KEY")) -}}
|
2023-02-16 01:53:45 -08:00
|
|
|
#!/usr/bin/env bash
|
2023-04-11 20:26:25 -07:00
|
|
|
# @file Tailscale
|
|
|
|
# @brief Connects the Tailscale client with the Tailscale network
|
|
|
|
# @description
|
|
|
|
# This script ensures the `tailscaled` system daemon is installed on macOS. Then, on both macOS and Linux, it connects to the Tailscale
|
|
|
|
# network if the `TAILSCALE_AUTH_KEY` variable is provided.
|
2023-02-16 01:53:45 -08:00
|
|
|
|
2023-04-17 21:57:54 -07:00
|
|
|
{{ includeTemplate "universal/profile" }}
|
|
|
|
{{ includeTemplate "universal/logg" }}
|
|
|
|
|
2023-02-16 01:53:45 -08:00
|
|
|
### Install the Tailscale system daemon
|
|
|
|
if [ -d /Applications ] && [ -d System ]; then
|
|
|
|
# macOS
|
|
|
|
if command -v tailscaled > /dev/null; then
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Ensuring tailscaled system daemon is installed'
|
2023-02-16 01:53:45 -08:00
|
|
|
sudo tailscaled install-system-daemon
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'tailscaled system daemon is now installed and will load on boot'
|
2023-02-16 01:53:45 -08:00
|
|
|
else
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'tailscaled does not appear to be installed'
|
2023-02-16 01:53:45 -08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Connect to Tailscale network
|
2023-06-18 20:30:41 -07:00
|
|
|
TAILSCALE_AUTH_KEY="{{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "TAILSCALE_AUTH_KEY")) }}{{ includeTemplate "secrets/TAILSCALE_AUTH_KEY" | decrypt | trim }}{{ else }}{{ env "TAILSCALE_AUTH_KEY" }}{{ end }}"
|
2023-02-16 01:53:45 -08:00
|
|
|
if command -v tailscale > /dev/null && [ "$TAILSCALE_AUTH_KEY" != "" ]; then
|
|
|
|
logg info 'Connecting to Tailscale with user-defined authentication key'
|
|
|
|
timeout 14 tailscale up --authkey="$TAILSCALE_AUTH_KEY" --accept-routes || EXIT_CODE=$?
|
|
|
|
if [ -n "$EXIT_CODE" ]; then
|
2023-11-04 18:46:18 -07:00
|
|
|
logg warn 'tailscale up timed out'
|
2023-02-16 01:53:45 -08:00
|
|
|
else
|
|
|
|
logg success 'Connected to Tailscale network'
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-01-25 21:02:34 -08:00
|
|
|
{{- end -}}
|