summaryrefslogtreecommitdiff
path: root/roles/storage/zfs/syncoid/templates/syncoid_finalize.j2
blob: 05423880ff48d66ee79b314dc98370abb52e6aa7 (plain) (blame)
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
63
64
65
66
67
68
69
#!/bin/bash
{% if zfs_syncoid_autosuspend is defined %}

echo "running sanoid --prune to cleanup old snapshots"
sleep 5
systemctl start --wait sanoid-prune.service

echo "wait 30s (autosuspend cooldown period)"
sleep 30
echo "done."
{% endif %}

if [ -z "$RUNTIME_DIRECTORY" ]; then
  exit 0
fi

ret=0
for line in $(cat "$RUNTIME_DIRECTORY/exit_codes"); do
  if [[ $line =~ [^[:digit:]] ]]; then
    ret=-1
    echo "invalid exit_code from pull script: $line"
  else
    ret=$line
  fi
  if [ $ret -ne 0 ]; then
    break
  fi
done

now=$(date +"%s")
starttime=$(cat "$RUNTIME_DIRECTORY/start")
duration=0
if [[ $starttime =~ [^[:digit:]] ]]; then
  ret=-1
  echo "invalid starttime from pull script: $starttime"
else
  duration=$(( now - starttime ))
fi

cat <<EOF >> "$RUNTIME_DIRECTORY/metrics"
syncoid_pull_run{backup_server="{{ inventory_hostname }}"} $now
syncoid_pull_duration_seconds{backup_server="{{ inventory_hostname }}"} $duration
EOF

if [ "$#" -eq 0 ]; then
  echo "not transferring prometheus metrics."
  exit $ret
fi


if [ -z "$1" ] || [ -z "$2" ] || [ "$1" == "--" ] || [ "$2" == "--" ]; then
  echo "$0 <host> <textfile-collector-path> [ -- <additional ssh arguments .. > ]"
  exit -2
fi
report_to=$1
textfile_collector_path=$2
shift 2
if [ -n "$1" ]; then
  if [ "$1" != "--" ]; then
    echo "additional arguments must be separated using '--'"
    exit -2
  fi
  shift
fi

echo "transferring prometheus metrics to: $report_to:$textfile_collector_path"
cat "$RUNTIME_DIRECTORY/metrics" | ssh -o "UserKnownHostsFile=/var/lib/syncoid/ssh.knownhosts" -o "HashKnownHosts=no" -i "/var/lib/syncoid/id_ssh_ed25519" "$@" "$report_to" "cat > '$textfile_collector_path/syncoid-pull-{{ inventory_hostname }}.prom'"

exit $ret