waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 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 | BUIDLING_BLOCK_TABLE_NAME="building_block_detail" |
| 25 | ORCH_FLOW_TABLE_NAME="orchestration_flow_reference" |
| 26 | NORTH_BOUND_TABLE_NAME="northbound_request_ref_lookup" |
| 27 | TABLE_EXISTS_QUERY="select count(*) from information_schema.tables WHERE table_schema='$CATALOG_DB' AND table_name='$BUIDLING_BLOCK_TABLE_NAME';" |
| 28 | BUILDING_BLOCK_COUNT_QUERY="select count(*) from $BUIDLING_BLOCK_TABLE_NAME;" |
| 29 | FLY_WAY_MIGRATION_QUERY="SELECT COUNT(*) FROM flyway_schema_history WHERE script LIKE '%R__MacroData%' AND installed_on IS NOT NULL;" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 30 | SLEEP_TIME=5 |
| 31 | TIME_OUT_DEFAULT_VALUE_SEC=1200 #20 mins |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 32 | SCRIPT_NAME=$(basename $0) |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 33 | |
| 34 | current_timestamp() |
| 35 | { |
| 36 | date +"%Y-%m-%d %H:%M:%S" |
| 37 | } |
| 38 | |
| 39 | wait_for_database_availability() |
| 40 | { |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 41 | echo "$SCRIPT_NAME $(current_timestamp): Checking for database availability" |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 42 | until echo '\q' | mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB; do |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 43 | >&2 echo "$SCRIPT_NAME $(current_timestamp): Database is unavailable - sleeping for ${SLEEP_TIME} seconds" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 44 | isTimeOut |
| 45 | sleep ${SLEEP_TIME} |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 46 | done |
| 47 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 48 | echo "$SCRIPT_NAME $(current_timestamp): Database is available now" |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | wait_container_to_create_table() |
| 52 | { |
| 53 | while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$TABLE_EXISTS_QUERY") -eq "0" ] ; do |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 54 | echo "$SCRIPT_NAME $(current_timestamp): Waiting for so-catalog container to create tables - sleeping for ${SLEEP_TIME} seconds" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 55 | isTimeOut |
| 56 | sleep ${SLEEP_TIME} |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 57 | done |
| 58 | |
| 59 | while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$BUILDING_BLOCK_COUNT_QUERY") -eq "0" ] ; do |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 60 | echo "$SCRIPT_NAME $(current_timestamp): Waiting for so-catalog container to insert records in $BUIDLING_BLOCK_TABLE_NAME - sleeping for ${SLEEP_TIME} seconds" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 61 | isTimeOut |
| 62 | sleep ${SLEEP_TIME} |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 63 | done |
| 64 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 65 | echo "$SCRIPT_NAME $(current_timestamp): $CATALOG_DB tables available now . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | wait_for_flyway_migration_to_finish() |
| 69 | { |
| 70 | while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$FLY_WAY_MIGRATION_QUERY") -ne "1" ] ; do |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 71 | echo "$SCRIPT_NAME $(current_timestamp): Waiting for flyway migration sql statement to finish with success - sleeping for ${SLEEP_TIME} seconds" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 72 | isTimeOut |
| 73 | sleep ${SLEEP_TIME} |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 74 | done |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 75 | echo "$SCRIPT_NAME $(current_timestamp): flyway migration finished . . . " |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | apply_workaround() |
| 80 | { |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 81 | echo "$SCRIPT_NAME $(current_timestamp): Applying workaround . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 82 | |
| 83 | wait_for_database_availability |
| 84 | wait_container_to_create_table |
| 85 | wait_for_flyway_migration_to_finish |
| 86 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 87 | echo "$SCRIPT_NAME $(current_timestamp): Will insert data into $CATALOG_DB" |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 88 | mysql -h $DB_HOST -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB << EOF |
| 89 | BEGIN; |
waqas.ikram | c44a753 | 2019-08-08 15:57:14 +0000 | [diff] [blame] | 90 | |
| 91 | UPDATE $NORTH_BOUND_TABLE_NAME SET SERVICE_TYPE="*"; |
| 92 | |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 93 | INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfInstantiateBB", "VNF", "ACTIVATE"); |
| 94 | INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfDeleteBB", "VNF", "DEACTIVATE"); |
| 95 | |
| 96 | DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Create"; |
| 97 | |
| 98 | INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 1 AS SEQ_NO, "AssignVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance"; |
| 99 | |
| 100 | INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 2 AS SEQ_NO, "EtsiVnfInstantiateBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance"; |
| 101 | |
| 102 | INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 3 AS SEQ_NO, "ActivateVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance"; |
| 103 | |
| 104 | DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Delete"; |
| 105 | |
| 106 | INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Delete" AS COMPOSITE_ACTION, 1 AS SEQ_NO, "EtsiVnfDeleteBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="deleteInstance"; |
| 107 | |
| 108 | INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Delete" AS COMPOSITE_ACTION, 2 AS SEQ_NO, "UnassignVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="deleteInstance"; |
| 109 | |
| 110 | UPDATE orchestration_status_state_transition_directive SET FLOW_DIRECTIVE='CONTINUE' WHERE RESOURCE_TYPE='VNF' AND ORCHESTRATION_STATUS='CREATED' AND TARGET_ACTION='ACTIVATE' AND FLOW_DIRECTIVE='FAIL'; |
| 111 | |
| 112 | COMMIT; |
| 113 | EOF |
| 114 | |
| 115 | if [ $? -ne 0 ]; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 116 | echo "$SCRIPT_NAME $(current_timestamp): Failed to execute workaround . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 117 | exit 1 |
| 118 | fi |
| 119 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 120 | echo "$SCRIPT_NAME $(current_timestamp): Finished applying workaround . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 123 | isTimeOut() |
| 124 | { |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 125 | if [ `date +%s` -gt $TIME_OUT_END_TIME_IN_SECONDS ]; then |
| 126 | echo "$SCRIPT_NAME $(current_timestamp): workaround script timed out . . ." |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 127 | exit 1; |
| 128 | fi |
| 129 | } |
| 130 | |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 131 | # main body |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 132 | if [ -z "$TIME_OUT_IN_SECONDS"]; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 133 | echo "$SCRIPT_NAME $(current_timestamp): TIME_OUT_IN_SECONDS attribute is empty will use default val: $TIME_OUT_DEFAULT_VALUE_SEC" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 134 | TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC |
| 135 | fi |
| 136 | |
| 137 | DIGITS_REGEX='^[0-9]+$' |
| 138 | if ! [[ $TIME_OUT_IN_SECONDS =~ $DIGIT_REGEX ]] ; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 139 | 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" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 140 | TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC |
| 141 | fi |
| 142 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 143 | START_TIME_IN_SECONDS=`date +%s` |
| 144 | TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_IN_SECONDS)); |
| 145 | echo "$SCRIPT_NAME $(current_timestamp): Workaround script will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`" |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 146 | |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 147 | apply_workaround |
| 148 | |