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