ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 1 | #!/bin/bash -x |
| 2 | ### |
| 3 | # ============LICENSE_START======================================================= |
| 4 | # ONAP CLAMP |
| 5 | # ================================================================================ |
| 6 | # Copyright (C) 2018 AT&T Intellectual Property. All rights |
| 7 | # reserved. |
| 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 | ### |
| 24 | KIBANA_CONF_FILE="/usr/share/kibana/config/kibana.yml" |
| 25 | SAVED_OBJECTS_ROOT="/saved-objects/" |
| 26 | RESTORE_CMD="/usr/local/bin/restore.py -H http://127.0.0.1:5601/ -f" |
| 27 | BACKUP_BIN="/usr/local/bin/backup.py" |
| 28 | KIBANA_START_CMD="/usr/local/bin/kibana-docker" |
| 29 | LOG_FILE="/tmp/load.kibana.log" |
| 30 | KIBANA_LOAD_CMD="/usr/local/bin/kibana-docker -H 127.0.0.1 -l $LOG_FILE" |
| 31 | TIMEOUT=60 |
| 32 | WAIT_TIME=2 |
ac2550 | 5a23e6a | 2018-10-11 09:27:15 +0200 | [diff] [blame^] | 33 | LOADED_FLAG=$SAVED_OBJECTS_ROOT/.loaded |
ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 34 | |
ac2550 | 5a23e6a | 2018-10-11 09:27:15 +0200 | [diff] [blame^] | 35 | if [ -f $LOADED_FLAG ]; |
ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 36 | then |
ac2550 | 5a23e6a | 2018-10-11 09:27:15 +0200 | [diff] [blame^] | 37 | echo "---- Kibana saved objects already restored. Remove $LOADED_FLAG if you want to restore them again." |
| 38 | elif [ -n "$(ls -A ${SAVED_OBJECTS_PATH})" ]; |
| 39 | then |
| 40 | echo "---- Waiting for elasticsearch to be up..." |
| 41 | RES=-1 |
| 42 | PING_TIMEOUT=60 |
| 43 | elastic_url=$(grep elasticsearch.url /usr/share/kibana/config/kibana.yml | cut -d\ -f2) |
| 44 | while [ ! "$RES" -eq "0" ] && [ "$PING_TIMEOUT" -gt "0" ]; |
| 45 | do |
| 46 | curl $elastic_url |
| 47 | RES=$? |
| 48 | sleep $WAIT_TIME |
| 49 | let PING_TIMEOUT=$PING_TIMEOUT-$WAIT_TIME |
| 50 | done |
| 51 | |
ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 52 | echo "---- Saved objects found, restoring files." |
| 53 | |
| 54 | $KIBANA_LOAD_CMD & |
| 55 | KIB_PID=$! |
| 56 | |
| 57 | # Wait for log file to be avaiable |
| 58 | LOG_TIMEOUT=60 |
| 59 | while [ ! -f $LOG_FILE ] && [ "$LOG_TIMEOUT" -gt "0" ]; |
| 60 | do |
| 61 | echo "Waiting for $LOG_FILE to be available..." |
| 62 | sleep $WAIT_TIME |
| 63 | let LOG_TIMEOUT=$LOG_TIMEOUT-$WAIT_TIME |
| 64 | done |
| 65 | |
| 66 | tail -f $LOG_FILE & |
| 67 | LOG_PID=$! |
| 68 | |
| 69 | # Wait for kibana to be listening |
| 70 | while [ -z "$(grep "Server running at" $LOG_FILE)" ] && [ "$TIMEOUT" -gt "0" ]; |
| 71 | do |
| 72 | echo "Waiting for kibana to start..." |
| 73 | sleep $WAIT_TIME |
| 74 | let TIMEOUT=$TIMEOUT-$WAIT_TIME |
| 75 | done |
| 76 | sleep 1 |
| 77 | |
| 78 | # restore files |
| 79 | for saved_objects_path in $SAVED_OBJECTS_ROOT/* |
| 80 | do |
ac2550 | 5a23e6a | 2018-10-11 09:27:15 +0200 | [diff] [blame^] | 81 | # skip files as we only need directories |
| 82 | [ -f $saved_objects_path ] && continue |
| 83 | |
ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 84 | echo "Restoring content of $saved_objects_path" |
| 85 | $RESTORE_CMD -C $saved_objects_path |
| 86 | sleep 1 |
| 87 | done |
ac2550 | 5a23e6a | 2018-10-11 09:27:15 +0200 | [diff] [blame^] | 88 | |
| 89 | touch $LOADED_FLAG |
| 90 | if [ "$?" != "0" ]; |
| 91 | then |
| 92 | echo "WARNING: Could not save $LOADED_FLAG, saved objects will be restored on next startup." >&2 |
| 93 | fi |
ac2550 | ead1051 | 2018-10-05 13:50:23 +0200 | [diff] [blame] | 94 | |
| 95 | # cleanup |
| 96 | kill $KIB_PID |
| 97 | kill $LOG_PID |
| 98 | else |
| 99 | echo "---- No saved object found" |
| 100 | ls -A ${SAVED_OBJECTS_PATH} |
| 101 | fi |
| 102 | |
| 103 | echo "---- Starting kibana" |
| 104 | |
| 105 | $KIBANA_START_CMD |
| 106 | |