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