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; |
| 90 | INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfInstantiateBB", "VNF", "ACTIVATE"); |
| 91 | INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfDeleteBB", "VNF", "DEACTIVATE"); |
| 92 | |
| 93 | DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Create"; |
| 94 | |
| 95 | 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"; |
| 96 | |
| 97 | 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"; |
| 98 | |
| 99 | 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"; |
| 100 | |
| 101 | DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Delete"; |
| 102 | |
| 103 | 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"; |
| 104 | |
| 105 | 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"; |
| 106 | |
| 107 | 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'; |
| 108 | |
| 109 | COMMIT; |
| 110 | EOF |
| 111 | |
| 112 | if [ $? -ne 0 ]; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 113 | echo "$SCRIPT_NAME $(current_timestamp): Failed to execute workaround . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 114 | exit 1 |
| 115 | fi |
| 116 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 117 | echo "$SCRIPT_NAME $(current_timestamp): Finished applying workaround . . ." |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 118 | } |
| 119 | |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 120 | isTimeOut() |
| 121 | { |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 122 | if [ `date +%s` -gt $TIME_OUT_END_TIME_IN_SECONDS ]; then |
| 123 | echo "$SCRIPT_NAME $(current_timestamp): workaround script timed out . . ." |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 124 | exit 1; |
| 125 | fi |
| 126 | } |
| 127 | |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 128 | # main body |
waqas.ikram | bfdec07 | 2019-07-19 16:41:37 +0000 | [diff] [blame] | 129 | if [ -z "$TIME_OUT_IN_SECONDS"]; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 130 | 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] | 131 | TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC |
| 132 | fi |
| 133 | |
| 134 | DIGITS_REGEX='^[0-9]+$' |
| 135 | if ! [[ $TIME_OUT_IN_SECONDS =~ $DIGIT_REGEX ]] ; then |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 136 | 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] | 137 | TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC |
| 138 | fi |
| 139 | |
waqas.ikram | 5fee448 | 2019-07-22 13:56:13 +0000 | [diff] [blame] | 140 | START_TIME_IN_SECONDS=`date +%s` |
| 141 | TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_IN_SECONDS)); |
| 142 | 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] | 143 | |
waqas.ikram | 7d75329 | 2019-07-18 16:41:17 +0000 | [diff] [blame] | 144 | apply_workaround |
| 145 | |