Skip to content

Commit

Permalink
Merge pull request #86 from entelecheia/entelecheia/issue77
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia authored Sep 27, 2023
2 parents c512a57 + e1b0d09 commit 80da7ca
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
96 changes: 96 additions & 0 deletions chezmoi/.chezmoi.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@
# {{- end -}}
# {{- $ngrok_config_file := or (env "NGROK_CONFIG") (joinPath .chezmoi.homeDir ".config/ngrok/ngrok.yml") -}}

# {{- $use_cloudflared := false -}}
# {{- if eq (env "DOTFILES_USE_CLOUDFLARED") "true" "True" "TRUE" "1" -}}
# {{- $use_cloudflared = true -}}
# {{- end -}}
# {{- if and (hasKey . "cloudflared") (hasKey .cloudflared "enabled") -}}
# {{- $use_cloudflared = .cloudflared.enabled -}}
# {{- end -}}
# {{- $cloudflared_tunnel_uuid := or (env "CLOUDFLARED_TUNNEL_UUID") "" -}}
# {{- if and (hasKey . "cloudflared") (hasKey .cloudflared "tunnel_uuid") -}}
# {{- $cloudflared_tunnel_uuid = .cloudflared.tunnel_uuid -}}
# {{- end -}}
# {{- $cloudflared_tunnel_name := or (env "CLOUDFLARED_TUNNEL_NAME") "" -}}
# {{- if and (hasKey . "cloudflared") (hasKey .cloudflared "tunnel_name") -}}
# {{- $cloudflared_tunnel_name = .cloudflared.tunnel_name -}}
# {{- end -}}
# {{- $cloudflared_tunnel_hostname := or (env "CLOUDFLARED_TUNNEL_HOSTNAME") "" -}}
# {{- if and (hasKey . "cloudflared") (hasKey .cloudflared "tunnel_hostname") -}}
# {{- $cloudflared_tunnel_hostname = .cloudflared.tunnel_hostname -}}
# {{- end -}}
# {{- $cloudflared_config_file := or (env "CLOUDFLARED_CONFIG") (joinPath .chezmoi.homeDir ".cloudflared/config.yml") -}}
# {{- $cloudflared_config_template := (joinPath .chezmoi.homeDir ".cloudflared/config.tmp") -}}
# {{- $cloudflared_credentials_file := "" -}}

# {{- $use_pass := true -}}
# {{- if eq (env "DOTFILES_USE_PASS") "false" "False" "FALSE" "0" -}}
# {{- $use_pass = false -}}
Expand Down Expand Up @@ -832,6 +855,68 @@
# {{- writeToStdout "❎ OpenAI API disabled\n" -}}
# {{- end -}}

# {{- if $interactive -}}
# {{- $question := "❔ Do you want to use cloudflared" -}}
# {{- $use_cloudflared = promptBool $question $use_cloudflared -}}
# {{- end -}}
# {{- if $use_cloudflared -}}
# {{- writeToStdout "✅ cloudflared enabled\n" -}}
# {{- else -}}
# {{- writeToStdout "❎ cloudflared disabled\n" -}}
# {{- end -}}
# {{- if $use_cloudflared -}}
# {{- if $interactive -}}
# {{- $question := "❔ What is your cloudflared tunnel name" -}}
# {{- $answer := "" -}}
# {{- if $cloudflared_tunnel_name -}}
# {{- $answer = promptString $question $cloudflared_tunnel_name -}}
# {{- else -}}
# {{- $answer = promptString $question -}}
# {{- end -}}
# {{- $cloudflared_tunnel_name = $answer -}}
# {{- end -}}
# {{- if $cloudflared_tunnel_name -}}
# {{- writeToStdout (printf "✅ cloudflared tunnel name set as '%s'\n" $cloudflared_tunnel_name) -}}
# {{- else -}}
# {{- writeToStdout "❎ cloudflared tunnel name not set\n" -}}
# {{- end -}}

