#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72V src/bos/usr/lib/nim/methods/cloud_setup.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2019,2020 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)13 1.2 src/bos/usr/lib/nim/methods/cloud_setup.sh, cmdnim, bos72V, v2020_18A4 4/24/20 13:48:51

# ---------------------------------------------------------
# Message IDs
# ---------------------------------------------------------
MSG_SET=2
MSG_PROG_INIT_RES=117
MSG_MISC_RES_NOT_CREATED=169
MSG_INSTALLING_FILESETS=240
MSG_DONE_RESTORED_RES=270
MSG_EXECUTING_CMD=273
MSG_DONE=278
MSG_SPACE_FS=283
MSG_ERR_EXP=284
MSG_BAD_ARG=288
MSG_ALREADY_INSTALLED=298
MSG_CHK_RES_GRP=321
MSG_FAILED=400
MSG_PLEASE_EXECUTE_CMD=451
MSG_MUST_BE_ROOT=468

# ---------------------------------------------------------
# Global variables
# ---------------------------------------------------------
PROGRAM=$0
solution=""
yum_website="http://ftp.software.ibm.com/aix/freeSoftware/aixtoolbox/ezinstall/ppc/yum.sh"
LOG="/var/adm/ras/cloudpkg_$$.log"
rcfile="/tmp/RC.$$"
RC=0

export PATH=/usr/sbin:/usr/bin:/bin:/opt/freeware/bin

# ---------------------------------------------------------
# Functions
# ---------------------------------------------------------
function catch_rc {

	echo $1 >> $rcfile
}

function get_rc {

	RC=$(cat $rcfile)
	rm $rcfile 2>/dev/null
}

function ck_fs {

	typeset fs_name=${1:-/opt}
	typeset -i space_need=${2:-250}
	typeset -i free_space=0

	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SPACE_FS} 'Checking %s space requirement...' ${fs_name} | tee -a ${LOG}

	# check / expand filesystem
	free_space="$( df -m ${fs_name} | awk 'NR==2{print $3}' 2>/dev/null )"

	if (( free_space < space_need )); then
		chfs -a size=+$(( space_need - free_space ))M ${fs_name} >/dev/null 2>&1

		[[ $? -ne 0 ]] && \
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ERR_EXP} 'Error expanding %s directory - Exiting.\n' ${fs_name} \
			 | tee -a ${LOG} \
			&& exit 1
	fi

	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | tee -a ${LOG}

	return 0
}

function install_yum {

	typeset user="anonymous"
	typeset pswd="anonymous"
	typeset path="aix/freeSoftware/aixtoolbox/ezinstall/ppc"

expect <<EOF
    log_user 0
    set timeout -1
    spawn ftp ftp.software.ibm.com
    expect "Name (ftp.software.ibm.com:*): "
    send "$user\r"
    expect "Password:"
    send "$pswd\r"
    expect "ftp>"
    send "bin\r"
    expect "ftp>"
    send "cd $path\r"
    expect "ftp>"
    send "get yum.sh\r"
    send "bye\r"
    expect eof
EOF
	if [[ -f "./yum.sh" ]]; then
		ck_fs /opt
		ck_fs /tmp
		ck_fs /var
		chmod +x ./yum.sh
		eval "./yum.sh" 2>&1
	else
		return 1
	fi

	return $?
}

function ck_deps {

	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROG_INIT_RES} 'Initializing resources ...\n\n' | tee -a ${LOG}

	# check for yum resource
	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CHK_RES_GRP} '\nChecking for resource group %s...' "yum" | tee -a ${LOG}

	rpm -q yum >/dev/null 2>&1 || install_yum

	if [[ $? -ne 0 ]] ; then
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' | tee -a ${LOG}
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PLEASE_EXECUTE_CMD} 'Please execute the command from %s.\n' $yum_website \
			| tee -a ${LOG}
		return -1
	else
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | tee -a ${LOG}
		rm ./yum.sh 2>/dev/null
	fi

	# check for python resource
	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CHK_RES_GRP} '\nChecking for resource group %s...' "python" | tee -a ${LOG}

	rpm -q python >/dev/null 2>&1

	[[ $? -ne 0 ]] && install_pkg python || \
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | tee -a ${LOG}

	# update version (if necessary)
	install_pkg python update

	# check for python-pip resource
	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CHK_RES_GRP} '\nChecking for resource group %s...' "python-pip" | tee -a ${LOG}

	rpm -q python-pip >/dev/null 2>&1

	[[ $? -ne 0 ]] && install_pkg python-pip || \
	       	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | tee -a ${LOG}

	return 0
}

