#!/bin/sh

set -e

if [ -r /usr/share/openstack-cluster-installer/variables.json ] ; then
	VARIABLE_FILE=/usr/share/openstack-cluster-installer/variables.json
elif [ -r /etc/ocicli/variables.json ] ; then
	VARIABLE_FILE=/etc/ocicli/variables.json
fi

if [ -z "${OCI_API_URL}" ] ; then
	echo "Please add: export OCI_API_URL=\"http(s)://<url-or-IP-of-your-oci-server>/oci/api.php?\""
	echo "to your .bashrc in order to use "$(basename $0)
	echo "You may as well want to set OCI_LOGIN and OCI_PASS to be authenticated."
	exit 1
fi

usage (){
	echo $(basename $0)": configure your baremetal cluster through the OCI API."
	echo ""
	echo $(basename $0)" <action>"
	echo ""
	echo "Actions are:"
	echo "  machine-list"
	echo "  machine-show <machine_serial>"
	echo "  machine-set <machine_serial> --use_ceph_if_available <yes/no> --install-on-raid <yes/no> --raid-type <0/1/10/5> --raid-dev0 sda --raid-dev1 sdb --raid-dev2 sdc --raid-dev3 sdd --serial-console-device <ttyS0/ttyS1/none> --dc --row --rack --ustart --uend --use-gpu --gpu-name --gpu-vendor-id --gpu-produc-id --vfio-ids --cpu-mode <host-model/host-passthrough/custom/cluster_value> --cpu-model <MODEL> --cpu-model-extra-flags <FLAG1,FLAG2> --nested-virt <yes/no> --swift-store-account <yes/no> --swift-store-container <yes/no> --swift-store-object <yes/no> --hostname <hostname> --servers-per-port <int> --disk-chunk-size <int> --network-chunk-size <int>"
	echo "  machine-add <machine_serial> <cluster_name> <role_name> <location_name>: add a machine to a cluster"
	echo "  machine-remove <machine_serial>: remove a machine from a cluster"
	echo "  machine-destroy <machine_serial>: completely remove a machine from OCI's db"
	echo "  machine-display-install-cmd <machine_serial>: display install command (without running it)"
	echo "  machine-reboot-on-hdd <machine_serial>"
	echo "  machine-reboot-on-live <machine_serial>"
	echo "  machine-ipmi-reboot-on-hdd <machine_serial>"
	echo "  machine-ipmi-reboot-on-live <machine_serial>"
	echo "  machine-install-os <machine_serial>"
	echo "  machine-install-log <machine_serial>"
	echo "  machine-set-ipmi [--do-ipmitool-cmds] [--ipmi-defgw GATEWAY] [--ipmi-netmask NETMASK] [--ipmi-call-chassis-bootdev yes/no] <serial> <use-ipmi:yes/no> <ipmi-ip-addr> <ipmi-port> <ipmi-username> <ipmi-password>"
	echo "  machine-console <machine_serial>"
	echo "  machine-ip-list <machine_serial>"
	echo "  machine-ip-add <machine_serial> <network-name> <ip-address>"
	echo "  machine-ip-remove <machine_serial> <ip-address>"
	echo "  ipmi-assign-check"
	echo ""
	echo "  network-list"
	echo "  network-set <network-name> [--cidr MASK] [--iface1 IFDEFINITION] [--iface2 IFDEFINITION] [--ip IPv4] [--is-public yes/no] [--location location-name] [--mtu MTU] [--role ROLE] [--vlan VLAN_NUM] [--bridge-name NAME] [--ipmi-match-addr <IP-ADDR>] [--ipmi-match-cidr <CIDR>]"
	echo "  network-create <network-name> <ip> <cidr_mask> <location-name> <is-public:yes/no>"
	echo "  network-delete <network-name>"
	echo "  network-add <network-name> <cluster_name> <role_name> <iface1> <iface2>"
	echo "  network-remove <network-name>"
	echo ""
	echo "  location-list"
	echo "  location-create <location-name> <swiftregion-name>"
	echo "  location-delete <location-name>"
	echo ""
	echo "  swift-region-list"
	echo "  swift-region-create <swiftregion-name>"
	echo "  swift-region-delete <swiftregion-name>"
	echo ""
	echo "  swift-calculate-ring <cluster-name>"
	echo ""
	echo "  cluster-list"
	echo "  cluster-create <cluster-name> <domaine-name.com>"
	echo "  cluster-delete <cluster-name>"
	echo "  cluster-show <cluster-name>"
	echo "  cluster-set <cluster-name> [--time-server-host <time-server-hostname>] [--vip-hostname <hostname>] [--swift-part-power <int>] [--swift-proxy-hostname <hostname>] [--swift-encryption-key-id <UUID>] [--swift-disable-encryption <yes/no>] [--amp-secgroup-list <SECGROUP-UUID-LIST>] [ --amp-boot-network-list <AMP_BOOT_NETWORK_LIST>] [--disable-notifications <yes/no>] [--enable-monitoring-graphs <yes/no>] [--monitoring-graphite-host <hostname>] [--monitoring-graphite-port <port>] [--statsd-hostname <hostname>] [--extswift-use-external <yes/no>] [--extswift-auth-url <https://api.example.com/identity/v3>] [--extswift-proxy-url <https://prx.example.com/object/v1/AUTH_>] [--extswift-project-name <myproj>] [--extswift-project-domain-name <default>] [--extswift-user-name <myuser>] [--extswift-user-domain-name <default>] [--extswift-password <password>] [--self-signed-api-cert <yes/no>] [--use-ovs-ifaces <yes/no>] [--cpu-mode <host-model/host-passthrough/custom>] [--cpu-model <MODEL>] [--cpu-model-extra-flags <FLAG1,FLAG2>] [--nested-virt <yes/no>]"
	echo "  cluster-show-networks <cluster-name>"
	echo "  cluster-show-machines <cluster-name>"
	echo "  cluster-show-ips <cluster-name>"
	echo "  cluster-install <cluster-name>"
	echo ""
	echo "  role-list"
	echo "  role-create <name>"
	echo "  role-delete <name>"
	exit 1
}

# Parameter: $1 = URL param
# return: API_RESPONSE_FILE
# The function exits printing API message if status != success
ocicli_call (){
	local STATUS MESSAGE URL_PARAM
	if [ -z "${1}" ] ; then
		echo "Missing URL_PARAM when calling ocicli_call()"
		exit 1
	fi
	URL_PARAM="${1}"

	if [ -n "${OCI_LOGIN}" ] && [ -n "${OCI_PASS}" ] ; then
		URL_PARAM="${URL_PARAM}&oci_login=${OCI_LOGIN}&oci_pass=${OCI_PASS}"
	fi

	API_RESPONSE_FILE=$(mktemp -t oci-poc-install-cluster.XXXXXX)
	if [ "${DEBUG}" = "on" ] ; then
		echo "curl -s \"${OCI_API_URL}${URL_PARAM}\""
	fi
	curl -s ${OCI_API_URL}${URL_PARAM} >${API_RESPONSE_FILE}
	STATUS=$(cat ${API_RESPONSE_FILE} | jq -r '.["status"]')
	if ! [ "${STATUS}" = "success" ] ; then
		echo "Could not query API:"
		MESSAGE=$(cat ${API_RESPONSE_FILE} | jq -r '.["message"]')
		echo $MESSAGE
		rm ${API_RESPONSE_FILE}
		exit 1
	fi
}

