Add job to auto-update new built packer images
[releng/builder.git] / jjb / opendaylight-infra-update-images.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 # Auto-update packer images:
13 # 1. Get a list of images from the releng/builder repository
14 # 2. Search openstack cloud for the latest images available
15 # 3. Compare the time stamps of the new image with the image in use
16 # 4. Update the image to the config files and yaml files
17 # 5. Push the change to Gerrit
18
19 virtualenv "/tmp/v/openstack"
20 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
21 source "/tmp/v/openstack/bin/activate"
22 pip install --upgrade --quiet "pip<10.0.0" setuptools
23 pip install --upgrade --quiet python-openstackclient
24 pip freeze
25
26 mkdir -p "$WORKSPACE/archives"
27 echo "List of images used on the source repository:"
28 grep -Er '(_system_image:|IMAGE_NAME)' \
29 | grep  ZZCI | awk -F: -e '{print $3}' \
30 | grep '\S' | tr -d \'\" | sort -n | uniq \
31 | tee "$WORKSPACE/archives/used_image_list.txt"
32
33 set -x
34
35 while read -r line ; do
36     # remove leading white spaces if they exists
37     image_in_use="${line#"${line%%[![:space:]]*}"}"
38     # remove trailing white spaces if they exists
39     image_in_use="${image_in_use%"${image_in_use##*[![:space:]]}"}"
40     # get image type - ex: builder, docker, gbp etc
41     image_type="${line% -*}"
42     # get the latest image available on the cloud
43     new_image=$(openstack image list --long -f value -c Name -c Protected \
44         | grep "${image_type}.*False" | tail -n-1 | sed 's/ False//')
45     [[ -z ${new_image} ]] && continue
46     # strip the timestamp from the image name amd compare
47     new_image_isotime=${new_image##*- }
48     image_in_use_isotime=${image_in_use##*- }
49     # compare timestamps
50     if [[ ${new_image_isotime//[\-\.]/} -gt ${image_in_use_isotime//[\-\.]/} ]]; then
51         # generate a patch to be submited to Gerrit
52         echo "Update old image: ${image_in_use} with new image: ${new_image}"
53         grep -rlE '(_system_image:|IMAGE_NAME)' | xargs sed -i "s/${image_in_use}/${new_image}/"
54     else
55         echo "No new image to update: ${new_image}"
56     fi
57 done < "$WORKSPACE/archives/used_image_list.txt"
58
59 git remote -v
60 git add -u
61 git status