Fix Centos Virl deployment and image build want list
[csit.git] / resources / tools / disk-image-builder / centos / run-listmaker.sh
1 #!/bin/bash
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # This script is to spin up a simulation in VIRL, and fetch the URLs for all packages
17 # that the user would obtain if they did an "yum update" today.
18 #
19 # This entire step is neither secure nor portable. The assumption --for now-- is that
20 # this will only ever be run in LF CSIT VIRL lab. Should the requirement arise to
21 # run this elsewhere, then additional work may be required to make this more
22 # portable.
23
24 # This script requires that the following two environment variables be defined-
25 #
26 # $VIRL_USER
27 # $VIRL_PASSWORD
28
29 VERSION=$(cat $(dirname $0)/CHANGELOG  | grep '^## ' | head -1 | sed -e 's/.*\[\(.*\)\].*/\1/')
30 if [ "${VERSION}" = "" ]
31 then
32   echo "Unable to determine build version from CHANGELOG file. Make sure"
33   echo "that there is an entry for the most recent version in CHANGELOG,"
34   echo "and that the entry is formated like"
35   echo
36   echo "## [1.0] - 2016-05-20"
37   exit 1
38 fi
39 DATE=$(date +%Y-%m-%d)
40
41 if [ "$1" == "centos-7-1511" ]
42 then
43     OS="centos-7-1511"
44     VIRL_TOPOLOGY_FILE="listmaker/virl-listmaker-centos-7-1511.yaml"
45 elif [ "$1" == "centos-7.3-1611" ]
46 then
47     OS="centos-7.3-1611"
48     VIRL_TOPOLOGY_FILE="listmaker/virl-listmaker-centos-7.3-1611.yaml"
49 else
50     echo specify argument -- probably centos-7-1511 or centos-7.3-1611
51     exit 1
52 fi
53
54 RELEASE="${OS}_${DATE}_${VERSION}"
55 OUTPUT_DIR="lists/${RELEASE}"
56
57 echo "Building release ${RELEASE}."
58 echo "Storing data in ${OUTPUT_DIR}/."
59
60
61 # RPM packages wanted
62
63 RPM_WANTLIST_INFRA="nfs-utils cloud-init pkgconfig yum-utils"
64 RPM_WANTLIST_CSIT="python-devel python-pip python-virtualenv python-setuptools python-pip openssl-devel git strongswan"
65 RPM_WANTLIST_VPP="dkms bridge-utils"
66 RPM_WANTLIST_TREX="zlib-devel unzip"
67 RPM_WANTLIST_MISC="gperftools glusterfs glusterfs-api libiscsi libibverbs libpcap libpcap-devel pixman libpng pulseaudio-libs librados2 librbd1 librdmacm libseccomp spice-server libusb usbredir glusterfs-devel seavgabios-bin sgabios-bin ipxe-roms-qemu nss-devel seabios-bin"
68
69 RPM_WANTLIST_NESTED="qemu-img-ev-2.3.0-31.el7_2.21.1.x86_64.rpm libcacard-ev-2.3.0-31.el7_2.21.1.x86_64.rpm libcacard-devel-ev-2.3.0-31.el7_2.21.1.x86_64.rpm qemu-kvm-ev-debuginfo-2.3.0-31.el7_2.21.1.x86_64.rpm qemu-kvm-tools-ev-2.3.0-31.el7_2.21.1.x86_64.rpm qemu-kvm-common-ev-2.3.0-31.el7_2.21.1.x86_64.rpm qemu-kvm-ev-2.3.0-31.el7_2.21.1.x86_64.rpm libcacard-tools-ev-2.3.0-31.el7_2.21.1.x86_64.rpm"
70 RPM_WANTLIST_JAVA="java-1.8.0-openjdk-headless java-1.8.0-openjdk-devel"
71 #RPM_WANTLIST_DOCKER="docker-engine"
72
73 ### For now, do not include WANTLIST_NESTED in the main list. We're installing qemu
74 ### separately because of the possible need for specific versions but the supported version seem to be ok for Centos 7.3
75 ##
76 RPM_WANTLIST="$RPM_WANTLIST_INFRA $RPM_WANTLIST_CSIT $RPM_WANTLIST_VPP $RPM_WANTLIST_TREX $RPM_WANTLIST_MISC $RPM_WANTLIST_JAVA"
77
78 RPM_OUTPUTFILE="${OUTPUT_DIR}/rpm-packages.txt"
79
80 # Python requirements file. Can point to a manually crafted file
81 # here, or to the actual CSIT requirements file, or to a symlink.
82
83 PIP_REQUIREMENTS="../../../../requirements.txt"
84 if [ ! -f ${PIP_REQUIREMENTS} ]
85 then
86   echo "PIP requirements file ${PIP_REQUIREMENTS} not found."
87   exit 1
88 fi
89
90 PIP_OUTPUTFILE="${OUTPUT_DIR}/pip-requirements.txt"
91
92 # These will be used for SSH to the listmaker VM, and must match with what
93 # was defined in the listmaker VM's kickstart file.
94 SSH_USER="root"
95 SSH_PASS="csit"
96
97 ###
98 ### Spin up simulation
99 ###
100 if [ "$VIRL_USER" = "" ] || [ "$VIRL_PASSWORD" = "" ]
101 then
102   echo '$VIRL_USER and $VIRL_PASSWORD environment variables must be defined'
103   exit 1
104 fi
105
106 output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} \
107   simengine-launch -f ${VIRL_TOPOLOGY_FILE} 2>&1)
108 id=$(echo "${output}" | grep "Simulation ID is " | cut -f 4 -d ' ')
109
110 if [ "$id" = "" ]
111 then
112   echo "Did not get a simulation ID. Aborting."
113   echo "Output was:"
114   echo "${output}"
115   exit 1
116 fi
117
118 echo My ID is ${id}
119 function stop_sim {
120   virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
121 }
122 trap stop_sim EXIT
123
124 ip="None"
125 while [ "${ip}" = "None" ] || [ "${ip}" = "" ]
126 do
127   sleep 5
128   output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-interfaces --session ${id} --nodes listmaker --interfaces management 2>&1)
129   ip=$(echo "${output}" | grep "u'ip-address" | cut -f 4 -d "'" | cut -f 1 -d '/')
130 done
131 echo "IP is $ip"
132
133 sleep 10
134
135 if ping -w 60 -c 2 $ip > /dev/null
136 then
137   echo Host $ip alive
138 else
139   echo Host $ip failed to respond to ping
140   exit 1
141 fi
142
143 # Wait for SSH to be up
144 while ! nc -z $ip 22
145 do
146   sleep 3
147 done
148
149 if [ ! -d ${OUTPUT_DIR} ]; then
150     mkdir -p $OUTPUT_DIR
151 fi
152
153 ###
154 ### SSH to the VM and perform package installation. Before each step,
155 ### dry-run and grab the URLs of the packages that would be installed.
156 ###
157
158 function do_ssh {
159   # Helper function: SSH and avoid password prompt
160   sshpass -p $SSH_PASS ssh -o StrictHostKeyChecking=false -o UserKnownHostsFile=/dev/null \
161     -o LogLevel=error ${SSH_USER}@${ip} "$@"
162 }
163
164 RPM_TEMPFILE=$(mktemp)
165 RPM_TEMPFILE2=$(mktemp)
166 RPM_URL_TEMPFILE=$(mktemp)
167 do_ssh yum clean all
168 do_ssh yum install -y @base
169 do_ssh yum install -y deltarpm
170 do_ssh yum update -y
171 do_ssh yum -y install epel-release
172 do_ssh yum update -y
173 do_ssh yum -y install $RPM_WANTLIST
174 for i in ${RPM_WANTLIST} ; do
175     echo $i >> $RPM_TEMPFILE
176 done
177
178 ###
179 ### Install qemu ($RPM_WANTLIST_NESTED) separately from PPA in case specific versions are required.
180 ###
181 for i in ${RPM_WANTLIST_NESTED};  do
182     echo $i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/ >> $RPM_URL_TEMPFILE
183 done
184 ###
185 ### Try 2 times for dependencies. Not in yum repo so it is not automatic"
186 ###
187 for i in ${RPM_WANTLIST_NESTED};  do
188     do_ssh rpm -i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/$i
189 done
190 for i in ${RPM_WANTLIST_NESTED};  do
191     do_ssh rpm -i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/$i
192 done
193
194
195 cat $RPM_TEMPFILE | sort > $RPM_TEMPFILE2
196 rm -f $RPM_TEMPFILE
197 cat $RPM_TEMPFILE2 > $RPM_OUTPUTFILE
198 cat $RPM_URL_TEMPFILE >> $RPM_OUTPUTFILE
199 rm -f $RPM_TEMPFILE2
200 rm -f $RPM_URL_TEMPFILE
201
202 ### Get Python data. We do this by installing as per our
203 ### requirements.txt file while fetching a list of all
204 ### installed modules before and after, and then comparing.
205
206 PIP_TEMPFILE_BEFORE=$(mktemp)
207 PIP_TEMPFILE_AFTER=$(mktemp)
208 do_ssh "cat - > /tmp/requirements.txt" < ${PIP_REQUIREMENTS}
209 do_ssh pip list | sort > $PIP_TEMPFILE_BEFORE
210 do_ssh pip install -r /tmp/requirements.txt
211 do_ssh pip list | sort > $PIP_TEMPFILE_AFTER
212
213 comm -1 -3 ${PIP_TEMPFILE_BEFORE} ${PIP_TEMPFILE_AFTER} | \
214   sed -e 's/\(.*\) (\(.*\))/\1==\2/' > $PIP_OUTPUTFILE
215 rm -f $PIP_TEMPFILE_BEFORE
216 rm -f $PIP_TEMPFILE_AFTER
217
218 ###
219 ### Stop VIRL session
220 ###
221 virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
222 trap "" EXIT