ocicli_machine_list (){
	ocicli_call "action=machine_list"
	if [ "${1}" = "-a" ] ; then
		echo "serial,Cur_ip,dhcp_ip,memory,status,puppet,lastseen,cluster,ladvd,product_name,BIOSver,IPMIver,IPMI_IP,hostname,notes,dc,row,rack,ustart,uend"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.product_name,.serial)|.["data"][] | "\(.serial),\(.ipaddr),\(.dhcp_ip),\(.memory),\(.status),\(.puppet_status),\(.lastseen),\(.cluster),\(.ladvd_report),\(.product_name),\(.bios_version),\(.ipmi_firmware_version),\(.ipmi_detected_ip),\(.hostname),\(.notes),\(.loc_dc),\(.loc_row),\(.loc_rack),\(.loc_u_start),\(.loc_u_end)"'
	else
		echo "serial,Cur_ip,status,puppet,cluster,IPMI_IP,hostname"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.product_name,.serial)|.["data"][] | "\(.serial),\(.ipaddr),\(.status),\(.puppet_status),\(.cluster),\(.ipmi_detected_ip),\(.hostname)"'
	fi
}

ocicli_machine_show (){
	local ROLES OPTION DISPLAY role DESCR SIZE_MB SIZE_GB network_name SPEED MACADDR H_SPEED MACADDR DISPLAY_SPEED CNT
	ocicli_call "action=machine_show&machine_serial=${1}"
#	echo "Attribute,value"
	# Normal attributes
	OCICLI_MACHINE_SHOW_SERIAL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.serial)"')
	OCICLI_MACHINE_SHOW_IPADDR=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipaddr)"')
	OCICLI_MACHINE_SHOW_MEMORY=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.memory)"')
	OCICLI_MACHINE_SHOW_STATUS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.status)"')
	OCICLI_MACHINE_SHOW_CLUSTER_ID=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cluster)"')
	OCICLI_MACHINE_SHOW_PUPPET=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.puppet_status)"')
	OCICLI_MACHINE_SHOW_LASTSEEN=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.lastseen)"')
	OCICLI_MACHINE_SHOW_ROLE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.role)"')
	OCICLI_MACHINE_SHOW_LADVD=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ladvd_report)"')
	OCICLI_MACHINE_SHOW_PRODUCT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.product_name)"')
	OCICLI_MACHINE_SHOW_BIOS_VER=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.bios_version)"')
	OCICLI_MACHINE_SHOW_LADVD_REPORT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ladvd_report)"')

	# Location attributes
	OCICLI_MACHINE_SHOW_LOC_DC=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.loc_dc)"')
	OCICLI_MACHINE_SHOW_LOC_ROW=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.loc_row)"')
	OCICLI_MACHINE_SHOW_LOC_RACK=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.loc_rack)"')
	OCICLI_MACHINE_SHOW_LOC_USTART=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.loc_u_start)"')
	OCICLI_MACHINE_SHOW_LOC_UEND=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.loc_u_end)"')
	OCICLI_MACHINE_SHOW_NOTES=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.notes)"')


	OCICLI_MACHINE_SHOW_IPMI_VER=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_firmware_version)"')
	OCICLI_MACHINE_SHOW_IPMI_DETECTED_IP=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_detected_ip)"')
	OCICLI_MACHINE_SHOW_USE_IPMI=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_use)"')
	OCICLI_MACHINE_SHOW_CALL_IPMI_BOOTDEV=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_call_chassis_bootdev)"')
	OCICLI_MACHINE_SHOW_IPMI_ADDR=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_addr)"')
	OCICLI_MACHINE_SHOW_IPMI_PORT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_port)"')
	OCICLI_MACHINE_SHOW_IPMI_USERNAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_username)"')
	OCICLI_MACHINE_SHOW_IPMI_PASSWORD=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.ipmi_password)"')

	OCICLI_MACHINE_SHOW_HOSTNAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.hostname)"')
	OCICLI_MACHINE_SHOW_NOTES=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.notes)"')
	OCICLI_MACHINE_SHOW_USE_CEPH=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.use_ceph_if_available)"')
	OCICLI_MACHINE_SHOW_NESTED_VIRT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.nested_virt)"')

	OCICLI_MACHINE_SHOW_INSTALL_ON_RAID=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.install_on_raid)"')
	OCICLI_MACHINE_SHOW_RAID_TYPE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.raid_type)"')
	OCICLI_MACHINE_SHOW_RAID_DEV0=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.raid_dev0)"')
	OCICLI_MACHINE_SHOW_RAID_DEV1=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.raid_dev1)"')
	OCICLI_MACHINE_SHOW_RAID_DEV2=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.raid_dev2)"')
	OCICLI_MACHINE_SHOW_RAID_DEV3=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.raid_dev3)"')

	OCICLI_MACHINE_SHOW_SERIAL_CONSOLE_DEV=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.serial_console_dev)"')

	OCICLI_MACHINE_SHOW_USE_GPU=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.use_gpu)"')
	OCICLI_MACHINE_SHOW_GPU_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.gpu_name)"')
	OCICLI_MACHINE_SHOW_GPU_VENDOR_ID=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.gpu_vendor_id)"')
	OCICLI_MACHINE_SHOW_GPU_PRODUCT_ID=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.gpu_product_id)"')
	OCICLI_MACHINE_SHOW_VFIO_IDS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.vfio_ids)"')

	# From cluster
	OCICLI_MACHINE_SHOW_CLUSTER_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.machine_cluster.name)"')

	OCICLI_MACHINE_SHOW_CPU_MODE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_mode)"')
	OCICLI_MACHINE_SHOW_CPU_MODEL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_model)"')
	OCICLI_MACHINE_SHOW_CPU_MODEL_EXTRA_FLAGS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_model_extra_flags)"')
	OCICLI_MACHINE_SHOW_FORCE_DHCP_AGENT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.force_dhcp_agent)"')

	OCICLI_MACHINE_SHOW_SWIFT_STORE_ACCOUNT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_store_account)"')
	OCICLI_MACHINE_SHOW_SWIFT_STORE_CONTAINER=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_store_container)"')
	OCICLI_MACHINE_SHOW_SWIFT_STORE_OBJECT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_store_object)"')

	echo "===== MACHINE ID =====,==============================================="
	echo "Serial:,${OCICLI_MACHINE_SHOW_SERIAL}"
	echo "Hostname:,${OCICLI_MACHINE_SHOW_HOSTNAME}"

	echo "====== CLUSTER =======,==============================================="
	echo "Cluster name:,${OCICLI_MACHINE_SHOW_CLUSTER_NAME}"
	echo "Cluster ID:,${OCICLI_MACHINE_SHOW_CLUSTER_ID}"
	echo "Role:,${OCICLI_MACHINE_SHOW_ROLE}"
	echo "Location:,DC:${OCICLI_MACHINE_SHOW_LOC_DC}/Row:${OCICLI_MACHINE_SHOW_LOC_ROW}/Rack:${OCICLI_MACHINE_SHOW_LOC_RACK}/U:${OCICLI_MACHINE_SHOW_LOC_USTART}-${OCICLI_MACHINE_SHOW_LOC_UEND}"
	echo "Notes:,${OCICLI_MACHINE_SHOW_NOTES}"

	echo "=== INSTALL STATUS ===,==============================================="
	echo "Status:,${OCICLI_MACHINE_SHOW_STATUS}"
	echo "Puppet:,${OCICLI_MACHINE_SHOW_PUPPET}"

	echo "=== HARDWARE SPECS ===,==============================================="
	echo "Product name:,${OCICLI_MACHINE_SHOW_PRODUCT}"
	echo "Memory:,${OCICLI_MACHINE_SHOW_MEMORY}"
	echo "BIOS version:,${OCICLI_MACHINE_SHOW_BIOS_VER}"
	echo -n "Block devices:,"
	for i in $(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.machine_blockdevices)"' | jq -r '.[].name') ; do
		SIZE_MB=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]["machine_blockdevices"][] | select(.name == "'${i}'") | .["size_mb"]')
		SIZE_GB=$((${SIZE_MB} / 1024))
		echo -n "${i}:${SIZE_GB}G "
	done
	echo ""
	echo -n "Interfaces:,"
	CNT=0
	for i in $(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.machine_ifs)"' | jq -r '.[].name') ; do
		SPEED=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]["machine_ifs"][] | select(.name == "'${i}'") | .["max_speed"]')
		MACADDR=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]["machine_ifs"][] | select(.name == "'${i}'") | .["macaddr"]')
		if [ "${SPEED}" -lt 1000 ] ; then
			H_SPEED="${SPEED}Mb/s"
		else
			DISPLAY_SPEED=$(( ${SPEED} / 1000 ))
			H_SPEED="${DISPLAY_SPEED}Gb/s"
		fi

		if [ ${CNT} -ge 1 ] ; then
			echo ""
			echo -n " ,"
		fi

		echo -n "${i} (${MACADDR}@${H_SPEED}) "
		CNT=$(( ${CNT} + 1 ))
	done
	echo ""


	echo "==== NETWORKING =====,==============================================="
	echo "LADVD report:,${OCICLI_MACHINE_SHOW_LADVD_REPORT}"
	echo "Last seen:,${OCICLI_MACHINE_SHOW_LASTSEEN}"
	echo "Current IP address:,${OCICLI_MACHINE_SHOW_IPADDR}"
	echo -n "IPs:,"
	CNT=1
	for i in $(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.machine_ips)"' | jq -r '.[].ipaddr') ; do
		network_name=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.machine_ips)"' | jq -r '.[].networkname' | head -n ${CNT} | tail -n 1)
		echo -n "${i} (${network_name}) "
		CNT=$(( ${CNT} + 1 ))
	done
	echo ""

	echo "======= IPMI ========,==============================================="
	echo "IPMI firmware:,${OCICLI_MACHINE_SHOW_IPMI_VER}"
	echo "Detected IPMI IP:,${OCICLI_MACHINE_SHOW_IPMI_DETECTED_IP}"
	echo "Use IPMI:,${OCICLI_MACHINE_SHOW_USE_IPMI}"
	echo "Call IPMI bootdev:,${OCICLI_MACHINE_SHOW_CALL_IPMI_BOOTDEV}"
	echo "IPMI addr:,${OCICLI_MACHINE_SHOW_IPMI_ADDR}",
	echo "IPMI port:,${OCICLI_MACHINE_SHOW_IPMI_PORT}",
	echo "IPMI username:,${OCICLI_MACHINE_SHOW_IPMI_USERNAME}"
	echo "IPMI password:,${OCICLI_MACHINE_SHOW_IPMI_PASSWORD}"

	echo "======= RAID ========,==============================================="
	echo "Install on RAID:,${OCICLI_MACHINE_SHOW_INSTALL_ON_RAID}"
	if [ "${OCICLI_MACHINE_SHOW_INSTALL_ON_RAID}" = "yes" ] ; then
		echo "Raid type:,${OCICLI_MACHINE_SHOW_RAID_TYPE}"
		echo "Raid device 0:,${OCICLI_MACHINE_SHOW_RAID_DEV0}"
		echo "Raid device 1:,${OCICLI_MACHINE_SHOW_RAID_DEV1}"
		case ${OCICLI_MACHINE_SHOW_RAID_TYPE} in
		0|1)
			echo -n ""
		;;
		5|10)
			echo "Raid device 2:,${OCICLI_MACHINE_SHOW_RAID_DEV2}"
			echo "Raid device 3:,${OCICLI_MACHINE_SHOW_RAID_DEV3}"
		;;
		esac
	fi

	echo "====== CONSOLE ======,==============================================="
	echo "Serial console device:,${OCICLI_MACHINE_SHOW_SERIAL_CONSOLE_DEV}"

	if [ "${OCICLI_MACHINE_SHOW_ROLE}" = "compute" ] || [ "${OCICLI_MACHINE_SHOW_ROLE}" = "swiftstore" ] || [ "${OCICLI_MACHINE_SHOW_ROLE}" = "swiftproxy" ] ; then
		echo "=== ROLE-SPECIFIC ===,==============================================="
	fi
	if [ "${OCICLI_MACHINE_SHOW_ROLE}" = "compute" ] ; then
		echo "Use Ceph if available:,${OCICLI_MACHINE_SHOW_USE_CEPH}"
		echo "Use GPU:,${OCICLI_MACHINE_SHOW_USE_GPU}"
		echo "GPU name:,${OCICLI_MACHINE_SHOW_GPU_NAME}"
		echo "GPU vendor ID:,${OCICLI_MACHINE_SHOW_GPU_VENDOR_ID}"
		echo "GPU product ID:,${OCICLI_MACHINE_SHOW_GPU_PRODUCT_ID}"
		echo "VFIO id list:,${OCICLI_MACHINE_SHOW_VFIO_IDS}"
		echo "Nested virtualization:,${OCICLI_MACHINE_SHOW_NESTED_VIRT}"

		echo "CPU mode:,${OCICLI_MACHINE_SHOW_CPU_MODE}"
		echo "CPU model:,${OCICLI_MACHINE_SHOW_CPU_MODEL}"
		echo "CPU model extra flags:,${OCICLI_MACHINE_SHOW_CPU_MODEL_EXTRA_FLAGS}"
		echo "Force DHCP agent deployment:,${OCICLI_MACHINE_SHOW_FORCE_DHCP_AGENT}"
	fi

	if [ "${OCICLI_MACHINE_SHOW_ROLE}" = "swiftstore" ] || [ "${OCICLI_MACHINE_SHOW_ROLE}" = "swiftproxy" ] ; then
		echo "Swift store account:,${OCICLI_MACHINE_SHOW_SWIFT_STORE_ACCOUNT}"
		echo "Swift store container:,${OCICLI_MACHINE_SHOW_SWIFT_STORE_CONTAINER}"
		echo "Swift store object:,${OCICLI_MACHINE_SHOW_SWIFT_STORE_OBJECT}"
	fi
	if [ -r ${VARIABLE_FILE} ] ; then
		for OPTION in $(cat ${VARIABLE_FILE} | jq -r '.machines | keys[]' | tr '\n' ' ') ; do
			VALUE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.'${OPTION}')"')
			ROLES=$(cat ${VARIABLE_FILE} | jq -r '.machines.server_per_port.roles[]' | tr '\n' ' ')
			DISPLAY=no
			if [ "${ROLES}" = "all" ] ; then
				DISPLAY=yes
			else
				for role in ${ROLES} ; do
					if [ "${OCICLI_MACHINE_SHOW_ROLE}" = "${role}" ] ; then
						DISPLAY=yes
					fi
				done
			fi
			if [ "${DISPLAY}" = "yes" ] ; then
				DESCR=$(cat ${VARIABLE_FILE} | jq -r ".machines.${OPTION}.description")
				echo "${DESCR},${VALUE}"
			fi
		done
	fi
}

