diff options
author | Leif Johansson <leifj@sunet.se> | 2014-10-22 14:28:05 +0200 |
---|---|---|
committer | Leif Johansson <leifj@sunet.se> | 2014-10-22 14:28:05 +0200 |
commit | 4f300ca63268f73a63e01f5528ecdc5e6f46d67e (patch) | |
tree | bb976aad52f01bf7bdb41e6fbe96e1ca3084a6cb /global | |
parent | 52311bfbe28db9f5c05047c7f379fbfb965062b3 (diff) |
simple kvm builder using cloud images
Diffstat (limited to 'global')
-rwxr-xr-x | global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 42 | ||||
-rw-r--r-- | global/overlay/etc/cosmos/apt/cosmos_1.2-2_all.deb | bin | 0 -> 11724 bytes | |||
-rwxr-xr-x | global/overlay/usr/local/sbin/cosmos_vm | 124 |
3 files changed, 166 insertions, 0 deletions
diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh new file mode 100755 index 0000000..28cbde0 --- /dev/null +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +set -e + +cmd_hostname="$1" +if test -z "$cmd_hostname"; then + echo "Usage: $0 HOSTNAME REPO TAGPATTERN" + exit 1 +fi + +cmd_repo="$2" +if test -z "$cmd_repo"; then + echo "Usage $0 HOSTNAME REPO TAGPATTERN" + exit 2 +fi + +cmd_tags="$3" +if test -z "$cmd_tags"; then + echo "Usage $0 HOSTNAME REPO TAGPATTERN" + exit 3 +fi + +set -x + +apt-get -y install rsync git-core wget +dpkg -i cosmos_1.2-2_all.deb + +if ! test -d /var/cache/cosmos/repo; then + cosmos clone "$cmd_repo" +fi + +hostname $cmd_hostname + +perl -pi -e "s,#COSMOS_REPO_MODELS=.*,COSMOS_REPO_MODELS=\"\\\$COSMOS_REPO/global/:\\\$COSMOS_REPO/$cmd_hostname/\"," /etc/cosmos/cosmos.conf +perl -pi -e "s,#COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=.*,COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=\"${cmd_tags}*\"," /etc/cosmos/cosmos.conf + +env COSMOS_BASE=/var/cache/cosmos COSMOS_KEYS=/var/cache/cosmos/repo/global/overlay/etc/cosmos/keys /var/cache/cosmos/repo/global/post-tasks.d/015cosmos-trust + +(date; nohup cosmos -v update && nohup cosmos -v apply; date) 2>&1 | tee /var/log/cosmos.log + + +exit 0 diff --git a/global/overlay/etc/cosmos/apt/cosmos_1.2-2_all.deb b/global/overlay/etc/cosmos/apt/cosmos_1.2-2_all.deb Binary files differnew file mode 100644 index 0000000..9fe4434 --- /dev/null +++ b/global/overlay/etc/cosmos/apt/cosmos_1.2-2_all.deb diff --git a/global/overlay/usr/local/sbin/cosmos_vm b/global/overlay/usr/local/sbin/cosmos_vm new file mode 100755 index 0000000..bf27576 --- /dev/null +++ b/global/overlay/usr/local/sbin/cosmos_vm @@ -0,0 +1,124 @@ +#!/bin/bash + +set -- $(getopt h?H:D?s:B:M:C:R:i:g:n:I:G:N: "$@") + +src_image="" +size="1G" +dhcp="no" +hostname="default" +bridge="br0" +cpus="1" +mem="1024" +repo="git://code.mnt.se/mnt-cosmos.git" +tag="eduid-cosmos" +ip="" +gateway="" +netmask="" +resolver="" +ip6="" +gateway6="" +netmask6="64" + +while [ $# -gt 0 ]; do + case "$1" in + (-h) echo "Usage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 0;; + (-H) hostname="$2"; shift;; + (-s) src_image="$2"; shift;; + (-D) dhcp="yes" ;; + (-S) size="$2"; shift;; + (-B) bridge="$2"; shift;; + (-M) mem="$2"; shift;; + (-C) cpus="$2"; shift;; + (-R) resolver="$2"; shift;; + (-i) ip="$2"; shift;; + (-g) gateway="$2"; shift;; + (-n) netmask="$2"; shift;; + (-I) ip6="$2"; shift;; + (-G) gateway6="$2"; shift;; + (-N) netmask6="$2"; shift;; + (--) shift; break;; + (-*) echo "Unknown option $1\nUsage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 1;; + (*) break;; + esac + shift +done + +id=$(uuidgen) + +seed=${id}_seed.img +disk=${id}.img + +rm -f ${seed} +truncate --size 2M ${seed} +mkfs.vfat -n cidata ${seed} 2>/dev/null + +user_data=$(mktemp) +cat > ${user_data} <<EOF +#cloud-config +password: ubuntu +chpasswd: { expire: False } +ssh_pwauth: True +ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVvB4gdJ6EWRmx8xUSxrhoUNnWxEf8ZwAqhzC1+7XBY/hSd/cbEotLB9gxgqt0CLW56VU4FPLTw8snD8tgsyZN6KH1Da7UXno8oMk8tJdwLQM0Ggx3aWuztItkDfBc3Lfvq5T07YfphqJO7rcSGbS4QQdflXuOM9JLi6NStVao0ia4aE6Tj68pVVb3++XYvqvbU6NtEICvkTxEY93YpnRSfeAi64hsbaqSTN4kpeltzoSD1Rikz2aQFtFXE03ZC48HtGGhdMFA/Ade6KWBDaXxHGARVQ9/UccfhaR2XSjVxSZ8FBNOzNsH4k9cQIb2ndkEOXZXnjF5ZjdI4ZU0F+t7 leifj+00060AD478D6@sunet.se +runcmd: + - ["mkdir","/tmp/seed"] + - ["mount","/dev/vdb","/tmp/seed"] + - ["cp","/tmp/seed/bootstrap-cosmos.sh","/tmp/seed/cosmos_1.2-2_all.deb","/root"] + - ["cd","/root"] + - "cd /root && /root/bootstrap-cosmos.sh ${hostname} ${repo} ${tag}" + +EOF + +meta_data=$(mktemp) +cat > ${meta_data} <<EOF +#cloud-config +instance-id: iid-${id} +local-hostname: ${hostname} +EOF + +if [ "x${dhcp}" = "xyes" ]; then +cat >> ${meta_data} <<EOF +network-interfaces: | + auto eth0 + iface eth0 inet dhcp +EOF +else + if [ "x${ip}" != "x" ]; then + cat >> ${meta_data} <<EOF +network-interfaces: | + auto eth0 + iface eth0 inet static + address ${ip} + netmask ${netmask} + gateway ${gateway} + dns-nameservers ${resolver} + +EOF + fi + + if [ "x${ip6}" != "x" ]; then + cat >> ${meta_data} <<EOF +network-interfaces: | + auto eth0 + iface eth0 inet6 static + address ${ip6} + netmask ${netmask6} + gateway ${gateway6} + dns-nameservers ${resolver} + +EOF + fi +fi + +mcopy -i ${seed} ${user_data} ::user-data 2>/dev/null +mcopy -i ${seed} ${meta_data} ::meta-data 2>/dev/null +mcopy -i ${seed} /etc/cosmos/apt/bootstrap-cosmos.sh /etc/cosmos/apt/cosmos_1.2-2_all.deb :: +mv ${seed} /var/lib/libvirt/images/ +virsh pool-refresh default + +virsh vol-clone --pool default ${src_image} ${disk} + +virt-install -r ${mem} -n ${hostname} --vcpus=${cpus} --autostart --memballoon virtio --network bridge=${bridge} --boot hd --disk vol=default/${disk},format=qcow2,bus=virtio --disk vol=default/${seed},bus=virtio + +rm -f ${user_data} +rm -f ${meta_data} |