Created
February 24, 2015 19:04
-
-
Save WaltHP/28a763d414cfee1dcbec 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> | |
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 | |
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 "sn: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 | |
;; | |
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 | |
if [ $START_VAGRANT -ne 0 ] | |
then | |
echo "Starting Vagrant" | |
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