Skip to content

Instantly share code, notes, and snippets.

@WaltHP
Created February 24, 2015 18:54
Show Gist options
  • Save WaltHP/deee2e0433bb713a8390 to your computer and use it in GitHub Desktop.
Save WaltHP/deee2e0433bb713a8390 to your computer and use it in GitHub Desktop.
#!/bin/bash
# script that creates a new vagrant in ~/vagrant/<name>
BASE_PATH="$HOME/vagrant"
VAGRANT_NAME=""
VAGRANT_BOX=""
START_VAGRANT=0
BYPASS_DEVSTACK="false"
VM_RAM=8192
VM_CPUS=2
VM_HOSTNAME="devstack"
# for whiptail sizing
lines=$(tput lines)
cols=$(tput cols)
menu_h=$((lines-3))
menu_w=$((cols-5))
menu_l=$((lines-11))
usage() {
cat <<EOF
usage: $0 options
OPTIONS:
-n Name of the vagrant (${VAGRANT_NAME})
-s Automatically start the vagrant? (Default: no)
-m Amount of ram for the vm (Default: 8192)
-c Number of CPUS (Default: 2)
-b Bypass devstack? (Default: No)
-h this help output
EOF
}
# Display a menu:
# Usage: title [choice...]
#
menu() {
title=$1
shift
typeset -a args
while [[ $# -gt 0 ]] ; do
args+=($1)
args+=($1)
shift
done
(whiptail --noitem --menu "$title" $menu_h $menu_w $menu_l ${args[@]} 3>&1 1>&2 2>&3)
}
while getopts "n:s:h:m:c:b" OPTION
do
case $OPTION in
n)
echo "using {$OPTARG} as vagrant name"
VAGRANT_NAME=$OPTARG
VM_HOSTNAME="$VAGRANT_NAME-vm"
;;
s)
echo "Starting Vagrant after build"
START_VAGRANT=1
VAGRANT_NAME=$OPTARG
;;
c)
echo "VM CPUS {$OPTARG}"
VM_CPUS=$OPTARG
;;
m)
echo "VM RAM {$OPTARG}"
VM_RAM=$OPTARG
;;
b)
echo "VM CPUS {$OPTARG}"
BYPASS_DEVSTACK="true"
;;
h)
usage
exit
;;
?)
usage
exit
;;
esac
done
if [ -z "$VAGRANT_NAME" ]; then
echo "You must specify the vagrant name"
usage
exit
fi
VAGRANT_PATH="$BASE_PATH/$VAGRANT_NAME"
if [ -d "$VAGRANT_PATH" ]; then
echo "Vagrant ${VAGRANT_NAME} already exists"
exit
fi
boxes=$(vagrant box list | grep libvirt | awk '{print $1}')
if [[ -z $boxes ]] ; then
echo "No boxes are available" >&2
exit 1
fi
# Prompt with a whiptail menu, and swap stderr and stdout
BOX_NAME=$(menu 'Available boxes:' $boxes)
[[ -n $BOX_NAME ]] || exit 1 # exit if user cancelled
# now clone the repository
cd $BASE_PATH
git clone git@csim-cloud.rose.hp.com:openstack/vagrant-devstack.git $VAGRANT_NAME
# now copy over the needed files
ln -s ~/.bashrc $VAGRANT_NAME/files/home
ln -s ~/.vimrc $VAGRANT_NAME/files/home
ln -s ~/.vim $VAGRANT_NAME/files/home
ln -s ~/.gitconfig $VAGRANT_NAME/files/home
cd $VAGRANT_NAME
cat > config.yaml <<CONFIG_YAML
box: $BOX_NAME
hostname: $VM_HOSTNAME
memory: $VM_RAM
cpus: $VM_CPUS
bypass_devstack: $BYPASS_DEVSTACK
install_powerline: true
forward_x11: true
CONFIG_YAML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment