CSIT Support for ServiceLevel PNF Software Upgrade
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / config / apply-workarounds.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 Nordix Foundation.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21
22 # @author Waqas Ikram (waqas.ikram@est.tech)
23
24 WORKFLOW_TABLE_NAME="workflow"
25 TABLE_EXISTS_QUERY="select count(*) from information_schema.tables WHERE table_schema='$CATALOG_DB' AND table_name='$WORKFLOW_TABLE_NAME';"
26 SLEEP_TIME=5
27 FLY_WAY_MIGRATION_QUERY="SELECT COUNT(*) FROM flyway_schema_history WHERE script LIKE '%R__MacroData%' AND installed_on IS NOT NULL;"
28 TIME_OUT_DEFAULT_VALUE_SEC=1200 #20 mins
29 SCRIPT_NAME=$(basename $0)
30
31 current_timestamp()
32 {
33  date +"%Y-%m-%d %H:%M:%S"
34 }
35
36 wait_for_database_availability()
37 {
38  echo "$SCRIPT_NAME $(current_timestamp): Checking for database availability"
39  until echo '\q' | mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB; do
40      >&2 echo "$SCRIPT_NAME $(current_timestamp): Database is unavailable - sleeping for ${SLEEP_TIME} seconds"
41      isTimeOut
42      sleep ${SLEEP_TIME}
43  done
44
45  echo "$SCRIPT_NAME $(current_timestamp): Database is available now"
46 }
47
48 wait_container_to_create_table()
49 {
50  while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$TABLE_EXISTS_QUERY") -eq "0" ] ; do
51      echo "$SCRIPT_NAME $(current_timestamp): Waiting for so-catalog container to create tables - sleeping for ${SLEEP_TIME} seconds"
52      isTimeOut
53      sleep ${SLEEP_TIME}
54  done
55  sleep 5s
56  echo "$SCRIPT_NAME $(current_timestamp): $CATALOG_DB tables available now . . ."
57 }
58
59 apply_workaround()
60 {
61  echo "$SCRIPT_NAME $(current_timestamp): Applying workaround . . ."
62
63  wait_for_database_availability
64  wait_container_to_create_table
65  echo "$SCRIPT_NAME $(current_timestamp): Will insert data into $CATALOG_DB"
66  mysql -h $DB_HOST -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB << EOF
67  BEGIN;
68   
69   insert into $WORKFLOW_TABLE_NAME(artifact_uuid, artifact_name, name, operation_name, version, description, body, resource_target, source) values
70   ('4752c287-c5a8-40a6-8fce-077e1d54104b','GenericPnfSoftwareUpgrade','GenericPnfSoftwareUpgrade','GenericPnfSoftwareUpgrade',1.0,'Pnf Workflow to upgrade software',null,'pnf','native');
71
72   insert into $WORKFLOW_TABLE_NAME(artifact_uuid, artifact_name, name, operation_name, version, description, body, resource_target, source) values
73   ('02bffbd9-6af0-4f8d-bf9b-d1dfccd28c84','GenericPnfSWUPDownload','GenericPnfSWUPDownload','GenericPnfSWUPDownload',1.0,'Pnf Workflow to download software',null,'pnf','native');
74
75   insert into $WORKFLOW_TABLE_NAME(artifact_uuid, artifact_name, name, operation_name, version, description, body, resource_target, source) values
76   ('03fcdjf2-6af0-4f8d-bf9b-s3frzca23c19','ServiceLevelUpgrade','ServiceLevelUpgrade','ServiceLevelUpgrade',1.0,'ServiceLevel Upgrade Workflow to upgrade software',null,'service','native');
77
78  COMMIT;
79 EOF
80
81  if [ $? -ne 0 ]; then
82     echo "$SCRIPT_NAME $(current_timestamp): Failed to execute workaround . . ."
83     exit 1
84  fi
85
86  echo "$SCRIPT_NAME $(current_timestamp): Finished applying workaround . . ."
87 }
88
89 isTimeOut()
90 {
91  if [ `date +%s` -gt $TIME_OUT_END_TIME_IN_SECONDS ]; then
92     echo "$SCRIPT_NAME $(current_timestamp): workaround script timed out . . ."
93     exit 1;
94  fi
95 }
96
97 # main body
98 if [ -z "$TIME_OUT_IN_SECONDS"]; then
99     echo "$SCRIPT_NAME $(current_timestamp): TIME_OUT_IN_SECONDS attribute is empty will use default val: $TIME_OUT_DEFAULT_VALUE_SEC"
100     TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
101 fi
102
103 DIGITS_REGEX='^[0-9]+$'
104 if ! [[ $TIME_OUT_IN_SECONDS =~ $DIGIT_REGEX ]] ; then
105     echo "$SCRIPT_NAME $(current_timestamp): TIME_OUT_IN_SECONDS attribute Must be number: $TIME_OUT_IN_SECONDS, will use default val: $TIME_OUT_DEFAULT_VALUE_SEC"
106     TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
107 fi
108
109 START_TIME_IN_SECONDS=`date +%s`
110 TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_IN_SECONDS));
111 echo "$SCRIPT_NAME $(current_timestamp): Workaround script will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`"
112
113 apply_workaround
114