function ck_version {

	typeset name=$1
	typeset cmd="${name} --version"

	# confirm command existence
	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE_RESTORED_RES} '\nDone verifying resources.\n' | tee -a ${LOG}

	(pip show $solution 2>/dev/null; catch_rc $?) | tee -a ${LOG}

	get_rc

	if [[ $RC -ne 0 ]]; then
			dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_EXECUTING_CMD} 'The %s %s resource was not created successfully.\n' "python-pip" "$solution" | tee -a ${LOG}
	else
			dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_EXECUTING_CMD} 'Executing command:\n%s\n' "$cmd" | tee -a ${LOG}
			eval $cmd 2>&1 | tee -a ${LOG}
	fi

	return $RC
}

function install_pkg {

	typeset pkgname=$1
	typeset option=${2:-install}
	typeset -i space_req=${3:-250}

	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INSTALLING_FILESETS} 'Installing filesets ...\n\n' | tee -a ${LOG}

	# add fudge factor / expand
	ck_fs /opt ${space_req}.15

	# attempt pkg install
	(yum -y $option $pkgname 2>&1; catch_rc $?) | tee -a ${LOG}

	get_rc ; [[ $RC -ne 0 ]] && exit -1

	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | tee -a ${LOG}

    return 0
}


# ---------------------------------------------------------
#               ######### MAIN ##############
# ---------------------------------------------------------

# set parameters from command line
while getopts :CIGv c
do
	case ${c} in

		C)		# set up/ install cloud-init
				solution="cloud-init"
				;;

		I)		# set up/ install awscli for IBM COS
				solution="awscli"
				;;

		G)		# set up/install gsutil for Google CS
				solution="gsutil"
				;;

		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
				;;

		\?)		# unknown option
				dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_BAD_ARG} 'Bad argument assignment : %s\n' $@ | tee -a ${LOG}

				echo 'Usage cloud_setup: Installs command line environment for cloud storage services.\ncloud_setup [-I | G | C] [-v]\n\n-I    Install the necessary RPMs for universal CLI (supports IBM Cloud Object Storage).\n-G    Install the necessary RPMs for gsutil CLI (Google Cloud Storage).\n-C    Install the necessary RPMs for cloud-init.\n-v    Enable debug output.\n'
				exit 1
				;;
	esac
done

# INIT
# - check for root
if [[ "$(id -u)" != "0" ]]; then
	dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MUST_BE_ROOT} '%1$s: You must be a root user to run this command.\n' ${PROGRAM} \
		| tee -a ${LOG}
	exit 1
fi

# create log / rc file
> ${LOG} ; > ${rcfile}

# check for dependency software
ck_deps || exit 1

# Begin install process
if [[ "$solution" = "gsutil" ]]
then
	# (if installed, list info); else continue
	if [[ -f "/opt/freeware/bin/gsutil" ]]; then

		echo '/opt/freeware/bin/gsutil \c' | tee -a ${LOG}
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ALREADY_INSTALLED} 'already installed.\n' | tee -a ${LOG}
		ck_version gsutil && \
		exit $?
	fi

	# setup deps
	install_pkg gcc
	install_pkg libffi-devel

	# add 350M for pip downloads
	ck_fs /opt 350
	ck_fs /tmp 350
	ck_fs /var 350

	# install gsutil
	(pip install --upgrade setuptools ; catch_rc $?) | tee -a ${LOG}
	get_rc ; [[ $RC -ne 0 ]] && exit -1

	(pip install --upgrade pip ; catch_rc $?) | tee -a ${LOG}
	get_rc ; [[ $RC -ne 0 ]] && exit -1

	(pip install gsutil ; catch_rc $?) | tee -a ${LOG}
	get_rc ; [[ $RC -ne 0 ]] && exit -1

	ck_version gsutil

elif [[ "$solution" = "awscli" ]]
then
	# (if installed, list info); else continue
	if [[ -f "/opt/freeware/bin/aws" ]]; then

		echo '/opt/freeware/bin/aws \c' | tee -a ${LOG}
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ALREADY_INSTALLED} 'already installed.\n' | tee -a ${LOG}
		ck_version aws && \
		exit $?
	fi

	# add 250M for pip downloads
	ck_fs /opt
	ck_fs /tmp
	ck_fs /var

	# install aws
	(pip install awscli ; catch_rc $?) | tee -a ${LOG}
	get_rc ; [[ $RC -ne 0 ]] && exit -1

	ck_version aws

elif [[ "$solution" = "cloud-init" ]]
then
	# (if installed, list info); else continue
	if [[ -f "/opt/freeware/bin/cloud-init" ]]; then

		echo '/opt/freeware/bin/cloud-init \c' | tee -a ${LOG}
		dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ALREADY_INSTALLED} 'already installed.\n' | tee -a ${LOG}
		ck_version cloud-init && \
		exit $?
	fi

	# add 250M for yum downloads
	ck_fs /opt
	ck_fs /tmp
	ck_fs /var

	# install cloud-init rpm
	install_pkg cloud-init

	ck_version cloud-init
fi

# done
exit $?
