diff --git a/.gitignore b/.gitignore index a096df7..cfe21bc 100644 --- a/.gitignore +++ b/.gitignore @@ -69,4 +69,5 @@ build/ .env go.work -go.work.sum \ No newline at end of file +go.work.sum +ngrok.url diff --git a/Tiltfile b/Tiltfile index e946a66..c9df878 100644 --- a/Tiltfile +++ b/Tiltfile @@ -13,6 +13,7 @@ dotenv() config.define_bool("enable_gitlab") config.define_bool("enable_github") config.define_bool("live_debug") +config.define_string("ngrok_fqdn") cfg = config.parse() allow_k8s_contexts([ @@ -30,8 +31,13 @@ k8s_resource( ) k8s_context=k8s_context() +# ///////////////////////////////////////////////////////////////////////////// +# N G R O K +# ///////////////////////////////////////////////////////////////////////////// + # Load NGROK Tiltfile -load('./localdev/ngrok/Tiltfile', 'get_ngrok_url') +load('./localdev/ngrok/Tiltfile', 'deploy_ngrok', 'get_ngrok_url') +deploy_ngrok(cfg) def checkEnvSet(key): if not os.getenv(key): @@ -43,7 +49,7 @@ def checkEnvSet(key): checkEnvSet("TFC_ORGANIZATION") checkEnvSet("TFC_TOKEN") -ngrok_url=get_ngrok_url() +ngrok_url=get_ngrok_url(cfg) org=str(os.getenv('TFC_ORGANIZATION')) tfcOutputs=local_terraform_resource( @@ -58,7 +64,7 @@ tfcOutputs=local_terraform_resource( 'localdev/terraform/*.tf', ], labels=["tfc"], - resource_deps=['wait-ngrok-url'] + resource_deps=[] ) if tfcOutputs: @@ -90,7 +96,6 @@ if cfg.get('enable_gitlab'): ], resource_deps=[ 'tf-tfc', - 'wait-ngrok-url', ], labels=['gitlab'] ) @@ -117,7 +122,6 @@ if cfg.get('enable_github'): ], resource_deps=[ 'tf-tfc', - 'wait-ngrok-url', ], labels=['github'] ) diff --git a/localdev/ngrok/Tiltfile b/localdev/ngrok/Tiltfile index aeaa870..2616467 100644 --- a/localdev/ngrok/Tiltfile +++ b/localdev/ngrok/Tiltfile @@ -1,48 +1,38 @@ -load('ext://secret', 'secret_from_dict') - # ///////////////////////////////////////////////////////////////////////////// # N G R O K # ///////////////////////////////////////////////////////////////////////////// -def get_ngrok_url(): - if config.tilt_subcommand == 'down': +check_cmd='/usr/bin/curl -v http://localhost:4040/api/tunnels | jq --raw-output ".tunnels[0].public_url"' + +def get_ngrok_url(cfg): + if cfg.get("ngrok_fqdn"): + return "https://"+cfg.get("ngrok_fqdn") + + elif config.tilt_subcommand == 'down': + local('rm ngrok.url || true') return 'http://xyz.ngrok.io' + else: - return str(local( - 'curl -s http://localhost:4040/api/tunnels \ - | jq --raw-output ".tunnels[0].public_url"'.format( - k8s_namespace, - k8s_context - ), - )).rstrip('\n') - -# Read Auth token for ngrok (assumes ngrok has been installed and configured) -ngrok_config_file = str(local('ngrok config check', quiet=True)).lstrip('Valid configuration file at ').rstrip('\n') -ngrok_config = read_yaml(ngrok_config_file) -k8s_yaml( - secret_from_dict("ngrok-config", inputs = { - 'NGROK_AUTHTOKEN' : ngrok_config['authtoken'], - }) -) -k8s_resource( - objects=['ngrok-config:secret'], - labels=["ngrok"], - new_name='ngrok-config', - resource_deps=['k8s:namespace'] -) - -# Deploy ngrok proxy -k8s_yaml( - kustomize('./') -) -k8s_resource( - 'ngrok', - resource_deps=['ngrok-config', 'k8s:namespace'], - labels=["ngrok"], - port_forwards=4040, -) -k8s_resource( - 'wait-ngrok-url', - resource_deps=['k8s:namespace', 'ngrok'], - labels=["ngrok"] -) + url=str(read_file('ngrok.url', "")).rstrip('\n') + + return url + +def deploy_ngrok(cfg): + + hostnameArg = "" + if cfg.get("ngrok_fqdn"): + hostnameArg = " --hostname {}".format(cfg.get("ngrok_fqdn")) + + local_resource( + "ngrok", + serve_cmd="ngrok http localhost:8080 {}".format(hostnameArg), + readiness_probe=probe( + initial_delay_secs=5, + period_secs=10, + exec=exec_action(["bash", "-c", 'localdev/ngrok/update-url.sh']) + ), + labels=["ngrok"], + links=["http://localhost:4040"] + ) + + watch_file('./ngrok.url') diff --git a/localdev/ngrok/deploy.yaml b/localdev/ngrok/deploy.yaml deleted file mode 100644 index 9ebdceb..0000000 --- a/localdev/ngrok/deploy.yaml +++ /dev/null @@ -1,31 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: ngrok -spec: - selector: - matchLabels: - app: ngrok - replicas: 1 - strategy: - type: Recreate - template: - metadata: - labels: - app: ngrok - spec: - containers: - - name: ngrok - image: ngrok/ngrok - imagePullPolicy: IfNotPresent - args: ["http", "tfbuddy:8080"] - ports: - - containerPort: 4040 - envFrom: - - secretRef: - name: ngrok-config - readinessProbe: - httpGet: - port: 4040 - path: /api/tunnels - diff --git a/localdev/ngrok/job.yaml b/localdev/ngrok/job.yaml deleted file mode 100644 index 85f66f1..0000000 --- a/localdev/ngrok/job.yaml +++ /dev/null @@ -1,16 +0,0 @@ -apiVersion: batch/v1 -kind: Job -metadata: - name: wait-ngrok-url -spec: - template: - spec: - containers: - - name: curl - image: registry.gitlab.com/gitlab-ci-utils/curl-jq:latest - command: [ - "/bin/bash", "-c", - "curl http://ngrok:4040/api/tunnels | jq --raw-output \".tunnels[0].public_url\" | grep ngrok.io" - ] - restartPolicy: OnFailure - backoffLimit: 10 diff --git a/localdev/ngrok/kustomization.yaml b/localdev/ngrok/kustomization.yaml deleted file mode 100644 index 1ce3fa1..0000000 --- a/localdev/ngrok/kustomization.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - -resources: - - deploy.yaml - - service.yaml - - job.yaml diff --git a/localdev/ngrok/service.yaml b/localdev/ngrok/service.yaml deleted file mode 100644 index 9fc2e3e..0000000 --- a/localdev/ngrok/service.yaml +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: ngrok -spec: - type: LoadBalancer - ports: - - name: http - port: 4040 - targetPort: 4040 - protocol: TCP - selector: - app: ngrok \ No newline at end of file diff --git a/localdev/ngrok/update-url.sh b/localdev/ngrok/update-url.sh new file mode 100755 index 0000000..f79f0c0 --- /dev/null +++ b/localdev/ngrok/update-url.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -eu + +url=$(curl -s http://localhost:4040/api/tunnels | jq --raw-output ".tunnels[0].public_url") + +if grep -q $url ngrok.url; then + echo "URL already set" +else + echo "Updating ngrok.url" + echo -n $url > ngrok.url +fi \ No newline at end of file