Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
75 changes: 75 additions & 0 deletions files/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/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 = / { print $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.
if [[ "${DISABLED}" -ne 1 ]]; then
[[ "${JOB_STATUS}" != "LOADED" ]] && fail
[[ "${JOB_SWITCH}" == "DISABLED" ]] && fail
[[ "${LAST_EXIT_CODE}" != "0" ]] && fail
fi
11 changes: 0 additions & 11 deletions tasks/check.yml

This file was deleted.

25 changes: 25 additions & 0 deletions tasks/consul.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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