2011-09-13 04:09:55 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Configurable params
|
|
|
|
BRIDGE=${BRIDGE:-br0}
|
2011-09-13 17:16:13 +00:00
|
|
|
CONTAINER=${CONTAINER:-STACK}
|
2011-09-13 04:09:55 +00:00
|
|
|
CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
|
|
|
|
CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
|
|
|
|
CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
|
|
|
|
CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
|
|
|
|
NAMESERVER=${NAMESERVER:-192.168.1.1}
|
|
|
|
COPYENV=${COPYENV:-1}
|
2011-09-13 17:40:04 +00:00
|
|
|
|
|
|
|
# Create lxc configuration
|
|
|
|
LXC_CONF=/tmp/$CONTAINER.conf
|
|
|
|
cat > $LXC_CONF <<EOF
|
|
|
|
lxc.network.type = veth
|
|
|
|
lxc.network.link = $BRIDGE
|
|
|
|
lxc.network.flags = up
|
|
|
|
lxc.network.ipv4 = $CONTAINER_CIDR
|
|
|
|
# allow tap/tun devices
|
|
|
|
lxc.cgroup.devices.allow = c 10:200 rwm
|
|
|
|
EOF
|
2011-09-13 04:09:55 +00:00
|
|
|
|
2011-09-13 16:17:56 +00:00
|
|
|
# Shutdown any existing container
|
2011-09-13 04:09:55 +00:00
|
|
|
lxc-stop -n $CONTAINER
|
2011-09-13 16:57:31 +00:00
|
|
|
|
2011-09-13 16:17:56 +00:00
|
|
|
# This prevents zombie containers
|
2011-09-13 10:02:38 +00:00
|
|
|
cgdelete -r cpu,net_cls:$CONTAINER
|
2011-09-13 16:57:31 +00:00
|
|
|
|
2011-09-13 17:40:04 +00:00
|
|
|
# Warm the base image on first install
|
2011-09-13 09:05:12 +00:00
|
|
|
CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
|
2011-09-13 17:40:04 +00:00
|
|
|
if [ ! -d $CACHEDIR ]; then
|
|
|
|
# trigger the initial debootstrap
|
|
|
|
lxc-create -n $CONTAINER -t natty -f $LXC_CONF
|
|
|
|
chroot $CACHEDIR apt-get update
|
|
|
|
chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
|
|
|
|
chroot $CACHEDIR pip install `cat pips/*`
|
2011-09-13 09:05:12 +00:00
|
|
|
fi
|
2011-09-13 04:09:55 +00:00
|
|
|
|
2011-09-13 17:40:04 +00:00
|
|
|
# Destroy the old container
|
|
|
|
lxc-destroy -n $CONTAINER
|
2011-09-13 04:09:55 +00:00
|
|
|
|
2011-09-13 16:57:31 +00:00
|
|
|
# Create the container
|
2011-09-13 16:17:56 +00:00
|
|
|
lxc-create -n $CONTAINER -t natty -f $LXC_CONF
|
2011-09-13 08:28:18 +00:00
|
|
|
|
2011-09-13 17:43:44 +00:00
|
|
|
# Specify where our container rootfs lives
|
2011-09-13 04:09:55 +00:00
|
|
|
ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
|
|
|
|
|
2011-09-13 16:57:31 +00:00
|
|
|
# Create a stack user that is a member of the libvirtd group so that stack
|
|
|
|
# is able to interact with libvirt.
|
|
|
|
chroot $ROOTFS groupadd libvirtd
|
|
|
|
chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
|
|
|
|
|
|
|
|
# a simple password - pass
|
|
|
|
echo stack:pass | chroot $ROOTFS chpasswd
|
|
|
|
|
|
|
|
# and has sudo ability (in the future this should be limited to only what
|
|
|
|
# stack requires)
|
|
|
|
echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
|
|
|
|
|
2011-09-13 08:28:18 +00:00
|
|
|
# Copy over your ssh keys and env if desired
|
|
|
|
if [ "$COPYENV" = "1" ]; then
|
2011-09-13 16:57:31 +00:00
|
|
|
cp -pr ~/.ssh $ROOTFS/opt/.ssh
|
|
|
|
cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
|
|
|
|
cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
|
|
|
|
cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
|
|
|
|
cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
|
2011-09-13 04:09:55 +00:00
|
|
|
fi
|
|
|
|
|
2011-09-13 17:05:07 +00:00
|
|
|
# Give stack ownership over /opt so it may do the work needed
|
2011-09-13 16:57:31 +00:00
|
|
|
chroot $ROOTFS chown -R stack /opt
|
|
|
|
|
2011-09-13 04:09:55 +00:00
|
|
|
# Configure instance network
|
|
|
|
INTERFACES=$ROOTFS/etc/network/interfaces
|
|
|
|
cat > $INTERFACES <<EOF
|
|
|
|
auto lo
|
|
|
|
iface lo inet loopback
|
|
|
|
|
|
|
|
auto eth0
|
|
|
|
iface eth0 inet static
|
|
|
|
address $CONTAINER_IP
|
|
|
|
netmask $CONTAINER_NETMASK
|
|
|
|
gateway $CONTAINER_GATEWAY
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Configure the first run installer
|
|
|
|
INSTALL_SH=$ROOTFS/root/install.sh
|
|
|
|
cat > $INSTALL_SH <<EOF
|
|
|
|
#!/bin/bash
|
2011-09-13 16:57:31 +00:00
|
|
|
# Disable startup script
|
2011-09-13 10:02:38 +00:00
|
|
|
echo \#\!/bin/sh -e > /etc/rc.local
|
2011-09-13 16:57:31 +00:00
|
|
|
# Make sure dns is set up
|
2011-09-13 04:09:55 +00:00
|
|
|
echo "nameserver $NAMESERVER" | resolvconf -a eth0
|
|
|
|
sleep 1
|
2011-09-13 09:05:12 +00:00
|
|
|
|
|
|
|
# Install and run stack.sh
|
2011-09-13 06:25:37 +00:00
|
|
|
apt-get update
|
|
|
|
apt-get -y --force-yes install git-core vim-nox sudo
|
2011-09-13 16:43:46 +00:00
|
|
|
su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
|
|
|
|
su -c "cd ~/nfs-stack && ./stack.sh" stack
|
2011-09-13 04:09:55 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
chmod 700 $INSTALL_SH
|
|
|
|
|
|
|
|
# Make installer run on boot
|
|
|
|
RC_LOCAL=$ROOTFS/etc/rc.local
|
|
|
|
cat > $RC_LOCAL <<EOF
|
|
|
|
#!/bin/sh -e
|
|
|
|
/root/install.sh
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Configure cgroup directory
|
2011-09-13 09:05:12 +00:00
|
|
|
mkdir -p /cgroup
|
|
|
|
mount none -t cgroup /cgroup
|
2011-09-13 04:09:55 +00:00
|
|
|
|
|
|
|
# Start our container
|
|
|
|
lxc-start -d -n $CONTAINER
|