blob: a232706e0d64e7c3f818cf1ade237e5a9843c704 [file] [log] [blame]
ac2550ead10512018-10-05 13:50:23 +02001#!/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###
24KIBANA_CONF_FILE="/usr/share/kibana/config/kibana.yml"
25SAVED_OBJECTS_ROOT="/saved-objects/"
26RESTORE_CMD="/usr/local/bin/restore.py -H http://127.0.0.1:5601/ -f"
27BACKUP_BIN="/usr/local/bin/backup.py"
28KIBANA_START_CMD="/usr/local/bin/kibana-docker"
29LOG_FILE="/tmp/load.kibana.log"
30KIBANA_LOAD_CMD="/usr/local/bin/kibana-docker -H 127.0.0.1 -l $LOG_FILE"
31TIMEOUT=60
32WAIT_TIME=2
ac25505a23e6a2018-10-11 09:27:15 +020033LOADED_FLAG=$SAVED_OBJECTS_ROOT/.loaded
ac2550ead10512018-10-05 13:50:23 +020034
ac25505a23e6a2018-10-11 09:27:15 +020035if [ -f $LOADED_FLAG ];
ac2550ead10512018-10-05 13:50:23 +020036then
ac25505a23e6a2018-10-11 09:27:15 +020037 echo "---- Kibana saved objects already restored. Remove $LOADED_FLAG if you want to restore them again."
38elif [ -n "$(ls -A ${SAVED_OBJECTS_PATH})" ];
39then
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
ac2550ead10512018-10-05 13:50:23 +020052 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
ac25505a23e6a2018-10-11 09:27:15 +020081 # skip files as we only need directories
82 [ -f $saved_objects_path ] && continue
83
ac2550ead10512018-10-05 13:50:23 +020084 echo "Restoring content of $saved_objects_path"
85 $RESTORE_CMD -C $saved_objects_path
86 sleep 1
87 done
ac25505a23e6a2018-10-11 09:27:15 +020088
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
ac2550ead10512018-10-05 13:50:23 +020094
95 # cleanup
96 kill $KIB_PID
97 kill $LOG_PID
98else
99 echo "---- No saved object found"
100 ls -A ${SAVED_OBJECTS_PATH}
101fi
102
103echo "---- Starting kibana"
104
105$KIBANA_START_CMD
106