ocicli_machine_set (){
	SERIAL=${1}
	shift
	CALL="action=machine_set&machine_serial=${SERIAL}"
	for i in $@ ; do
		case "${1}" in
		"--use_ceph_if_available")
			CALL="${CALL}&use_ceph=${2}"
			shift
			shift
		;;
		"--install-on-raid")
			CALL="${CALL}&install_on_raid=${2}"
			shift
			shift
		;;
		"--raid-type")
			CALL="${CALL}&raid_type=${2}"
			shift
			shift
		;;
		"--raid-dev0")
			CALL="${CALL}&raid_dev0=${2}"
			shift
			shift
		;;
		"--raid-dev1")
			CALL="${CALL}&raid_dev1=${2}"
			shift
			shift
		;;
		"--raid-dev2")
			CALL="${CALL}&raid_dev2=${2}"
			shift
			shift
		;;
		"--raid-dev3")
			CALL="${CALL}&raid_dev3=${2}"
			shift
			shift
		;;
		"--serial-console-device")
			CALL="${CALL}&serial_console_device=${2}"
			shift
			shift
		;;
		"--dc")
			CALL="${CALL}&loc_dc=${2}"
			shift
			shift
		;;
		"--row")
			CALL="${CALL}&loc_row=${2}"
			shift
			shift
		;;
		"--rack")
			CALL="${CALL}&loc_rack=${2}"
			shift
			shift
		;;
		"--ustart")
			CALL="${CALL}&loc_u_start=${2}"
			shift
			shift
		;;
		"--uend")
			CALL="${CALL}&loc_u_end=${2}"
			shift
			shift
		;;
		"--use-gpu")
			CALL="${CALL}&use_gpu=${2}"
			shift
			shift
		;;
		"--gpu-name")
			CALL="${CALL}&gpu_name=${2}"
			shift
			shift
		;;
		"--gpu-vendor-id")
			CALL="${CALL}&gpu_vendor_id=${2}"
			shift
			shift
		;;
		"--gpu-produc-id")
			CALL="${CALL}&gpu_product_id=${2}"
			shift
			shift
		;;
		"--vfio-ids")
			IDS=$(echo ${2} | tr ',' '+')
			CALL="${CALL}&vfio_ids=${IDS}"
			shift
			shift
		;;
		"--nested-virt")
			CALL="${CALL}&nested_virt=${2}"
			shift
			shift
		;;
		"--cpu-mode")
			if [ -z "${2}" ] ; then
				echo "No parameter for --cpu-mode"
				usage
				exit 1
			fi
			if [ "${2}" != "host-model" ] && [ "${2}" != "host-passthrough" ] && [ "${2}" != "custom" ] && [ "${2}" != "cluster_value" ] ; then
				echo "Wrong parameter for --cpu-mode"
				usage
				exit 1
			fi
			CALL="${CALL}&cpu_mode=${2}"
			shift
			shift
		;;
		"--cpu-model")
			if [ -z "${2}" ] ; then
				echo "No parameter for --cpu-model"
				usage
				exit 1
			fi
			CALL="${CALL}&cpu_model=${2}"
			shift
			shift
		;;
		"--cpu-model-extra-flags")
			if [ -z "${2}" ] ; then
				echo "No parameter for --cpu-model-extra-flags"
				usage
				exit 1
			fi
			CALL="${CALL}&cpu_model_extra_flags=${2}"
			shift
			shift
		;;
		"--force-dhcp-agent")
			if [ -z "${2}" ] ; then
				echo "No parameter for --force-dhcp-agent"
				usage
				exit 1
			fi
			if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
				echo "Wrong parameter for --force-dhcp-agent"
				usage
				exit 1
			fi
			CALL="${CALL}&force_dhcp_agent=${2}"
			shift
			shift
		;;
		"--swift-store-account")
			if [ -z "${2}" ] ; then
				echo "No parameter for --swift-store-account"
				usage
				exit 1
			fi
			if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
				echo "Wrong parameter for --swift-store-account"
				usage
				exit 1
			fi
			CALL="${CALL}&swift_store_account=${2}"
			shift
			shift
		;;
		"--swift-store-container")
			if [ -z "${2}" ] ; then
				echo "No parameter for --swift-store-container"
				usage
				exit 1
			fi
			if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
				echo "Wrong parameter for --swift-store-container"
				usage
				exit 1
			fi
			CALL="${CALL}&swift_store_container=${2}"
			shift
			shift
		;;
		"--swift-store-object")
			if [ -z "${2}" ] ; then
				echo "No parameter for --swift-store-object"
				usage
				exit 1
			fi
			if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
				echo "Wrong parameter for --swift-store-object"
				usage
				exit 1
			fi
			CALL="${CALL}&swift_store_object=${2}"
			shift
			shift
		;;
		"--hostname")
			if [ -z "${2}" ] ; then
				echo "No parameter for --hostname"
				usage
				exit 1
			fi
			CALL="${CALL}&hostname=${2}"
			shift
			shift
		;;
		*)
			if [ -r ${VARIABLE_FILE} ] ; then
				OPTIONS=$(cat ${VARIABLE_FILE} | jq -r '.machines | keys[]' | tr '\n' ' ')
				for OPTION in ${OPTIONS} ; do
					MY_OPTION="--"$(echo ${OPTION} | sed 's/_/-/g')
					if [ "${MY_OPTION}" = "${1}" ] ; then
						CALL="${CALL}&${OPTION}=${2}"
						shift
						shift
						continue
					fi
				done
			else
				echo -n ""
			fi
		;;
		esac
	done
	ocicli_call ${CALL}
	ocicli_display_status
}

