Fix rtd merge job to handle new tag uploaded
[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 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
26 pip freeze
27
28 set -e
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     fi
54     [[ -z ${new_image} ]] && continue
55
56     # strip the timestamp from the image name amd compare
57     new_image_isotime=${new_image##*- }
58     image_in_use_isotime=${image_in_use##*- }
59     # compare timestamps
60     if [[ ${new_image_isotime//[\-\.]/} -gt ${image_in_use_isotime//[\-\.]/} ]]; then
61         # generate a patch to be submited to Gerrit
62         echo "Update old image: ${image_in_use} with new image: ${new_image}"
63         grep -rlE '(_system_image:|IMAGE_NAME)' | xargs sed -i "s/${image_in_use}/${new_image}/"
64         # When the script is triggered by upstream packer-merge job
65         # update only the requested image and break the loop
66         [[ ${NEW_IMAGE_NAME} != all ]] && break
67     else
68         echo "No new image to update: ${new_image}"
69     fi
70 done < "$WORKSPACE/archives/used_image_list.txt"
71
72 git remote -v
73 git status
74 git diff > "$WORKSPACE/archives/new-images-patchset.diff"
75 git add -u