# {{- if $interactive -}}
# {{- $question := "❔ What is your cloudflared tunnel uuid" -}}
# {{- $answer := "" -}}
# {{- if $cloudflared_tunnel_uuid -}}
# {{- $answer = promptString $question $cloudflared_tunnel_uuid -}}
# {{- else -}}
# {{- $answer = promptString $question -}}
# {{- end -}}
# {{- $cloudflared_tunnel_uuid = $answer -}}
# {{- end -}}
# {{- if $cloudflared_tunnel_uuid -}}
# {{- writeToStdout (printf "✅ cloudflared tunnel uuid set as '%s'\n" $cloudflared_tunnel_uuid) -}}
# {{- $cloudflared_credentials_file = joinPath .chezmoi.homeDir (printf ".cloudflared/%s.json" $cloudflared_tunnel_uuid) -}}
# {{- else -}}
# {{- writeToStdout "❎ cloudflared tunnel uuid not set\n" -}}
# {{- end -}}

# {{- if $interactive -}}
# {{- $question := "❔ What is your cloudflared tunnel hostname" -}}
# {{- $answer := "" -}}
# {{- if $cloudflared_tunnel_hostname -}}
# {{- $answer = promptString $question $cloudflared_tunnel_hostname -}}
# {{- else -}}
# {{- $answer = promptString $question -}}
# {{- end -}}
# {{- $cloudflared_tunnel_hostname = $answer -}}
# {{- end -}}
# {{- if $cloudflared_tunnel_hostname -}}
# {{- writeToStdout (printf "✅ cloudflared tunnel hostname set as '%s'\n" $cloudflared_tunnel_hostname) -}}
# {{- else -}}
# {{- writeToStdout "❎ cloudflared tunnel hostname not set\n" -}}
# {{- end -}}

# {{- end -}}


# {{- if $use_ngrok -}}
# {{- writeToStdout "✅ ngrok enabled\n" -}}
# {{- else -}}
Expand Down Expand Up @@ -998,6 +1083,9 @@
# {{- if $use_ngrok -}}
# {{- $pkgs_user = printf "%s ngrok" $pkgs_user -}}
# {{- end -}}
# {{- if $use_cloudflared -}}
# {{- $pkgs_user = printf "%s cloudflared" $pkgs_user -}}
# {{- end -}}
# {{- if $use_pass -}}
# {{- $pkgs_user = printf "%s pass" $pkgs_user -}}
# {{- end -}}
Expand Down Expand Up @@ -1173,6 +1261,14 @@ data:
authtoken: {{ $ngrok_authtoken | quote }}
config_file: {{ $ngrok_config_file | quote }}
ssh_port: {{ $ngrok_ssh_port | quote }}
cloudflared:
enabled: {{ $use_cloudflared }}
config_file: {{ $cloudflared_config_file | quote }}
config_template: {{ $cloudflared_config_template | quote }}
tunnel_hostname: {{ $cloudflared_tunnel_hostname | quote }}
tunnel_name: {{ $cloudflared_tunnel_name | quote }}
tunnel_uuid: {{ $cloudflared_tunnel_uuid | quote }}
credentials_file: {{ $cloudflared_credentials_file | quote }}
install_packages:
user: {{ $pkgs_user }}
python:
Expand Down
4 changes: 4 additions & 0 deletions chezmoi/.chezmoidata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ packages:
apt: true
brew: true
scope: minimal
- name: cloudflared
script: true
script_cmd: install-cloudflared
scope: optional
- name: code
apt: true
keyring_file: packages.microsoft.gpg
Expand Down
46 changes: 46 additions & 0 deletions chezmoi/Taskfile.dist.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,49 @@ tasks:
- inxi -F

{{- end }}

{{- if .cloudflared.enabled | not | not }}

cloudflared-tunnel-login:
desc: Authenticate with your Cloudflare account
cmds:
- cloudflared tunnel login

cloudflared-tunnel-create:
desc: Create a new Cloudflare Tunnel
cmds:
- cloudflared tunnel create $CLOUDFLARED_TUNNEL_NAME
env:
CLOUDFLARED_TUNNEL_NAME: {{ .cloudflared.tunnel_name }}

cloudflared-tunnel-list:
desc: List Cloudflare Tunnels
cmds:
- cloudflared tunnel list

cloudflared-init-config:
desc: Initialize the Cloudflare Tunnel configuration from the template ({{.cloudflared.config_template }})
cmds:
- cp {{ .cloudflared.config_template }} {{ .cloudflared.config_file }}

cloudflared-create-dns-record:
desc: Create a DNS record for the Cloudflare Tunnel
cmds:
- cloudflared tunnel route dns $CLOUDFLARED_TUNNEL_NAME $CLOUDFLARED_TUNNEL_HOSTNAME
env:
CLOUDFLARED_TUNNEL_NAME: {{ .cloudflared.tunnel_name }}
CLOUDFLARED_TUNNEL_HOSTNAME: {{ .cloudflared.tunnel_hostname }}

