#!/bin/sh

set -e
#set -x

if ! [ -r /etc/oci-poc/oci-poc.conf ] ; then
	echo "Cannot read /etc/oci-poc/oci-poc.conf"
fi
. /etc/oci-poc/oci-poc.conf

# Check that we really have NUMBER_OF_GUESTS machines available
# before starting anything
check_enough_vms_available () {
	EXPECTED_NUM_OF_SLAVES=${1}

	NUM_VM=$(ocicli -csv machine-list | q -d , -H "SELECT COUNT(*) AS count FROM -")
	if [ ${NUM_VM} -lt ${EXPECTED_NUM_OF_SLAVES} ] ; then
		echo "Num of VM too low... exiting"
		exit 1
	fi
}

check_enough_vms_available $((${NUMBER_OF_GUESTS} - 1))

echo "===> Setting-up IPMI ports on VMs"
for i in $(seq 2 $((${NUMBER_OF_GUESTS} + 1))) ; do
	VM_SERIAL=$(printf "%X\n" $((0xBF + ${i})))
	VNC_PORT=$((9000 + $i))
	echo "Setting IPMI for VM with serial: $VM_SERIAL and VNC port: $VNC_PORT"
	ocicli machine-set-ipmi ${VM_SERIAL} yes 192.168.100.1 ${VNC_PORT} ipmiusr test
done

echo "===> Creating regions and locations"
ocicli swift-region-create swift-region1
ocicli swift-region-create swift-region2
ocicli swift-region-create swift-region3
ocicli swift-region-create l2
ocicli swift-region-create pub-region

ocicli location-create reg-1-zone-1 swift-region1
ocicli location-create reg-2-zone-1 swift-region2
ocicli location-create reg-3-zone-1 swift-region3
ocicli location-create reg-l2-zone-1 l2
ocicli location-create pub-zone pub-region

echo "===> Creating networks"
ocicli network-create reg1-zone1-net1 192.168.101.0 26 reg-1-zone-1 no
ocicli network-create reg2-zone1-net1 192.168.101.64 26 reg-2-zone-1 no
ocicli network-create reg3-zone1-net1 192.168.101.128 26 reg-3-zone-1 no
# We use the L2 used for booting-up servesr in rack 3...
ocicli network-create reg-l2 192.168.113.0 24 reg-l2-zone-1 no
# ...though to avoid conclifts, we use the last IPs of the range.
ocicli network-set reg-l2 --ip 192.168.113.0 --first-ip 192.168.113.200 --last-ip 192.168.113.220 --vlan 11 --iface1 1g1 --iface2 none

#ocicli network-create br-lb 0.0.0.0 24 reg-1-zone-1 no
ocicli network-create pub-net 192.168.106.1 32 pub-zone yes

# Set the IPMI network
if [ "${USE_AUTOMATIC_IPMI_SETUP}" = "yes" ] ; then
	echo "===> Setting-up automatic IPMI assignation"
	ocicli network-create ipmi 192.168.200.0 24 reg-1-zone-1 no
	ocicli network-set ipmi --role ipmi --ipmi-match-addr 192.168.0.0 --ipmi-match-cidr 16
	ssh ${HOST_NETWORK_PREFIX}.2 "sed -i s/automatic_ipmi_numbering=no/automatic_ipmi_numbering=yes/ /etc/openstack-cluster-installer/openstack-cluster-installer.conf" 1>/dev/null 2>/dev/null
	ssh ${HOST_NETWORK_PREFIX}.2 "mkdir -p /var/www/.ssh" 1>/dev/null 2>/dev/null
	ssh ${HOST_NETWORK_PREFIX}.2 "chown www-data:www-data /var/www/.ssh" 1>/dev/null 2>/dev/null
fi

echo "===> Creating cluster cl1"
ocicli cluster-create cl1 infomaniak.ch

echo "===> Setting BGP-to-host in cl1"
ocicli cluster-set cl1 --bgp-to-the-host yes --asn 65499 --neutron-use-dvr no

echo "===> Setting-up messaging VIP"
ocicli network-create messaging-vip 192.168.106.2 32 pub-zone no
ocicli network-add messaging-vip cl1 vip 1g1 none
ocicli network-set messaging-vip --role vip

echo "===> Adding networks to cl1"
ocicli network-add reg1-zone1-net1 cl1 all 1g1 1g2
ocicli network-add reg2-zone1-net1 cl1 all 1g1 1g2
ocicli network-add reg3-zone1-net1 cl1 all 1g1 1g2
ocicli network-add reg-l2 cl1 all 1g1 none
ocicli network-add pub-net cl1 all ens5 none
ocicli network-set pub-net --role vip
ocicli network-set reg1-zone1-net1 --vlan 10

echo "===> Adding controller nodes to cl1"
# 3x Controller machines (includes Swift proxies)
ocicli machine-add C1 cl1 controller reg-1-zone-1
ocicli machine-add C2 cl1 controller reg-2-zone-1
ocicli machine-add C3 cl1 controller reg-3-zone-1

