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 echo "---> update-cloud-images.sh"
25 # shellcheck disable=SC1090
28 lf-activate-venv python-openstackclient
30 mkdir -p "$WORKSPACE/archives"
31 echo "INFO: List of images in use on the source repository:"
32 grep -Er '(_system_image:|IMAGE_NAME)' \
33 --exclude-dir="global-jjb" --exclude-dir="common-packer" \
34 | grep -oP 'ZZCI\s+.*\d+-\d+\.\d+' | sort -n | uniq \
35 | tee "$WORKSPACE/archives/used_image_list.txt"
37 while read -r line ; do
38 image_in_use="${line}"
40 # get image type - ex: builder, docker, gbp etc
41 image_type="${line% -*}"
42 # Get the latest images available on the cloud, when $NEW_IMAGE_NAME env
43 # var is unset and update all images on Jenkins to the latest.
44 if [[ $NEW_IMAGE_NAME != all ]]; then
45 new_image=${NEW_IMAGE_NAME}
46 new_image_type="${NEW_IMAGE_NAME% -*}"
47 # get the $new_image_type to check the image type is being compared
48 if [[ ${new_image_type} != "${image_type}" ]]; then
49 echo "INFO: Image type does not match, continue ..."
53 new_image=$(openstack image list --long -f value -c Name -c Protected \
54 | grep "${image_type}.*False" | tail -n-1 | sed 's/ False//') \
57 [[ -z $new_image ]] && continue
58 echo "INFO: Found image type match, compare timestamps."
60 # strip the timestamp from the image name
61 new_image_isotime=${new_image##*- }
62 image_in_use_isotime=${image_in_use##*- }
63 # Remove '-' & '.' from the timestamp and perform numeric compare
64 if [[ ${new_image_isotime//[\-\.]/} -gt ${image_in_use_isotime//[\-\.]/} ]]; then
65 # generate a patch to be submited to Gerrit
66 echo "INFO: Update old image: $image_in_use with new image: $new_image"
67 grep -rlE '(_system_image:|IMAGE_NAME)' \
68 | xargs sed -i "s/${image_in_use}/${new_image}/"
69 # When the script is triggered by upstream packer-merge job
70 # update only the requested image and break the loop
71 [[ $NEW_IMAGE_NAME != all ]] && break
73 echo "INFO: No new image to update: $new_image"
75 done < "$WORKSPACE/archives/used_image_list.txt"
77 git diff > "$WORKSPACE/archives/new-images-patchset.diff"