#!/bin/bash # # Additional functions that would mostly just pertain to a Ubuntu + Qemu setup # function install_prep_packages() { # Called before devstack exclaim 'Updating dependencies (part 1a)...' pkg_update exclaim 'Installing dependencies (part 1b)...' pkg_install git-core kvm-ipxe } 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} VM=$3 DISTRO=$4 SERVICE_TYPE=$5 readonly SSH_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 GUEST_USERNAME export NETWORK_GATEWAY export REDSTACK_SCRIPTS export SERVICE_TYPE 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 ${PATH_DISKIMAGEBUILDER}/bin/disk-image-create -a amd64 -o "${VM}" -x ${DISTRO} ${EXTRA_ELEMENTS} vm heat-cfntools ${DISTRO}-guest ${DISTRO}-${SERVICE_TYPE} } function cmd_build_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' if [ `expr "$VALID_SERVICES" : ".*$SERVICE"` -eq 0 ] ; then exclaim "You did not pass in a valid image type. Valid types are:" $VALID_SERVICES exit 1 fi iniset $TROVE_CONF_DIR/trove.conf DEFAULT service_type $SERVICE_TYPE GUEST_LOGDIR=$(iniget $TROVE_SOURCE/etc/trove/trove-guestagent.conf.sample DEFAULT log_dir) GUEST_LOGFILE=$(iniget $TROVE_SOURCE/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: " $TROVE_SOURCE/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 doesnt 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 QCOW_IMAGE=`find $VM_PATH -name '*.qcow2'` TROVE_TOKEN=`retrieve_token trove TROVE-PASS trove` # Now upload it upload_image_to_glance "${IMAGENAME}" ovf qcow2 $TROVE_TOKEN $QCOW_IMAGE $SERVICE_TYPE } 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 } 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" } 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 }