Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Configurable params |
| 3 | BRIDGE=${BRIDGE:-br0} |
Anthony Young | b748e69 | 2011-09-13 10:16:13 -0700 | [diff] [blame] | 4 | CONTAINER=${CONTAINER:-STACK} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 5 | CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} |
| 6 | CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} |
| 7 | CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0} |
| 8 | CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1} |
| 9 | NAMESERVER=${NAMESERVER:-192.168.1.1} |
| 10 | COPYENV=${COPYENV:-1} |
| 11 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 12 | # Create lxc configuration |
| 13 | LXC_CONF=/tmp/$CONTAINER.conf |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 14 | cat > $LXC_CONF <<EOF |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 15 | lxc.network.type = veth |
| 16 | lxc.network.link = $BRIDGE |
| 17 | lxc.network.flags = up |
| 18 | lxc.network.ipv4 = $CONTAINER_CIDR |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 19 | # allow tap/tun devices |
Anthony Young | f49d7ee | 2011-09-13 03:29:52 -0700 | [diff] [blame] | 20 | lxc.cgroup.devices.allow = c 10:200 rwm |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 21 | EOF |
| 22 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 23 | # Shutdown any existing container |
| 24 | lxc-stop -n $CONTAINER |
| 25 | |
| 26 | # This prevents zombie containers |
| 27 | cgdelete -r cpu,net_cls:$CONTAINER |
| 28 | |
| 29 | # Warm the base image on first install |
| 30 | CACHEDIR=/var/cache/lxc/natty/rootfs-amd64 |
| 31 | if [ ! -d $CACHEDIR ]; then |
| 32 | # trigger the initial debootstrap |
| 33 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
| 34 | chroot $CACHEDIR apt-get update |
| 35 | chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"` |
| 36 | chroot $CACHEDIR pip install `cat pips/*` |
| 37 | fi |
| 38 | |
| 39 | # Destroy the old container |
| 40 | lxc-destroy -n $CONTAINER |
| 41 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 42 | # Create the container |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 43 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 44 | |
Anthony Young | f9998ab | 2011-09-13 10:43:44 -0700 | [diff] [blame^] | 45 | # Specify where our container rootfs lives |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 46 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 47 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 48 | # Create a stack user that is a member of the libvirtd group so that stack |
| 49 | # is able to interact with libvirt. |
| 50 | chroot $ROOTFS groupadd libvirtd |
| 51 | chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd |
| 52 | |
| 53 | # a simple password - pass |
| 54 | echo stack:pass | chroot $ROOTFS chpasswd |
| 55 | |
| 56 | # and has sudo ability (in the future this should be limited to only what |
| 57 | # stack requires) |
| 58 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 59 | |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 60 | # Copy over your ssh keys and env if desired |
| 61 | if [ "$COPYENV" = "1" ]; then |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 62 | cp -pr ~/.ssh $ROOTFS/opt/.ssh |
| 63 | cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys |
| 64 | cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig |
| 65 | cp -pr ~/.vimrc $ROOTFS/opt/.vimrc |
| 66 | cp -pr ~/.bashrc $ROOTFS/opt/.bashrc |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 67 | fi |
| 68 | |
Anthony Young | 1003952 | 2011-09-13 10:05:07 -0700 | [diff] [blame] | 69 | # Give stack ownership over /opt so it may do the work needed |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 70 | chroot $ROOTFS chown -R stack /opt |
| 71 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 72 | # Configure instance network |
| 73 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 74 | cat > $INTERFACES <<EOF |
| 75 | auto lo |
| 76 | iface lo inet loopback |
| 77 | |
| 78 | auto eth0 |
| 79 | iface eth0 inet static |
| 80 | address $CONTAINER_IP |
| 81 | netmask $CONTAINER_NETMASK |
| 82 | gateway $CONTAINER_GATEWAY |
| 83 | EOF |
| 84 | |
| 85 | # Configure the first run installer |
| 86 | INSTALL_SH=$ROOTFS/root/install.sh |
| 87 | cat > $INSTALL_SH <<EOF |
| 88 | #!/bin/bash |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 89 | # Disable startup script |
Anthony Young | eeba886 | 2011-09-13 03:02:38 -0700 | [diff] [blame] | 90 | echo \#\!/bin/sh -e > /etc/rc.local |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 91 | # Make sure dns is set up |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 92 | echo "nameserver $NAMESERVER" | resolvconf -a eth0 |
| 93 | sleep 1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 94 | |
| 95 | # Install and run stack.sh |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame] | 96 | apt-get update |
| 97 | apt-get -y --force-yes install git-core vim-nox sudo |
Anthony Young | bdbe6d9 | 2011-09-13 09:43:46 -0700 | [diff] [blame] | 98 | su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack |
| 99 | su -c "cd ~/nfs-stack && ./stack.sh" stack |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 100 | EOF |
| 101 | |
| 102 | chmod 700 $INSTALL_SH |
| 103 | |
| 104 | # Make installer run on boot |
| 105 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 106 | cat > $RC_LOCAL <<EOF |
| 107 | #!/bin/sh -e |
| 108 | /root/install.sh |
| 109 | EOF |
| 110 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 111 | # Configure cgroup directory |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 112 | mkdir -p /cgroup |
| 113 | mount none -t cgroup /cgroup |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 114 | |
| 115 | # Start our container |
| 116 | lxc-start -d -n $CONTAINER |