From 7efef2a489bd2ea8b1e014ec69eefaa43120e846 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 19 Sep 2013 12:02:14 -0300 Subject: [PATCH] Just enough code to deal with building base ubuntu boxes with support for configuration management tools --- boxes/build-ubuntu-box.sh | 75 ++++++++++++++++++++++++++++------- boxes/common/install-babushka | 5 +-- boxes/common/install-chef | 3 +- boxes/common/install-puppet | 3 +- boxes/common/install-salt | 3 +- 5 files changed, 66 insertions(+), 23 deletions(-) diff --git a/boxes/build-ubuntu-box.sh b/boxes/build-ubuntu-box.sh index 1e3e902..76de546 100755 --- a/boxes/build-ubuntu-box.sh +++ b/boxes/build-ubuntu-box.sh @@ -1,29 +1,67 @@ #!/bin/bash +# set -x +set -e + # Script used to build Ubuntu base vagrant-lxc containers +# # USAGE: -# $ sudo ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH +# $ cd boxes && sudo ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH +# +# To enable Chef or any other configuration management tool pass '1' to it: +# $ CHEF=1 sudo -E ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH +# $ PUPPET=1 sudo -E ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH +# $ SALT=1 sudo -E ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH +# $ BABUSHKA=1 sudo -E ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH # TODO: * Add support for flushing cache and specifying a custom base Ubuntu lxc # template instead of system's built in # * Embed vagrant public key # * Add date to metadata.json # * Ensure it is in sync with master +# * Stuff from locales (rcarmo and discourse stuff) +# * Clean up when finished + +################################################################################## +# 0 - Initial setup and sanity checks + +RELEASE=${1:-"raring"} +ARCH=${2:-"amd64"} +PKG=vagrant-lxc-${RELEASE}-${ARCH}.box +WORKING_DIR=/tmp/vagrant-lxc-${RELEASE} + +# Providing '1' will enable these tools +CHEF=${CHEF:-0} +PUPPET=${PUPPET:-0} +SALT=${SALT:-0} +BABUSHKA=${BABUSHKA:-0} + +# Set up a working dir +mkdir -p $WORKING_DIR + +if [ -f "${WORKING_DIR}/${PKG}" ]; then + echo "Found a box on ${WORKING_DIR}/${PKG} already!" + exit 1 +fi ################################################################################## # 1 - Create the base container -RELEASE=${1:-"raring"} -ARCH=${2:-"amd64"} - -lxc-create -n ${RELEASE}-base -t ubuntu -- --release ${RELEASE} --arch ${ARCH} +if $(lxc-ls | grep -q "${RELEASE}-base"); then + echo "Base container already exists, please remove it with \`lxc-destroy -n ${RELEASE}-base\`!" + exit 1 +else + lxc-create -n ${RELEASE}-base -t ubuntu -- --release ${RELEASE} --arch ${ARCH} +fi ################################################################################## # 2 - Prepare vagrant user ROOTFS=/var/lib/lxc/${RELEASE}-base/rootfs +mv ${ROOTFS}/home/{ubuntu,vagrant} chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu +chroot ${ROOTFS} groupmod -n vagrant ubuntu echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd @@ -46,15 +84,28 @@ sed -i -e \ ################################################################################## # 4 - Add some goodies -PACKAGES=(vim curl wget manpages bash-completion) +PACKAGES=(vim curl wget man-db bash-completion) chroot ${ROOTFS} apt-get install ${PACKAGES[*]} -y --force-yes ################################################################################## # 5 - Configuration management tools +if [ $CHEF = 1 ]; then + ./common/install-chef $ROOTFS +fi -# TODO +if [ $PUPPET = 1 ]; then + ./common/install-puppet $ROOTFS +fi + +if [ $SALT = 1 ]; then + ./common/install-salt $ROOTFS +fi + +if [ $BABUSHKA = 1 ]; then + ./common/install-babushka $ROOTFS +fi ################################################################################## @@ -67,22 +118,18 @@ chroot ${ROOTFS} apt-get clean ################################################################################## # 7 - Build box package -# Set up a working dir -mkdir -p /tmp/vagrant-lxc-${RELEASE} - # Compress container's rootfs -cd /var/lib/lxc/${RELEASE}-base +cd $(dirname $ROOTFS) tar --numeric-owner -czf /tmp/vagrant-lxc-${RELEASE}/rootfs.tar.gz ./rootfs/* # Prepare package contents -cd /tmp/vagrant-lxc-${RELEASE} +cd $WORKING_DIR wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc-template wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc.conf wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/metadata.json chmod +x lxc-template # Vagrant box! -PKG=vagrant-lxc-${RELEASE}-${ARCH}.box tar -czf $PKG ./* -echo "The base box was built successfully to ${PKG}" +echo "The base box was built successfully to ${WORKING_DIR}/${PKG}" diff --git a/boxes/common/install-babushka b/boxes/common/install-babushka index 23a7455..6d43d76 100755 --- a/boxes/common/install-babushka +++ b/boxes/common/install-babushka @@ -1,12 +1,11 @@ #!/bin/bash -cache=`readlink -f .` -rootfs="${cache}/rootfs" +rootfs=$1 echo "installing babushka" cat > $rootfs/tmp/install-babushka.sh << EOF #!/bin/sh -curl -L https://babushka.me/up | sudo bash < /dev/null +curl https://babushka.me/up | sudo bash EOF chmod +x $rootfs/tmp/install-babushka.sh diff --git a/boxes/common/install-chef b/boxes/common/install-chef index 55fd5e3..d99d0d7 100755 --- a/boxes/common/install-chef +++ b/boxes/common/install-chef @@ -1,7 +1,6 @@ #!/bin/bash -cache=`readlink -f .` -rootfs="${cache}/rootfs" +rootfs=$1 echo "installing chef" cat > $rootfs/tmp/install-chef.sh << EOF diff --git a/boxes/common/install-puppet b/boxes/common/install-puppet index a746229..1e1650e 100755 --- a/boxes/common/install-puppet +++ b/boxes/common/install-puppet @@ -1,7 +1,6 @@ #!/bin/bash -cache=`readlink -f .` -rootfs="${cache}/rootfs" +rootfs=$1 echo "installing puppet" wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb -O "${rootfs}/tmp/puppetlabs-release-stable.deb" diff --git a/boxes/common/install-salt b/boxes/common/install-salt index daab9ab..e7f74a2 100755 --- a/boxes/common/install-salt +++ b/boxes/common/install-salt @@ -1,7 +1,6 @@ #!/bin/bash -cache=`readlink -f .` -rootfs="${cache}/rootfs" +rootfs=$1 echo "installing salt" chroot $rootfs apt-add-repository -y ppa:saltstack/salt