2013-09-19 01:52:47 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-09-19 15:02:14 +00:00
|
|
|
# set -x
|
|
|
|
set -e
|
|
|
|
|
2013-09-19 01:52:47 +00:00
|
|
|
# Script used to build Ubuntu base vagrant-lxc containers
|
2013-09-19 15:02:14 +00:00
|
|
|
#
|
2013-09-19 01:52:47 +00:00
|
|
|
# USAGE:
|
2013-09-19 15:02:14 +00:00
|
|
|
# $ cd boxes && sudo ./build-ubuntu-box.sh UBUNTU_RELEASE BOX_ARCH
|
|
|
|
#
|
2013-09-19 21:14:09 +00:00
|
|
|
# To enable Chef or any other configuration management tool pass '1' to the
|
|
|
|
# corresponding env var:
|
|
|
|
# $ 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
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
##################################################################################
|
2013-09-19 15:02:14 +00:00
|
|
|
# 0 - Initial setup and sanity checks
|
2013-09-19 01:52:47 +00:00
|
|
|
|
2013-09-19 21:41:23 +00:00
|
|
|
TODAY=$(date -u +"%Y-%m-%d")
|
2013-09-21 01:12:26 +00:00
|
|
|
NOW=$(date -u)
|
2013-09-19 01:52:47 +00:00
|
|
|
RELEASE=${1:-"raring"}
|
|
|
|
ARCH=${2:-"amd64"}
|
2013-09-19 21:41:23 +00:00
|
|
|
PKG=vagrant-lxc-${RELEASE}-${ARCH}-${TODAY}.box
|
2013-09-19 15:02:14 +00:00
|
|
|
WORKING_DIR=/tmp/vagrant-lxc-${RELEASE}
|
2013-09-19 21:58:06 +00:00
|
|
|
VAGRANT_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key"
|
2013-09-21 01:12:26 +00:00
|
|
|
ROOTFS=/var/lib/lxc/${RELEASE}-base/rootfs
|
2013-09-19 15:02:14 +00:00
|
|
|
|
|
|
|
# Providing '1' will enable these tools
|
|
|
|
CHEF=${CHEF:-0}
|
|
|
|
PUPPET=${PUPPET:-0}
|
|
|
|
SALT=${SALT:-0}
|
|
|
|
BABUSHKA=${BABUSHKA:-0}
|
2013-09-19 01:52:47 +00:00
|
|
|
|
2013-09-19 21:41:23 +00:00
|
|
|
# Path to files bundled with the box
|
|
|
|
CWD=`readlink -f .`
|
|
|
|
LXC_TEMPLATE=${CWD}/common/lxc-template
|
|
|
|
LXC_CONF=${CWD}/common/lxc.conf
|
|
|
|
METATADA_JSON=${CWD}/common/metadata.json
|
|
|
|
|
2013-09-19 15:02:14 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
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
|
2013-09-19 01:52:47 +00:00
|
|
|
|
2013-09-21 02:01:24 +00:00
|
|
|
# Fixes some networking issues
|
|
|
|
# See https://github.com/fgrehm/vagrant-lxc/issues/91 for more info
|
|
|
|
echo 'ff02::3 ip6-allhosts' >> ${ROOTFS}/etc/hosts
|
|
|
|
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
##################################################################################
|
|
|
|
# 2 - Prepare vagrant user
|
|
|
|
|
2013-09-19 15:02:14 +00:00
|
|
|
mv ${ROOTFS}/home/{ubuntu,vagrant}
|
2013-09-19 01:52:47 +00:00
|
|
|
chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu
|
2013-09-19 15:02:14 +00:00
|
|
|
chroot ${ROOTFS} groupmod -n vagrant ubuntu
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd
|
|
|
|
|
|
|
|
|
|
|
|
##################################################################################
|
|
|
|
# 3 - Setup SSH access and passwordless sudo
|
|
|
|
|
|
|
|
# Configure SSH access
|
|
|
|
mkdir -p ${ROOTFS}/home/vagrant/.ssh
|
2013-09-19 21:58:06 +00:00
|
|
|
echo $VAGRANT_KEY > ${ROOTFS}/home/vagrant/.ssh/authorized_keys
|
2013-09-19 01:52:47 +00:00
|
|
|
chroot ${ROOTFS} chown -R vagrant: /home/vagrant/.ssh
|
|
|
|
|
|
|
|
# Enable passwordless sudo for users under the "sudo" group
|
2013-11-16 01:27:25 +00:00
|
|
|
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > ${ROOTFS}/etc/sudoers.d/vagrant
|
2013-11-16 01:19:35 +00:00
|
|
|
chmod 0440 ${ROOTFS}/etc/sudoers.d/vagrant
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
##################################################################################
|
2013-09-19 21:14:09 +00:00
|
|
|
# 4 - Add some goodies and update packages
|
2013-09-19 01:52:47 +00:00
|
|
|
|
2013-10-26 05:51:13 +00:00
|
|
|
PACKAGES=(vim curl wget man-db bash-completion python-software-properties software-properties-common)
|
2013-09-19 01:52:47 +00:00
|
|
|
chroot ${ROOTFS} apt-get install ${PACKAGES[*]} -y --force-yes
|
2013-09-19 21:14:09 +00:00
|
|
|
chroot ${ROOTFS} apt-get upgrade -y --force-yes
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
##################################################################################
|
|
|
|
# 5 - Configuration management tools
|
|
|
|
|
2013-09-19 15:02:14 +00:00
|
|
|
if [ $CHEF = 1 ]; then
|
|
|
|
./common/install-chef $ROOTFS
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $PUPPET = 1 ]; then
|
|
|
|
./common/install-puppet $ROOTFS
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $SALT = 1 ]; then
|
|
|
|
./common/install-salt $ROOTFS
|
|
|
|
fi
|
2013-09-19 01:52:47 +00:00
|
|
|
|
2013-09-19 15:02:14 +00:00
|
|
|
if [ $BABUSHKA = 1 ]; then
|
|
|
|
./common/install-babushka $ROOTFS
|
|
|
|
fi
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
##################################################################################
|
|
|
|
# 6 - Free up some disk space
|
|
|
|
|
|
|
|
rm -rf ${ROOTFS}/tmp/*
|
|
|
|
chroot ${ROOTFS} apt-get clean
|
|
|
|
|
|
|
|
|
|
|
|
##################################################################################
|
|
|
|
# 7 - Build box package
|
|
|
|
|
|
|
|
# Compress container's rootfs
|
2013-09-19 15:02:14 +00:00
|
|
|
cd $(dirname $ROOTFS)
|
2013-09-19 01:52:47 +00:00
|
|
|
tar --numeric-owner -czf /tmp/vagrant-lxc-${RELEASE}/rootfs.tar.gz ./rootfs/*
|
|
|
|
|
|
|
|
# Prepare package contents
|
2013-09-19 15:02:14 +00:00
|
|
|
cd $WORKING_DIR
|
2013-09-19 21:41:23 +00:00
|
|
|
cp $LXC_TEMPLATE .
|
|
|
|
cp $LXC_CONF .
|
|
|
|
cp $METATADA_JSON .
|
2013-09-19 01:52:47 +00:00
|
|
|
chmod +x lxc-template
|
2013-09-19 21:41:23 +00:00
|
|
|
sed -i "s/<TODAY>/${NOW}/" metadata.json
|
2013-09-19 01:52:47 +00:00
|
|
|
|
|
|
|
# Vagrant box!
|
|
|
|
tar -czf $PKG ./*
|
|
|
|
|
2013-09-21 02:13:30 +00:00
|
|
|
chmod +rw ${WORKING_DIR}/${PKG}
|
|
|
|
mkdir -p ${CWD}/output
|
|
|
|
mv ${WORKING_DIR}/${PKG} ${CWD}/output
|
|
|
|
|
|
|
|
# Clean up after ourselves
|
|
|
|
rm -rf ${WORKING_DIR}
|
|
|
|
lxc-destroy -n ${RELEASE}-base
|
|
|
|
|
|
|
|
echo "The base box was built successfully to ${CWD}/output/${PKG}"
|