diff --git a/defaults/main.yml b/defaults/main.yml index ee91697..d4e6e65 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -31,8 +31,6 @@ launchd_timer_consul_service_id: '{{ launchd_timer_consul_service_name }}' launchd_timer_consul_check_path: '/usr/local/bin/check-timer-status.sh' launchd_timer_consul_address: '{{ ansible_local.wireguard.vpn_ip }}' launchd_timer_consul_warning: false - -# Tags launchd_timer_consul_extra_tags: [] launchd_timer_consul_default_tags: ['timer'] launchd_timer_consul_tags: | diff --git a/files/check.sh b/files/check.sh new file mode 100644 index 0000000..ee25c0b --- /dev/null +++ b/files/check.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# vim: ft=bash +set -uo pipefail + +NAME="" +DOMAIN="system" +WARNING=0 +DISABLED=0 + +# Parse CLI flags +while (($#)); do + case "$1" in + --name=*) NAME="${1#*=}" ;; + --user) DOMAIN="user/$(id -u)" ;; + --disabled) DISABLED=1 ;; + --warning) WARNING=1 ;; + -*|--*) echo "ERROR unknown option: $1"; exit 1;; + esac + shift +done + +[[ -z "${NAME}" ]] && { echo "No job name provided!"; exit 1; } + +if [[ -z "${DOMAIN}" ]]; then + if [[ "$(id -u)" -eq 0 ]]; then + DOMAIN="system" + else + DOMAIN="gui/$(id -u)" + fi +fi + +TARGET="${DOMAIN}/${NAME}" + +JOB_STATUS="MISSING" +JOB_SWITCH="UNKNOWN" +JOB_HEALTH="UNKNOWN" +LAST_EXIT_CODE="UNKNOWN" + +if JOB_OUT="$(launchctl print "${TARGET}" 2>/dev/null)"; then + JOB_STATUS="LOADED" + + JOB_HEALTH=$( + awk -F'= ' ' + /^[[:space:]]*state = / { gsub(/ /, "_", $2); print toupper($2); found=1; exit } + END { if (!found) print "unknown" } + ' <<< "${JOB_OUT}" + ) + + LAST_EXIT_CODE=$( + awk -F'= ' ' + /^[[:space:]]*last exit code = / { print $2; found=1; exit } + END { if (!found) print "0" } + ' <<< "${JOB_OUT}" + ) +fi + +DISABLED_OUT="$(launchctl print-disabled "${DOMAIN}" 2>/dev/null || true)" + +if grep -Eq "\"${NAME}\"[[:space:]]*=>[[:space:]]*disabled" <<< "${DISABLED_OUT}"; then + JOB_SWITCH="DISABLED" +else + JOB_SWITCH="ENABLED" +fi + +# Print current state. +echo "${NAME}: ${JOB_STATUS}, ${JOB_SWITCH}, ${JOB_HEALTH}, last_exit_code=${LAST_EXIT_CODE}" + +function fail { [[ "${WARNING}" == 1 ]] && exit 1 || exit 2; } + +# Verify health. +[[ "${DISABLED}" -eq 1 ]] && exit 0 + +[[ "${JOB_STATUS}" != "LOADED" ]] && fail +[[ "${JOB_SWITCH}" == "DISABLED" ]] && fail +[[ "${LAST_EXIT_CODE}" == "(never exited)" ]] && exit 0 +[[ "${LAST_EXIT_CODE}" != "0" ]] && fail diff --git a/tasks/check.yml b/tasks/check.yml deleted file mode 100644 index 56648a8..0000000 --- a/tasks/check.yml +++ /dev/null @@ -1,11 +0,0 @@ ---- -- name: Create Consul service definition - include_role: name=infra-role-consul-service - vars: - consul_config_name: '{{ launchd_timer_consul_service_id }}' - consul_services: - - id: '{{ launchd_timer_consul_service_id }}' - name: '{{ launchd_timer_consul_service_name }}' - tags: '{{ launchd_timer_consul_tags }}' - meta: '{{ launchd_timer_consul_meta }}' - address: '{{ launchd_timer_consul_address }}' diff --git a/tasks/consul.yml b/tasks/consul.yml new file mode 100644 index 0000000..824e813 --- /dev/null +++ b/tasks/consul.yml @@ -0,0 +1,25 @@ +--- +- name: '{{ launchd_timer_name }} Create check script' + copy: + src: 'check.sh' + dest: '{{ launchd_timer_consul_check_path }}' + mode: 0755 + +- name: Create Consul service definition + include_role: name=infra-role-consul-service + vars: + consul_config_name: '{{ launchd_timer_consul_service_id }}' + consul_services: + - id: '{{ launchd_timer_consul_service_id }}' + name: '{{ launchd_timer_consul_service_name }}' + tags: '{{ launchd_timer_consul_tags }}' + meta: '{{ launchd_timer_consul_meta }}' + address: '{{ launchd_timer_consul_address }}' + checks: + - name: '{{ beacon_node_consul_service_name }}-rest-health' + type: 'script' + script: >- + {{ launchd_timer_consul_check_path }} + {%- if launchd_timer_consul_warning %} --warning{% endif %} + {%- if not launchd_timer_enabled %} --disabled{% endif %} + --name={{ launchd_timer_name }} diff --git a/tasks/main.yml b/tasks/main.yml index 7dbf5d7..66220b4 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,4 +2,4 @@ - include_tasks: script.yml when: launchd_timer_script_content is not none - include_tasks: timer.yml -- include_tasks: check.yml +- include_tasks: consul.yml