Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / update-cloud-images.sh
1 #!/bin/bash
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 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
20
21 echo "---> update-cloud-images.sh"
22
23 set -euf -o pipefail
24
25 # shellcheck disable=SC1090
26 source ~/lf-env.sh
27
28 lf-activate-venv python-openstackclient
29
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"
36
37 while read -r line ; do
38     image_in_use="${line}"
39
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 ..."
50             continue
51         fi
52     else
53         new_image=$(openstack image list --long -f value -c Name -c Protected \
54             | grep "${image_type}.*False" | tail -n-1 | sed 's/ False//')     \
55             || true
56     fi
57     [[ -z $new_image ]] && continue
58     echo "INFO: Found image type match, compare timestamps."
59
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
72     else
73         echo "INFO: No new image to update: $new_image"
74     fi
75 done < "$WORKSPACE/archives/used_image_list.txt"
76
77 git diff > "$WORKSPACE/archives/new-images-patchset.diff"
78 git add -u
79 git status