Anthony Young | 1b7a42e | 2011-10-19 02:34:06 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Echo commands |
| 4 | set -o xtrace |
| 5 | |
| 6 | # Keep track of the current directory |
| 7 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 8 | |
| 9 | ROOT_PASSWORD=${ROOT_PASSWORD:password} |
| 10 | PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack} |
| 11 | IMAGES_DIR=$PERSIST_DIR/images |
| 12 | mkdir -p $UEC_DIR |
| 13 | |
| 14 | # Move to top devstack dir |
| 15 | cd .. |
| 16 | |
| 17 | # Abort if localrc is not set |
| 18 | if [ ! -e ./localrc ]; then |
| 19 | echo "You must have a localrc with ALL necessary passwords defined before proceeding." |
| 20 | echo "See stack.sh for required passwords." |
| 21 | exit 1 |
| 22 | fi |
| 23 | |
| 24 | # Source params |
| 25 | source ./stackrc |
| 26 | |
| 27 | # Base image (oneiric by default) |
| 28 | IMAGE_FNAME=natty.raw |
| 29 | IMAGE_NAME=natty |
| 30 | |
| 31 | BASE_IMAGE=$PERSIST_DIR/images/natty.raw |
| 32 | BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy |
| 33 | |
| 34 | VM_NAME=${VM_NAME:-kvmstack} |
| 35 | virsh shutdown $VM_NAME |
| 36 | virsh destroy $VM_NAME |
| 37 | |
| 38 | VM_DIR=$PERSIST_DIR/instances/$VM_NAME |
| 39 | |
| 40 | mkdir -p $VM_DIR |
| 41 | |
| 42 | # Where to mount |
| 43 | COPY_DIR=$VM_DIR/copy |
| 44 | mkdir -p $COPY_DIR |
| 45 | |
| 46 | |
| 47 | if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then |
| 48 | cd $TOOLS_DIR |
| 49 | ./make_image.sh -m -r 5000 natty raw |
| 50 | mv natty.raw $BASE_IMAGE |
| 51 | cd $TOP_DIR |
| 52 | fi |
| 53 | |
| 54 | function unmount_images() { |
| 55 | # unmount the filesystem |
| 56 | while df | grep -q $COPY_DIR; do |
| 57 | umount $COPY_DIR || echo 'ok' |
| 58 | sleep 1 |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | # unmount from failed runs |
| 63 | unmount_images |
| 64 | |
| 65 | function kill_tail() { |
| 66 | unmount_images |
| 67 | exit 1 |
| 68 | } |
| 69 | |
| 70 | if [ ! -e $BASE_IMAGE_COPY ]; then |
| 71 | cp -p $BASE_IMAGE $BASE_IMAGE_COPY |
| 72 | fi |
| 73 | |
| 74 | # Install deps |
| 75 | apt-get install -y kvm libvirt-bin kpartx |
| 76 | |
| 77 | # Let Ctrl-c kill tail and exit |
| 78 | trap kill_tail SIGINT |
| 79 | |
| 80 | # Where code will live in image |
| 81 | DEST=${DEST:-/opt/stack} |
| 82 | |
| 83 | # Mount the file system |
| 84 | mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR |
| 85 | |
| 86 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 87 | # be owned by the installation user, we create the directory and change the |
| 88 | # ownership to the proper user. |
| 89 | function git_clone { |
| 90 | if [ ! -d $2 ]; then |
| 91 | sudo mkdir $2 |
| 92 | sudo chown `whoami` $2 |
| 93 | git clone $1 $2 |
| 94 | cd $2 |
| 95 | # This checkout syntax works for both branches and tags |
| 96 | git checkout $3 |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | # Make sure that base requirements are installed |
| 101 | cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf |
| 102 | chroot $COPY_DIR apt-get update |
| 103 | chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"` |
| 104 | chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server |
| 105 | chroot $COPY_DIR pip install `cat files/pips/*` |
| 106 | |
| 107 | # Clean out code repos if directed to do so |
| 108 | if [ "$CLEAN" = "1" ]; then |
| 109 | rm -rf $COPY_DIR/$DEST |
| 110 | fi |
| 111 | |
| 112 | # Cache openstack code |
| 113 | mkdir -p $COPY_DIR/$DEST |
| 114 | git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH |
| 115 | git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH |
| 116 | git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH |
| 117 | git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH |
| 118 | git_clone $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_TAG |
| 119 | git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH |
| 120 | git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH |
| 121 | git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH |
| 122 | git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH |
| 123 | |
| 124 | # unmount the filesystems |
| 125 | unmount_images |
| 126 | |
| 127 | rm -f $VM_DIR/kernel |
| 128 | rm -f $VM_DIR/disk |
| 129 | |
| 130 | cd $VM_DIR |
| 131 | qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk |
| 132 | |
| 133 | BRIDGE=${BRIDGE:-br0} |
| 134 | CONTAINER=${CONTAINER:-STACK} |
| 135 | CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} |
| 136 | CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} |
| 137 | CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0} |
| 138 | CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1} |
| 139 | CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7} |
| 140 | |
| 141 | # Create configuration |
| 142 | LIBVIRT_XML=libvirt.xml |
| 143 | cat > $LIBVIRT_XML <<EOF |
| 144 | <domain type='kvm'> |
| 145 | <name>$VM_NAME</name> |
| 146 | <memory>1524288</memory> |
| 147 | <os> |
| 148 | <type>hvm</type> |
| 149 | <!-- |
| 150 | <kernel>$VM_DIR/kernel</kernel> |
| 151 | <cmdline>root=/dev/vda console=ttyS0</cmdline> |
| 152 | --> |
| 153 | </os> |
| 154 | <features> |
| 155 | <acpi/> |
| 156 | </features> |
| 157 | <vcpu>1</vcpu> |
| 158 | <devices> |
| 159 | <disk type='file'> |
| 160 | <driver type='qcow2'/> |
| 161 | <source file='$VM_DIR/disk'/> |
| 162 | <target dev='vda' bus='virtio'/> |
| 163 | </disk> |
| 164 | |
| 165 | <interface type='bridge'> |
| 166 | <source bridge='$BRIDGE'/> |
| 167 | <mac address='$CONTAINER_MAC'/> |
| 168 | </interface> |
| 169 | |
| 170 | <!-- The order is significant here. File must be defined first --> |
| 171 | <serial type="file"> |
| 172 | <source path='$VM_DIR/console.log'/> |
| 173 | <target port='1'/> |
| 174 | </serial> |
| 175 | |
| 176 | <console type='pty' tty='/dev/pts/2'> |
| 177 | <source path='/dev/pts/2'/> |
| 178 | <target port='0'/> |
| 179 | </console> |
| 180 | |
| 181 | <serial type='pty'> |
| 182 | <source path='/dev/pts/2'/> |
| 183 | <target port='0'/> |
| 184 | </serial> |
| 185 | |
| 186 | <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/> |
| 187 | </devices> |
| 188 | </domain> |
| 189 | EOF |
| 190 | |
| 191 | ROOTFS=$VM_DIR/root |
| 192 | mkdir -p $ROOTFS |
| 193 | |
| 194 | modprobe nbd max_part=63 |
| 195 | |
| 196 | umount $ROOTFS || echo 'ok' |
| 197 | qemu-nbd -d /dev/nbd5 || echo 'ok' |
| 198 | |
| 199 | qemu-nbd -c /dev/nbd5 disk |
| 200 | mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4 |
| 201 | |
| 202 | # Configure instance network |
| 203 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 204 | cat > $INTERFACES <<EOF |
| 205 | auto lo |
| 206 | iface lo inet loopback |
| 207 | |
| 208 | auto eth0 |
| 209 | iface eth0 inet static |
| 210 | address $CONTAINER_IP |
| 211 | netmask $CONTAINER_NETMASK |
| 212 | gateway $CONTAINER_GATEWAY |
| 213 | EOF |
| 214 | |
| 215 | chroot $ROOTFS groupadd libvirtd |
| 216 | chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd |
| 217 | cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack |
| 218 | echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd |
| 219 | |
| 220 | # a simple password - pass |
| 221 | echo "stack:pass" | chroot $ROOTFS chpasswd |
| 222 | |
| 223 | # stack requires) |
| 224 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 225 | |
| 226 | # Gracefully cp only if source file/dir exists |
| 227 | function cp_it { |
| 228 | if [ -e $1 ] || [ -d $1 ]; then |
| 229 | cp -pRL $1 $2 |
| 230 | fi |
| 231 | } |
| 232 | |
| 233 | # Copy over your ssh keys and env if desired |
| 234 | COPYENV=${COPYENV:-1} |
| 235 | if [ "$COPYENV" = "1" ]; then |
| 236 | cp_it ~/.ssh $ROOTFS/$DEST/.ssh |
| 237 | cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys |
| 238 | cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig |
| 239 | cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc |
| 240 | cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc |
| 241 | fi |
| 242 | |
| 243 | # Configure the runner |
| 244 | RUN_SH=$ROOTFS/$DEST/run.sh |
| 245 | cat > $RUN_SH <<EOF |
| 246 | #!/usr/bin/env bash |
| 247 | sleep 1 |
| 248 | |
| 249 | # Kill any existing screens |
| 250 | killall screen |
| 251 | |
| 252 | # Install and run stack.sh |
| 253 | sudo apt-get update |
| 254 | sudo apt-get -y --force-yes install git-core vim-nox sudo |
| 255 | if [ ! -d "$DEST/devstack" ]; then |
| 256 | git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack |
| 257 | fi |
| 258 | cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log |
| 259 | echo >> /$DEST/run.sh.log |
| 260 | echo >> /$DEST/run.sh.log |
| 261 | echo "All done! Time to start clicking." >> /$DEST/run.sh.log |
| 262 | EOF |
| 263 | |
| 264 | # Make the run.sh executable |
| 265 | chmod 755 $RUN_SH |
| 266 | |
| 267 | # Make runner launch on boot |
| 268 | RC_LOCAL=$ROOTFS/etc/init.d/local |
| 269 | cat > $RC_LOCAL <<EOF |
| 270 | #!/bin/sh -e |
| 271 | su -c "$DEST/run.sh" stack |
| 272 | EOF |
| 273 | |
| 274 | # Make our ip address hostnames look nice at the command prompt |
| 275 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc |
| 276 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile |
| 277 | |
| 278 | # Give stack ownership over $DEST so it may do the work needed |
| 279 | chroot $ROOTFS chown -R stack $DEST |
| 280 | |
| 281 | chmod +x $RC_LOCAL |
| 282 | chroot $ROOTFS sudo update-rc.d local defaults 80 |
| 283 | |
| 284 | umount $ROOTFS |
| 285 | qemu-nbd -d /dev/nbd5 |
| 286 | |
| 287 | cd $VM_DIR |
| 288 | virsh create libvirt.xml |