forked from openstack/openstack-ansible-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync-test-repos.sh
executable file
·228 lines (178 loc) · 7.27 KB
/
sync-test-repos.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# Copyright 2017, SUSE LINUX GmbH.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script will sync $files_to_sync files across all supported repositories as
# returned by the gen-projects-list.sh script. The goal here is to try and sync
# all these files at regular intervals so the code in the repositories is
# tested in the same way on both the OpenStack CI and the Vagrant platforms.
# This script will open reviews in the OpenStack gerrit so make sure your system is
# configured properly to submit upstream reviews. Use ./sync-test-repos.sh -h
# to get more information on how to use this script. Bugs about this script
# should be submitted to the openstack-ansible project on launchpad as usual.
# This script has a partner which is executed by the proposal bot here:
# https://opendev.org/openstack/project-config/src/playbooks/proposal/sync_openstack_ansible_common_files.sh
# Changes made to this file should be mirrored there when applicable.
set -eu
usage() {
cat <<EOF
${0} [options]
Valid options are:
-h, --help: This message
-i, --interactive: Shows git diffs and requires user confirmation before
submitting reviews
-n, --dry-run: Shows git diffs and executes 'git-review -n' to show
what will happen on a normal run
EOF
}
exclude_project() {
excluded_projects+="${1} "
}
cleanup() {
[[ -d ${tempdir} ]] && { echo "Cleaning up ${tempdir}"; rm -rf ${tempdir}; }
}
process_changes() {
local project=${1}
local review=${2}
(
cd ${project}
# if nothing changed just return
git diff --quiet && echo "No new changes to commit" && return 0
${dry_run} || ${interactive} && git diff
if ${interactive}; then
read -p 'Submit review? [y/N] ' review
! [[ ${review} =~ ^(Y|y) ]] && return 0
fi
# Prepare gerrit
git review -s
# Commit changes
git add .
git commit $([[ ${review} == "__no_review__" ]] || printf %s '--amend') -m 'Updated from OpenStack Ansible Tests'
git review $(${dry_run} && printf %s '-n')
)
}
check_and_ignore() {
for z in $(echo ${excluded_projects} | tr ' ' '\n'); do
[[ ${1} == ${z} ]] && return 0
done
return 1
}
copy_files() {
local osa_project=${1}
# Copy files
for src_path in ${files_to_sync[@]}; do
# If the source repo does not have the file to copy
# then skip to the next file. This covers the situation
# where this script runs against old branches which
# do not have the same set of files. If the src_path
# is 'sync/tasks/*' because the folder does not exist
# then it will fail this test too.
[[ ! -e ${src_path} ]] && continue
# For the sync/* files in the array, the destination
# path is different to the source path. To work out
# the destination path we remove the 'sync/' prefix.
dst_path=${src_path#sync/}
# If the target repo does not have such a file already
# then it's probably best to leave it alone.
[[ ! -e ${osa_project}/${dst_path} ]] && continue
# We don't preserve anything from the target repo.
# We simply assume that all OSA projects need the same
# $files_to_sync
cp ${src_path} ${osa_project}/${dst_path}
done
}
# Do not change these files unless you know what you are doing
declare -ra files_to_sync=(run_tests.sh bindep.txt Vagrantfile tests/tests-repo-clone.sh .gitignore sync/tasks/*)
declare -r openstack_git_url="https://opendev.org"
excluded_projects=
dry_run=false
interactive=false
OPTS=$(getopt -o hin --long help,interactive,dry-run -n '$(basename ${0}' -- "$@")
eval set -- "${OPTS}"
while true; do
case "${1}" in
-i|--interactive) interactive=true; shift ;;
-n|--dry-run) dry_run=true; shift ;;
--) shift; break ;;
-h|--help) usage; exit 1 ;;
esac
done
# Always exclude openstack-ansible-tests repository. This is not
# necessary because osa_projects should never include "openstack-ansible-tests"
# but it can serve as an example for users who may add more
# projects in the future.
exclude_project "openstack-ansible-tests"
############################# ZUUL SYNCING ###################################
# If we running in the OpenStack CI then the first argument is going to be the
# project directory and all we need to do is to simply copy files. The
# environment is already prepared.
if env | grep -q ^ZUUL; then
# Some debug information.
echo "Running in a Zuul environment"
echo "Current directory: $(pwd)"
echo "OSA project: '${1}'"
# Do we need to skip that repo?
check_and_ignore ${1} && exit 0
# This should never happen if Zuul is working properly
[[ ! -d ${1} ]] && { echo "${1} does not exit! Refusing to proceed"; exit 1; }
copy_files ${1}
# Return back to zuul. No furher processing is required.
exit 0
else
declare -ra osa_projects=($(./gen-projects-list.sh))
fi
# Make sure interactive and dry run can't be used together
${dry_run} && ${interactive} && \
echo "Can't use interactive and dry-run at the same time. Disabling interactive mode..." && \
interactive=false
# Create a temporary directory
tempdir=$(mktemp -d -q || { echo "Failed to create temporary directory"; exit 1; })
trap cleanup EXIT
# make sure this is brand new
cleanup
echo "=> Temporary directory for OSA repositories: ${tempdir}"
mkdir ${tempdir}
pushd ${tempdir} &> /dev/null
echo "=> Cloning openstack-ansible-tests repository"
eval git clone ${openstack_git_url}/openstack/openstack-ansible-tests
echo -e "\n---------------------------------------------\n"
for proj in ${osa_projects[@]}; do
proj_dir=$(basename ${proj})
# Skip the project if it is in the excluded list
check_and_ignore ${proj_dir} && continue
echo "=> ##### ${proj} #####"
eval git clone ${openstack_git_url}/$proj
pushd $proj_dir &> /dev/null
git checkout -b openstack/openstack-ansible-tests/sync-tests
# if there an open review, re-use it
open_review=$(git review --no-color -l | \
grep -v "^Found" | \
grep "Updated from OpenStack Ansible Tests" | \
tail -n1 | awk '{print $1}')
[[ -n ${open_review} ]] && \
echo "Using existing review #${open_review} for ${proj_dir}" && \
git review -x ${open_review}
popd &> /dev/null
# Copy files
pushd openstack-ansible-tests &> /dev/null
copy_files ${proj_dir}
popd &> /dev/null
process_changes ${proj_dir} ${open_review:="__no_review__"}
# Clean up the directory
rm -rf ${proj_dir}
echo -e "=> ##################################################\n"
done
popd &> /dev/null
echo "All OpenStack Ansible repositories have been synced successfully!"
echo "Happy testing ;-)"
exit 0