CSIT-192: Add bridge-utils, docker, java8 to VM image
[csit.git] / resources / tools / disk-image-builder / ubuntu / 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 "apt-get update", "apt-get upgrade" 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 OS="ubuntu-14.04.4"
41 RELEASE="${OS}_${DATE}_${VERSION}"
42 OUTPUT_DIR="lists/${RELEASE}"
43
44 echo "Building release ${RELEASE}."
45 echo "Storinging data in ${OUTPUT_DIR}/."
46
47
48 # APT packages wanted
49
50 APT_WANTLIST_INFRA="nfs-common cloud-init"
51 APT_WANTLIST_CSIT="python-dev python-virtualenv git"
52 APT_WANTLIST_VPP="dkms bridge-utils"
53 APT_WANTLIST_TREX="zlib1g-dev unzip"
54 APT_WANTLIST_NESTED="qemu-system-x86"
55 APT_WANTLIST_JAVA="openjdk-8-jdk-headless"
56 APT_WANTLIST_DOCKER="docker-engine"
57
58 # For now, let us NOT incude WANTLIST_NESTED in the below. We're installing qemu
59 # separately from a separate source.
60 APT_WANTLIST="$APT_WANTLIST_INFRA $APT_WANTLIST_CSIT $APT_WANTLIST_VPP $WANTLIST_TREX"
61
62 APT_OUTPUTFILE="${OUTPUT_DIR}/apt-packages.txt"
63
64 # Python requirements file. Can point to a manually crafted file
65 # here, or to the actual CSIT requirements file, or to a symlink.
66
67 PIP_REQUIREMENTS="../../../../requirements.txt"
68 if [ ! -f ${PIP_REQUIREMENTS} ]
69 then
70   echo "PIP requirements file ${PIP_REQUIREMENTS} not found."
71   exit 1
72 fi
73
74 PIP_OUTPUTFILE="${OUTPUT_DIR}/pip-requirements.txt"
75
76 # These will be used for SSH to the listmaker VM, and must match with what
77 # was defined in the listmaker VM's kickstart file.
78 SSH_USER="root"
79 SSH_PASS="csit"
80
81 VIRL_TOPOLOGY_FILE="listmaker/virl-listmaker.yaml"
82
83 ###
84 ### Spin up simulation
85 ###
86 if [ "$VIRL_USER" = "" ] || [ "$VIRL_PASSWORD" = "" ]
87 then
88   echo '$VIRL_USER and $VIRL_PASSWORD environment variables must be defined'
89   exit 1
90 fi
91
92 output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} \
93   simengine-launch -f ${VIRL_TOPOLOGY_FILE} 2>&1)
94 id=$(echo "${output}" | grep "Simulation ID is " | cut -f 4 -d ' ')
95
96 if [ "$id" = "" ]
97 then
98   echo "Did not get a simulation ID. Aborting."
99   echo "Output was:"
100   echo "${output}"
101   exit 1
102 fi
103
104 echo My ID is ${id}
105 function stop_sim {
106   virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
107 }
108 trap stop_sim EXIT
109
110 ip="None"
111 while [ "${ip}" = "None" ] || [ "${ip}" = "" ]
112 do
113   sleep 5
114   output=$(virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-interfaces --session ${id} --nodes listmaker --interfaces management 2>&1)
115   ip=$(echo "${output}" | grep "u'ip-address" | cut -f 4 -d "'" | cut -f 1 -d '/')
116 done
117 echo "IP is $ip"
118
119 sleep 10
120
121 if ping -w 60 -c 2 $ip > /dev/null
122 then
123   echo Host $ip alive
124 else
125   echo Host $ip failed to respond to ping
126   exit 1
127 fi
128
129 # Wait for SSH to be up
130 while ! nc -z $ip 22
131 do
132   sleep 3
133 done
134
135 mkdir -p $OUTPUT_DIR
136
137 ###
138 ### SSH to the VM and perform package installation. Before each step,
139 ### dry-run and grab the URLs of the packages that would be installed.
140 ###
141
142 function do_ssh {
143   # Helper function: SSH and avoid password prompt
144   sshpass -p $SSH_PASS ssh -o StrictHostKeyChecking=false -o UserKnownHostsFile=/dev/null \
145     -o LogLevel=error ${SSH_USER}@${ip} "$@"
146 }
147
148 do_ssh "cat - > /etc/apt/sources.list" <<_EOF
149 deb http://us.archive.ubuntu.com/ubuntu/ trusty main restricted
150 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty main restricted
151 deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
152 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
153 deb http://us.archive.ubuntu.com/ubuntu/ trusty universe
154 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty universe
155 deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
156 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
157 deb http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
158 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
159 deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
160 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
161 deb http://us.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
162 deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
163 deb http://security.ubuntu.com/ubuntu trusty-security main restricted
164 deb-src http://security.ubuntu.com/ubuntu trusty-security main restricted
165 deb http://security.ubuntu.com/ubuntu trusty-security universe
166 deb-src http://security.ubuntu.com/ubuntu trusty-security universe
167 deb http://security.ubuntu.com/ubuntu trusty-security multiverse
168 deb-src http://security.ubuntu.com/ubuntu trusty-security multiverse
169 _EOF
170
171
172 ### FIXME: Need error handling around all this
173 do_ssh apt-get update
174
175 APT_TEMPFILE=$(mktemp)
176 do_ssh apt-get --print-uris -y dist-upgrade >> $APT_TEMPFILE
177 do_ssh DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
178 do_ssh apt-get --print-uris -y install $APT_WANTLIST >> $APT_TEMPFILE
179 do_ssh DEBIAN_FRONTEND=noninteractive apt-get -y install $APT_WANTLIST
180
181 ### Install qemu ($APT_WANTLIST_NESTED) separately from PPA
182 do_ssh "cat - >> /etc/apt/sources.list" <<_EOF
183 # For a custom qemu build
184 deb http://ppa.launchpad.net/syseleven-platform/virtualization/ubuntu trusty main
185 deb-src http://ppa.launchpad.net/syseleven-platform/virtualization/ubuntu trusty main
186 _EOF
187 do_ssh apt-get --allow-unauthenticated update
188 do_ssh apt-get --print-uris --allow-unauthenticated -y install $APT_WANTLIST_NESTED >> $APT_TEMPFILE
189 do_ssh DEBIAN_FRONTEND=noninteractive apt-get --allow-unauthenticated -y install $APT_WANTLIST_NESTED
190
191 ### Install Java ($APT_WANTLIST_JAVA) separately from PPA
192 do_ssh "cat - >> /etc/apt/sources.list" <<_EOF
193 # For java
194 deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
195 _EOF
196 do_ssh apt-get --allow-unauthenticated update
197 do_ssh apt-get --print-uris --allow-unauthenticated -y install $APT_WANTLIST_JAVA >> $APT_TEMPFILE
198 do_ssh DEBIAN_FRONTEND=noninteractive apt-get --allow-unauthenticated -y install $APT_WANTLIST_JAVA
199
200 ### Install Docker ($APT_WANTLIST_DOCKER) separately from PPA
201 do_ssh "cat - >> /etc/apt/sources.list" <<_EOF
202 # For Docker
203 deb https://apt.dockerproject.org/repo ubuntu-trusty main
204 _EOF
205 do_ssh apt-get --allow-unauthenticated update
206 do_ssh apt-get --print-uris --allow-unauthenticated -y install $APT_WANTLIST_DOCKER >> $APT_TEMPFILE
207 do_ssh DEBIAN_FRONTEND=noninteractive apt-get --allow-unauthenticated -y install $APT_WANTLIST_DOCKER
208
209 cat $APT_TEMPFILE | grep MD5Sum | sort > $APT_OUTPUTFILE
210 rm -f $APT_TEMPFILE
211
212 ### Get Python data. We do this by installing as per our
213 ### requirements.txt file while fetching a list of all
214 ### installed modules before and after, and then comparing.
215
216 PIP_TEMPFILE_BEFORE=$(mktemp)
217 PIP_TEMPFILE_AFTER=$(mktemp)
218 do_ssh "cat - > /tmp/requirements.txt" < ${PIP_REQUIREMENTS}
219 do_ssh pip list | sort > $PIP_TEMPFILE_BEFORE
220 do_ssh pip install -r /tmp/requirements.txt
221 do_ssh pip list | sort > $PIP_TEMPFILE_AFTER
222
223 comm -1 -3 ${PIP_TEMPFILE_BEFORE} ${PIP_TEMPFILE_AFTER} | \
224   sed -e 's/\(.*\) (\(.*\))/\1==\2/' > $PIP_OUTPUTFILE
225 rm -f $PIP_TEMPFILE_BEFORE
226 rm -f $PIP_TEMPFILE_AFTER
227
228 ###
229 ### Stop VIRL session
230 ###
231 virl_std_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} simengine-stop --session ${id}
232 trap "" EXIT