Skip to content

Instantly share code, notes, and snippets.

@bradsokol
Created November 2, 2015 20:57
Show Gist options
  • Save bradsokol/8418fbfc1b644e085465 to your computer and use it in GitHub Desktop.
Save bradsokol/8418fbfc1b644e085465 to your computer and use it in GitHub Desktop.
Generate Vagrantfile and provisioning files for various needs
#! /bin/bash -e
usage()
{
cat << EOF
usage: $0 [options...] box-name
Generates the files necessary to provision a virtual Ubuntu sever managed by Vagrant.
OPTIONS:
-d Install Docker
-m Install Mercurial
-p Install Python
-r Install Ruby
-4 Use Ubuntu 14.04 as the base. Default is 12.04.
EOF
}
write_vagrantfile() {
if [ $UBUNTU1404 ]; then
BOX=ubuntu/trusty64
else
BOX=ubuntu/precise64
fi
cat <<EOF > Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "$BOX"
config.vm.provider :virtualbox do |vb|
vb.memory = 2048
vb.name = "$BOX_NAME"
end
config.vm.hostname = "$BOX_NAME"
config.vm.provision "shell", path: "bootstrap.sh"
end
EOF
}
write_bootstrap() {
cat <<EOF > bootstrap.sh
#! /bin/bash -e
echo "Canada/Eastern" > /etc/timezone
dpkg-reconfigure --frontend noninteractive tzdata
add-apt-repository ppa:git-core/ppa
apt-get update
apt-get install -y python-software-properties
apt-get install -y build-essential curl git tmux vim wget zip
EOF
if [ $DOCKER ]; then
if [ $UBUNTU1404 ]; then
PPA="deb https://apt.dockerproject.org/repo ubuntu-trusty main"
else
PPA="deb https://apt.dockerproject.org/repo ubuntu-precise main"
REBOOT=true
cat <<EOF >> bootstrap.sh
apt-get install -y linux-image-generic-lts-trusty
EOF
fi
cat <<EOF >> bootstrap.sh
apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo $PPA > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get purge lxc-docker*
apt-get install -y docker-engine
gpasswd -a vagrant docker
EOF
fi
if [ $MERCURIAL ]; then
cat <<EOF >> bootstrap.sh
add-apt-repository ppa:mercurial-ppa/releases
apt-get update
apt-get install -y mercurial
EOF
fi
if [ $PYTHON ]; then
cat <<EOF >> bootstrap.sh
apt-get install -y python-dev python-pip python-virtualenv
pip install virtualenvwrapper
pip install autoenv
pip install flake
EOF
fi
if [ $RUBY ]; then
cat <<EOF >> bootstrap.sh
apt-get install -y libssl-dev libreadline-dev zlib1g-dev
EOF
fi
cat <<EOF >> bootstrap.sh
sudo -u vagrant -H bash -c /vagrant/bootstrap-user.sh
EOF
if [ $REBOOT ]; then
cat <<EOF >> bootstrap.sh
echo "**********************************************************"
echo "*************** REBOOTING FOR KERNEL UPDATE **************"
echo "**********************************************************"
reboot
EOF
fi
chmod a+x bootstrap.sh
}
write_bootstrap_user() {
cat <<EOF > bootstrap-user.sh
#! /bin/bash -e
cd /home/vagrant
mkdir -p src
cd src
git clone https://github.com/bradsokol/home-dir.git
cd home-dir
for f in .*; do
if [ -f "\$f" ]; then
rm -f \$HOME/\$f
ln -s \$(pwd)/\$f \$HOME/\$f
fi
done
cd \$HOME
mkdir -p .vim/autoload .vim/bundle && \
curl -LSso .vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
cd \$HOME/.vim/bundle
git clone https://github.com/mileszs/ack.vim.git
git clone https://github.com/kien/ctrlp.vim.git
git clone https://github.com/scrooloose/nerdtree.git
git clone https://github.com/ervandew/supertab.git
git clone https://github.com/tpope/vim-commentary.git
git clone https://github.com/mhinz/vim-signify
git clone https://github.com/tpope/vim-vividchalk.git
git clone https://github.com/bling/vim-airline
EOF
if [ $DOCKER ]; then
cat <<EOF >> bootstrap-user.sh
cd \$HOME/.vim/bundle
git clone https://github.com/ekalinin/Dockerfile.vim.git
EOF
fi
if [ $PYTHON ]; then
cat <<EOF >> bootstrap-user.sh
cd \$HOME/.vim/bundle
git clone https://github.com/nvie/vim-flake8.git
EOF
fi
if [ $RUBY ]; then
cat <<EOF >> bootstrap-user.sh
cd \$HOME
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="\$HOME/.rbenv/bin:\$PATH"' >> ~/.bash_local
echo 'eval "\$(rbenv init -)"' >> .bash_local
source ~/.bash_local
rbenv install 2.2.2
EOF
fi
chmod a+x bootstrap-user.sh
}
while getopts ":dhmpr4" OPTION; do
case $OPTION in
d)
DOCKER=1
;;
h)
usage
exit 0
;;
m)
MERCURIAL=1
;;
p)
PYTHON=1
;;
r)
RUBY=1
;;
4)
UBUNTU1404=1
;;
?)
echo "Invalid option: -$OPTARG"
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z $1 ]; then
echo Box name is required.
usage
exit 1
fi
BOX_NAME=$1
if [ -d $BOX_NAME ]; then
echo Directory $BOX_NAME already exists.
exit 1
fi
mkdir $BOX_NAME
cd $BOX_NAME
write_vagrantfile $BOX_NAME
write_bootstrap
write_bootstrap_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment