-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
128 lines (103 loc) · 4.35 KB
/
start.sh
File metadata and controls
128 lines (103 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
#
# /usr/local/bin/start.sh
# Start Elasticsearch services
#
# spujadas 2015-10-09; added initial pidfile removal and graceful termination
# WARNING - This script assumes that the ELK services are not running, and is
# only expected to be run once, when the container is started.
# Do not attempt to run this script if the ELK services are running (or be
# prepared to reap zombie processes).
## handle termination gracefully
_term() {
echo "Terminating ES"
service elasticsearch stop
exit 0
}
trap _term SIGTERM SIGINT
## remove pidfiles in case previous graceful termination failed
# NOTE - This is the reason for the WARNING at the top - it's a bit hackish,
# but if it's good enough for Fedora (https://goo.gl/88eyXJ), it's good
# enough for me :)
rm -f /var/run/elasticsearch/elasticsearch.pid
## initialise list of log files to stream in console (initially empty)
OUTPUT_LOGFILES=""
## start services as needed
### crond
service cron start
### Elasticsearch
if [ -z "$ELASTICSEARCH_START" ]; then
ELASTICSEARCH_START=1
fi
if [ "$ELASTICSEARCH_START" -ne "1" ]; then
echo "ELASTICSEARCH_START is set to something different from 1, not starting..."
else
# update permissions of ES data directory
chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
# override ES_HEAP_SIZE variable if set
if [ ! -z "$ES_HEAP_SIZE" ]; then
awk -v LINE="-Xmx$ES_HEAP_SIZE" '{ sub(/^.Xmx.*/, LINE); print; }' ${ES_PATH_CONF}/jvm.options \
> ${ES_PATH_CONF}/jvm.options.new && mv ${ES_PATH_CONF}/jvm.options.new ${ES_PATH_CONF}/jvm.options
awk -v LINE="-Xms$ES_HEAP_SIZE" '{ sub(/^.Xms.*/, LINE); print; }' ${ES_PATH_CONF}/jvm.options \
> ${ES_PATH_CONF}/jvm.options.new && mv ${ES_PATH_CONF}/jvm.options.new ${ES_PATH_CONF}/jvm.options
fi
# override ES_JAVA_OPTS variable if set
if [ ! -z "$ES_JAVA_OPTS" ]; then
awk -v LINE="ES_JAVA_OPTS=\"$ES_JAVA_OPTS\"" '{ sub(/^#?ES_JAVA_OPTS=.*/, LINE); print; }' /etc/default/elasticsearch \
> /etc/default/elasticsearch.new && mv /etc/default/elasticsearch.new /etc/default/elasticsearch
fi
# override MAX_OPEN_FILES variable if set
if [ ! -z "$MAX_OPEN_FILES" ]; then
awk -v LINE="MAX_OPEN_FILES=$MAX_OPEN_FILES" '{ sub(/^#?MAX_OPEN_FILES=.*/, LINE); print; }' /etc/init.d/elasticsearch \
> /etc/init.d/elasticsearch.new && mv /etc/init.d/elasticsearch.new /etc/init.d/elasticsearch \
&& chmod +x /etc/init.d/elasticsearch
fi
# override MAX_MAP_COUNT variable if set
if [ ! -z "$MAX_MAP_COUNT" ]; then
awk -v LINE="MAX_MAP_COUNT=$MAX_MAP_COUNT" '{ sub(/^#?MAX_MAP_COUNT=.*/, LINE); print; }' /etc/init.d/elasticsearch \
> /etc/init.d/elasticsearch.new && mv /etc/init.d/elasticsearch.new /etc/init.d/elasticsearch \
&& chmod +x /etc/init.d/elasticsearch
fi
service elasticsearch start
# wait for Elasticsearch to start up before either starting Kibana (if enabled)
# or attempting to stream its log file
# - https://github.com/elasticsearch/kibana/issues/3077
# set number of retries (default: 30, override using ES_CONNECT_RETRY env var)
re_is_numeric='^[0-9]+$'
if ! [[ $ES_CONNECT_RETRY =~ $re_is_numeric ]] ; then
ES_CONNECT_RETRY=30
fi
if [ -z "$ELASTICSEARCH_URL" ]; then
ELASTICSEARCH_URL=${ES_PROTOCOL:-http}://localhost:9201
fi
counter=0
while [ ! "$(curl -k ${ELASTICSEARCH_URL} 2> /dev/null)" -a $counter -lt $ES_CONNECT_RETRY ]; do
sleep 1
((counter++))
echo "waiting for Elasticsearch to be up ($counter/$ES_CONNECT_RETRY)"
done
if [ ! "$(curl -k ${ELASTICSEARCH_URL} 2> /dev/null)" ]; then
echo "Couln't start Elasticsearch. Exiting."
echo "Elasticsearch log follows below."
cat /var/log/elasticsearch/elasticsearch.log
exit 1
fi
# wait for cluster to respond before getting its name
counter=0
while [ -z "$CLUSTER_NAME" -a $counter -lt 30 ]; do
sleep 1
((counter++))
CLUSTER_NAME=$(curl -k ${ELASTICSEARCH_URL}/_cat/health?h=cluster 2> /dev/null | tr -d '[:space:]')
echo "Waiting for Elasticsearch cluster to respond ($counter/30)"
done
if [ -z "$CLUSTER_NAME" ]; then
echo "Couln't get name of cluster. Exiting."
echo "Elasticsearch log follows below."
cat /var/log/elasticsearch/elasticsearch.log
exit 1
fi
OUTPUT_LOGFILES+="/var/log/elasticsearch/${CLUSTER_NAME}.log "
fi
touch $OUTPUT_LOGFILES
tail -f $OUTPUT_LOGFILES &
wait