echo "===> Adding compute nodes to cl1"
# 2x Compute (with Ceph OSD hyperconverged)
ocicli machine-add C4 cl1 compute reg-1-zone-1
ocicli machine-add C5 cl1 compute reg-2-zone-1

ocicli machine-add E1 cl1 compute reg-1-zone-1
ocicli machine-add E2 cl1 compute reg-2-zone-1
ocicli machine-add E3 cl1 compute reg-3-zone-1
ocicli machine-add E4 cl1 compute reg-1-zone-1
ocicli machine-add E5 cl1 compute reg-2-zone-1
ocicli machine-add E6 cl1 compute reg-3-zone-1

echo "===> Setting compute nodes to use Ceph and nova-reserved-host-memory-mb to 8192"
ocicli machine-set C4 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set C5 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192

ocicli machine-set E1 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set E2 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set E3 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set E1 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set E2 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192
ocicli machine-set E3 --use_ceph_if_available yes --nova-reserved-host-memory-mb 8192

#echo "===> Addig Ceph OSD feature to compute nodes"
#ocicli machine-set C4 --compute-is-cephosd yes
#ocicli machine-set C5 --compute-is-cephosd yes

echo "===> Adding network nodes to cl1"
# 2x Network
ocicli machine-add C6 cl1 network reg-l2-zone-1
ocicli machine-add C7 cl1 network reg-l2-zone-1

echo "===> Disabling BGP-to-host on network nodes, and adding DRAgent"
ocicli machine-set C6 --force-no-bgp2host yes --neutron-install-dragent yes
ocicli machine-set C7 --force-no-bgp2host yes --neutron-install-dragent yes

echo "===> Adding swiftproxy nodes to cl1"
# 1x Swift proxy
ocicli machine-add C8 cl1 swiftproxy reg-2-zone-1

echo "===> Adding a tempest node to cl1"
# 1x tempest
ocicli machine-add C9 cl1 tempest reg-3-zone-1

echo "===> Adding DNS nodes"
ocicli machine-add CA cl1 dns reg-2-zone-1
ocicli machine-add CB cl1 dns reg-3-zone-1


echo "===> Adding cephmon nodes to cl1"
# 3x Ceph Mon
ocicli machine-add CC cl1 cephmon reg-1-zone-1
ocicli machine-add CD cl1 cephmon reg-2-zone-1
ocicli machine-add CE cl1 cephmon reg-3-zone-1

echo "===> Adding swiftstore nodes to cl1"
# 5x Swift Store
ocicli machine-add CF cl1 swiftstore reg-1-zone-1
ocicli machine-add D0 cl1 swiftstore reg-2-zone-1
ocicli machine-add D1 cl1 swiftstore reg-3-zone-1

echo "===> Adding volume nodes to cl1"
# 2x Volume
ocicli machine-add D2 cl1 volume reg-1-zone-1
ocicli machine-add D3 cl1 volume reg-2-zone-1
ocicli machine-add D4 cl1 volume reg-3-zone-1

echo "===> Adding messaging nodes to cl1"
# 3x Messaging
ocicli machine-add D5 cl1 messaging reg-1-zone-1
ocicli machine-add D6 cl1 messaging reg-2-zone-1
ocicli machine-add D7 cl1 messaging reg-3-zone-1

echo "===> Adding billmon nodes"
ocicli machine-add D8 cl1 billmon reg-1-zone-1
ocicli machine-add D9 cl1 billmon reg-2-zone-1
ocicli machine-add DA cl1 billmon reg-3-zone-1

echo "===> Adding billosd nodes"
ocicli machine-add DB cl1 billosd reg-1-zone-1
ocicli machine-add DC cl1 billosd reg-2-zone-1
ocicli machine-add DD cl1 billosd reg-3-zone-1

echo "===> Adding cephosd nodes"
ocicli machine-add DE cl1 cephosd reg-1-zone-1
ocicli machine-add DF cl1 cephosd reg-2-zone-1
ocicli machine-add E0 cl1 cephosd reg-3-zone-1

echo "===> Reduce the (default) number of fragment in swift"
ocicli cluster-set cl1 --swift-ec-num-data-fragments 6 --swift-ec-num-parity-fragments 2 --swift-ec-enable no --install-designate yes --disable-notifications yes --glance-prefer-ceph-over-swift no

echo "===> Setting-up cl1-swiftproxy-1.infomaniak.ch as swift-proxy-hostname"
ocicli cluster-set cl1 --swift-proxy-hostname cl1-swiftproxy-1.infomaniak.ch --swift-public-cloud-middlewares yes

if [ "${USE_IKVSWITCH}" = yes ] ; then
	echo "===> Setting-up BGP and VLAN"
	ocicli cluster-set cl1 --bgp-to-the-host yes
	ocicli network-set reg1-zone1-net1 --vlan ${BGP_TO_HOST_VLAN}
	ocicli network-set reg2-zone1-net1 --vlan ${BGP_TO_HOST_VLAN}
	ocicli network-set reg3-zone1-net1 --vlan ${BGP_TO_HOST_VLAN}
fi

echo "===> Calculating swift ring"
# Calculate ring
ocicli swift-calculate-ring cl1 100 100 100

exit 0
