45ec06522a7f9f1014d5f426a03c21ff1c4f237e
[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 "         -f           - update release data in policy parent"
59     echo "         -t tag       - 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_file=false
82
83 while getopts "hd:l:r:pcmoxkft: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     f)
117         update_file=true
118         ;;
119     t)
120         release_data_file_tag="$OPTARG"_
121         ;;
122     s)
123         update_snapshot=true
124         ;;
125     \?)
126         usage
127         exit 1
128         ;;
129     esac
130 done
131
132 if [ $OPTIND -eq 1 ]
133 then
134     echo "no arguments were specified"
135     usage
136 fi
137
138 if [[ -z "$repo_location" ]]
139 then
140     echo "policy repo location not specified on -l flag"
141     exit 1
142 fi
143
144 if ! [ -d "$repo_location" ]
145 then
146     echo "policy repo location '$repo_location' not found"
147     exit 1
148 fi
149
150 if [[ -z "$release_data_file" ]]
151 then
152     echo "policy release data file not specified on -d flag"
153     exit 1
154 fi
155
156 if ! [ -f "$release_data_file" ]
157 then
158     echo "policy release data file '$release_data_file' not found"
159     exit 1
160 fi
161
162 if [ -z "$specified_repo" ]
163 then
164     echo "repo not specified on -r flag"
165     exit 1
166 fi
167
168 # shellcheck disable=SC2034
169 # shellcheck disable=SC2046
170 read -r parent_repo \
171      parent_latest_released_tag \
172      parent_latest_snapshot_tag \
173      parent_changed_files \
174      parent_docker_images \
175     <<< $(grep policy/parent "$release_data_file" | tr ',' ' ' )
176
177 # shellcheck disable=SC2034
178 # shellcheck disable=SC2046
179 read -r common_repo \
180      common_latest_released_tag \
181      common_latest_snapshot_tag \
182      common_changed_files \
183      common_docker_images \
184     <<< $(grep policy/common "$release_data_file" | tr ',' ' ' )
185
186 # shellcheck disable=SC2034
187 # shellcheck disable=SC2046
188 read -r docker_repo \
189      docker_latest_released_tag \
190      docker_latest_snapshot_tag \
191      docker_changed_files \
192      docker_docker_images \
193     <<< $(grep policy/docker "$release_data_file" | tr ',' ' ' )
194
195 # shellcheck disable=SC2034
196 # shellcheck disable=SC2046
197 read -r models_repo \
198      models_latest_released_tag \
199      models_latest_snapshot_tag \
200      models_changed_files \
201      models_docker_images \
202     <<< $(grep policy/models "$release_data_file" | tr ',' ' ' )
203
204 # shellcheck disable=SC2034
205 # shellcheck disable=SC2046
206 read -r drools_pdp_repo \
207      drools_pdp_latest_released_tag \
208      drools_pdp_latest_snapshot_tag \
209      drools_pdp_changed_files \
210      drools_pdp_docker_images \
211     <<< $(grep policy/drools-pdp "$release_data_file" | tr ',' ' ' )
212
213 # shellcheck disable=SC2034
214 # shellcheck disable=SC2046
215 read -r apex_pdp_repo \
216      apex_pdp_latest_released_tag \
217      apex_pdp_latest_snapshot_tag \
218      apex_pdp_changed_files \
219      apex_pdp_docker_images \
220     <<< $(grep policy/apex-pdp "$release_data_file" | tr ',' ' ' )
221
222 # shellcheck disable=SC2034
223 # shellcheck disable=SC2046
224 read -r target_repo \
225          target_latest_released_tag \
226          target_latest_snapshot_tag \
227          target_changed_files \
228          target_docker_images \
229         <<< $(grep "$specified_repo" "$release_data_file" | tr ',' ' ' )
230
231 if [ -z "$target_repo" ]
232 then
233     echo "specified repo '$specified_repo' not found in policy release data file '$release_data_file'"
234     exit 1
235 fi
236
237 if [ ! "$specified_repo" = "$target_repo" ]
238 then
239     echo "specified repo '$specified_repo' does not match target repo '$target_repo'"
240     exit 1
241 fi
242
243 if [ "$update_parent" = true ]
244 then
245     if [ "$specified_repo" = "policy/parent" ]
246     then
247         if [ "$update_snapshot" = true ]
248         then
249             major_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^([0-9]*)\.[0-9]*\.[0-9]*$/\1/')
250             minor_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^[0-9]*\.([0-9]*)\.[0-9]*$/\1/')
251             patch_version=$(echo "$parent_latest_released_tag" | $SED -E 's/^[0-9]*\.[0-9]*\.([0-9]*)$/\1/')
252
253             new_patch_version=$((patch_version+1))
254             new_snapshot_tag="$major_version"."$minor_version"."$new_patch_version"-SNAPSHOT
255
256             echo updating policy parent reference to "$new_snapshot_tag" on "$repo_location/$target_repo" . . .
257             $SED -i \
258                 "s/<version.parent.resources>.*<\/version.parent.resources>/<version.parent.resources>$new_snapshot_tag<\/version.parent.resources>/" \
259                  "$repo_location/policy/parent/integration/pom.xml"
260             result_code=$?
261         else
262             next_release_version=${parent_latest_snapshot_tag%-*}
263
264             echo "updating policy parent reference to $next_release_version on $repo_location/$target_repo . . ."
265             $SED -i \
266                 "s/<version.parent.resources>.*<\/version.parent.resources>/<version.parent.resources>$next_release_version<\/version.parent.resources>/" \
267                 "$repo_location/policy/parent/integration/pom.xml"
268             result_code=$?
269         fi
270     else
271         if [ "$update_snapshot" = true ]
272         then
273             echo "updating policy parent reference to $parent_latest_snapshot_tag on $repo_location/$target_repo . . ."
274             updateParentRef.sh \
275                 -f "$repo_location/$target_repo/pom.xml" \
276                 -g org.onap.policy.parent \
277                 -a integration \
278                 -v "$parent_latest_snapshot_tag"
279             result_code=$?
280         else
281             echo "updating policy parent reference to $parent_latest_released_tag on $repo_location/$target_repo . . ."
282             updateParentRef.sh \
283                 -f "$repo_location/$target_repo/pom.xml" \
284                 -g org.onap.policy.parent \
285                 -a integration \
286                 -v "$parent_latest_released_tag"
287             result_code=$?
288         fi
289     fi
290     if [[ "$result_code" -eq 0 ]]
291     then
292         echo "policy parent reference updated on $repo_location/$target_repo"
293     else
294         echo "policy parent reference update failed on $repo_location/$target_repo"
295         exit 1
296     fi
297 fi
298
299 if [ "$update_common" = true ]
300 then
301     if [ "$update_snapshot" = true ]
302     then
303         echo "updating policy common reference to $common_latest_snapshot_tag on $repo_location/$target_repo . . ."
304         $SED -i \
305             -e "s/<policy.common.version>.*<\/policy.common.version>/<policy.common.version>$common_latest_snapshot_tag<\/policy.common.version>/" \
306             -e "s/<version.policy.common>.*<\/version.policy.common>/<version.policy.common>$common_latest_snapshot_tag<\/version.policy.common>/" \
307             "$repo_location/$target_repo/pom.xml"
308         result_code=$?
309     else
310         echo "updating policy common reference to $common_latest_released_tag on $repo_location/$target_repo . . ."
311         $SED -i \
312             -e "s/<policy.common.version>.*<\/policy.common.version>/<policy.common.version>$common_latest_released_tag<\/policy.common.version>/" \
313             -e "s/<version.policy.common>.*<\/version.policy.common>/<version.policy.common>$common_latest_released_tag<\/version.policy.common>/" \
314             "$repo_location/$target_repo/pom.xml"
315         result_code=$?
316     fi
317     if [[ "$result_code" -eq 0 ]]
318     then
319         echo "policy common reference updated on $repo_location/$target_repo"
320     else
321         echo "policy common reference update failed on $repo_location/$target_repo"
322         exit 1
323     fi
324 fi
325
326 if [ "$update_models" = true ]
327 then
328     if [ "$update_snapshot" = true ]
329     then
330         echo "updating policy models reference to $models_latest_snapshot_tag on $repo_location/$target_repo . . ."
331         $SED -i \
332             -e "s/<policy.models.version>.*<\/policy.models.version>/<policy.models.version>$models_latest_snapshot_tag<\/policy.models.version>/" \
333             -e "s/<version.policy.models>.*<\/version.policy.models>/<version.policy.models>$models_latest_snapshot_tag<\/version.policy.models>/" \
334             "$repo_location/$target_repo/pom.xml"
335         result_code=$?
336     else
337         echo "updating policy models reference to $models_latest_released_tag on $repo_location/$target_repo . . ."
338         $SED -i \
339             -e "s/<policy.models.version>.*<\/policy.models.version>/<policy.models.version>$models_latest_released_tag<\/policy.models.version>/" \
340             -e "s/<version.policy.models>.*<\/version.policy.models>/<version.policy.models>$models_latest_released_tag<\/version.policy.models>/" \
341             "$repo_location/$target_repo/pom.xml"
342         result_code=$?
343     fi
344     if [[ "$result_code" -eq 0 ]]
345     then
346         echo "policy models reference updated on $repo_location/$target_repo"
347     else
348         echo "policy models reference update failed on $repo_location/$target_repo"
349         exit 1
350     fi
351 fi
352
353 if [ "$update_drools_pdp" = true ]
354 then
355     if [ "$update_snapshot" = true ]
356     then
357         echo "updating policy drools-pdp reference to $drools_pdp_latest_snapshot_tag on $repo_location/$target_repo . . ."
358         $SED -i \
359             -e "s/<policy.drools-pdp.version>.*<\/policy.drools-pdp.version>/<policy.drools-pdp.version>$drools_pdp_latest_snapshot_tag<\/policy.drools-pdp.version>/" \
360             -e "s/<version.policy.drools-pdp>.*<\/version.policy.drools-pdp>/<version.policy.drools-pdp>$drools_pdp_latest_snapshot_tag<\/version.policy.drools-pdp>/" \
361             "$repo_location/$target_repo/pom.xml"
362         result_code=$?
363     else
364         echo "updating policy drools-pdp reference to $drools_pdp_latest_released_tag on $repo_location/$target_repo . . ."
365         $SED -i \
366             -e "s/<policy.drools-pdp.version>.*<\/policy.drools-pdp.version>/<policy.drools-pdp.version>$drools_pdp_latest_released_tag<\/policy.drools-pdp.version>/" \
367             -e "s/<version.policy.drools-pdp>.*<\/version.policy.drools-pdp>/<version.policy.drools-pdp>$drools_pdp_latest_released_tag<\/version.policy.drools-pdp>/" \
368             "$repo_location/$target_repo/pom.xml"
369         result_code=$?
370     fi
371     if [[ "$result_code" -eq 0 ]]
372     then
373         echo "policy drools-pdp reference updated on $repo_location/$target_repo"
374     else
375         echo "policy drools-pdp reference update failed on $repo_location/$target_repo"
376         exit 1
377     fi
378 fi
379
380 if [ "$update_apex_pdp" = true ]
381 then
382     if [ "$update_snapshot" = true ]
383     then
384         echo "updating policy apex-pdp reference to $apex_pdp_latest_snapshot_tag on $repo_location/$target_repo . . ."
385         $SED -i \
386             -e "s/<policy.apex-pdp.version>.*<\/policy.apex-pdp.version>/<policy.apex-pdp.version>$apex_pdp_latest_snapshot_tag<\/policy.apex-pdp.version>/" \
387             -e "s/<version.policy.apex-pdp>.*<\/version.policy.apex-pdp>/<version.policy.apex-pdp>$apex_pdp_latest_snapshot_tag<\/version.policy.apex-pdp>/" \
388             "$repo_location/$target_repo/pom.xml"
389         result_code=$?
390     else
391         echo "updating policy apex-pdp reference to $apex_pdp_latest_released_tag on $repo_location/$target_repo . . ."
392         $SED -i \
393             -e "s/<policy.apex-pdp.version>.*<\/policy.apex-pdp.version>/<policy.apex-pdp.version>$apex_pdp_latest_released_tag<\/policy.apex-pdp.version>/" \
394             -e "s/<version.policy.apex-pdp>.*<\/version.policy.apex-pdp>/<version.policy.apex-pdp>$apex_pdp_latest_released_tag<\/version.policy.apex-pdp>/" \
395             "$repo_location/$target_repo/pom.xml"
396         result_code=$?
397     fi
398     if [[ "$result_code" -eq 0 ]]
399     then
400         echo "policy apex-pdp reference updated on $repo_location/$target_repo"
401     else
402         echo "policy apex-pdp reference update failed on $repo_location/$target_repo"
403         exit 1
404     fi
405 fi
406
407 if [ "$update_docker" = true ] && [ "$target_docker_images" != "" ]
408 then
409     if [ "$update_snapshot" == true ]
410     then
411         echo "updating docker base images to version $docker_latest_snapshot_tag on repo $repo_location/$target_repo . . ."
412         find "$repo_location/$target_repo" \
413             -name '*Docker*' \
414             -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/" {} \;
415         result_code=$?
416     else
417         echo "updating docker base images to version $docker_latest_released_tag on repo $repo_location/$target_repo . . ."
418         find "$repo_location/$target_repo" \
419             -name '*Docker*' \
420             -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/" {} \;
421         result_code=$?
422     fi
423
424     if [[ "$result_code" -eq 0 ]]
425     then
426         echo "docker base images updated on $repo_location/$target_repo"
427     else
428         echo "docker base images update failed on $repo_location/$target_repo"
429         exit 1
430     fi
431 fi
432
433 if [ "$update_file" = true ]
434 then
435     if [ ! "$target_repo" = "policy/parent" ]
436     then
437         echo "update of data file can only be done on the policy/parent repo"
438         exit 1
439     fi
440
441     release_data_file_name="$release_data_file_tag$release_data_file"
442
443     echo \
444         "updating release data at" \
445         "$repo_location/$target_repo/integration/src/main/resources/release/$release_data_file_name"
446     cp \
447         "$release_data_file" \
448         "$repo_location/$target_repo/integration/src/main/resources/release/$release_data_file_name"
449     echo \
450         "updated release data at" \
451         "$repo_location/$target_repo/integration/src/main/resources/release/$release_data_file_name"
452 fi