297da33959e8ab6bacae83b60b167fa16f00cd81
[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 "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"
37
38 while read -r line ; do
39     image_in_use="${line}"
40
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}
47         new_image_type="${NEW_IMAGE_NAME% -*}"
48         # get the $new_image_type to check the image type is being compared
49         [[ ${new_image_type} =~ ${image_type} ]] && continue
50     else
51         new_image=$(openstack image list --long -f value -c Name -c Protected \
52             | grep "${image_type}.*False" | tail -n-1 | sed 's/ False//')   \
53             || true
54     fi
55     [[ -z $new_image ]] && continue
56
57     # strip the timestamp from the image name
58     new_image_isotime=${new_image##*- }
59     image_in_use_isotime=${image_in_use##*- }
60     # Remove '-' & '.' from the timestamp and perform numeric compare
61     if [[ ${new_image_isotime//[\-\.]/} -gt ${image_in_use_isotime//[\-\.]/} ]]; then
62         # generate a patch to be submited to Gerrit
63         echo "Update old image: $image_in_use with new image: $new_image"
64         grep -rlE '(_system_image:|IMAGE_NAME)' \
65             | xargs sed -i "s/${image_in_use}/${new_image}/"
66         # When the script is triggered by upstream packer-merge job
67         # update only the requested image and break the loop
68         [[ $NEW_IMAGE_NAME != all ]] && break
69     else
70         echo "No new image to update: $new_image"
71     fi
72 done < "$WORKSPACE/archives/used_image_list.txt"
73
74 git diff > "$WORKSPACE/archives/new-images-patchset.diff"
75 git add -u
76 git status