summaryrefslogtreecommitdiff
path: root/colocation-client.in
blob: 0226682e832a5604dd33db6f207808e146905fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash

configuration_file="${1:-#ETC#/colocation-client.conf}"

jq -c '{
  colocation_server,
  machine: .machines[]
} | {
  url: (
    .colocation_server +
    "update.php" +
    (
      "?machine=" +
      .machine.machine +
      "&key=" +
      .machine.key
    )
  ),
  test: (
    .machine.values |
    to_entries |
    .[] |
    {
      state: .key,
      command: (.value.command // "true"),
      result: (.value.result // "exitcode")
    }
  )
} | {
  url: ( .url + "&" + .test.state + "=" ),
  command: .test.command,
  result: .test.result
}' \
<"${configuration_file}" \
| sed '
  s@^{"url":"\([^"]\+\)","command":"\([^"]\+\)","result":"\([^"]\+\)"}$@\1\n\2\n\3@
  t
  d
' \
| while read -r url && read -r command && read -r result_strategy; do
  case "${result_strategy}" in
    'exitcode')
      ${command} 2>/dev/null >/dev/null
      result=$?
    ;;
    'stderr')
      result=$(${command} >/dev/null 2>&1)
    ;;
    'stdout')
      result=$(${command} 2>/dev/null)
    ;;
    *)
      >&2 printf 'unknown result strategy "%s"\n' "${result_strategy}"
      exit 1
    ;;
  esac
  if printf '%s\n' "${result}" | grep -vxF '0' | grep -qvxF '1'; then
    >&2 printf 'result must be 0 or 1, but is "%s" for command "%s" with result strategy "%s"\n' "${result}" "${command}" "${result_strategy}"
    exit 1
  fi
  curl -Ss "${url}${result}"
done