diff --git a/jobs/socs/dcusr-listmonk.hcl b/jobs/socs/dcusr-listmonk.hcl deleted file mode 100644 index 3f9ea7b..0000000 --- a/jobs/socs/dcusr-listmonk.hcl +++ /dev/null @@ -1,105 +0,0 @@ -job "dcusr-listmonk" { - datacenters = ["aperture"] - type = "service" - - meta { - domain = "lists.solarracing.ie" - } - - group "listmonk" { - network { - port "http" { - } - - port "db" { - to = 5432 - } - } - - service { - name = "listmonk" - port = "http" - - check { - type = "http" - path = "/" - interval = "10s" - timeout = "2s" - } - - tags = [ - "traefik.enable=true", - "traefik.port=${NOMAD_PORT_http}", - "traefik.http.routers.dcusr-listmonk.entrypoints=web,websecure", - "traefik.http.routers.dcusr-listmonk.rule=Host(`${NOMAD_META_domain}`)", - "traefik.http.routers.dcusr-listmonk.tls=true", - "traefik.http.routers.dcusr-listmonk.tls.certresolver=lets-encrypt", - ] - } - - task "app" { - driver = "docker" - - config { - image = "listmonk/listmonk:latest" - ports = ["http"] - - entrypoint = ["./listmonk", "--static-dir=/listmonk/static"] - - volumes = [ - "/storage/nomad/dcusr-listmonk/static:/listmonk/static", - "/storage/nomad/dcusr-listmonk/postgres/:/var/lib/postgresql/data", - "local/config.toml:/listmonk/config.toml" - ] - } - - resources { - cpu = 1000 - memory = 500 - } - - template { - data = < /tmp/op_backup_response.json || { + echo "Failed to POST backup creation" + exit 1 + } + +status_path="$(jq -r ._links.job_status.href < /tmp/op_backup_response.json 2>/dev/null || true)" +if [ -z "${status_path}" ] || [ "${status_path}" = "null" ]; then + echo "Could not obtain job_status link from API response" + cat /tmp/op_backup_response.json || true + exit 1 +fi + +status_url="https://${OPENPROJECT_DOMAIN}${status_path}" + +while true; do + curl -s -u "apikey:${OPENPROJECT_API_KEY}" "${status_url}" > /tmp/op_status.json || { + echo "Failed to fetch status from ${status_url}" + sleep 5 + continue + } + + status="$(jq -r .status < /tmp/op_status.json 2>/dev/null || true)" + if [ "${status}" = "success" ]; then + echo "Backup ready for download" + break + fi + + if [ "${status}" = "failed" ] || [ "${status}" = "error" ]; then + echo "Backup job failed (status=${status})" + cat /tmp/op_status.json || true + if [ -n "${DISCORD_WEBHOOK}" ]; then + curl -s -H "Content-Type: application/json" -d \ + "{\"content\": \"<@&585512338728419341> \`OpenProject\` backup for **${job_name}** has **FAILED**. Status: ${status}. Date: $(TZ=Europe/Dublin date)\"}" \ + "${DISCORD_WEBHOOK}" || true + fi + exit 2 + fi + + echo "waiting for backup generation (current status=${status})" + sleep 5 +done + +download_path="$(jq -r .payload.download < /tmp/op_status.json 2>/dev/null || true)" +if [ -z "${download_path}" ] || [ "${download_path}" = "null" ]; then + echo "No download path found in status payload" + exit 1 +fi + +download_url="https://${OPENPROJECT_DOMAIN}${download_path}" + +curl -s -u "apikey:${OPENPROJECT_API_KEY}" -L "${download_url}" -o "${outfile}" || { + echo "Failed to download backup to ${outfile}" + [ -f "${outfile}" ] && rm -f "${outfile}" + exit 1 +} + +if [ -s "${outfile}" ]; then + echo "Backup successful: ${outfile}" +else + echo "Downloaded file is empty or missing: ${outfile}" + rm -f "${outfile}" || true + if [ -n "${DISCORD_WEBHOOK}" ]; then + curl -s -H "Content-Type: application/json" -d \ + "{\"content\": \"<@&585512338728419341> \`OpenProject\` backup for **${job_name}** has just **FAILED** (empty file). File: ${outfile} Date: $(TZ=Europe/Dublin date)\"}" \ + "${DISCORD_WEBHOOK}" || true + fi + exit 1 +fi + +find "${BACKUP_BASE}" -name 'openproject-*.zip' -ctime +3 -exec rm {} \; || true + +exit 0 +EOH + } + + template { + destination = "local/consul_env" + env = true + data = <