#!/bin/bash
# Reboot local node.

. /usr/share/shaman/functions

verify_caller

# sleep period to let other services write their log contents
sleep_duration=10

# log message
msg="forcefully rebooting node $(cat /etc/vstorage/host_id) in $sleep_duration seconds"
# cluster connection timeout
timeout=3

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

# if kvstore cluster name is provided, then post cluster event
if [ "$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
echo -ne \
"import os\n"\
"for l in ('/var/log/messages', '/var/log/shaman/shaman.log', '/var/log/shaman/shaman-monitor.log'):\n"\
"  try:\n"\
"    with open(l, 'r') as f:\n"\
"      os.fdatasync(f.fileno())\n"\
"  except:\n"\
"    pass\n" | python

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

echo b > /proc/sysrq-trigger

exit 1
