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