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