cloudflared-tunnel-run:
desc: Run the Cloudflare Tunnel
cmds:
- cloudflared tunnel run $CLOUDFLARED_TUNNEL_NAME
env:
CLOUDFLARED_TUNNEL_NAME: {{ .cloudflared.tunnel_name }}

cloudflared-service-install:
desc: Install the Cloudflare Tunnel as a systemd service (cloudflared should be installed at the system level.)
cmds:
- cloudflared --config {{ .cloudflared.config_file }} service install

{{- end }}
14 changes: 14 additions & 0 deletions chezmoi/dot_cloudflared/config.tmp.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# cloudflared tunnel login
# cloudflared tunnel create <tunnel_name>
# cloudflared tunnel route dns add <tunnel_name> <your_domain>
# cloudflared tunnel run <tunnel_name>
#
tunnel: {{ .cloudflared.tunnel_uuid }}
credentials-file: {{ .cloudflared.credentials_file }}
warp-routing:
enabled: true

ingress:
- hostname: {{ .cloudflared.tunnel_hostname }}
service: http://localhost:8888
- service: http_status:404
6 changes: 6 additions & 0 deletions chezmoi/dot_config/shrc/00-dotfiles-export.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export WORKSPACE_CONTAINER_DIR="{{ .workspace.container_dir }}"
export WORKSPACE_MODEL_DIR="{{ .workspace.model_dir }}"
export WORKSPACE_DATASET_DIR="{{ .workspace.dataset_dir }}"

export DOTFILES_USE_CLOUDFLARED="{{ .cloudflared.enabled }}"
export CLOUDFLARED_CONFIG="{{ .cloudflared.config_file }}"
export CLOUDFLARED_TUNNEL_UUID="{{ .cloudflared.tunnel_uuid }}"
export CLOUDFLARED_TUNNEL_NAME="{{ .cloudflared.tunnel_name }}"
export CLOUDFLARED_TUNNEL_HOSTNAME="{{ .cloudflared.tunnel_hostname }}"

export DOCKER_USERNAME="{{ .docker.username }}"
export CONTAINER_REGISTRY="{{ .docker.container_registry }}"
export DOCKERFILES_SHARE_DIR="{{ .docker.share_dir }}"
Expand Down
44 changes: 44 additions & 0 deletions chezmoi/dot_local/bin/executable_install-cloudflared.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

shopt -s nocaseglob

OUT_FILE=${1:-${HOME}/.local/bin/cloudflared}

# Type
NATIVE_OS=$(uname | tr '[:upper:]' '[:lower:]')
if [[ $NATIVE_OS == *"linux"* ]]; then
OS=linux
elif [[ $NATIVE_OS == *"darwin"* ]]; then
OS=darwin
else
echo "Could not determine OS automatically, please check the release page manually: https://github.com/cloudflare/cloudflared/releases"
exit 1
fi
echo "You are running on ${OS}"

NATIVE_ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
if [[ $NATIVE_ARCH == *"x86_64"* ]]; then
ARCH=amd64
elif [[ $NATIVE_ARCH == *"arm64"* || $NATIVE_ARCH == *"aarch64"* ]]; then
ARCH=arm64
elif [[ $NATIVE_ARCH == *"x86"* ]]; then
ARCH=386
elif [[ $NATIVE_ARCH == *"armv7"* ]]; then
ARCH=arm
else
echo "Could not determine Architecure automatically, please check the release page manually: https://github.com/cloudflare/cloudflared/releases"
exit 1
fi
echo "The architecture is ${ARCH}"

if [[ $OS == "linux" ]]; then
wget wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-${OS}-${ARCH} -O "${OUT_FILE}"
chmod +x "${OUT_FILE}"

echo "Successfully installed cloudflared to ${OUT_FILE}"
elif [[ $OS == "darwin" ]]; then
brew install cloudflare/cloudflare/cloudflared
fi

echo "After installing cloudflared, you will need to login to your Cloudflare account by running the following command: cloudflared tunnel login"
echo "For more details, please visit: https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/install-and-setup/tunnel-guide/local/"

0 comments on commit 80da7ca

Please sign in to comment.