Update docker files base images to snapshots
[policy/parent.git] / integration / src / main / scripts / release / updateRefs.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2021-2022 Nordix Foundation.
8 # =========================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END==================================================
21 #
22
23 set -e
24
25 SCRIPT_NAME=$(basename "$0")
26 repo_location="./"
27 release_data_file="./pf_release_data.csv"
28
29 # Use the bash internal OSTYPE variable to check for MacOS
30 if [[ "$OSTYPE" == "darwin"* ]]
31 then
32     SED="gsed"
33 else
34     SED="sed"
35 fi
36
37 usage()
38 {
39     echo ""
40     echo "$SCRIPT_NAME - updates the inter-repo references in Policy Framework POM files"
41     echo ""
42     echo "       usage:  $SCRIPT_NAME [-options]"
43     echo ""
44     echo "       options"
45     echo "         -h           - this help message"
46     echo "         -d data_file - the policy release data file to use, generated by the 'getReleaseData.sh' script,"
47     echo "                        defaults to '$release_data_file'"
48     echo "         -l location  - the location of the policy framework repos on the file system,"
49     echo "                        defaults to '$repo_location'"
50     echo "         -r repo      - the policy repo to update"
51     echo "         -p           - update policy/parent references"
52     echo "         -c           - update policy/common references"
53     echo "         -m           - update policy/model references"
54     echo "         -o           - update policy/drools-pdp references"
55     echo "         -x           - update policy/apex-pdp references"
56     echo "         -k           - update docker base images in Dockerfiles"
57     echo "         -f           - update release data in policy parent"
58     echo "         -s           - update release references to snapshot references,"
59     echo "                        if omitted, snapshot references are updated to release references"
60     echo ""
61     echo " examples:"
62     echo "  $SCRIPT_NAME -pcm -r policy/pap"
63     echo "              update the parent, common, and models references of policy/pap"
64     echo "              to the current released version"
65     echo ""
66     echo "  $SCRIPT_NAME -c -m -s -r policy/api"
67     echo "              update the common and models references of policy/api"
68     echo "              to the current snapshot version"
69     exit 255;
70 }
71
72 update_parent=false
73 update_common=false
74 update_models=false
75 update_drools_pdp=false
76 update_apex_pdp=false
77 update_snapshot=false
78 update_docker=false
79 update_file=false
80
81 while getopts "hd:l:r:pcmoxkfs" opt
82 do
83     case $opt in
84     h)
85         usage
86         ;;
87     d)
88         release_data_file=$OPTARG
89         ;;
90     l)
91         repo_location=$OPTARG
92         ;;
93     r)
94         specified_repo=$OPTARG
95         ;;
96     p)
97         update_parent=true
98         ;;
99     c)
100         update_common=true
101         ;;
102     m)
103         update_models=true
104         ;;
105     o)
106         update_drools_pdp=true
107         ;;
108     x)
109         update_apex_pdp=true
110         ;;
111     k)
112         update_docker=true
113         ;;
114     f)
115         update_file=true
116         ;;
117     s)
118         update_snapshot=true
119         ;;
120     \?)
121         usage
122         exit 1
123         ;;
124     esac
125 done
126
127 if [ $OPTIND -eq 1 ]
128 then
129     echo "no arguments were specified"
130     usage
131 fi
132
133 if [[ -z "$repo_location" ]]
134 then
135     echo "policy repo location not specified on -l flag"
136     exit 1
137 fi
138
139 if ! [ -d "$repo_location" ]
140 then
141     echo "policy repo location '$repo_location' not found"
142     exit 1
143 fi
144
145 if [[ -z "$release_data_file" ]]
146 then
147     echo "policy release data file not specified on -d flag"
148     exit 1
149 fi
150
151 if ! [ -f "$release_data_file" ]
152 then
153     echo "policy release data file '$release_data_file' not found"
154     exit 1
155 fi
156
157 if [ -z "$specified_repo" ]
158 then
159     echo "repo not specified on -r flag"
160     exit 1
161 fi
162
163 # shellcheck disable=SC2034
164 # shellcheck disable=SC2046
165 read -r parent_repo \
166      parent_latest_released_tag \
167      parent_latest_snapshot_tag \
168      parent_changed_files \
169      parent_docker_images \
170     <<< $(grep policy/parent "$release_data_file" | tr ',' ' ' )
171
172 # shellcheck disable=SC2034
173 # shellcheck disable=SC2046
174 read -r common_repo \
175      common_latest_released_tag \
176      common_latest_snapshot_tag \
177      common_changed_files \
178      common_docker_images \
179     <<< $(grep policy/common "$release_data_file" | tr ',' ' ' )
180
181 # shellcheck disable=SC2034
182 # shellcheck disable=SC2046
183 read -r docker_repo \
184      docker_latest_released_tag \
185      docker_latest_snapshot_tag \
186      docker_changed_files \
187      docker_docker_images \
188     <<< $(grep policy/docker "$release_data_file" | tr ',' ' ' )
189
190 # shellcheck disable=SC2034
191 # shellcheck disable=SC2046
192 read -r models_repo \
193      models_latest_released_tag \
194      models_latest_snapshot_tag \
195      models_changed_files \
196      models_docker_images \
197     <<< $(grep policy/models "$release_data_file" | tr ',' ' ' )
198
199 # shellcheck disable=SC2034
200 # shellcheck disable=SC2046
201 read -r drools_pdp_repo \
202      drools_pdp_latest_released_tag \
203      drools_pdp_latest_snapshot_tag \
204      drools_pdp_changed_files \
205      drools_pdp_docker_images \
206     <<< $(grep policy/drools-pdp "$release_data_file" | tr ',' ' ' )
207
208 # shellcheck disable=SC2034
209 # shellcheck disable=SC2046
210 read -r apex_pdp_repo \
211      apex_pdp_latest_released_tag \
212      apex_pdp_latest_snapshot_tag \
213      apex_pdp_changed_files \
214      apex_pdp_docker_images \
215     <<< $(grep policy/apex-pdp "$release_data_file" | tr ',' ' ' )
216
217 # shellcheck disable=SC2034
218 # shellcheck disable=SC2046
219 read -r target_repo \
220          target_latest_released_tag \
221          target_latest_snapshot_tag \
222          target_changed_files \
223          target_docker_images \
224         <<< $(grep "$specified_repo" "$release_data_file" | tr ',' ' ' )
225
226 if [ -z "$target_repo" ]
227 then
228     echo "specified repo '$specified_repo' not found in policy release data file '$release_data_file'"
229     exit 1
230 fi
231
232 if [ ! "$specified_repo" = "$target_repo" ]
233 then
234     echo "specified repo '$specified_repo' does not match target repo '$target_repo'"
235     exit 1
236 fi
237
238 if [ "$update_parent" = true ]
239 then
240     if [ "$specified_repo" = "policy/parent" ]
241     then
242         if [ "$update_snapshot" = true ]
243         then
244             major_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^([0-9]*)\.[0-9]*\.[0-9]*$/\1/')
245             minor_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^[0-9]*\.([0-9]*)\.[0-9]*$/\1/')
246             patch_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^[0-9]*\.[0-9]*\.([0-9]*)$/\1/')
247             new_patch_version=$(("$patch_version"+1))
248
249             new_snapshot_tag="$major_version"."$minor_version"."$new_patch_version"-SNAPSHOT
250
251             echo updating policy parent reference to "$new_snapshot_tag" on "$repo_location/$target_repo" . . .
252             $SED -i \
253                 "s/<version.parent.resources>.*<\/version.parent.resources>/<version.parent.resources>$new_snapshot_tag<\/version.parent.resources>/" \
254                  "$repo_location/policy/parent/integration/pom.xml"
255             result_code=$?
256         else
257             next_release_version=${parent_latest_snapshot_tag%-*}
258
259             echo "updating policy parent reference to $next_release_version on $repo_location/$target_repo . . ."
260             $SED -i \
261                 "s/<version.parent.resources>.*<\/version.parent.resources>/<version.parent.resources>$next_release_version<\/version.parent.resources>/" \
262                 "$repo_location/policy/parent/integration/pom.xml"
263             result_code=$?
264         fi
265     else
266         if [ "$update_snapshot" = true ]
267         then
268             echo "updating policy parent reference to $parent_latest_snapshot_tag on $repo_location/$target_repo . . ."
269             updateParentRef.sh \
270                 -f "$repo_location/$target_repo/pom.xml" \
271                 -g org.onap.policy.parent \
272                 -a integration \
273                 -v "$parent_latest_snapshot_tag"
274             result_code=$?
275         else
276             echo "updating policy parent reference to $parent_latest_released_tag on $repo_location/$target_repo . . ."
277             updateParentRef.sh \
278                 -f "$repo_location/$target_repo/pom.xml" \
279                 -g org.onap.policy.parent \
280                 -a integration \
281                 -v "$parent_latest_released_tag"
282             result_code=$?
283         fi
284     fi
285     if [[ "$result_code" -eq 0 ]]
286     then
287         echo "policy parent reference updated on $repo_location/$target_repo"
288     else
289         echo "policy parent reference update failed on $repo_location/$target_repo"
290         exit 1
291     fi
292 fi
293
294 if [ "$update_common" = true ]
295 then
296     if [ "$update_snapshot" = true ]
297     then
298         echo "updating policy common reference to $common_latest_snapshot_tag on $repo_location/$target_repo . . ."
299         $SED -i \
300             -e "s/<policy.common.version>.*<\/policy.common.version>/<policy.common.version>$common_latest_snapshot_tag<\/policy.common.version>/" \
301             -e "s/<version.policy.common>.*<\/version.policy.common>/<version.policy.common>$common_latest_snapshot_tag<\/version.policy.common>/" \
302             "$repo_location/$target_repo/pom.xml"
303         result_code=$?
304     else
305         echo "updating policy common reference to $common_latest_released_tag on $repo_location/$target_repo . . ."
306         $SED -i \
307             -e "s/<policy.common.version>.*<\/policy.common.version>/<policy.common.version>$common_latest_released_tag<\/policy.common.version>/" \
308             -e "s/<version.policy.common>.*<\/version.policy.common>/<version.policy.common>$common_latest_released_tag<\/version.policy.common>/" \
309             "$repo_location/$target_repo/pom.xml"
310         result_code=$?
311     fi
312     if [[ "$result_code" -eq 0 ]]
313     then
314         echo "policy common reference updated on $repo_location/$target_repo"
315     else
316         echo "policy common reference update failed on $repo_location/$target_repo"
317         exit 1
318     fi
319 fi
320
321 if [ "$update_models" = true ]
322 then
323     if [ "$update_snapshot" = true ]
324     then
325         echo "updating policy models reference to $models_latest_snapshot_tag on $repo_location/$target_repo . . ."
326         $SED -i \
327             -e "s/<policy.models.version>.*<\/policy.models.version>/<policy.models.version>$models_latest_snapshot_tag<\/policy.models.version>/" \
328             -e "s/<version.policy.models>.*<\/version.policy.models>/<version.policy.models>$models_latest_snapshot_tag<\/version.policy.models>/" \
329             "$repo_location/$target_repo/pom.xml"
330         result_code=$?
331     else
332         echo "updating policy models reference to $models_latest_released_tag on $repo_location/$target_repo . . ."
333         $SED -i \
334             -e "s/<policy.models.version>.*<\/policy.models.version>/<policy.models.version>$models_latest_released_tag<\/policy.models.version>/" \
335             -e "s/<version.policy.models>.*<\/version.policy.models>/<version.policy.models>$models_latest_released_tag<\/version.policy.models>/" \
336             "$repo_location/$target_repo/pom.xml"
337         result_code=$?
338     fi
339     if [[ "$result_code" -eq 0 ]]
340     then
341         echo "policy models reference updated on $repo_location/$target_repo"
342     else
343         echo "policy models reference update failed on $repo_location/$target_repo"
344         exit 1
345     fi
346 fi
347
348 if [ "$update_drools_pdp" = true ]
349 then
350     if [ "$update_snapshot" = true ]
351     then
352         echo "updating policy drools-pdp reference to $drools_pdp_latest_snapshot_tag on $repo_location/$target_repo . . ."
353         $SED -i \
354             -e "s/<policy.drools-pdp.version>.*<\/policy.drools-pdp.version>/<policy.drools-pdp.version>$drools_pdp_latest_snapshot_tag<\/policy.drools-pdp.version>/" \
355             -e "s/<version.policy.drools-pdp>.*<\/version.policy.drools-pdp>/<version.policy.drools-pdp>$drools_pdp_latest_snapshot_tag<\/version.policy.drools-pdp>/" \
356             "$repo_location/$target_repo/pom.xml"
357         result_code=$?
358     else
359         echo "updating policy drools-pdp reference to $drools_pdp_latest_released_tag on $repo_location/$target_repo . . ."
360         $SED -i \
361             -e "s/<policy.drools-pdp.version>.*<\/policy.drools-pdp.version>/<policy.drools-pdp.version>$drools_pdp_latest_released_tag<\/policy.drools-pdp.version>/" \
362             -e "s/<version.policy.drools-pdp>.*<\/version.policy.drools-pdp>/<version.policy.drools-pdp>$drools_pdp_latest_released_tag<\/version.policy.drools-pdp>/" \
363             "$repo_location/$target_repo/pom.xml"
364         result_code=$?
365     fi
366     if [[ "$result_code" -eq 0 ]]
367     then
368         echo "policy drools-pdp reference updated on $repo_location/$target_repo"
369     else
370         echo "policy drools-pdp reference update failed on $repo_location/$target_repo"
371         exit 1
372     fi
373 fi
374
375 if [ "$update_apex_pdp" = true ]
376 then
377     if [ "$update_snapshot" = true ]
378     then
379         echo "updating policy apex-pdp reference to $apex_pdp_latest_snapshot_tag on $repo_location/$target_repo . . ."
380         $SED -i \
381             -e "s/<policy.apex-pdp.version>.*<\/policy.apex-pdp.version>/<policy.apex-pdp.version>$apex_pdp_latest_snapshot_tag<\/policy.apex-pdp.version>/" \
382             -e "s/<version.policy.apex-pdp>.*<\/version.policy.apex-pdp>/<version.policy.apex-pdp>$apex_pdp_latest_snapshot_tag<\/version.policy.apex-pdp>/" \
383             "$repo_location/$target_repo/pom.xml"
384         result_code=$?
385     else
386         echo "updating policy apex-pdp reference to $apex_pdp_latest_released_tag on $repo_location/$target_repo . . ."
387         $SED -i \
388             -e "s/<policy.apex-pdp.version>.*<\/policy.apex-pdp.version>/<policy.apex-pdp.version>$apex_pdp_latest_released_tag<\/policy.apex-pdp.version>/" \
389             -e "s/<version.policy.apex-pdp>.*<\/version.policy.apex-pdp>/<version.policy.apex-pdp>$apex_pdp_latest_released_tag<\/version.policy.apex-pdp>/" \
390             "$repo_location/$target_repo/pom.xml"
391         result_code=$?
392     fi
393     if [[ "$result_code" -eq 0 ]]
394     then
395         echo "policy apex-pdp reference updated on $repo_location/$target_repo"
396     else
397         echo "policy apex-pdp reference update failed on $repo_location/$target_repo"
398         exit 1
399     fi
400 fi
401
402 if [ "$update_docker" = true ] && [ "$target_docker_images" != "" ]
403 then
404     echo "updating docker base images to version $docker_latest_released_tag on repo $repo_location/$target_repo . . ."
405     find "$repo_location/$target_repo" \
406         -name '*Docker*'
407
408     if [ "$update_snapshot" == true ]
409     then
410         find "$repo_location/$target_repo" \
411             -name '*Docker*' \
412             -exec $SED -r -i "s/^(FROM onap\/policy-j[d|r][k|e]-alpine:)[0-9]*.[0-9]*.[0-9]*$/\1$docker_latest_snapshot_tag/" {} \;
413         result_code=$?
414     else
415         find "$repo_location/$target_repo" \
416             -name '*Docker*' \
417             -exec $SED -r -i "s/^(FROM onap\/policy-j[d|r][k|e]-alpine:)[0-9]*.[0-9]*.[0-9]*$/\1$docker_latest_released_tag/" {} \;
418         result_code=$?
419     fi
420
421     if [[ "$result_code" -eq 0 ]]
422     then
423         echo "docker base images updated on $repo_location/$target_repo"
424     else
425         echo "docker base images update failed on $repo_location/$target_repo"
426         exit 1
427     fi
428 fi
429
430 if [ "$update_file" = true ]
431 then
432     if [ ! "$target_repo" = "policy/parent" ]
433     then
434         echo "update of data file can only be done on the policy/parent repo"
435         exit 1
436     fi
437
438     echo "updating release data at $repo_location/$target_repo/integration/src/main/resources/release . . ."
439     cp "$release_data_file" "$repo_location/$target_repo"/integration/src/main/resources/release
440     echo "updated release data at $repo_location/$target_repo/integration/src/main/resources/release"
441 fi