e52e3e55f831597b65c642699a5529eac33e95f0
[apex.git] / build / instack.sh
1 #!/bin/sh
2 set -e
3 declare -i CNT
4
5 rdo_images_uri=https://ci.centos.org/artifacts/rdo/images/liberty/delorean/stable
6
7 vm_index=4
8 RDO_RELEASE=liberty
9 SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null)
10
11 # check for dependancy packages
12 for i in rpm-build createrepo libguestfs-tools python-docutils bsdtar; do
13     if ! rpm -q $i > /dev/null; then
14         sudo yum install -y $i
15     fi
16 done
17
18 # RDO Manager expects a stack user to exist, this checks for one
19 # and creates it if you are root
20 if ! id stack > /dev/null; then
21     sudo useradd stack;
22     sudo echo 'stack ALL=(root) NOPASSWD:ALL' | sudo tee -a /etc/sudoers.d/stack
23     sudo echo 'Defaults:stack !requiretty' | sudo tee -a /etc/sudoers.d/stack
24     sudo chmod 0440 /etc/sudoers.d/stack
25     echo 'Added user stack'
26 fi
27
28 # ensure that I can ssh as the stack user
29 if ! sudo grep "$(cat ~/.ssh/id_rsa.pub)" /home/stack/.ssh/authorized_keys; then
30     if ! sudo ls -d /home/stack/.ssh/ ; then
31         sudo mkdir /home/stack/.ssh
32         sudo chown stack:stack /home/stack/.ssh
33         sudo chmod 700 /home/stack/.ssh
34     fi
35     USER=$(whoami) sudo sh -c "cat ~$USER/.ssh/id_rsa.pub >> /home/stack/.ssh/authorized_keys"
36     sudo chown stack:stack /home/stack/.ssh/authorized_keys
37 fi
38
39 # clean up stack user previously build instack disk images
40 ssh -T ${SSH_OPTIONS[@]} stack@localhost "rm -f instack*.qcow2"
41
42 # Yum repo setup for building the undercloud
43 if ! rpm -q rdo-release > /dev/null && [ "$1" != "-master" ]; then
44     #pulling from current-passed-ci instead of release repos
45     #sudo yum install -y https://rdoproject.org/repos/openstack-${RDO_RELEASE}/rdo-release-${RDO_RELEASE}.rpm
46     sudo yum -y install yum-plugin-priorities
47     sudo yum-config-manager --disable openstack-${RDO_RELEASE}
48     sudo curl -o /etc/yum.repos.d/delorean.repo http://trunk.rdoproject.org/centos7-liberty/current-passed-ci/delorean.repo
49     sudo curl -o /etc/yum.repos.d/delorean-deps.repo http://trunk.rdoproject.org/centos7-liberty/delorean-deps.repo
50     sudo rm -f /etc/yum.repos.d/delorean-current.repo
51 elif [ "$1" == "-master" ]; then
52     sudo yum -y install yum-plugin-priorities
53     sudo yum-config-manager --disable openstack-${RDO_RELEASE}
54     sudo curl -o /etc/yum.repos.d/delorean.repo http://trunk.rdoproject.org/centos7/current-passed-ci/delorean.repo
55     sudo curl -o /etc/yum.repos.d/delorean-deps.repo http://trunk.rdoproject.org/centos7-liberty/delorean-deps.repo
56     sudo rm -f /etc/yum.repos.d/delorean-current.repo
57 fi
58
59 # install the opendaylight yum repo definition
60 cat << 'EOF' | sudo tee /etc/yum.repos.d/opendaylight.repo
61 [opendaylight]
62 name=OpenDaylight $releasever - $basearch
63 baseurl=http://cbs.centos.org/repos/nfv7-opendaylight-3-candidate/$basearch/os/
64 enabled=1
65 gpgcheck=0
66 EOF
67
68 # ensure the undercloud package is installed so we can build the undercloud
69 if ! rpm -q instack-undercloud > /dev/null; then
70     sudo yum install -y python-tripleoclient
71 fi
72
73 # ensure openvswitch is installed
74 if ! rpm -q openvswitch > /dev/null; then
75     sudo yum install -y openvswitch
76 fi
77
78 # ensure libvirt is installed
79 if ! rpm -q libvirt-daemon-kvm > /dev/null; then
80     sudo yum install -y libvirt-daemon-kvm
81 fi
82
83 # clean this up incase it's there
84 sudo rm -f /tmp/instack.answers
85
86 # ensure that no previous undercloud VMs are running
87 sudo ../ci/clean.sh
88 # and rebuild the bare undercloud VMs
89 ssh -T ${SSH_OPTIONS[@]} stack@localhost <<EOI
90 set -e
91 NODE_COUNT=5 NODE_CPU=2 NODE_MEM=8192 TESTENV_ARGS="--baremetal-bridge-names 'brbm brbm1'" instack-virt-setup
92 EOI
93
94 # let dhcp happen so we can get the ip
95 # just wait instead of checking until we see an address
96 # because there may be a previous lease that needs
97 # to be cleaned up
98 sleep 5
99
100 # get the undercloud ip address
101 UNDERCLOUD=$(grep instack /var/lib/libvirt/dnsmasq/default.leases | awk '{print $3}' | head -n 1)
102 if [ -z "$UNDERCLOUD" ]; then
103   #if not found then dnsmasq may be using leasefile-ro
104   instack_mac=$(ssh -T ${SSH_OPTIONS[@]} stack@localhost "virsh domiflist instack" | grep default | \
105                 grep -Eo "[0-9a-f\]+:[0-9a-f\]+:[0-9a-f\]+:[0-9a-f\]+:[0-9a-f\]+:[0-9a-f\]+")
106   UNDERCLOUD=$(arp -e | grep ${instack_mac} | awk {'print $1'})
107
108   if [ -z "$UNDERCLOUD" ]; then
109     echo "\n\nNever got IP for Instack. Can Not Continue."
110     exit 1
111   fi
112 else
113    echo -e "${blue}\rInstack VM has IP $UNDERCLOUD${reset}"
114 fi
115
116 # ensure that we can ssh to the undercloud
117 CNT=10
118 while ! ssh -T ${SSH_OPTIONS[@]}  "root@$UNDERCLOUD" "echo ''" > /dev/null && [ $CNT -gt 0 ]; do
119     echo -n "."
120     sleep 3
121     CNT=CNT-1
122 done
123 # TODO fail if CNT=0
124
125 # yum repo, triple-o package and ssh key setup for the undercloud
126 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
127 set -e
128
129 if ! rpm -q epel-release > /dev/null; then
130     yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
131 fi
132
133 yum -y install yum-plugin-priorities
134 curl -o /etc/yum.repos.d/delorean.repo http://trunk.rdoproject.org/centos7-liberty/current-passed-ci/delorean.repo
135 curl -o /etc/yum.repos.d/delorean-deps.repo http://trunk.rdoproject.org/centos7-liberty/delorean-deps.repo
136
137 cp /root/.ssh/authorized_keys /home/stack/.ssh/authorized_keys
138 chown stack:stack /home/stack/.ssh/authorized_keys
139 EOI
140
141 # copy instackenv file for future virt deployments
142 if [ ! -d stack ]; then mkdir stack; fi
143 scp ${SSH_OPTIONS[@]} stack@$UNDERCLOUD:instackenv.json stack/instackenv.json
144
145 # make a copy of instack VM's definitions, and disk image
146 # it must be stopped to make a copy of its disk image
147 ssh -T ${SSH_OPTIONS[@]} stack@localhost <<EOI
148 set -e
149 echo "Shutting down instack to gather configs"
150 virsh shutdown instack
151
152 echo "Waiting for instack VM to shutdown"
153 CNT=20
154 while virsh list | grep instack > /dev/null && [ $CNT -gt 0 ]; do
155     echo -n "."
156     sleep 5
157     CNT=CNT-1
158 done
159 if virsh list | grep instack > /dev/null; then
160     echo "instack failed to shutdown for copy"
161     exit 1
162 fi
163
164 echo $'\nGenerating libvirt configuration'
165 for i in \$(seq 0 $vm_index); do
166   virsh dumpxml baremetalbrbm_brbm1_\$i | awk '/model type='\''virtio'\''/{c++;if(c==2){sub("model type='\''virtio'\''","model type='\''rtl8139'\''");c=0}}1' > baremetalbrbm_brbm1_\$i.xml
167 done
168
169 virsh dumpxml instack > instack.xml
170 virsh net-dumpxml brbm > brbm-net.xml
171 virsh net-dumpxml brbm1 > brbm1-net.xml
172 virsh pool-dumpxml default > default-pool.xml
173 EOI
174
175 # copy off the instack artifacts
176 echo "Copying instack files to build directory"
177 for i in $(seq 0 $vm_index); do
178   scp ${SSH_OPTIONS[@]} stack@localhost:baremetalbrbm_brbm1_${i}.xml .
179 done
180
181 scp ${SSH_OPTIONS[@]} stack@localhost:instack.xml .
182 scp ${SSH_OPTIONS[@]} stack@localhost:brbm-net.xml .
183 scp ${SSH_OPTIONS[@]} stack@localhost:brbm1-net.xml .
184 scp ${SSH_OPTIONS[@]} stack@localhost:default-pool.xml .
185
186 # pull down the the built images
187 echo "Copying overcloud resources"
188 IMAGES="overcloud-full.tar"
189 IMAGES+=" undercloud.qcow2"
190
191 for i in $IMAGES; do
192   # download prebuilt images from RDO Project
193   if [ "$(curl -L $rdo_images_uri/${i}.md5 | awk {'print $1'})" != "$(md5sum stack/$i | awk {'print $1'})" ] ; then
194     #if [ $i == "undercloud.qcow2" ]; then
195     ### there's a problem with the Content-Length reported by the centos artifacts
196     ### server so using wget for it until a resolution is figured out.
197     wget -nv -O stack/$i $rdo_images_uri/$i
198     #else
199     #  curl $rdo_images_uri/$i -o stack/$i --verbose --silent --location
200     #fi
201   fi
202   # only untar the tar files
203   if [ "${i##*.}" == "tar" ]; then tar -xf stack/$i -C stack/; fi
204 done
205
206 #Adding OpenStack packages to undercloud
207 pushd stack
208 cp undercloud.qcow2 instack.qcow2
209 LIBGUESTFS_BACKEND=direct virt-customize --install yum-priorities -a instack.qcow2
210 PACKAGES="qemu-kvm-common,qemu-kvm,libvirt-daemon-kvm,libguestfs,python-libguestfs,openstack-nova-compute"
211 PACKAGES+=",openstack-swift,openstack-ceilometer-api,openstack-neutron-ml2,openstack-ceilometer-alarm"
212 PACKAGES+=",openstack-nova-conductor,openstack-ironic-inspector,openstack-ironic-api,python-openvswitch"
213 PACKAGES+=",openstack-glance,python-glance,python-troveclient,openstack-puppet-modules"
214 PACKAGES+=",openstack-neutron,openstack-neutron-openvswitch,openstack-nova-scheduler,openstack-keystone,openstack-swift-account"
215 PACKAGES+=",openstack-swift-container,openstack-swift-object,openstack-swift-plugin-swift3,openstack-swift-proxy"
216 PACKAGES+=",openstack-nova-api,openstack-nova-cert,openstack-heat-api-cfn,openstack-heat-api,"
217 PACKAGES+=",openstack-ceilometer-central,openstack-ceilometer-polling,openstack-ceilometer-collector,"
218 PACKAGES+=",openstack-heat-api-cloudwatch,openstack-heat-engine,openstack-heat-common,openstack-ceilometer-notification"
219 PACKAGES+=",hiera,puppet,memcached,keepalived,mariadb,mariadb-server,rabbitmq-server,python-pbr,python-proliantutils"
220
221 LIBGUESTFS_BACKEND=direct virt-customize --install $PACKAGES -a instack.qcow2
222 popd
223
224
225 #Adding OpenDaylight to overcloud
226 pushd stack
227 # make a copy of the cached overcloud-full image
228 cp overcloud-full.qcow2 overcloud-full-odl.qcow2
229
230 # remove unnessesary packages and install nessesary packages
231 LIBGUESTFS_BACKEND=direct virt-customize --run-command "yum remove -y openstack-neutron-openvswitch" \
232     --upload /etc/yum.repos.d/opendaylight.repo:/etc/yum.repos.d/opendaylight.repo \
233     --install opendaylight,python-networking-odl -a overcloud-full-odl.qcow2
234
235 ## WORK AROUND
236 ## when OpenDaylight lands in upstream RDO manager this can be removed
237
238 # upload the opendaylight puppet module
239 rm -rf puppet-opendaylight
240 git clone https://github.com/dfarrell07/puppet-opendaylight
241 pushd puppet-opendaylight
242 git archive --format=tar.gz --prefix=opendaylight/ HEAD > ../puppet-opendaylight.tar.gz
243 popd
244 LIBGUESTFS_BACKEND=direct virt-customize --upload puppet-opendaylight.tar.gz:/etc/puppet/modules/ \
245                                          --run-command "cd /etc/puppet/modules/ && tar xzf puppet-opendaylight.tar.gz" -a overcloud-full-odl.qcow2
246
247 # Patch in OpenDaylight installation and configuration
248 LIBGUESTFS_BACKEND=direct virt-customize --upload ../opendaylight-tripleo-heat-templates.patch:/tmp \
249                                          --run-command "cd /usr/share/openstack-tripleo-heat-templates/ && patch -Np1 < /tmp/opendaylight-tripleo-heat-templates.patch" \
250                                          -a instack.qcow2
251 LIBGUESTFS_BACKEND=direct virt-customize --upload ../opendaylight-puppet-neutron.patch:/tmp \
252                                          --run-command "cd /etc/puppet/modules/neutron && patch -Np1 < /tmp/opendaylight-puppet-neutron.patch" \
253                                          -a overcloud-full-odl.qcow2
254 ## END WORK AROUND
255 popd
256
257 # resize instack machine
258 echo "Checking if instack needs to be resized..."
259 instack_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a stack/instack.qcow2 |grep device | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
260 if [ "$instack_size" -lt 30 ]; then
261   qemu-img create -f qcow2 -o preallocation=metadata newinstack.qcow2 30G
262   LIBGUESTFS_BACKEND=direct virt-resize --expand /dev/sda1 stack/instack.qcow2 newinstack.qcow2;
263   LIBGUESTFS_BACKEND=direct virt-customize -a newinstack.qcow2 --run-command 'xfs_growfs -d /dev/sda1 || true'
264   LIBGUESTFS_BACKEND=direct virt-sparsify newinstack.qcow2 stack/instack.qcow2
265   new_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a stack/instack.qcow2 |grep device | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
266   if [ "$new_size" -lt 30 ]; then
267     echo "Error resizing instack machine, disk size is ${new_size}"
268     exit 1
269   else
270     echo "instack successfully resized"
271   fi
272 fi
273 # move and Sanitize private keys from instack.json file
274 mv stack/instackenv.json instackenv-virt.json
275 sed -i '/pm_password/c\      "pm_password": "INSERT_STACK_USER_PRIV_KEY",' instackenv-virt.json
276 sed -i '/ssh-key/c\  "ssh-key": "INSERT_STACK_USER_PRIV_KEY",' instackenv-virt.json
277
278 # clean up the VMs
279 ssh -T ${SSH_OPTIONS[@]} stack@localhost <<EOI
280 set -e
281 virsh destroy instack 2> /dev/null || echo -n ''
282 virsh undefine instack --remove-all-storage 2> /dev/null || echo -n ''
283 for i in \$(seq 0 $vm_index); do
284   virsh destroy baremetalbrbm_brbm1_\$i 2> /dev/null || echo -n ''
285   virsh undefine baremetalbrbm_brbm1_\$i --remove-all-storage 2> /dev/null || echo -n ''
286 done
287 EOI
288