124 lines
3.8 KiB
Bash
124 lines
3.8 KiB
Bash
#!/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}
|
|
|
|
|
|
readonly SSH_DIR=${HOMEDIR}/.ssh
|
|
manage_ssh_keys
|
|
MYSQL_ELEMENT="${SERVICE_TYPE}"
|
|
|
|
mkdir -p $VM_PATH
|
|
export RELEASE=precise
|
|
export DIB_RELEASE=$RELEASE
|
|
export DIB_CLOUD_IMAGES=cloud-images.ubuntu.com
|
|
export HOST_USERNAME
|
|
export GUEST_USERNAME
|
|
export NETWORK_GATEWAY
|
|
export REDSTACK_SCRIPTS
|
|
export SERVICE_TYPE
|
|
export ESCAPED_PATH_TROVE
|
|
export SSH_DIR
|
|
export ELEMENTS_PATH=$REDSTACK_SCRIPTS/files/elements
|
|
${PATH_DISKIMAGEBUILDER}/bin/disk-image-create -a amd64 -o "${VM_PATH}/${UBUNTU_DISTRO_NAME}_mysql_image" -x vm guest ${MYSQL_ELEMENT}
|
|
}
|
|
|
|
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
|
|
|
|
if [ $SERVICE_TYPE != mysql ] && [ $SERVICE_TYPE != percona ] ; then
|
|
exclaim "Warning: You did not pass in a valid image type, the image may fail"
|
|
exit 1
|
|
fi
|
|
|
|
iniset $TROVE_CONF_DIR/trove.conf DEFAULT service_type $SERVICE_TYPE
|
|
|
|
USERNAME=`whoami`
|
|
mkdir -p $USERHOME/images
|
|
# set values to readonly as they should not change here forward
|
|
readonly UBUNTU_DISTRO=ubuntu_precise
|
|
readonly UBUNTU_DISTRO_NAME=precise
|
|
readonly VM_PATH=$USERHOME/images/${UBUNTU_DISTRO_NAME}_${SERVICE_TYPE}_image
|
|
|
|
# If the path doesnt exist, build it, otherwise just upload it
|
|
if [ ! -d $VM_PATH ]
|
|
then
|
|
build_vm $USERHOME $USERNAME $UBUNTU_DISTRO_NAME $VM_PATH $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 "${UBUNTU_DISTRO_NAME}_${SERVICE_TYPE}_image" 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
|
|
}
|
|
|