#!/usr/bin/bash
# Reboot local node.

. /usr/share/shaman/functions

verify_caller

# sleep period to let other services write their log contents
SLEEP_DURATION=10
# cluster connection timeout
TIMEOUT=3


node_id="$(cat /etc/vstorage/host_id)"
if [ -n "$NODE_ADDRESS" ]; then
	node_id="$node_id ($NODE_ADDRESS)"
fi

# log message
msg="forcefully rebooting node $node_id in $SLEEP_DURATION seconds"

logger -t "shaman" -i "$msg"

# if kvstore cluster name is provided, then post cluster event
if [ -n "$CLUSTER_NAME" ]; then
	# all messages from stderr will end up in shaman-monitor.log
	vstorage -c "$CLUSTER_NAME" --timeout "$TIMEOUT" put-event --warn --sub=tools "shaman: $msg"
fi

# make rsyslogd reopen all its files, effectively flushing file buffers
systemctl kill --signal=HUP rsyslog.service

# sync all relevant log files' contents onto disk
for l in /var/log/messages /var/log/shaman/shaman.log /var/log/shaman/shaman-monitor.log; do
	[ -f "$l" ] && sync -d "$l" 2>/dev/null
done

# and sleep for a while to let other services write their log contents
sleep "$SLEEP_DURATION"

echo b > /proc/sysrq-trigger

exit 1
