install.fairie/home/dot_local/bin/post-installx/executable_post-cloudflared.sh

96 lines
4.4 KiB
Bash
Raw Normal View History

2024-05-04 21:05:33 -07:00
#!/usr/bin/env bash
# @file Cloudflared Configuration
# @brief Applies cloudflared configuration, connects to Argo tunnel with managed configuration, and enables it on system start
2024-05-13 20:43:17 -07:00
# @description
# 1. Skips the deletion of a tunnel when it is currently in use
2024-05-04 21:05:33 -07:00
2024-05-27 20:45:14 -07:00
set -Eeuo pipefail
2024-05-27 20:50:11 -07:00
trap "gum log -sl error 'Script encountered an error!'" ERR
2024-05-27 04:15:03 -07:00
2024-05-09 19:53:38 -07:00
if command -v cloudflared > /dev/null; then
2024-05-27 23:55:42 -07:00
### Show error message about ~/.cloudflared already existing
2024-05-04 21:05:33 -07:00
if [ -d "$HOME/.cloudflared" ]; then
2024-05-27 23:55:42 -07:00
gum log -sl error '~/.cloudflared is already in the home directory - to ensure proper deployment, remove the ~/.cloudflared configuration folder' && exit 1
2024-05-04 21:05:33 -07:00
fi
2024-05-27 23:55:42 -07:00
### Use lowercased hostname / tunnel ID
2024-05-17 22:10:17 -07:00
HOSTNAME_LOWER="host-$(hostname -s | tr '[:upper:]' '[:lower:]')"
2024-05-09 19:53:38 -07:00
### Remove previous tunnels connected to host
while read TUNNEL_ID; do
2024-05-27 20:50:11 -07:00
gum log -sl info "Deleteing CloudFlared tunnel ID $TUNNEL_ID"
2024-05-13 20:43:17 -07:00
unset TUNNEL_EXIT_CODE
sudo cloudflared tunnel delete "$TUNNEL_ID" || TUNNEL_EXIT_CODE=$?
2024-05-27 20:45:14 -07:00
if [ -z "${TUNNEL_EXIT_CODE:-}" ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Removing credentials for $TUNNEL_ID which is not in use"
2024-05-13 20:43:17 -07:00
sudo rm -f "/usr/local/etc/cloudflared/${TUNNEL_ID}.json"
else
2024-05-27 23:55:42 -07:00
gum log -sl info "Skipping deletion of $TUNNEL_ID credentials since it is in use"
2024-05-13 20:43:17 -07:00
fi
2024-05-17 22:10:17 -07:00
done< <(sudo cloudflared tunnel list | grep "$HOSTNAME_LOWER" | sed 's/ .*//')
2024-05-09 19:53:38 -07:00
2024-05-04 21:05:33 -07:00
### Register tunnel (if not already registered)
2024-05-27 20:50:11 -07:00
gum log -sl info "Creating CloudFlared tunnel named "$HOSTNAME_LOWER""
2024-05-27 23:55:42 -07:00
sudo cloudflared tunnel create "$HOSTNAME_LOWER" || true
2024-05-04 21:05:33 -07:00
2024-05-13 20:43:17 -07:00
### Acquire TUNNEL_ID and symlink credentials.json
2024-05-17 22:10:17 -07:00
TUNNEL_ID="$(sudo cloudflared tunnel list | grep "$HOSTNAME_LOWER" | sed 's/ .*//')"
2024-05-27 20:50:11 -07:00
gum log -sl info "Tunnel ID: $TUNNEL_ID"
gum log -sl info "Symlinking /usr/local/etc/cloudflared/$TUNNEL_ID.json to /usr/local/etc/cloudflared/credentials.json"
2024-05-09 19:53:38 -07:00
sudo rm -f /usr/local/etc/cloudflared/credentials.json
sudo ln -s /usr/local/etc/cloudflared/$TUNNEL_ID.json /usr/local/etc/cloudflared/credentials.json
2024-05-04 21:05:33 -07:00
2024-05-13 20:43:17 -07:00
### Configure DNS
2024-05-27 23:55:42 -07:00
### Note: The DNS records that are added via cloudflared must be deleted manually if no longer used
2024-05-27 20:50:11 -07:00
gum log -sl info 'Setting up DNS records for CloudFlare Argo tunnels'
2024-05-13 20:43:17 -07:00
while read DOMAIN; do
2024-05-17 22:13:13 -07:00
if [ "$DOMAIN" != 'null' ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Setting up $DOMAIN for access through cloudflared (Tunnel ID: $TUNNEL_ID)"
gum log -sl info "Running sudo cloudflared tunnel route dns -f "$TUNNEL_ID" "$DOMAIN""
2024-05-27 23:55:42 -07:00
sudo cloudflared tunnel route dns -f "$TUNNEL_ID" "$DOMAIN"
gum log -sl info "Successfully routed $DOMAIN to this machine's cloudflared Argo tunnel"
2024-05-17 22:13:13 -07:00
fi
2024-05-17 23:09:14 -07:00
done< <(yq '.ingress[].hostname' /usr/local/etc/cloudflared/config.yml)
2024-05-17 22:10:17 -07:00
2024-05-17 23:09:14 -07:00
### Update /usr/local/etc/cloudflared/config.yml
2024-05-27 20:50:11 -07:00
gum log -sl info 'Updating /usr/local/etc/cloudflared/config.yml to reference tunnel ID'
2024-05-17 23:09:14 -07:00
sudo yq eval -i ".tunnel = \"$HOSTNAME_LOWER\"" /usr/local/etc/cloudflared/config.yml
2024-05-13 20:43:17 -07:00
2024-05-04 21:05:33 -07:00
### Set up service
if [ -d /Applications ] && [ -d /System ]; then
2024-05-09 19:53:38 -07:00
### macOS
2024-05-04 21:05:33 -07:00
if [ -f /Library/LaunchDaemons/com.cloudflare.cloudflared.plist ]; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'cloudflared service is already installed'
2024-05-04 21:05:33 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Running sudo cloudflared service install'
2024-05-09 19:53:38 -07:00
sudo cloudflared service install
2024-05-04 21:05:33 -07:00
fi
2024-05-27 23:55:42 -07:00
### Apply patched version of the LaunchDaemon
load-service com.cloudflare.cloudflared
2024-05-04 21:05:33 -07:00
elif [ -f /etc/os-release ]; then
2024-05-09 19:53:38 -07:00
### Linux
2024-05-04 21:05:33 -07:00
if systemctl --all --type service | grep -q "cloudflared" > /dev/null; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'cloudflared service is already available as a service'
2024-05-04 21:05:33 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Running sudo cloudflared service install'
2024-05-09 19:53:38 -07:00
sudo cloudflared service install
2024-05-04 21:05:33 -07:00
fi
2024-05-27 23:55:42 -07:00
### Start / enabled the systemd service
2024-05-27 20:50:11 -07:00
gum log -sl info 'Ensuring cloudflared service is started'
2024-05-04 21:05:33 -07:00
sudo systemctl start cloudflared
2024-05-27 20:50:11 -07:00
gum log -sl info 'Enabling cloudflared as a boot systemctl service'
2024-05-04 21:05:33 -07:00
sudo systemctl enable cloudflared
else
# System is Windows
cloudflared service install
mkdir C:\Windows\System32\config\systemprofile\.cloudflared
# Copy same cert.pem as being used above
# copy C:\Users\%USERNAME%\.cloudflared\cert.pem C:\Windows\System32\config\systemprofile\.cloudflared\cert.pem
# https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/local/as-a-service/windows/
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'cloudflared was not installed so CloudFlare Tunnels cannot be enabled'
2024-05-04 21:05:33 -07:00
fi