iacconfDemo2026/awx/uptime_app.yaml

159 lines
6.4 KiB
YAML
Raw Normal View History

2026-09-22 00:03:45 +00:00
---
- name: Deploy Uptime Kuma on GCP and Configure Non-Interactively
2026-09-22 01:12:06 +00:00
hosts: all
2026-09-22 00:03:45 +00:00
vars:
# GCP Configuration
gcp_project: "your-gcp-project-id"
gcp_region: "us-central1"
2026-10-06 15:13:41 +00:00
service_name: "uptime-kuma"
ar_repo_name: "dockerhub"
data_bucket_name: "{{ gcp_project }}-uptime-kuma-data"
2026-09-22 00:03:45 +00:00
# Uptime Kuma Credentials & Target
kuma_admin_user: "admin"
kuma_admin_pass: "ChangeMeImmediately123!"
target_monitor_name: "Target Web App"
target_monitor_url: "https://example.com"
tasks:
# -------------------------------------------------------------
# 1. Local Prerequisites
# -------------------------------------------------------------
- name: Ensure uptime-kuma-api Python library is installed locally
2026-10-06 14:05:43 +00:00
ansible.builtin.shell:
executable: /bin/bash
cmd: |
apt update
apt install python3-pip -y
python3 -m pip install --user uptime-kuma-api --break-system-packages
python3 -m pip install uptime-kuma-api --break-system-packages
register: pip_install
changed_when: "'Successfully installed' in pip_install.stdout"
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
# 2. Enable APIs, Storage Bucket and Docker Hub Mirror
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
- name: Enable required GCP APIs, create data bucket and Docker Hub remote repo
2026-10-06 14:05:43 +00:00
ansible.builtin.shell:
executable: /bin/bash
cmd: |
2026-10-06 14:50:27 +00:00
if [ -f '/home/builder/google-cloud-sdk/path.bash.inc' ]; then . '/home/builder/google-cloud-sdk/path.bash.inc'; fi
2026-10-06 15:13:41 +00:00
set -e
gcloud services enable run.googleapis.com artifactregistry.googleapis.com storage.googleapis.com \
2026-10-06 14:22:54 +00:00
--project={{ gcp_project }}
2026-09-22 00:03:45 +00:00
2026-10-06 15:13:41 +00:00
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"
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
# 3. Deploy to Cloud Run (idempotent: deploy updates in place)
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
# Kuma uses SQLite and websockets: one always-on instance, CPU always allocated.
- name: Deploy Uptime Kuma to Cloud Run
2026-10-06 14:05:43 +00:00
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
2026-10-06 15:13:41 +00:00
gcloud run deploy {{ service_name }} \
2026-10-06 14:22:54 +00:00
--project={{ gcp_project }} \
2026-10-06 15:13:41 +00:00
--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
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
# 4. Fetch Service URL and Wait for Readiness
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
- name: Fetch Cloud Run service URL
2026-10-06 14:05:43 +00:00
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
2026-10-06 15:13:41 +00:00
gcloud run services describe {{ service_name }} \
--region={{ gcp_region }} \
2026-10-06 14:22:54 +00:00
--project={{ gcp_project }} \
2026-10-06 15:13:41 +00:00
--format="value(status.url)"
register: kuma_url
2026-09-22 00:03:45 +00:00
changed_when: false
- name: Display Uptime Kuma URL
ansible.builtin.debug:
2026-10-06 15:13:41 +00:00
msg: "Uptime Kuma is running at: {{ kuma_url.stdout.strip() }}"
2026-09-22 00:03:45 +00:00
- name: Wait for Uptime Kuma to respond with HTTP 200
ansible.builtin.uri:
2026-10-06 15:13:41 +00:00
url: "{{ kuma_url.stdout.strip() }}"
2026-09-22 00:03:45 +00:00
status_code: 200
register: readiness
until: readiness.status == 200
retries: 30
delay: 6
# -------------------------------------------------------------
2026-10-06 15:13:41 +00:00
# 5. Non-Interactive Configuration via API
2026-09-22 00:03:45 +00:00
# -------------------------------------------------------------
- name: Perform headless setup and add monitor
2026-10-06 14:05:43 +00:00
ansible.builtin.shell:
cmd: |
2026-09-22 00:03:45 +00:00
python3 -c "
import sys
from uptime_kuma_api import UptimeKumaApi, MonitorType
2026-10-06 15:13:41 +00:00
api = UptimeKumaApi('{{ kuma_url.stdout.strip() }}')
2026-09-22 00:03:45 +00:00
# 1. Initial admin account creation if not yet initialized
if api.need_setup():
api.setup('{{ kuma_admin_user }}', '{{ kuma_admin_pass }}')
print('ADMIN_CREATED')
# 2. Login
api.login('{{ kuma_admin_user }}', '{{ kuma_admin_pass }}')
# 3. Add monitor idempotently
current_monitors = [m['name'] for m in api.get_monitors()]
if '{{ target_monitor_name }}' not in current_monitors:
api.add_monitor(
type=MonitorType.HTTP,
name='{{ target_monitor_name }}',
url='{{ target_monitor_url }}',
interval=60
)
print('MONITOR_ADDED')
else:
print('MONITOR_ALREADY_EXISTS')
api.disconnect()
"
2026-10-06 14:05:43 +00:00
executable: /bin/bash
2026-09-22 00:03:45 +00:00
register: setup_output
changed_when: "'MONITOR_ADDED' in setup_output.stdout"
- name: Show setup summary
ansible.builtin.debug:
msg:
- "Setup Status: {{ setup_output.stdout.strip() }}"
2026-10-06 15:13:41 +00:00
- "Access UI at: {{ kuma_url.stdout.strip() }}"
2026-09-22 00:03:45 +00:00
- "Username: {{ kuma_admin_user }}"