~thomnico/sademos/juju-on-openstack-resumed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
#
#    orange-box-setup-kvms - create some virtual machines
#    Copyright (C) 2014 Canonical Ltd.
#
#    Authors: Ronald McCollam <ronald.mccollam@canonical.com>
#             Dustin Kirkland <kirkland@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

trap 'echo Error at about $LINENO' ERR

# Source file to get OB number variable
. /etc/orange-box.conf

# Set network variables
external_ip="172.27.252.${orangebox_number}"
internal_ip="172.27.${orangebox_number}.1"
internal_network="172.27.${orangebox_number}.0"

# If user is not root then exit
if [ "$(id -u)" != "0" ]; then
  echo "Must be run with sudo or by root"
  exit 77
fi

set -e
set -aux

virsh net-info default && virsh net-destroy default && virsh net-undefine default

# If directory exist then change ownership
[ -d /var/lib/maas ] || install -d /var/lib/maas --owner maas --group maas

# Create the /var/lib/orange-box directory
[ -d /var/lib/orange-box ] || mkdir /var/lib/orange-box

# Change login shell to Bash for MAAS user
chsh maas -s /bin/bash

# If directory exist then make directory and change ownership
[ -d /var/lib/maas/.ssh ] || mkdir /var/lib/maas/.ssh && chown maas:maas /var/lib/maas/.ssh

# If file exist then echo 3 returns but do not output trailing newlines. Create SSH key for MAAS user with no passphrase of type RSA v2 at the specified location
[ -f /var/lib/maas/.ssh/id_rsa ] || echo -e "\n\n\n" | sudo -u maas ssh-keygen -N "" -t rsa -f /var/lib/maas/.ssh/id_rsa

# If file exist then gather known host keys of MAAS user for OB and append to the end of specified file while discarding output
[ -f /var/lib/maas/.ssh/known_hosts ] || sudo -u maas ssh-keyscan 172.27.${orangebox_number}.1 |sudo -u maas tee -a /var/lib/maas/.ssh/known_hosts > /dev/null

# Always add host keys .. not an issue if files grows
sudo -u maas ssh-keyscan 172.27.${orangebox_number}.1 |sudo -u maas  tee -a /var/lib/maas/.ssh/known_hosts > /dev/null

# if very first install ubuntu user might not have authorized keys set.
if [ ! -f /home/ubuntu/.ssh/authorized_keys ]; then
    mkdir -p /home/ubuntu/.ssh/
    cat /var/lib/maas/.ssh/id_rsa.pub | tee -a /home/ubuntu/.ssh/authorized_keys
    chown -R ubuntu /home/ubuntu/.ssh/
    chmod 700  /home/ubuntu/.ssh/
fi

sudo -u maas virsh -c qemu+ssh://ubuntu@172.27.${orangebox_number}.1/system list > /dev/null || sed -i '/ maas@/d' /home/ubuntu/.ssh/authorized_keys

# If there is no key /new install or just removed add it"
# echo -e "\n\n\n" | sudo -u maas ssh-keygen -N "" -t rsa -f /var/lib/maas/.ssh/id_rsa
grep 'maas@' /home/ubuntu/.ssh/authorized_keys || cat /var/lib/maas/.ssh/id_rsa.pub | tee -a /home/ubuntu/.ssh/authorized_keys
printf "%s,%s %s %s\n" "node0.maas" "$internal_ip" $(awk '{print $1 " " $2}' /etc/ssh/ssh_host_ecdsa_key.pub) | tee -a /var/lib/maas/.ssh/known_hosts

#Change ownership of specified directory
chown -R ubuntu:ubuntu /home/ubuntu

# Add virtual node tags in MAAS
maas admin tags new name=virtual || true
maas admin tags new name=bootstrap || true
maas admin tags new name=neutron || true

# Mark all nodes to be fast path installed
# Support MAAS 1.5, and 1.6
maas_ver=$(dpkg -l maas | tail -n1 | awk '{print $3}')
if dpkg --compare-versions $maas_ver lt 1.6; then
	maas admin tags new name='use-fastpath-installer' comment='fp' "definition=true()" || true
else
	maas admin tags new name='use-fastpath-installer' comment='fp' || true
fi

#Create zone0 for virtual nodes
maas admin zone read zone0 || maas admin zones create name=zone0 description="Virtual machines on node0"

#Create maas-br0 network if it does not already exist
### For APAC Roadshow
maas admin subnet read maas-br0 || maas admin subnets create name=maas-br0 ip=${internal_network} cidr=23 default_gateway=${internal_ip} dns_servers=${internal_ip} description="Auto created when running orange-box-add-virtual-nodes"

for i in {0..2}; do
	hostname="node00vm${i}ob${orangebox_number}.maas"
	virsh destroy $hostname || true
	virsh undefine $hostname || true
	echo "INFO: Installing virtual machine"
        if [ $i = 0 ]; then
	virt-install --debug --name $hostname --ram 6144 --disk=path=/var/lib/orange-box/${hostname}-1.img,size=20 --disk=path=/var/lib/orange-box/${hostname}-2.img,size=2 --vcpus=2 --os-type=linux --pxe --network=bridge=br0 --network=bridge=br0 --boot network --video=cirrus --graphics vnc|| true
        else
	virt-install --debug --name $hostname --ram 4096 --disk=path=/var/lib/orange-box/${hostname}-1.img,size=20 --disk=path=/var/lib/orange-box/${hostname}-2.img,size=2 --vcpus=2 --os-type=linux --pxe --network=bridge=br0 --network=bridge=br0 --boot network --video=cirrus --graphics vnc|| true
        fi
	virsh console $hostname || true
	virsh autostart $hostname
	mac=$(virsh dumpxml $hostname | python -c 'import sys, lxml.etree; print list(lxml.etree.parse(sys.stdin).iter("mac"))[0].get("address")')
	system_id=$(maas admin nodes list mac_address=$mac | grep system_id | cut -d'"' -f4)
	if [ -n "$system_id" ]; then
		maas admin node update $system_id hostname=$hostname power_type=virsh power_parameters_power_address=qemu+ssh://ubuntu@${internal_ip}/system power_parameters_power_id=$hostname
		maas admin tag update-nodes "virtual" add=$system_id
		case $i in
			0)
				maas admin tag update-nodes "bootstrap" add=$system_id
			;;
			1)
				maas admin tag update-nodes "neutron" add=$system_id
			;;
		esac
		maas admin tag update-nodes use-fastpath-installer add=$system_id
		maas admin node commission $system_id || true
		maas admin nodes set-zone zone=zone0 nodes=$system_id
	else
		echo "ERROR: Could not find virtual machine in MAAS" 1>&2
		exit 1
	fi
done