1+ #! /bin/bash -e
2+
3+ # This script is using the given ELASTICSEARCH_HOSTS to populate required parameters
4+ # to setup a Multi-Node Elasticsearch cluster.
5+ # If parameters (network.publish_host, http.port, transport.port) are already
6+ # given externally they are used with precedence.
7+
8+ # Get the number of the Elasticsearch node based on the node.name (e.g. elasticsearch2)
9+ nodeBasename=` echo node.name | awk ' match(ENVIRON[$0], /([a-zA-Z]*)([0-9]{1,})/, v) { print v[1] }' `
10+ nodeNumber=` echo node.name | awk ' match(ENVIRON[$0], /([a-zA-Z]*)([0-9]{1,})/, v) { print v[2] }' `
11+
12+ count=1
13+ params=" "
14+ seedHosts=" "
15+ initialMasterNodes=" "
16+ for host in ${ELASTICSEARCH_HOSTS// ,/ }
17+ do
18+ # Use all nodes as initial master node, when initializing the cluster
19+ # It is assumed, that node-names are sequentially counted elasticsearch1, elasticsearch2, ...
20+ if [ " ${initCluster} " = " true" ]; then
21+ if [ " ${initialMasterNodes} " == " " ]; then
22+ initialMasterNodes=" -E cluster.initial_master_nodes=${nodeBasename}${count} "
23+ else
24+ initialMasterNodes=" ${initialMasterNodes} ,${nodeBasename}${count} "
25+ fi
26+ fi
27+ # Use all declared hosts as seed hosts if
28+ # Seeds hosts are not given externally and the standard transport ports are used
29+ discoverySeedHosts=` echo discovery.seed_hosts | awk ' {print ENVIRON[$0]}' `
30+ discoveryTransportPort=` echo transport.port | awk ' {print ENVIRON[$0]}' `
31+ if [ -z " ${discoverySeedHosts} " -a -z " ${discoveryTransportPort} " ]
32+ then
33+ # Get the hostname and transport from the URL
34+ discoverPublishHostname=` echo $host | awk ' match($0, /https?:\/\/(.*)\:(\d*)/, v) { print v[1] }' `
35+ discoveryTransportPort=` echo $host | awk ' match($0,/https?:\/\/(.*)\:[0-9]{2}([0-9]{2})/, v) { print 93v[2] }' `
36+ if [ " ${seedHosts} " == " " ]; then
37+ seedHosts=" -E discovery.seed_hosts=${discoverPublishHostname} :${discoveryTransportPort} "
38+ else
39+ seedHosts=" ${seedHosts} ,${discoverPublishHostname} :${discoveryTransportPort} "
40+ fi
41+ fi
42+
43+ if [ " ${count} " == " ${nodeNumber} " ]; then
44+ echo " Setting up Elasticsearch node: ${nodeNumber} "
45+ publishHost=` echo network.publish_host | awk ' {print ENVIRON[$0]}' `
46+ if [ -z " ${publishHost} " ]; then
47+ publishHost=` echo $host | awk ' match($0, /https?:\/\/(.*)\:(\d*)/, v) { print v[1] }' `
48+ params=" $params -E network.publish_host=${publishHost} "
49+ echo " Set network.publish_host=${publishHost} based on given host: ${host} "
50+ else
51+ echo " network.publish_host=${publishHost} taken from envionment variable"
52+ fi
53+
54+ httpPort=` echo http.port | awk ' {print ENVIRON[$0]}' `
55+ if [ -z " ${httpPort} " ]
56+ then
57+ httpPort=` echo $host | awk ' match($0,/https?:\/\/(.*)\:([0-9]*)/, v) { print v[2] }' `
58+ params=" $params -E http.port=${httpPort} "
59+ echo " Set http.port=${httpPort} based on given host: ${host} "
60+ else
61+ echo " http.port=${httpPort} taken from envionment variable"
62+ fi
63+
64+ transportPort=` echo transport.port | awk ' {print ENVIRON[$0]}' `
65+ if [ -z " ${transportPort} " ]
66+ then
67+ transportPort=` echo $host | awk ' match($0,/https?:\/\/(.*)\:[0-9]{2}([0-9]{2})/, v) { print 93v[2] }' `
68+ params=" $params -E transport.port=${transportPort} "
69+ echo " Set transport.port=${transportPort} based on given host: ${host} "
70+ else
71+ echo " transport.port=${transportPort} taken from envionment variable"
72+ fi
73+ fi
74+ count=` expr $count + 1`
75+ done
76+
77+ # Finally call the original Docker-Entrypoint
78+ /usr/local/bin/docker-entrypoint.sh elasticsearch ${params} ${seedHosts} ${initialMasterNodes}
0 commit comments