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