#!/bin/bash
# Script that checks whether bms-network-config was running successfully
# If so, it disables the network config in cloud-init, as this creates a
# conflict otherwise on SLES12 where it renames eth0 to bond0 ...
# Reference: osTicket #579132
# (c) Kurt Garloff <kurt.garloff@t-systems.com>, 9/2017
# License: CC-BY-SA 3.0

BMSCLOUDCFG=${BMSCLOUDCFG:-/etc/cloud/cloud.cfg.d/10_bms_netdisable.cfg}
BMSCLOUDDS=${BMSCLOUDDS:-/etc/cloud/cloud.cfg.d/11_bms_configdrivedisable.cfg}

disable_netcfg()
{
	echo "Disabling network.config for cloud-init $BL"
	cat >$BMSCLOUDCFG <<EOT
## File autogenerated by bms-disable-cloudinit-network
## Your changes will be overwritten!
network: {config: disabled}
EOT
	echo "Disabling ConfigDrive DataSource for cloud-init"
	cat >$BMSCLOUDDS <<EOT
## File autogenerated by bms-disable-cloudinit-network
## Your changes will be overwritten!
# Remove ConfigDrive from OTC standard DataSource list
datasource_list: [ NoCloud, OpenStack, Ec2, OpenNebula, Azure, AltCloud, OVF, MAAS, GCE, CloudSigma, CloudStack, None ]
EOT
	echo "Disabling cloud-init-local"
	systemctl disable cloud-init-local.service
}

enable_netcfg()
{
	echo "Enabling network.config for cloud-init"
	cat >$BMSCLOUDCFG <<EOT
## File autogenerated by bms-disable-cloudinit-network
## Your changes will be overwritten!
EOT
	echo "Enabling ConfigDrive DataSource for cloud-init"
	cat >$BMSCLOUDDS <<EOT
## File autogenerated by bms-disable-cloudinit-network
## Your changes will be overwritten!
# Remove ConfigDrive from OTC standard DataSource list
#datasource_list: [ NoCloud, OpenStack, ConfigDrive, Ec2, OpenNebula, Azure, AltCloud, OVF, MAAS, GCE, CloudSigma, CloudStack, None ]
EOT
	echo "Enabling cloud-init-local"
	systemctl enable cloud-init-local.service
}

is_disabled()
{
	grep '^[^#].*config: disabled[ }]*$' $BMSCLOUDCFG >/dev/null 2>&1
}


detect_bms()
{
	if test -n "$DEBUG"; then return $DEBUG; fi
	# We don't have network yet, so we can't just ask the metadata server
	# Instead check for network_data on a ConfigDrive
	#file -s /dev/sdb1 | grep cloud >/dev/null 2>&1
	test -e /dev/disk/by-label/config-2 || return 1
	# This needs more sophisitcation:
	# A customer could attach a config drive himself, so this is no safe
	# indication of a BMS instance yet.
	# We could check for the meta_data.json on the ConfigDrive to be sure
	# or check the return code from bms-network-config
	# NOTE: We have disabled the network data src retrieval in bms-network-config
	# so checking the exit state is relatively safe.
	#systemctl status bms-network-config >/dev/null
	# The better way is to check for config-2 drive network config
	mkdir -p /mnt/config-2
	mount -o ro /dev/disk/by-label/config-2 /mnt/config-2
	BL=$(grep 'bond_links' /mnt/config-2/openstack/latest/network_data.json 2>/dev/null | sed 's/^.*bond_links": \([^]]*\]\).*/\1/')
	ERR=${PIPESTATUS[0]}
	umount /mnt/config-2
	# Only disable normal cloud-init-local network setup IF eth0 is enslaved
	if test $ERR == 0 && [[ "$BL" = *eth0* ]]; then return 0; fi
	return 1
}

# main
if detect_bms; then
	if ! is_disabled; then disable_netcfg; fi
else
	if is_disabled; then enable_netcfg; fi
fi
