Created
February 24, 2015 23:29
-
-
Save WaltHP/26b9f254752d74125531 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# script that creates a new vagrant in ~/vagrant/<name> | |
# For debugging this script -- skips the slow stuff (vagrant box list, git clone, vagrant up) | |
# DEBUG=1 | |
# Capture dir in which this script lives | |
SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") | |
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 | |
-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 | |
-l Local.conf to use | |
EOF | |
} | |
# Display a menu: | |
# Usage: title [choice...] | |
# | |
menu() { | |
title=$1 | |
shift | |
typeset -a args | |
while [[ $# -gt 0 ]] ; do | |
args+=($1) # Programmatic tag to return when this item chosen | |
args+=($1) # Descriptive text to display | |
shift | |
done | |
# run whiptail and swap stderr and stdout | |
(whiptail --noitem --menu "$title" $menu_h $menu_w $menu_l ${args[@]} 3>&1 1>&2 2>&3) | |
} | |
while getopts "sn:h:m:c:bl:" 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 | |
;; | |
c) | |
echo "VM CPUS {$OPTARG}" | |
VM_CPUS=$OPTARG | |
;; | |
m) | |
echo "VM RAM {$OPTARG}" | |
VM_RAM=$OPTARG | |
;; | |
b) | |
echo "Bypass devstack" | |
BYPASS_DEVSTACK="true" | |
;; | |
h) | |
usage | |
exit | |
;; | |
l) | |
echo "Local conf ${OPTARG}" | |
LOCAL_CONF=$OPTARG | |
;; | |
?) | |
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 | |
# Get a list of available vagrant boxes | |
if [[ $DEBUG -eq 1 ]] ; then | |
boxes="boxone boxtwo boxthree" | |
else | |
boxes=$(vagrant box list | grep libvirt | awk '{print $1}') | |
fi | |
if [[ -z $boxes ]] ; then | |
echo "No boxes are available" >&2 | |
exit 1 | |
fi | |
# Display menu of available boxes | |
BOX_NAME=$(menu 'Available boxes:' $boxes) | |
[[ -n $BOX_NAME ]] || exit 1 # exit if user cancelled | |
# now clone the repository | |
cd $BASE_PATH | |
if [[ $DEBUG -eq 1 ]] ; then | |
mkdir -p $VAGRANT_NAME/files/home | |
else | |
git clone git@csim-cloud.rose.hp.com:openstack/vagrant-devstack.git $VAGRANT_NAME | |
fi | |
cd $VAGRANT_NAME | |
# link to dot files in home dir | |
for file in .bashrc .vim .vimrc .gitconfig ; do | |
if [ -f $HOME/$file ] ; then | |
ln -s $HOME/$file files/home | |
fi | |
done | |
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 | |
# Prompt for which local.conf to use | |
if [[ -z $LOCAL_CONF ]] ; then | |
conf="(default) " | |
conf+=$(ls -1 ${SCRIPTDIR}/conf | grep ^local.conf) | |
LOCAL_CONF=$(menu 'Available local.confs:' $conf) | |
[[ -n $LOCAL_CONF ]] || exit 1 # exit if user cancelled | |
fi | |
if [[ $LOCAL_CONF != "(default)" ]] ; then | |
cp ${SCRIPTDIR}/conf/$LOCAL_CONF files/local.conf | |
fi | |
if [ $START_VAGRANT -ne 0 ] | |
then | |
echo "Starting Vagrant" | |
[[ $DEBUG -eq 1 ]] || vagrant up | |
else | |
echo "Not starting vagrant" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment