#!/bin/bash
#
# Copyright (c) 2019-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 2 as shown at https://oss.oracle.com/licenses/GPL-2.

whitelist="olcne-nginx.service
keepalived.service
crio.service
kubelet.service
"

if echo "$whitelist" | grep -q -x "$2"; then
	systemctl "$1" "$2"
	if [ "$1" == "stop" -a "$2" == "keepalived.service" ]; then
		# The stop behavior for keepalived is inconsistent.
		# There are cases where the daemon keeps running, but
		# leaves the group, which can result in the VIP residing
		# on multiple nodes.  For obvious reasons, this is bad.
		# Just like the mob, make it sure gets a double tap.
		i=0
		while pkill keepalived; do
			i=$((i+1))
			if [ "$i" -gt 5 ]; then
				pkill -9 keepalived
				break
			fi
			sleep 1
		done
	fi
	exit 0
fi

echo "$1 cannot be manipulated by this script"
exit 1
