2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
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 ##############################################################################
12 # Auto-update packer image{s} when the job is started manually or a single
13 # image passed by upstream packer merge job:
14 # 1. Get a list of image{s} from the releng/builder repository
15 # 2. Search openstack cloud for the latest image{s} available or use the image
16 # name passed down from the upstream job.
17 # 3. Compare the time stamps of the new image{s} with the image in use
18 # 4. Update the image{s} in the config files and yaml files
19 # 5. Push the change to Gerrit
21 virtualenv "/tmp/v/openstack"
22 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
23 source "/tmp/v/openstack/bin/activate"
24 pip install --upgrade --quiet "pip<10.0.0" setuptools
25 pip install --upgrade --quiet python-openstackclient
30 mkdir -p "$WORKSPACE/archives"
31 echo "List of images used on the source repository:"
32 grep -Er '(_system_image:|IMAGE_NAME)' \
33 --exclude-dir="global-jjb" --exclude-dir="common-packer" \
34 | grep ZZCI | awk -F: -e '{print $3}' \
35 | grep '\S' | tr -d \'\" | sort -n | uniq \
36 | tee "$WORKSPACE/archives/used_image_list.txt"
38 while read -r line ; do
39 image_in_use="${line}"
41 # get image type - ex: builder, docker, gbp etc
42 image_type="${line% -*}"
43 # Get the latest images available on the cloud, when $NEW_IMAGE_NAME env
44 # var is unset and update all images on Jenkins to the latest.
45 if [[ ${NEW_IMAGE_NAME} != all ]]; then
46 new_image=${NEW_IMAGE_NAME}
48 new_image=$(openstack image list --long -f value -c Name -c Protected \
49 | grep "${image_type}.*False" | tail -n-1 | sed 's/ False//')
51 [[ -z ${new_image} ]] && continue
53 # strip the timestamp from the image name amd compare
54 new_image_isotime=${new_image##*- }
55 image_in_use_isotime=${image_in_use##*- }
57 if [[ ${new_image_isotime//[\-\.]/} -gt ${image_in_use_isotime//[\-\.]/} ]]; then
58 # generate a patch to be submited to Gerrit
59 echo "Update old image: ${image_in_use} with new image: ${new_image}"
60 grep -rlE '(_system_image:|IMAGE_NAME)' | xargs sed -i "s/${image_in_use}/${new_image}/"
61 # When the script is triggered by upstream packer-merge job
62 # update only the requested image and break the loop
63 [[ ${NEW_IMAGE_NAME} != all ]] && break
65 echo "No new image to update: ${new_image}"
67 done < "$WORKSPACE/archives/used_image_list.txt"
71 git diff > "$WORKSPACE/archives/new-images-patchset.diff"