ocicli_location_list (){
	ocicli_call "action=location_list"
	echo "id,name,swiftregion"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"][] | "\(.id),\(.name),\(.swiftregion)"'
}


ocicli_network_list (){
	ocicli_call "action=network_list"
	DATA=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]' 2>/dev/null)
	HEAD="name,ip,cidr,is_public,cluster,role,iface1,iface2,bridgename,vlan,mtu,location,ipmi_match_addr,ipmi_match_cidr"
	if [ "${DATA}" = "null" ] ; then
		echo ${HEAD}
	else
		echo ${HEAD}
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.name)|.["data"][] | "\(.name),\(.ip),\(.cidr),\(.is_public),\(.cluster),\(.role),\(.iface1),\(.iface2),\(.bridgename),\(.vlan),\(.mtu),\(.location),\(.ipmi_match_addr),\(.ipmi_match_cidr)"'
	fi
}

ocicli_cluster_list (){
	ocicli_call "action=cluster_list"
	echo "id,name,domain"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.name)|.["data"][] | "\(.id),\(.name),\(.domain)"'
}

ocicli_machine_ip_list (){
	ocicli_call "action=machine_ip_list&serial=${1}"
	echo "networkname,ipaddr"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.networkname)|.["data"][] | "\(.networkname),\(.ipaddr)"'
}

