From e2db3b7d282b14c57a65383f1e45c318246de5ef Mon Sep 17 00:00:00 2001 From: Isaac Johnson Date: Tue, 6 Oct 2026 10:13:41 -0500 Subject: [PATCH] change to cloud run --- awx/uptime_app.yaml | 174 +++++++++++++++----------------------------- 1 file changed, 59 insertions(+), 115 deletions(-) diff --git a/awx/uptime_app.yaml b/awx/uptime_app.yaml index 2bc91f7..abe3257 100644 --- a/awx/uptime_app.yaml +++ b/awx/uptime_app.yaml @@ -6,11 +6,9 @@ # GCP Configuration gcp_project: "your-gcp-project-id" gcp_region: "us-central1" - gcp_zone: "us-central1-a" - instance_name: "uptime-kuma-vm" - machine_type: "e2-small" # 2GB RAM is plenty for Uptime Kuma - static_ip_name: "uptime-kuma-ip" - firewall_rule_name: "allow-uptime-kuma-3001" + service_name: "uptime-kuma" + ar_repo_name: "dockerhub" + data_bucket_name: "{{ gcp_project }}-uptime-kuma-data" # Uptime Kuma Credentials & Target kuma_admin_user: "admin" @@ -34,132 +32,79 @@ changed_when: "'Successfully installed' in pip_install.stdout" # ------------------------------------------------------------- - # 2. Allocate Static Public IP (Idempotent) + # 2. Enable APIs, Storage Bucket and Docker Hub Mirror # ------------------------------------------------------------- - - name: Check if static IP exists + - name: Enable required GCP APIs, create data bucket and Docker Hub remote repo ansible.builtin.shell: executable: /bin/bash cmd: | - # The next line updates PATH for the Google Cloud SDK. if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute addresses describe {{ static_ip_name }} \ - --region={{ gcp_region }} \ + set -e + gcloud services enable run.googleapis.com artifactregistry.googleapis.com storage.googleapis.com \ --project={{ gcp_project }} - register: ip_check - failed_when: false - changed_when: false - - name: Allocate static external IP in GCP + if ! gcloud storage buckets describe gs://{{ data_bucket_name }} --project={{ gcp_project }} >/dev/null 2>&1; then + gcloud storage buckets create gs://{{ data_bucket_name }} \ + --project={{ gcp_project }} --location={{ gcp_region }} + echo BUCKET_CREATED + fi + + if ! gcloud artifacts repositories describe {{ ar_repo_name }} \ + --location={{ gcp_region }} --project={{ gcp_project }} >/dev/null 2>&1; then + gcloud artifacts repositories create {{ ar_repo_name }} \ + --repository-format=docker --mode=remote-repository \ + --remote-docker-repo=DOCKER-HUB \ + --location={{ gcp_region }} --project={{ gcp_project }} + echo REPO_CREATED + fi + register: prereq_output + changed_when: "'CREATED' in prereq_output.stdout" + + # ------------------------------------------------------------- + # 3. Deploy to Cloud Run (idempotent: deploy updates in place) + # ------------------------------------------------------------- + # Kuma uses SQLite and websockets: one always-on instance, CPU always allocated. + - name: Deploy Uptime Kuma to Cloud Run ansible.builtin.shell: executable: /bin/bash cmd: | - # The next line updates PATH for the Google Cloud SDK. if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute addresses create {{ static_ip_name }} \ + gcloud run deploy {{ service_name }} \ + --project={{ gcp_project }} \ + --region={{ gcp_region }} \ + --image={{ gcp_region }}-docker.pkg.dev/{{ gcp_project }}/{{ ar_repo_name }}/louislam/uptime-kuma:1 \ + --port=3001 \ + --cpu=1 --memory=1Gi \ + --min-instances=1 --max-instances=1 \ + --no-cpu-throttling \ + --execution-environment=gen2 \ + --allow-unauthenticated \ + --add-volume=name=kuma-data,type=cloud-storage,bucket={{ data_bucket_name }} \ + --add-volume-mount=volume=kuma-data,mount-path=/app/data + register: deploy_output + + # ------------------------------------------------------------- + # 4. Fetch Service URL and Wait for Readiness + # ------------------------------------------------------------- + - name: Fetch Cloud Run service URL + ansible.builtin.shell: + executable: /bin/bash + cmd: | + if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi + gcloud run services describe {{ service_name }} \ --region={{ gcp_region }} \ --project={{ gcp_project }} \ - --description="Static IP for Uptime Kuma" - when: ip_check.rc != 0 - - # ------------------------------------------------------------- - # 3. Create Firewall Rule (Port 3001) - # ------------------------------------------------------------- - - name: Check if firewall rule exists - ansible.builtin.shell: - executable: /bin/bash - cmd: | - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute firewall-rules describe {{ firewall_rule_name }} \ - --project={{ gcp_project }} - register: fw_check - failed_when: false - changed_when: false - - - name: Allow TCP traffic to port 3001 - ansible.builtin.shell: - executable: /bin/bash - cmd: | - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute firewall-rules create {{ firewall_rule_name }} \ - --project={{ gcp_project }} \ - --allow=tcp:3001 \ - --target-tags=uptime-kuma \ - --source-ranges=0.0.0.0/0 \ - --description="Allow traffic to Uptime Kuma UI & API" - when: fw_check.rc != 0 - - # ------------------------------------------------------------- - # 4. Provision VM with Container-Optimized OS - # ------------------------------------------------------------- - - name: Check if instance already exists - ansible.builtin.shell: - executable: /bin/bash - cmd: | - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute instances describe {{ instance_name }} \ - --zone={{ gcp_zone }} \ - --project={{ gcp_project }} - register: instance_check - failed_when: false - changed_when: false - - - name: Launch Compute Engine VM with Uptime Kuma container - ansible.builtin.shell: - executable: /bin/bash - cmd: | - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute instances create-with-container {{ instance_name }} \ - --project={{ gcp_project }} \ - --zone={{ gcp_zone }} \ - --machine-type={{ machine_type }} \ - --tags=uptime-kuma \ - --address={{ static_ip_name }} \ - --boot-disk-size=20GB \ - --container-image=louislam/uptime-kuma:1 \ - --container-mount-host-path=host-path=/var/uptime-kuma,mount-path=/app/data,mode=rw - when: instance_check.rc != 0 - - # ------------------------------------------------------------- - # 5. Fetch Public IP and Wait for Service Readiness - # ------------------------------------------------------------- - - name: Fetch instance public IP address - ansible.builtin.shell: - cmd: | - # The next line updates PATH for the Google Cloud SDK. - if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi - # The next line enables shell command completion for gcloud. - if [ -f '/home/builder/google-cloud-sdk/completion.bash.inc' ]; then . '/home/builder/google-cloud-sdk/completion.bash.inc'; fi - gcloud compute instances describe {{ instance_name }} \ - --zone={{ gcp_zone }} \ - --project={{ gcp_project }} \ - --format="value(networkInterfaces[0].accessConfigs[0].natIP)" - executable: /bin/bash - register: vm_ip + --format="value(status.url)" + register: kuma_url changed_when: false - name: Display Uptime Kuma URL ansible.builtin.debug: - msg: "Uptime Kuma is booting at: http://{{ vm_ip.stdout.strip() }}:3001" + msg: "Uptime Kuma is running at: {{ kuma_url.stdout.strip() }}" - name: Wait for Uptime Kuma to respond with HTTP 200 ansible.builtin.uri: - url: "http://{{ vm_ip.stdout.strip() }}:3001" + url: "{{ kuma_url.stdout.strip() }}" status_code: 200 register: readiness until: readiness.status == 200 @@ -167,7 +112,7 @@ delay: 6 # ------------------------------------------------------------- - # 6. Non-Interactive Configuration via API + # 5. Non-Interactive Configuration via API # ------------------------------------------------------------- - name: Perform headless setup and add monitor ansible.builtin.shell: @@ -176,8 +121,7 @@ import sys from uptime_kuma_api import UptimeKumaApi, MonitorType - kuma_url = 'http://{{ vm_ip.stdout.strip() }}:3001' - api = UptimeKumaApi(kuma_url) + api = UptimeKumaApi('{{ kuma_url.stdout.strip() }}') # 1. Initial admin account creation if not yet initialized if api.need_setup(): @@ -210,5 +154,5 @@ ansible.builtin.debug: msg: - "Setup Status: {{ setup_output.stdout.strip() }}" - - "Access UI at: http://{{ vm_ip.stdout.strip() }}:3001" + - "Access UI at: {{ kuma_url.stdout.strip() }}" - "Username: {{ kuma_admin_user }}"