2023-04-16 16:31:18 -07:00
{{- if (and (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "DIGITALOCEAN_ACCESS_TOKEN")) (env "DIGITALOCEAN_ACCESS_TOKEN")) -}}
#!/usr/bin/env bash
# @file DigitalOcean Kubernetes
# @brief Connects to DigitalOcean Kubernetes cluster
# @description
# This script runs when `DIGITALOCEAN_ACCESS_TOKEN` is defined as an environment variable or as an encrypted key (see
# [Secrets documentation](https://install.doctor/docs/customization/secrets #encrypted - secrets ) ) . If the check passes ,
# then the script ensures the DigitalOcean CLI is installed (i.e. `doctl`). Then, it uses `doctl` to connect to the Kubernetes
2023-04-17 21:57:54 -07:00
# cluster defined by the the configuration stored under `.user.digitalOceanClusterId` in `home/.chezmoi.yaml.tmpl`.
2023-04-16 16:31:18 -07:00
2023-04-17 21:57:54 -07:00
{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}
### Ensure `DIGITALOCEAN_ACCESS_TOKEN` is defined (used for headlessly connecting to the k8s cluster)
2023-06-18 20:30:41 -07:00
export DIGITALOCEAN_ACCESS_TOKEN="{{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "DIGITALOCEAN_ACCESS_TOKEN")) }}{{ includeTemplate "secrets/DIGITALOCEAN_ACCESS_TOKEN" | decrypt | trim }}{{ else }}{{ env "DIGITALOCEAN_ACCESS_TOKEN" }}{{ end }}"
2023-04-16 16:31:18 -07:00
2023-04-17 21:57:54 -07:00
if [ -n " $ DIGITALOCEAN_ACCESS_TOKEN " ] && [ -n '{{ .user.digitalOceanClusterId }}' ]; then
### Ensure DigitalOcean CLI is instaled
if ! command -v doctl > /dev/null; then
2024-05-27 20:50:11 -07:00
gum log -sl info 'doctl is missing - installing via Homebrew'
2023-11-29 23:52:35 -08:00
brew install --quiet doctl
2023-04-17 21:57:54 -07:00
fi
### Connect to the k8s cluster with `doctl`
2024-05-27 20:50:11 -07:00
gum log -sl info 'Connecting to the DigitalOcean k8s cluster with doctl'
2023-04-17 21:57:54 -07:00
doctl kubernetes cluster kubeconfig save {{ .user.digitalOceanClusterId }}
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Skipping connecting to the DigitalOcean k8s cluster because either the DIGITALOCEAN_ACCESS_TOKEN or the .user.digitalOceanClusterId is not defined'
2023-04-17 21:57:54 -07:00
fi
2023-04-16 16:31:18 -07:00
{{ end -}}