CSIT-727 Add the tcpdump application into the VIRL image
[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_TLDK="tcpdump"
66 RPM_WANTLIST_VPP="dkms bridge-utils"
67 RPM_WANTLIST_TREX="zlib-devel unzip"
68 RPM_WANTLIST_MISC="socat psmisc 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"
69
70 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"
71 RPM_WANTLIST_JAVA="java-1.8.0-openjdk-headless java-1.8.0-openjdk-devel"
72 #RPM_WANTLIST_DOCKER="docker-engine"
73
74 ### For now, do not include WANTLIST_NESTED in the main list. We're installing qemu
75 ### separately because of the possible need for specific versions but the supported version seem to be ok for Centos 7.3
76 ##
77 RPM_WANTLIST="$RPM_WANTLIST_INFRA $RPM_WANTLIST_CSIT $RPM_WANTLIST_VPP $RPM_WANTLIST_TREX $RPM_WANTLIST_MISC $RPM_WANTLIST_JAVA $RPM_WANTLIST_TLDK"
78
79 RPM_OUTPUTFILE="${OUTPUT_DIR}/rpm-packages.txt"
80
81 # Python requirements file. Can point to a manually crafted file
82 # here, or to the actual CSIT requirements file, or to a symlink.
83
84 PIP_REQUIREMENTS="../../../../requirements.txt"
85 if [ ! -f ${PIP_REQUIREMENTS} ]
86 then
87   echo "PIP requirements file ${PIP_REQUIREMENTS} not found."
88   exit 1
89 fi
90
91 PIP_OUTPUTFILE="${OUTPUT_DIR}/pip-requirements.txt"
92
93 # These will be used for SSH to the listmaker VM, and must match with what
94 # was defined in the listmaker VM's kickstart file.
95 SSH_USER="root"
96 SSH_PASS="csit"
97
98 ###
99 ### Spin up simulation
100 ###
101 if [ "$VIRL_USER" = "" ] || [ "$VIRL_PASSWORD" = "" ]
102 then
103   echo '$VIRL_USER and $VIRL_PASSWORD environment variables must be defined'
104   exit 1
105 fi
106
107 output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} \
108   simengine-launch -f ${VIRL_TOPOLOGY_FILE} 2>&1)
109 id=$(echo "${output}" | grep "Simulation ID is " | cut -f 4 -d ' ')
110
111 if [ "$id" = "" ]
112 then
113   echo "Did not get a simulation ID. Aborting."
114   echo "Output was:"
115   echo "${output}"
116   exit 1
117 fi
118
119 echo My ID is ${id}
120 function stop_sim {
121   virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
122 }
123 trap stop_sim EXIT
124
125 ip="None"
126 while [ "${ip}" = "None" ] || [ "${ip}" = "" ]
127 do
128   sleep 5
129   output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-interfaces --session ${id} --nodes listmaker --interfaces management 2>&1)
130   ip=$(echo "${output}" | grep "u'ip-address" | cut -f 4 -d "'" | cut -f 1 -d '/')
131 done
132 echo "IP is $ip"
133
134 sleep 10
135
136 if ping -w 60 -c 2 $ip > /dev/null
137 then
138   echo Host $ip alive
139 else
140   echo Host $ip failed to respond to ping
141   exit 1
142 fi
143
144 # Wait for SSH to be up
145 while ! nc -z $ip 22
146 do
147   sleep 3
148 done
149
150 if [ ! -d ${OUTPUT_DIR} ]; then
151     mkdir -p $OUTPUT_DIR
152 fi
153
154 ###
155 ### SSH to the VM and perform package installation. Before each step,
156 ### dry-run and grab the URLs of the packages that would be installed.
157 ###
158
159 function do_ssh {
160   # Helper function: SSH and avoid password prompt
161   sshpass -p $SSH_PASS ssh -o StrictHostKeyChecking=false -o UserKnownHostsFile=/dev/null \
162     -o LogLevel=error ${SSH_USER}@${ip} "$@"
163 }
164
165 RPM_TEMPFILE=$(mktemp)
166 RPM_TEMPFILE2=$(mktemp)
167 RPM_URL_TEMPFILE=$(mktemp)
168 do_ssh yum clean all
169 do_ssh yum install -y @base
170 do_ssh yum install -y deltarpm
171 do_ssh yum update -y
172 do_ssh yum -y install epel-release
173 do_ssh yum update -y
174 do_ssh yum -y install $RPM_WANTLIST
175 for i in ${RPM_WANTLIST} ; do
176     echo $i >> $RPM_TEMPFILE
177 done
178
179 ###
180 ### Install qemu ($RPM_WANTLIST_NESTED) separately from PPA in case specific versions are required.
181 ###
182 for i in ${RPM_WANTLIST_NESTED};  do
183     echo $i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/ >> $RPM_URL_TEMPFILE
184 done
185 ###
186 ### Try 2 times for dependencies. Not in yum repo so it is not automatic"
187 ###
188 for i in ${RPM_WANTLIST_NESTED};  do
189     do_ssh rpm -i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/$i
190 done
191 for i in ${RPM_WANTLIST_NESTED};  do
192     do_ssh rpm -i http://cbs.centos.org/kojifiles/packages/qemu-kvm-ev/2.3.0/31.el7_2.21.1/x86_64/$i
193 done
194
195
196 cat $RPM_TEMPFILE | sort > $RPM_TEMPFILE2
197 rm -f $RPM_TEMPFILE
198 cat $RPM_TEMPFILE2 > $RPM_OUTPUTFILE
199 cat $RPM_URL_TEMPFILE >> $RPM_OUTPUTFILE
200 rm -f $RPM_TEMPFILE2
201 rm -f $RPM_URL_TEMPFILE
202
203 ### Get Python data. We do this by installing as per our
204 ### requirements.txt file while fetching a list of all
205 ### installed modules before and after, and then comparing.
206
207 PIP_TEMPFILE_BEFORE=$(mktemp)
208 PIP_TEMPFILE_AFTER=$(mktemp)
209 do_ssh "cat - > /tmp/requirements.txt" < ${PIP_REQUIREMENTS}
210 do_ssh pip list | sort > $PIP_TEMPFILE_BEFORE
211 do_ssh pip install -r /tmp/requirements.txt
212 do_ssh pip list | sort > $PIP_TEMPFILE_AFTER
213
214 comm -1 -3 ${PIP_TEMPFILE_BEFORE} ${PIP_TEMPFILE_AFTER} | \
215   sed -e 's/\(.*\) (\(.*\))/\1==\2/' > $PIP_OUTPUTFILE
216 rm -f $PIP_TEMPFILE_BEFORE
217 rm -f $PIP_TEMPFILE_AFTER
218
219 ###
220 ### Stop VIRL session
221 ###
222 virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
223 trap "" EXIT