Improvements to re-spin scripts
[ci-management.git] / vagrant / lib / respin-functions.sh
1 #!/bin/bash
2
3 # Copyright 2016 The Linux Foundation
4
5 source ${CI_MGMT}/vagrant/lib/vagrant-functions.sh
6
7
8 source ${PVERC}
9
10 pip install -q --upgrade pip setuptools python-{cinder,glance,keystone,neutron,nova,openstack}client
11
12 #
13 # usage:
14 #   AGE_JSON=$(latest_src_age ${DIST} ${VERSION} ${ARCH})
15 #
16 function latest_src_age ()
17 {
18     SRC_TS=$(latest_src_timestamp "$@")
19     NOW_TS=$(new_timestamp)
20
21     perl -I${CI_MGMT}/vagrant/lib -MRespin -e 'Respin::latest_src_age( @ARGV )' "${NOW_TS}" "${SRC_TS}"
22
23     return 0
24 }
25
26 function new_timestamp ()
27 {
28     date +'%F T %T' | sed -e 's/[-: ]//g'
29 }
30
31 function new_dst_timestamp ()
32 {
33     if [ -z "${DST_TIMESTAMP}" ]
34     then
35         DST_TIMESTAMP=$(new_timestamp)
36     fi
37
38     echo ${DST_TIMESTAMP}
39     return 0
40 }
41
42 function new_src_timestamp ()
43 {
44     if [ -z "${SRC_TIMESTAMP}" ]
45     then
46         SRC_TIMESTAMP=$(date +'%F T %T' | sed -e 's/[-: ]//g')
47     fi
48
49     echo ${SRC_TIMESTAMP}
50     return 0
51 }
52
53 function latest_src_timestamp ()
54 {
55     if [ -z "${SRC_TIMESTAMP}" ]
56     then
57         SRC_TIMESTAMP=$(glance image-list | perl -n -e 'if( /\((\S+)\) - LF upload/ ){ print "$1\n" }' | sort | tail -1)
58     fi
59
60     echo ${SRC_TIMESTAMP}
61     return 0
62 }
63
64 #
65 # usage:
66 #   glance_image_create ${IMG_NAME} ${IMG_PATH}
67 #
68 # example:
69 #   glance_image_create "CentOS 7 (20160517T143002) - LF upload" /var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud.qcow2c
70 #
71 function glance_image_create ()
72 {
73     glance image-create --disk-format qcow2 --container-format bare --progress \
74            --name "${1}" --file "${2}"
75 }
76
77 function setup_rh ()
78 {
79     SRC_TIMESTAMP=$(new_src_timestamp)
80     DIST=$1
81     VERSION=$2
82     ARCH=$3
83     ARCH=${ARCH:-${RH_ARCH64}}
84     IMG_FNAME="${DIST}-${VERSION}-${ARCH}-GenericCloud.qcow2c"
85     IMG_PATH="${LV_IMG_DIR}/${IMG_FNAME}"
86     IMG_NAME="${DIST} ${VERSION} (${SRC_TIMESTAMP}) - LF upload"
87 }
88
89 #
90 # usage:
91 #   create_rh_image ${DIST} ${VERSION} ${ARCH}
92 #
93 # example:
94 #   create_rh_image CentOS 7 x86_64
95 #
96 function create_rh_image ()
97 {
98     setup_rh "$@"
99
100     if [ ! -f ${IMG_PATH} ]; then download_rh_image "$@"; fi
101
102     glance_image_create "${IMG_NAME}" "${IMG_PATH}"
103 }
104
105 function download_rh_image ()
106 {
107     setup_rh "$@"
108     echo "--> Fetching image file for ${DIST} ${VERSION}"
109     wget -qcP ${LV_IMG_DIR} "http://cloud.centos.org/centos/${VERSION}/images/${IMG_FNAME}"
110 }
111
112
113 declare -A deb_codename_map
114 deb_codename_map=(['3.0']=woody \
115                   ['3.1']=sarge \
116                   ['4']=etch \
117                   ['5']=lenny \
118                   ['6']=squeeze \
119                   ['7']=wheezy \
120                   ['8']=jessie \
121                   ['9']=stretch \
122                   ['10']=buster \
123                  )
124 declare -A ubuntu_codename_map
125 ubuntu_codename_map=(['6.06']=dapper \
126                      ['8.04']=hardy \
127                      ['10.04']=lucid \
128                      ['12.04']=precise \
129                      ['14.04']=trusty \
130                      ['16.04']=xenial \
131                      )
132 DEB_CURRENT_VER='8.4.0'
133 DEB_CURRENT_CODENAME='jessie'
134
135 DEB_TESTING_VER='9.0.0'
136 DEB_TESTING_CODENAME='stretch'
137
138 DEB_UNSTABLE_VER='10.0.0'
139 DEB_UNSTABLE_CODENAME='buster'
140
141 function setup_deb ()
142 {
143     SRC_TIMESTAMP=$(new_src_timestamp)
144     DIST=$1
145     VERSION=$2
146     ARCH=$3
147     ARCH=${ARCH:-${DEB_ARCH64}}
148
149     declare -A V
150     VVAL=$(echo ${VERSION} | perl -ne 'm/(?:(\d+)(?:\.(\d+))?)(?:\.(\d+))?/; $min=$2 // 0; $mic = $3 // 0; print qq{([maj]=$1 [min]=$min [mic]=$mic)}')
151     eval "V=${VVAL}"
152
153     LCDIST=$(echo ${DIST} | perl -ne 'print lc')
154
155     MAJOR_VERSION="${V['maj']}"
156     MINOR_VERSION="${MAJOR_VERSION}.${V['min']}"
157     MICRO_VERSION="${MINOR_VERSION}.${V['mic']}"
158
159     CODENAME=""
160
161     if [ "Debian" == "${DIST}" ]
162     then
163         CODENAME="${deb_codename_map[$MINOR_VERSION]}"
164         CODENAME=${CODENAME:-${deb_codename_map[$MAJOR_VERSION]}}
165         if [ -z "$CODENAME" ]
166         then
167             echo "--> no codename for ${DIST} v${MICRO_VERSION}"
168             return -2
169         fi
170
171         URL_PFX="http://cdimage.debian.org/cdimage/openstack/"
172
173         if [ "${DEB_CURRENT_CODENAME}" == "${CODENAME}" ]
174         then
175             OSTACK_SUBDIR='current'
176             QCOW_VER=${MICRO_VERSION}
177         elif [ "${DEB_TESTING_CODENAME}" == "${CODENAME}" ]
178         then
179             OSTACK_SUBDIR='testing'
180             QCOW_VER='testing'
181         else
182             echo "--> Not certain where to find images for ${DIST} v${MICRO_VERSION}"
183         fi
184
185         IMG_FNAME="${LCDIST}-${QCOW_VER}-openstack-${ARCH}.qcow2"
186         URL="http://cdimage.debian.org/cdimage/openstack/${OSTACK_SUBDIR}/${IMG_FNAME}"
187
188     elif [ "Ubuntu" == "${DIST}" ]
189     then
190         CODENAME="${ubuntu_codename_map[$MINOR_VERSION]}"
191         if [ -z "$CODENAME" ]
192         then
193             echo "--> no codename for ${DIST} v${MICRO_VERSION}"
194             return -2
195         fi
196
197         IMG_FNAME="${CODENAME}-server-cloudimg-${ARCH}-disk1.img"
198         URL="https://cloud-images.ubuntu.com/${CODENAME}/current/${IMG_FNAME}"
199     else
200         echo "--> unrecognized distribution: ${DIST}"
201         return -1
202     fi
203
204     export IMG_PATH="${LV_IMG_DIR}/${IMG_FNAME}"
205     export IMG_NAME="${DIST} ${VERSION} (${SRC_TIMESTAMP}) - LF upload"
206
207 }
208 #
209 # usage:
210 #   download_deb_image ${DIST} ${VERSION} ${ARCH}
211 #
212 # example:
213 #   download_deb_image Ubuntu 14.04 amd64
214 #
215 function download_deb_image ()
216 {
217     setup_deb "$@"
218
219     if [ -z "$URL" ]; then echo "Cannot fetch qcow2 image for ${DIST} v${MICRO_VERSION}"; return -3; fi
220     echo "--> Fetching image file for ${DIST} ${VERSION}"
221     wget -qcP ${LV_IMG_DIR} "${URL}"
222 }
223
224 # Used to upload
225 #
226 # usage:
227 #   create_deb_image ${DIST} ${VERSION} ${ARCH}
228 #
229 # example:
230 #   create_deb_image Ubuntu 14.04 amd64
231 #
232 function create_deb_image ()
233 {
234     setup_deb "$@"
235
236     if [ ! -f ${IMG_PATH} ]; then download_deb_image "$@"; fi
237
238     echo "--> Pushing image ${IMG_NAME}"
239     glance_image_create "${IMG_NAME}" "${IMG_PATH}"
240 }
241
242 function respin_deb_image ()
243 {
244     SRC_TIMESTAMP=$(latest_src_timestamp)
245     DST_TIMESTAMP=$(new_dst_timestamp)
246     setup_deb "$@"
247     export IMAGE="${IMG_NAME}"
248     echo "--> creating instance of image '${IMG_NAME}' as server name '${SERVER_NAME}'"
249     vagrant up --provider=openstack
250     if [ "Ubuntu" == "${DIST}" ]
251     then
252         DST_IMAGE="${DIST} ${VERSION} LTS - basebuild - ${DST_TIMESTAMP}"
253     elif [ "Debian" == "${DIST}" ]
254     then
255         DST_IMAGE="${DIST} ${VERSION} - basebuild - ${DST_TIMESTAMP}"
256     else
257         echo "unrecognized disribution: ${DIST}"
258         exit -4
259     fi
260     echo "--> Taking snapshot of image '${IMG_NAME}' with name '${DST_IMAGE}'"
261     nova image-create --poll "${SERVER_NAME}" "${DST_IMAGE}"
262     echo "--> Bringing down vagrant instance"
263     vagrant destroy
264 }
265
266 function respin_rh_image ()
267 {
268     SRC_TIMESTAMP=$(latest_src_timestamp)
269     DST_TIMESTAMP=$(new_dst_timestamp)
270     setup_rh "$@"
271     export IMAGE="${IMG_NAME}"
272     echo "--> creating instance of image '${IMG_NAME}' as server name '${SERVER_NAME}'"
273     vagrant up --provider=openstack
274     DST_IMAGE="${DIST} ${VERSION} - basebuild - ${DST_TIMESTAMP}"
275     echo "--> Taking snapshot of image '${IMG_NAME}' with name '${DST_IMAGE}'"
276     nova image-create --poll "${SERVER_NAME}" "${DST_IMAGE}"
277     echo "--> Bringing down vagrant instance"
278     vagrant destroy
279 }
280
281 function dist_type ()
282 {
283   case "${1}" in
284       CentOS | RHEL | SuSE)
285           echo "rh" ;;
286       Debian | Ubuntu | Kali | ProxMox | VyOS)
287           echo "deb" ;;
288       *)
289           echo "Unrecognized distribution: ${1}"
290           exit 2 ;;
291   esac
292
293 }
294