ocicli_cluster_show (){
	ocicli_call "action=cluster_show&name=${1}"
	OCICLI_CLUSTER_SHOW_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.name)"')
	OCICLI_CLUSTER_SHOW_DOMAIN=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.domain)"')
	OCICLI_CLUSTER_SHOW_INITIAL_CLUSTER_SETUP=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.initial_cluster_setup)"')
	OCICLI_CLUSTER_SHOW_VIP_HOSTNAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.vip_hostname)"')
	OCICLI_CLUSTER_SHOW_SWIFT_PART_POWER=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_part_power)"')
	OCICLI_CLUSTER_SHOW_SWIFT_REPLICAS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_replicas)"')
	OCICLI_CLUSTER_SHOW_SWIFT_MIN_PART_HOURS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_min_part_hours)"')
	OCICLI_CLUSTER_SHOW_SWIFT_PROXY_HOSTNAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_proxy_hostname)"')
	OCICLI_CLUSTER_SHOW_SWIFT_ENCRYPTION_KEY=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_encryption_key_id)"')
	OCICLI_CLUSTER_SHOW_SWIFT_DISABLE_ENCRYPTION=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_disable_encryption)"')
	OCICLI_CLUSTER_SHOW_SWIFT_OBJECT_REPLICATOR_CONCURRENCY=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_object_replicator_concurrency)"')
	OCICLI_CLUSTER_SHOW_SWIFT_RSYNC_CONNECTION_LIMIT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.swift_rsync_connection_limit)"')

	OCICLI_CLUSTER_SHOW_EXTSWIFT_USE_EXTERNAL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_use_external)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_AUTH_URL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_auth_url)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_PROXY_URL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_proxy_url)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_PROJECT_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_project_name)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_PROJECT_DOMAIN_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_project_domain_name)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_USER_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_user_name)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_USER_DOMAIN_NAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_user_domain_name)"')
	OCICLI_CLUSTER_SHOW_EXTSWIFT_PASSWORD=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.extswift_password)"')

	OCICLI_CLUSTER_SHOW_HAPROXY_CUSTOM_URL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.haproxy_custom_url)"')
	OCICLI_CLUSTER_SHOW_STATSD_HOSTNAME=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.statsd_hostname)"')
	OCICLI_CLUSTER_SHOW_TIME_SERVER_HOST=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.time_server_host)"')
	OCICLI_CLUSTER_SHOW_AMP_SECGROUP_LIST=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.amp_secgroup_list)"')
	OCICLI_CLUSTER_SHOW_AMP_BOOT_NETWORK_LIST=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.amp_boot_network_list)"')
	OCICLI_CLUSTER_SHOW_DISABLE_NOTIFICATIONS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.disable_notifications)"')
	OCICLI_CLUSTER_SHOW_ENABLE_MONITORING_GRAPHS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.enable_monitoring_graphs)"')
	OCICLI_CLUSTER_SHOW_MONITORING_GRAPHITE_HOST=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.monitoring_graphite_host)"')
	OCICLI_CLUSTER_SHOW_MONITORING_GRAPHITE_PORT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.monitoring_graphite_port)"')
	OCICLI_CLUSTER_SHOW_SELF_SIGNED_API_CERT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.self_signed_api_cert)"')
	OCICLI_CLUSTER_SHOW_NESTED_VIRT=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.nested_virt)"')
	OCICLI_CLUSTER_SHOW_USE_OVS_IFACES=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.use_ovs_ifaces)"')

	OCICLI_CLUSTER_SHOW_CPU_MODE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_mode)"')
	OCICLI_CLUSTER_SHOW_CPU_MODEL=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_model)"')
	OCICLI_CLUSTER_SHOW_CPU_MODEL_EXTRA_FLAGS=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.cpu_model_extra_flags)"')

	echo "Name:,${OCICLI_CLUSTER_SHOW_NAME}"
	echo "Domain:,${OCICLI_CLUSTER_SHOW_DOMAIN}"
	echo "Initial cluster setup:,${OCICLI_CLUSTER_SHOW_INITIAL_CLUSTER_SETUP}"
	echo "VIP Hostname:,${OCICLI_CLUSTER_SHOW_VIP_HOSTNAME}"
	echo "Swift part power:,${OCICLI_CLUSTER_SHOW_SWIFT_PART_POWER}"
	echo "Swift replicas:,${OCICLI_CLUSTER_SHOW_SWIFT_REPLICAS}"
	echo "Swift min part hours:,${OCICLI_CLUSTER_SHOW_SWIFT_MIN_PART_HOURS}"
	echo "Swift proxy hostname:,${OCICLI_CLUSTER_SHOW_SWIFT_PROXY_HOSTNAME}"
	echo "Swift encryption key uuid:,${OCICLI_CLUSTER_SHOW_SWIFT_ENCRYPTION_KEY}"
	echo "Swift disable encryption:,${OCICLI_CLUSTER_SHOW_SWIFT_DISABLE_ENCRYPTION}"
	echo "Swift object replicator concurrency:,${OCICLI_CLUSTER_SHOW_SWIFT_OBJECT_REPLICATOR_CONCURRENCY}"
	echo "Swift rsync connection limit:,${OCICLI_CLUSTER_SHOW_SWIFT_RSYNC_CONNECTION_LIMIT}"
	echo "Swift Account + Container store on proxies:,${OCICLI_CLUSTER_SHOW_SWIFT_AC_ON_PROXY}"

	echo "External swift use:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_USE_EXTERNAL}"
	echo "External swift auth URL:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_AUTH_URL}"
	echo "External swift proxy URL:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_PROXY_URL}"
	echo "External swift project name:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_PROJECT_NAME}"
	echo "External swift project domain name:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_PROJECT_DOMAIN_NAME}"
	echo "External swift user name:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_USER_NAME}"
	echo "External swift user domain name:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_USER_DOMAIN_NAME}"
	echo "External swift password:,${OCICLI_CLUSTER_SHOW_EXTSWIFT_PASSWORD}"

	echo "Haproxy custom url:,${OCICLI_CLUSTER_SHOW_HAPROXY_CUSTOM_URL}"
	echo "Statsd hostname:,${OCICLI_CLUSTER_SHOW_STATSD_HOSTNAME}"
	echo "Time server host:,${OCICLI_CLUSTER_SHOW_TIME_SERVER_HOST}"
	echo "amp_secgroup_list:,${OCICLI_CLUSTER_SHOW_AMP_SECGROUP_LIST}"
	echo "amp_boot_network_list:,${OCICLI_CLUSTER_SHOW_AMP_BOOT_NETWORK_LIST}"
	echo "disable_notifications:,${OCICLI_CLUSTER_SHOW_DISABLE_NOTIFICATIONS}"
	echo "enable_monitoring_graphs:,${OCICLI_CLUSTER_SHOW_ENABLE_MONITORING_GRAPHS}"
	echo "monitoring_graphite_host:,${OCICLI_CLUSTER_SHOW_MONITORING_GRAPHITE_HOST}"
	echo "monitoring_graphite_port:,${OCICLI_CLUSTER_SHOW_MONITORING_GRAPHITE_PORT}"
	echo "Self signed API cert:,${OCICLI_CLUSTER_SHOW_SELF_SIGNED_API_CERT}"
	echo "Nested virtualization:,${OCICLI_CLUSTER_SHOW_NESTED_VIRT}"
	echo "Use OpenVSwitch /etc/network/interfaces:,${OCICLI_CLUSTER_SHOW_USE_OVS_IFACES}"

	echo "CPU mode:,${OCICLI_CLUSTER_SHOW_CPU_MODE}"
	echo "CPU model:,${OCICLI_CLUSTER_SHOW_CPU_MODEL}"
	echo "CPU model extra flags:,${OCICLI_CLUSTER_SHOW_CPU_MODEL_EXTRA_FLAGS}"


	if [ -r ${VARIABLE_FILE} ] ; then
		for OPTION in $(cat ${VARIABLE_FILE} | jq -r '.clusters | keys[]' | tr '\n' ' ') ; do
			VALUE=$(cat ${API_RESPONSE_FILE} | jq -r '.["data"]| "\(.'${OPTION}')"')
			DESCR=$(cat ${VARIABLE_FILE} | jq -r ".clusters.${OPTION}.description")
			echo "${DESCR},${VALUE}"
		done
	fi

}

ocicli_swiftregion_list () {
	ocicli_call "action=swiftregion_list"
	echo "id,name"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.name)|.["data"][] | "\(.id),\(.name)"'
}

ocicli_cluster_show_networks (){
	ocicli_call "action=cluster_show_networks&cluster_name=${1}"
	echo "name,ip,cidr,is_public,cluster,role,iface1,iface2,location_id"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.name)|.["data"][] | "\(.name),\(.ip),\(.cidr),\(.is_public),\(.cluster),\(.role),\(.iface1),\(.iface2),\(.location_id)"'
}

ocicli_cluster_show_machines (){
	ocicli_call "action=cluster_show_machines&cluster_name=${1}"
	echo "serial,ipaddr,memory,status,lastseen,hostname"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.serial)|.["data"][] | "\(.serial),\(.ipaddr),\(.memory),\(.status),\(.lastseen),\(.hostname)"'
}

