3
# hp-proliant-add-virtual-nodes - create some virtual machines
4
# Copyright (C) 2014 Canonical Ltd.
6
# Authors: Ronald McCollam <ronald.mccollam@canonical.com>
7
# Dustin Kirkland <kirkland@canonical.com>
8
# Kent Baxley <kent.baxley@canonical.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, version 3 of the License.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
if [ "$(id -u)" != "0" ]; then
23
echo "Must be run with sudo or by root"
30
if [ -f /etc/hp-proliant.conf ]; then
31
PREFIX=$(awk -F '[:]' NR==1'{print $3"."$4}' /etc/hp-proliant.conf)
36
virsh net-info default && virsh net-destroy default && virsh net-undefine default
38
[ -d /home/maas ] || install -d /home/maas --owner maas --group maas
39
chsh maas -s /bin/bash
40
[ -d /home/maas/.ssh ] || echo -e "\n\n\n" | sudo -u maas ssh-keygen -N "" -t rsa -f /home/maas/.ssh/id_rsa
41
grep 'maas@' /home/ubuntu/.ssh/authorized_keys || cat /home/maas/.ssh/id_rsa.pub | tee -a /home/ubuntu/.ssh/authorized_keys
42
printf "%s,%s %s %s\n" "moonshot-maas.maas" "${PREFIX}.4.1" $(awk '{print $1 " " $2}' /etc/ssh/ssh_host_ecdsa_key.pub) | tee -a /home/maas/.ssh/known_hosts
43
chown -R ubuntu:ubuntu /home/ubuntu
45
maas admin tags new name=virtual || true
46
maas admin tags new name=bootstrap || true
47
maas admin tags new name=neutron || true
48
maas admin tags new name='use-fastpath-installer' comment='fp' "definition=true()" || true
50
hostname="moonshotvm.maas"
51
virsh destroy $hostname || true
52
virsh undefine $hostname || true
54
echo "INFO: Installing virtual machine"
55
virt-install --name $hostname --ram 2048 --disk=path=/var/lib/hp-proliant/${hostname}.img,size=10 --vcpus=2 --os-type=linux --pxe --network=bridge=br0 --boot network || true
57
virsh console $hostname || true
58
virsh autostart $hostname
59
mac=$(virsh dumpxml $hostname | python -c 'import sys, lxml.etree; print list(lxml.etree.parse(sys.stdin).iter("mac"))[0].get("address")')
60
system_id=$(maas admin nodes list mac_address=$mac | grep system_id | cut -d'"' -f4)
62
if [ -n "$system_id" ]; then
64
echo "DONE: VM creation done successfully"
66
echo "ERROR: creating the VM lets try to start again"
67
virsh start $hostname || true
68
virsh console $hostname || true
69
mac=$(virsh dumpxml $hostname | python -c 'import sys, lxml.etree; print list(lxml.etree.parse(sys.stdin).iter("mac"))[0].get("address")')
70
system_id=$(maas admin nodes list mac_address=$mac | grep system_id | cut -d'"' -f4)
74
if [ -n "$system_id" ]; then
75
maas admin node update $system_id hostname=$hostname power_type=virsh power_parameters_power_address=qemu+ssh://ubuntu@${PREFIX}.4.1/system power_parameters_power_id=$hostname
76
maas admin tag update-nodes "virtual" add=$system_id
77
maas admin tag update-nodes "bootstrap" add=$system_id
78
maas admin tag update-nodes use-fastpath-installer add=$system_id
79
maas admin node commission $system_id || true
81
virsh console $hostname || true
83
echo "ERROR: Could not find virtual machine in MAAS" 1>&2