Skip to content

Instantly share code, notes, and snippets.

@calbrecht
Created September 8, 2013 18:15
Show Gist options
  • Save calbrecht/6487121 to your computer and use it in GitHub Desktop.
Save calbrecht/6487121 to your computer and use it in GitHub Desktop.
vagrant-lxc provider test
#!/bin/bash
if [[ "root" != "$(whoami)" ]] ; then
echo 'Err: you must be root to run this script'
exit 1
fi
if [[ ! $(which vagrant >/dev/null) ]] ; then
wget http://files.vagrantup.com/packages/b12c7e8814171c1295ef82416ffe51e8a168a244/vagrant_1.3.1_x86_64.deb
dpkg -i ./vagrant_1.3.1_x86_64.deb
rm ./vagrant_1.3.1_x86_64.deb
vagrant plugin list | grep -q vagrant-lxc || vagrant plugin install vagrant-lxc
fi
which lxc-create >/dev/null || apt-get install lxc -y
which bsdtar >/dev/null || apt-get install bsdtar -y
which redir >/dev/null || apt-get install redir -y
__DIR__=$(readlink -e $(dirname $0))
RELEASE=raring
ARCH=amd64
ROOTFS=rootfs
FORCE=0
TARBALL=${__DIR__}/archive-${RELEASE}-${ARCH}.tar
MIRROR=http://de.archive.ubuntu.com/ubuntu
exclude=busybox-initramfs,e2fslibs,e2fsprogs,initramfs-tools,initramfs-tools-bin,libplymouth2,multiarch-support,plymouth
include=vim,wget,openssh-server
if [[ ! -f ${TARBALL} || $FORCE = 1 ]] ; then
debootstrap --download-only --variant=minbase --exclude=$exclude --include=$include --arch=${ARCH} --make-tarball=${TARBALL} ${RELEASE} ${ROOTFS} ${MIRROR}
fi
if [[ ! -d ${ROOTFS} || $FORCE = 1 ]] ; then
debootstrap --variant=minbase --exclude=$exclude --include=$include --arch=${ARCH} --unpack-tarball=${TARBALL} ${RELEASE} ${ROOTFS} ${MIRROR}
fi
cat > ${ROOTFS}/etc/apt/sources.list <<EOD
deb ${MIRROR} ${RELEASE} main universe
deb ${MIRROR} ${RELEASE}-updates main universe
deb ${MIRROR} ${RELEASE}-security main universe
EOD
if [[ ! $(grep eth0 ${ROOTFS}/etc/network/interfaces) ]] ; then
cat >> ${ROOTFS}/etc/network/interfaces <<EOD
eth0 auto
iface eth0 inet dhcp
EOD
fi
sed -i 's/PermitRootLogin yes/PermitRootLogin without-password/' ${ROOTFS}/etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' ${ROOTFS}/etc/ssh/sshd_config
chroot="chroot ${ROOTFS}"
if [[ ! $($chroot which puppet) ]] ; then
$chroot wget http://apt.puppetlabs.com/puppetlabs-release-${RELEASE}.deb -O /tmp/puppetlabs-release-${RELEASE}.deb
$chroot dpkg -i /tmp/puppetlabs-release-${RELEASE}.deb
$chroot apt-get update
$chroot apt-get upgrade -y --force-yes
$chroot apt-get install puppet -y --force-yes
$chroot apt-get clean
fi
upd=$(stat -c %Y ${ROOTFS}/var/cache/apt/)
now=$(date +%s)
if [[ $((now - upd)) -gt 36000 ]] ; then
$chroot apt-get update
$chroot apt-get upgrade -y --force-yes
$chroot apt-get clean
fi
if [[ ! -f ${__DIR__}/id_rsa ]] ; then
ssh-keygen -q -t rsa -f ${__DIR__}/id_rsa -N ''
fi
mkdir -p ${ROOTFS}/root/.ssh
cat ${__DIR__}/id_rsa.pub > ${ROOTFS}/root/.ssh/authorized_keys
if [[ ! -f ${__DIR__}/lxc-template ]] ; then
wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc-template
fi
if [[ ! -f ${__DIR__}/lxc.conf ]] ; then
wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/lxc.conf
fi
if [[ ! -f ${__DIR__}/metadata.json ]] ; then
wget https://raw.github.com/fgrehm/vagrant-lxc/master/boxes/common/metadata.json
fi
chmod +x lxc-template
BOX_NAME=test-vagrant-lxc
BOX_URL=vagrant-lxc-${RELEASE}.box
tar --numeric-owner -C ${__DIR__} -cvzf rootfs.tar.gz rootfs/*
tar --numeric-owner -C ${__DIR__} -cvzf ${BOX_URL} rootfs.tar.gz lxc-template lxc.conf metadata.json
cat > Vagrantfile <<EOD
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "${BOX_NAME}"
config.vm.box_url = "${BOX_URL}"
config.ssh.username = "root"
config.ssh.private_key_path = "id_rsa"
end
EOD
echo "vagrant destroy; vagrant box remove ${BOX_NAME} lxc; vagrant box add ${BOX_NAME} ${BOX_URL} --provider lxc; vagrant up --provider lxc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment