trove-integration/scripts/functions_qemu

129 lines
4.2 KiB
Bash

#!/bin/bash
#
# Additional functions that would mostly just pertain to a Ubuntu + Qemu setup
#
function build_vm() {
exclaim "Actually building the image, this can take up to 15 minutes"
# set variables here and ensure they are not changed during the duration of this script
readonly HOMEDIR=$1
readonly HOST_USERNAME=$2
GUEST_USERNAME=${GUEST_USERNAME:-$2}
HOST_SCP_USERNAME=${HOST_SCP_USERNAME:-$2}
VM=$3
DISTRO=$4
SERVICE_TYPE=$5
readonly SSH_DIR=${KEY_DIR:-${HOMEDIR}/.ssh}
manage_ssh_keys
if [ $DISTRO == 'ubuntu' ]; then
export RELEASE=precise
export DIB_RELEASE=$RELEASE
export DIB_CLOUD_IMAGES=cloud-images.ubuntu.com
fi
if [ $DISTRO == 'fedora' ]; then
EXTRA_ELEMENTS=selinux-permissive
fi
export HOST_USERNAME
export HOST_SCP_USERNAME
export GUEST_USERNAME
export NETWORK_GATEWAY
export REDSTACK_SCRIPTS
export SERVICE_TYPE
export PATH_TROVE
export ESCAPED_PATH_TROVE
export SSH_DIR
export GUEST_LOGDIR
export ESCAPED_GUEST_LOGDIR
export ELEMENTS_PATH=$REDSTACK_SCRIPTS/files/elements:$PATH_TRIPLEO_ELEMENTS/elements
export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive"
${PATH_DISKIMAGEBUILDER}/bin/disk-image-create -a amd64 -o "${VM}" -x ${DISTRO} ${EXTRA_ELEMENTS} vm heat-cfntools cloud-init-datasources ${DISTRO}-guest ${DISTRO}-${SERVICE_TYPE}
}
function build_guest_image() {
exclaim "Building an image for use with development and integration tests."
if [ -z "$1" ]
then
echo "You must pass an image type to build, like mysql"
exit 1
fi
SERVICE_TYPE=$1
VALID_SERVICES='mysql percona redis cassandra couchbase mongodb postgresql'
if ! [[ " $VALID_SERVICES " =~ " $SERVICE_TYPE " ]]; then
exclaim "You did not pass in a valid image type. Valid types are:" $VALID_SERVICES
exit 1
fi
GUEST_LOGDIR=$(iniget $PATH_TROVE/etc/trove/trove-guestagent.conf.sample DEFAULT log_dir)
GUEST_LOGFILE=$(iniget $PATH_TROVE/etc/trove/trove-guestagent.conf.sample DEFAULT log_file)
if [ -z $GUEST_LOGDIR ] || [ -z $GUEST_LOGFILE ]
then
exclaim "error: log_dir and log_file are required in: " $PATH_TROVE/etc/trove/trove-guestagent.conf.sample
exit 1
fi
ESCAPED_GUEST_LOGDIR=`echo $GUEST_LOGDIR | sed 's/\//\\\\\//g'`
USERNAME=`whoami`
# To change the distro, edit the redstack.rc file
readonly IMAGENAME=${DISTRO}_${SERVICE_TYPE}
readonly VM_PATH=$USERHOME/images/${IMAGENAME}
readonly VM_PATH_NAME=${VM_PATH}/${IMAGENAME}
mkdir -p $VM_PATH
# If the path does not exist, build it, otherwise just upload it
if [ ! -d $VM_PATH ] || [ `ls -1 $VM_PATH | wc -l` -eq '0' ]
then
build_vm $USERHOME $USERNAME $VM_PATH_NAME $DISTRO $SERVICE_TYPE
else
exclaim "Found image in $VM_PATH using the qcow2 image found here..."
fi
}
function clean_instances() {
LIST=`virsh -q list|awk '{print $1}'`
for i in $LIST; do sudo virsh destroy $i; done
}
function manage_ssh_keys() {
if [ -e ${SSH_DIR} ]; then
echo "${SSH_DIR} already exists"
else
echo "Creating ${SSH_DIR} for ${HOST_USERNAME}"
sudo -Hiu ${HOST_USERNAME} mkdir -p ${SSH_DIR}
fi
if [ ! -f ${SSH_DIR}/id_rsa.pub ]; then
sudo apt-get -y install expect
generate_empty_passphrase_ssh_key ${HOST_USERNAME}
fi
add_host_key_to_authorizedkeys
}
function generate_empty_passphrase_ssh_key () {
echo "generating a empty passphrase DEV ONLY rsa key"
expect -c "
spawn sudo -Hiu ${HOST_USERNAME} /usr/bin/ssh-keygen -f ${SSH_DIR}/id_rsa -q
expect \"empty for no passphrase\"
send \n
expect assphrase
send \n
expect eof"
}
function add_host_key_to_authorizedkeys () {
# test to see if the host key is already in its own authorized_keys file - if not then add it. This is then later copied
# to the guest image
is_in_keyfile=`cat ${SSH_DIR}/id_rsa.pub | grep -f - ${SSH_DIR}/authorized_keys | wc -l`
if [ $is_in_keyfile == 0 ]; then
echo "Adding keyfile to authorized_keys, it does not yet exist"
cat ${SSH_DIR}/id_rsa.pub >> ${SSH_DIR}/authorized_keys
else
echo "Keyfile already exists in authorized_keys - skipping"
fi
}