ocicli_cluster_wait_for () {
	local MACHINE_SERIAL=$1
	local FIELD=$2
	local VALUE=$3
	local CNT=600
	while [ "${CNT}" -gt 0 -o $(ocicli -csv machine-list | q -H -d, "SELECT ${FIELD} FROM - WHERE serial='${MACHINE_SERIAL}'") = "${VALUE}" ] ; do
		echo -n "."
		sleep 2
	done
}

ocicli_cluster_wait_for_puppet () {
	local MACHINE_SERIAL=$1
	local CNT=400
	local PUPPET_STATUS=$(ocicli -csv machine-list | q -H -d, "SELECT puppet FROM - WHERE serial='${MACHINE_SERIAL}'")
	while [ "${CNT}" -gt 0 -o "${PUPPET_STATUS}" = "success" -o "${PUPPET_STATUS}" = "failure" ] ; do
		echo -n "."
		sleep 2
		PUPPET_STATUS=$(ocicli -csv machine-list | q -H -d, "SELECT puppet FROM - WHERE serial='${MACHINE_SERIAL}'")
	done
}

ocicli_cluster_install () {
	local CLUSTER_NAME=${1}
	local EXISTS=$(ocicli -csv cluster-list | q -H -d, "SELECT COUNT(id) FROM - WHERE name='${CLUSTER_NAME}'")
	if [ "${EXISTS}" != "1" ] ; then
		echo "Could not find cluster named ${CLUSTER_NAME}"
		exit 1
	fi
	echo "Installing cluster ${CLUSTER_NAME}..."
	local DOMAIN_NAME=$(ocicli -csv cluster-list | q -H -d, "SELECT domain FROM - WHERE name='${CLUSTER_NAME}'")
	local CLUSTER_ID=$(ocicli -csv cluster-list | q -H -d, "SELECT id FROM - WHERE name='${CLUSTER_NAME}'")
	echo "Cluster has domain name: ${DOMAIN_NAME} and ID: ${CLUSTER_ID}"
	local CEPH_MONS=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE hostname LIKE 'z-cephmon-%'" | tr '\n' ' ')
	local CEPH_OSDS=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE hostname LIKE 'z-cephosd-%'" | tr '\n' ' ')
	local CONTROLLERS=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE hostname LIKE 'z-controller-%'" | tr '\n' ' ')
	local COMPUTES=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE hostname LIKE 'z-controller-%'" | tr '\n' ' ')
	local VOLUMES=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE hostname LIKE 'z-volume-%'" | tr '\n' ' ')

	if [ -n "${CEPH_MONS}" ] ; then
		echo -n "Installing CEPH MON machines: ${CEPH_MONS} "
		for i in ${CEPH_MONS} ; do
			ocicli machine-install-os $i
		done
		for i in ${CEPH_MONS} ; do
			ocicli_cluster_wait_for $i status installed
		done
		echo ""
	fi
	if [ -n "${CEPH_OSDS}" ] ; then
		echo -n "Installing CEPH OSD machines: ${CEPH_OSDS} "
		for i in ${CEPH_OSDS} ; do
			ocicli machine-install-os $i
		done
		for i in ${CEPH_OSDS} ; do
			ocicli_cluster_wait_for $i status installed
		done
		echo ""
	fi
	if [ -n "${CONTROLLERS}" ] ; then
		echo -n "Installing controller machines: ${CONTROLLERS} "
		for i in ${CONTROLLERS} ; do
			ocicli machine-install-os $i
		done
		for i in ${CONTROLLERS} ; do
			ocicli_cluster_wait_for $i status installed
		done
		for i in ${CONTROLLERS} ; do
			ocicli_cluster_wait_for_puppet $i
		done
	fi
	if [ -n "${COMPUTES}" ] ; then
		echo -n "Installing compute machines: ${COMPUTES} "
		for i in ${COMPUTES} ; do
			ocicli machine-install-os $i
		done
		for i in ${COMPUTES} ; do
			ocicli_cluster_wait_for $i status installed
		done
		for i in ${COMPUTES} ; do
			ocicli_cluster_wait_for $i puppet success
		done
	fi
	if [ -n "${VOLUMES}" ] ; then
		echo -n "Installing volume machines: ${VOLUMES} "
		for i in ${VOLUMES} ; do
			ocicli machine-install-os $i
		done
		for i in ${VOLUMES} ; do
			ocicli_cluster_wait_for $i status installed
		done
		for i in ${VOLUMES} ; do
			ocicli_cluster_wait_for $i puppet success
		done
	fi
}

ocicli_role_list (){
	ocicli_call "action=role_list"
	echo "id,name"
	cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.name)|.["data"][] | "\(.id),\(.name)"'
}

ocicli_display_status (){
	STATUS=$(cat ${API_RESPONSE_FILE} | jq -r '.["status"]')
	if [ "${STATUS}" = 'success' ] ; then
		echo "Ok."
	else
		echo "Status not ok:"
		cat ${API_RESPONSE_FILE} | jq -r '.["message"]'
	fi
}

#########################
### Parameter parsing ###
#########################

if [ -z "${1}" ] ; then
	usage
fi

if [ "${1}" = -d ] ; then
	DEBUG=on
	MINUS_D="-d"
	shift
fi

# If using -csv, procress as normal
if [ "${1}" = -csv ] ; then
	shift
else
	# If not, (and not for all commands...), we call ourself with -csv and filter the CSV output through the column program
	case ${1} in
	"machine-install-os")
		echo -n ""
	;;
	"machine-display-install-cmd")
		echo -n ""
	;;
	"machine-install-log")
		echo -n ""
	;;
	"machine-set-ipmi")
		echo -n ""
	;;
	"machine-console")
		echo -n ""
	;;
	"swift-calculate-ring")
		echo -n ""
	;;
	*)
		$0 ${MINUS_D} -csv $@ | column -t -s $','
		exit 0
	;;
	esac
fi

ACTION=${1}
shift

case "${ACTION}" in
	"machine-list")
		ocicli_machine_list $@
	;;
	"machine-show")
		ocicli_machine_show ${1}
	;;
	"machine-set")
		ocicli_machine_set $@
	;;
	# Add a machine to a cluster
	"machine-add")
		ocicli_call "action=machine_add&machine_serial=${1}&cluster_name=${2}&role_name=${3}&location_name=${4}"  
		ocicli_display_status
	;;
	"machine-remove")
		ocicli_call "action=machine_remove&machine_serial=${1}"
		ocicli_display_status
	;;
	"machine-destroy")
		ocicli_call "action=machine_destroy&machine_serial=${1}"
		ocicli_display_status
	;;
	"machine-show-from-hostname")
		ocicli_call "action=machine_show_from_hostname&hostname=${1}"
		echo "serial,ipaddr,memory,status,lastseen,hostname"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"] | "\(.serial),\(.ipaddr),\(.memory),\(.status),\(.lastseen),\(.hostname)"'
	;;
	"machine-reboot-on-hdd")
		ocicli_call "action=machine_reboot_on_hdd&serial=${1}"
		ocicli_display_status
	;;
	"machine-reboot-on-live")
		ocicli_call "action=machine_reboot_on_live&serial=${1}"
		ocicli_display_status
	;;
	"machine-ipmi-reboot-on-hdd")
		ocicli_call "action=ipmi_reboot_on_hdd&serial=${1}"
		ocicli_display_status
	;;
	"machine-ipmi-reboot-on-live")
		ocicli_call "action=ipmi_reboot_on_live&serial=${1}"
		ocicli_display_status
	;;
	"machine-install-os")
		ocicli_call "action=machine_install_os&serial=${1}"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]'
	;;
	"machine-display-install-cmd")
		ocicli_call "action=machine_display_install_cmd&serial=${1}"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]'
	;;
	"machine-install-log")
		ocicli_call "action=machine_install_log&serial=${1}"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]'
	;;
	"machine-set-ipmi")
		# Prototype: machine-set-ipmi [--do-ipmitool-cmds] [--ipmi-defgw GATEWAY] [--ipmi-netmask NETMASK] [--ipmi-call-chassis-bootdev yes/no] <serial> <use-ipmi:yes/no> <ipmi-ip-addr> <ipmi-port> <ipmi-username> <ipmi-password>
		if [ "${1}" = "--do-ipmitool-cmds" ] ; then
			MORE_PARAMS="&perform_ipmitool_cmd=yes";
			shift
			if [ "${1}" = "--ipmi-defgw" ] ; then
				MORE_PARAMS="${MORE_PARAMS}&ipmi_default_gw=${2}"
				shift
				shift
			fi
			if [ "${1}" = "--ipmi-netmask" ] ; then
				MORE_PARAMS="${MORE_PARAMS}&ipmi_netmask=${2}"
				shift
				shift
			fi
			if [ "${1}" = "--ipmi-call-chassis-bootdev" ] ; then
				MORE_PARAMS="${MORE_PARAMS}&ipmi_call_chassis_bootdev=${2}"
				shift
				shift
			fi
		fi
		ocicli_call "action=machine_set_ipmi&serial=${1}&ipmi_use=${2}&ipmi_addr=${3}&ipmi_port=${4}&ipmi_username=${5}&ipmi_password=${6}${MORE_PARAMS}"
		cat ${API_RESPONSE_FILE} | jq -r '.["message"]'
	;;
	"machine-console")
		ocicli_call "action=ipmi_show_cmd_console&serial=${1}"
		cat ${API_RESPONSE_FILE} | jq -r '.["message"]'
	;;
	"machine-ip-add")
		ocicli_call "action=machine_ip_add&serial=${1}&network_name=${2}&ip_addr=${3}"
		ocicli_display_status
	;;
	"machine-ip-list")
		ocicli_machine_ip_list $1
	;;
	"machine-ip-remove")
		ocicli_call "action=machine_ip_remove&serial=${1}&ip_addr=${2}"
		ocicli_display_status
	;;
	"ipmi-assign-check")
		ocicli_call "action=ipmi_assign_check"
		cat ${API_RESPONSE_FILE} | jq -r '.["message"]'
	;;
	"location-list")
		ocicli_location_list
	;;
	"location-create")
		ocicli_call "action=location_create&name=${1}&swiftregion=${2}"
		ocicli_display_status
	;;
	"location-delete")
		ocicli_call "action=location_delete&name=${1}"  
		ocicli_display_status
	;;

	"network-create")
		ocicli_call "action=new_network&name=${1}&ipaddr=${2}&cidr_mask=${3}&location=${4}&is_public=${5}"
		ocicli_display_status
	;;
	"network-list")
		ocicli_network_list
	;;
	"network-delete")
		ocicli_call "action=network_delete&name=${1}"
		ocicli_display_status
	;;
	"cluster-set")
		CALL="action=cluster_set&name=${1}"
		shift
		for i in $@ ; do
			case "${1}" in
			"--initial-cluster-setup")
				if [ -z "${2}" ] ; then
					echo "No parameter for --initial-cluster-setup"
					usage
					exit 1
				fi
				CALL="${CALL}&initial_cluster_setup=${2}"
				shift
				shift
			;;
			"--vip-hostname")
				if [ -z "${2}" ] ; then
					echo "No parameter for --vip-hostname"
					usage
					exit 1
				fi
				CALL="${CALL}&vip_hostname=${2}"
				shift
				shift
			;;
			"--swift-part-power")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-part-power"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_part_power=${2}"
				shift
				shift
			;;
			"--time-server-host")
				if [ -z "${2}" ] ; then
					echo "No parameter for --time-server-host"
					usage
					exit 1
				fi
				CALL="${CALL}&time_server_host=${2}"
				shift
				shift
			;;
			"--swift-encryption-key-id")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-encryption-key-id"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_encryption_key_id=${2}"
				shift
				shift
			;;
			"--swift-proxy-hostname")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-proxy-hostname"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_proxy_hostname=${2}"
				shift
				shift
			;;
			"--swift-disable-encryption")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-disable-encryption"
					usage
					exit 1
				fi
				if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
					echo "Wrong parameter for --swift-disable-encryption"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_disable_encryption=${2}"
				shift
				shift
			;;
			"--swift-object-replicator-concurrency")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-object-replicator-concurrency"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_object_replicator_concurrency=${2}"
				shift
				shift
			;;
			"--swift-rsync-connection-limit")
				if [ -z "${2}" ] ; then
					echo "No parameter for --swift-rsync-connection-limit"
					usage
					exit 1
				fi
				CALL="${CALL}&swift_rsync_connection_limit=${2}"
				shift
				shift
			;;
			"--extswift-use-external")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-use-external"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_use_external=${2}"
				shift
				shift
			;;
			"--extswift-auth-url")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-auth-url"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_auth_url=${2}"
				shift
				shift
			;;
			"--extswift-proxy-url")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-proxy-url"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_proxy_url=${2}"
				shift
				shift
			;;
			"--extswift-project-name")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-project-name"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_project_name=${2}"
				shift
				shift
			;;
			"--extswift-project-domain-name")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-project-domain-name"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_project_domain_name=${2}"
				shift
				shift
			;;
			"--extswift-user-name")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-user-name"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_user_name=${2}"
				shift
				shift
			;;
			"--extswift-user-domain-name")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-user-domain-name"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_user_domain_name=${2}"
				shift
				shift
			;;
			"--extswift-password")
				if [ -z "${2}" ] ; then
					echo "No parameter for --extswift-password"
					usage
					exit 1
				fi
				CALL="${CALL}&extswift_password=${2}"
				shift
				shift
			;;
			"--amp-secgroup-list")
				if [ -z "${2}" ] ; then
					echo "No parameter for --amp-secgroup-list"
					usage
					exit 1
				fi
				CALL="${CALL}&amp_secgroup_list=${2}"
				shift
				shift
			;;
			"--amp-boot-network-list")
				if [ -z "${2}" ] ; then
					echo "No parameter for --amp-boot-network-list"
					usage
					exit 1
				fi
				CALL="${CALL}&amp_boot_network_list=${2}"
				shift
				shift
			;;
			"--disable-notifications")
				if [ "${2}" != 'yes' ] && [ "${2}" != 'no' ] ; then
					echo "Paramter for --disable-notifications isn't yes or no"
					usage
					exit 1
				fi
				CALL="${CALL}&disable_notifications=${2}"
				shift
				shift
			;;
			"--enable-monitoring-graphs")
				if [ "${2}" != 'yes' ] && [ "${2}" != 'no' ] ; then
					echo "Paramter for --disable-notifications isn't yes or no"
					usage
					exit 1
				fi
				CALL="${CALL}&enable_monitoring_graphs=${2}"
				shift
				shift
			;;
			"--monitoring-graphite-host")
				if [ -z "${2}" ] ; then
					echo "No parameter for --monitoring-graphite-host"
					usage
					exit 1
				fi
				CALL="${CALL}&monitoring_graphite_host=${2}"
				shift
				shift
			;;
			"--monitoring-graphite-port")
				if [ -z "${2}" ] ; then
					echo "No parameter for --monitoring-graphite-port"
					usage
					exit 1
				fi
				CALL="${CALL}&monitoring_graphite_port=${2}"
				shift
				shift
			;;
			"--statsd-hostname")
				if [ -z "${2}" ] ; then
					echo "No parameter for --statsd-hostname"
					usage
					exit 1
				fi
				CALL="${CALL}&statsd_hostname=${2}"
				shift
				shift
			;;
			"--self-signed-api-cert")
				if [ -z "${2}" ] ; then
					echo "No parameter for --self-signed-api-cert"
					usage
					exit 1
				fi
				if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
					echo "Wrong parameter for --self-signed-api-cert"
					usage
					exit 1
				fi
				CALL="${CALL}&self_signed_api_cert=${2}"
				shift
				shift
			;;
			"--nested-virt")
				if [ -z "${2}" ] ; then
					echo "No parameter for --nested-virt"
					usage
					exit 1
				fi
				if [ "${2}" != "yes" ] && [ "${2}" != "no" ]  && [ "${2}" != "cluster_value" ]; then
					echo "Wrong parameter for --nested-virt"
					usage
					exit 1
				fi
				CALL="${CALL}&nested_virt=${2}"
				shift
				shift
			;;
			"--use-ovs-ifaces")
				if [ -z "${2}" ] ; then
					echo "No parameter for --use-ovs-ifaces"
					usage
					exit 1
				fi
				if [ "${2}" != "yes" ] && [ "${2}" != "no" ] ; then
					echo "Wrong parameter for --use-ovs-ifaces"
					usage
					exit 1
				fi
				CALL="${CALL}&use_ovs_ifaces=${2}"
				shift
				shift
			;;
			"--cpu-mode")
				if [ -z "${2}" ] ; then
					echo "No parameter for --cpu-mode"
					usage
					exit 1
				fi
				if [ "${2}" != "host-model" ] && [ "${2}" != "host-passthrough" ] && [ "${2}" != "custom" ] ; then
					echo "Wrong parameter for --cpu-mode"
					usage
					exit 1
				fi
				CALL="${CALL}&cpu_mode=${2}"
				shift
				shift
			;;
			"--cpu-model")
				if [ -z "${2}" ] ; then
					echo "No parameter for --cpu-model"
					usage
					exit 1
				fi
				CALL="${CALL}&cpu_model=${2}"
				shift
				shift
			;;
			"--cpu-model-extra-flags")
				if [ -z "${2}" ] ; then
					echo "No parameter for --cpu-model-extra-flags"
					usage
					exit 1
				fi
				ENCODED=$(urlencode ${2})
				CALL="${CALL}&cpu_model_extra_flags=${ENCODED}"
				shift
				shift
			;;
			*)
				if [ -r ${VARIABLE_FILE} ] ; then
					OPTIONS=$(cat ${VARIABLE_FILE} | jq -r '.clusters | keys[]' | tr '\n' ' ')
					for OPTION in ${OPTIONS} ; do
						MY_OPTION="--"$(echo ${OPTION} | sed 's/_/-/g')
						if [ "${MY_OPTION}" = "${1}" ] ; then
							CALL="${CALL}&${OPTION}=${2}"
							shift
							shift
							continue
						fi
					done
				else
					echo -n ""
				fi
			;;
			esac
		done
		ocicli_call $CALL
		ocicli_display_status
	;;
	"network-set")
		CALL="action=network_set&network_name=${1}"
		shift
		for i in $@ ; do
			case "${1}" in
			"--role")
				if [ -z "${2}" ] ; then
					echo "No parameter for --role"
					usage
					exit 1
				fi
				CALL="${CALL}&role=${2}"
				shift
				shift
			;;
			"--iface1")
				if [ -z "${2}" ] ; then
					echo "No parameter for --iface1"
					usage
					exit 1
				fi
				CALL="${CALL}&iface1_name=${2}"
				shift
				shift
			;;
			"--iface2")
				if [ -z "${2}" ] ; then
					echo "No parameter for --iface2"
					usage
					exit 1
				fi
				CALL="${CALL}&iface2_name=${2}"
				shift
				shift
			;;
			"--ip")
				if [ -z "${2}" ] ; then
					echo "No parameter for --ip"
					usage
					exit 1
				fi
				CALL="${CALL}&ip=${2}"
				shift
				shift
			;;
			"--cidr")
				if [ -z "${2}" ] ; then
					echo "No parameter for --cidr"
					usage
					exit 1
				fi
				CALL="${CALL}&cidr=${2}"
				shift
				shift
			;;
			"--is-public")
				if [ -z "${2}" ] ; then
					echo "No parameter for --is-public"
					usage
					exit 1
				fi
				CALL="${CALL}&is_public=${2}"
				shift
				shift
			;;
			"--mtu")
				if [ -z "${2}" ] ; then
					echo "No parameter for --mtu"
					usage
					exit 1
				fi
				CALL="${CALL}&mtu=${2}"
				shift
				shift
			;;
			"--vlan")
				if [ -z "${2}" ] ; then
					echo "No parameter for --vlan"
					usage
					exit 1
				fi
				CALL="${CALL}&vlan=${2}"
				shift
				shift
			;;
			"--location")
				if [ -z "${2}" ] ; then
					echo "No parameter for --location"
					usage
					exit 1
				fi
				CALL="${CALL}&location=${2}"
				shift
				shift
			;;
			"--bridge-name")
				if [ -z "${2}" ] ; then
					echo "No parameter for --bridge-name"
					usage
					exit 1
				fi
				CALL="${CALL}&bridgename=${2}"
				shift
				shift
			;;
			"--ipmi-match-addr")
				if [ -z "${2}" ] ; then
					echo "No parameter for --ipmi-match-addr"
					usage
					exit 1
				fi
				CALL="${CALL}&ipmi_match_addr=${2}"
				shift
				shift
			;;
			"--ipmi-match-cidr")
				if [ -z "${2}" ] ; then
					echo "No parameter for --ipmi-match-cidr"
					usage
					exit 1
				fi
				CALL="${CALL}&ipmi_match_cidr=${2}"
				shift
				shift
			;;
			*)
			;;
			esac
		done
		ocicli_call $CALL
		ocicli_display_status
	;;
	# Add a network to a cluster
	"network-add")
		ocicli_call "action=network_add&network_name=${1}&cluster_name=${2}&role_name=${3}&iface1=${4}&iface2=${5}"
		ocicli_display_status
	;;
	# Remove a network from a cluster
	"network-remove")
		ocicli_call "action=network_remove&network_name=${1}"
		ocicli_display_status
	;;

	"cluster-list")
		ocicli_cluster_list
	;;
	"cluster-create")
		ocicli_call "action=cluster_create&name=${1}&domain=${2}"
		ocicli_display_status
	;;
	"cluster-delete")
		ocicli_call "action=cluster_delete&name=${1}"
		ocicli_display_status
	;;
	"cluster-show")
		ocicli_cluster_show ${1}
	;;
	"cluster-show-networks")
		ocicli_cluster_show_networks $@
	;;
	"cluster-show-machines")
		ocicli_cluster_show_machines $@
	;;
	"cluster-show-ips")
		ocicli_call "action=cluster_show_ips&name=${1}"
		echo "hostname,ip"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]|=sort_by(.hostname)|.["data"][] | "\(.hostname),\(.ipaddr)"'
	;;
	"cluster-install")
		ocicli_cluster_install $@
	;;
	"swift-region-list")
		ocicli_swiftregion_list
	;;
	"swift-region-create")
		ocicli_call "action=swiftregion_create&name=${1}"
		ocicli_display_status
	;;
	"swift-region-delete")
		ocicli_call "action=swiftregion_delete&name=${1}"
		ocicli_display_status
	;;
	"swift-calculate-ring")
		ocicli_call "action=swift_caculate_ring&cluster_name=${1}"
		echo "Output for the job:"
		cat ${API_RESPONSE_FILE} | jq -r '.["data"]'
	;;
	"role-list")
		ocicli_role_list
	;;
	"role-create")
		ocicli_call "action=role_create&name=${1}"
		ocicli_display_status
	;;
	"role-delete")
		ocicli_call "action=role_delete&name=${1}"
		ocicli_display_status
	;;
	*)
		usage
	;;
esac
