Skip to content

Commit 50c81e2

Browse files
committed
Added code for fwdays conference
1 parent 17c431b commit 50c81e2

5 files changed

Lines changed: 535 additions & 503 deletions

File tree

bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
docker-compose.yml

bin/start-local.sh

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
#!/bin/sh
2+
# Run Elasticsearch and Kibana on Linux for local testing
3+
# Note: do not use this script in a production environment
4+
5+
set -eu
6+
7+
echo
8+
echo ' ______ _ _ _ '
9+
echo ' | ____| | | | (_) '
10+
echo ' | |__ | | __ _ ___| |_ _ ___ '
11+
echo ' | __| | |/ _` / __| __| |/ __|'
12+
echo ' | |____| | (_| \__ \ |_| | (__ '
13+
echo ' |______|_|\__,_|___/\__|_|\___|'
14+
echo '--------------------------------------------------------'
15+
echo 'Run Elasticsearch and Kibana for local testing'
16+
echo 'Note: do not use this script in a production environment'
17+
echo '--------------------------------------------------------'
18+
19+
# Trap ctrl-c
20+
trap ctrl_c INT
21+
22+
ctrl_c() { cleanup; }
23+
24+
# Check if a command exists
25+
available() { command -v $1 >/dev/null; }
26+
27+
# Revert the status (remove generated files)
28+
cleanup() { rm docker-compose.yml .env >/dev/null 2>&1; }
29+
30+
# Generates a random password with letters and numbers
31+
# You can pass the size of the password as first parameter (default is 8 characters)
32+
random_password() {
33+
local LENGTH="${1:-8}"
34+
echo $(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c ${LENGTH})
35+
}
36+
37+
# Returns the latest Elasticsearch tag version
38+
get_latest_version() {
39+
local version=$(curl -s "https://api.github.com/repos/elastic/elasticsearch/tags" | grep -m 1 '"name"' | grep -Eo '[0-9.]+')
40+
echo $version
41+
}
42+
43+
# Check the requirements
44+
if ! available "docker"; then
45+
echo "Error: docker command is required"
46+
echo "You can install it from https://docs.docker.com/engine/install/."
47+
exit 1
48+
fi
49+
if ! available "curl"; then
50+
echo "Error: curl command is required"
51+
echo "You can install it from https://curl.se/download.html."
52+
exit 1
53+
fi
54+
if ! available "grep"; then
55+
echo "Error: grep command is required"
56+
echo "You can install it from https://www.gnu.org/software/grep/."
57+
exit 1
58+
fi
59+
# Check for "docker compose"
60+
set +e
61+
docker compose >/dev/null 2>&1
62+
if [ $? -ne 0 ]; then
63+
echo "Error: docker compose is required"
64+
echo "You can install it from https://docs.docker.com/compose/install/"
65+
exit 1
66+
fi
67+
set -e
68+
69+
if [ -f "docker-compose.yml" ]; then
70+
echo "Error: a docker-compose.yml already exists. Please move to another folder or remove the file."
71+
exit 1
72+
fi
73+
if [ -f ".env" ]; then
74+
echo "Error: a .env file already exists. Please move to another folder or remove the file."
75+
exit 1
76+
fi
77+
78+
# Generate random passwords
79+
es_password=$(random_password)
80+
kibana_password=$(random_password)
81+
es_version=$(get_latest_version)
82+
kibana_encryption_key=$(random_password 32)
83+
84+
# Create the .env file
85+
cat > .env <<- EOM
86+
ES_LOCAL_VERSION="$es_version"
87+
ES_LOCAL_CONTAINER_NAME="es-local-dev"
88+
ES_LOCAL_DOCKER_NETWORK="elastic-net"
89+
ES_LOCAL_PASSWORD="$es_password"
90+
ES_LOCAL_PORT="9200"
91+
ES_HEAP_INIT="128m"
92+
ES_HEAP_MAX="2g"
93+
KIBANA_LOCAL_CONTAINER_NAME="kibana-local-dev"
94+
KIBANA_LOCAL_PORT="5601"
95+
KIBANA_LOCAL_PASSWORD="$kibana_password"
96+
KIBANA_ENCRYPTION_KEY="$kibana_encryption_key"
97+
EOM
98+
99+
# Equivalent of source command in sh
100+
set +e
101+
. ./.env
102+
if [ $? -ne 0 ]; then
103+
echo "Error: the .env file is not valid"
104+
cleanup
105+
exit 1
106+
fi
107+
set -e
108+
109+
echo "Set up of Elasticsearch and Kibana v${ES_LOCAL_VERSION}..."
110+
echo "- Generated random passwords"
111+
echo "- Created a .env file with the settings"
112+
113+
# Create the docker-compose.yml file
114+
cat >docker-compose.yml <<'EOL'
115+
services:
116+
es01:
117+
image: docker.elastic.co/elasticsearch/elasticsearch:${ES_LOCAL_VERSION}
118+
container_name: ${ES_LOCAL_CONTAINER_NAME}
119+
volumes:
120+
- dev-es01:/usr/share/elasticsearch/data
121+
ports:
122+
- 127.0.0.1:${ES_LOCAL_PORT}:9200
123+
environment:
124+
- discovery.type=single-node
125+
- ELASTIC_PASSWORD=${ES_LOCAL_PASSWORD}
126+
- xpack.security.enabled=true
127+
- xpack.security.http.ssl.enabled=false
128+
- xpack.license.self_generated.type=trial
129+
- xpack.ml.use_auto_machine_memory_percent=true
130+
- ES_JAVA_OPTS=-Xms${ES_HEAP_INIT} -Xmx${ES_HEAP_MAX}
131+
ulimits:
132+
memlock:
133+
soft: -1
134+
hard: -1
135+
healthcheck:
136+
test:
137+
[
138+
"CMD-SHELL",
139+
"curl --output /dev/null --silent --head --fail -u elastic:${ES_LOCAL_PASSWORD} http://es01:${ES_LOCAL_PORT}",
140+
]
141+
interval: 5s
142+
timeout: 5s
143+
retries: 10
144+
145+
kibana_password:
146+
depends_on:
147+
es01:
148+
condition: service_healthy
149+
image: docker.elastic.co/elasticsearch/elasticsearch:${ES_LOCAL_VERSION}
150+
restart: no
151+
command: >
152+
bash -c '
153+
echo "Setup the kibana_system password";
154+
curl -u "elastic:${ES_LOCAL_PASSWORD}" -X POST http://es01:${ES_LOCAL_PORT}/_security/user/kibana_system/_password -d "{\"password\":\"'${KIBANA_LOCAL_PASSWORD}'\"}" -H "Content-Type: application/json";
155+
'
156+
157+
kibana:
158+
depends_on:
159+
kibana_password:
160+
condition: service_completed_successfully
161+
image: docker.elastic.co/kibana/kibana:${ES_LOCAL_VERSION}
162+
container_name: ${KIBANA_LOCAL_CONTAINER_NAME}
163+
volumes:
164+
- dev-kibana:/usr/share/kibana/data
165+
ports:
166+
- 127.0.0.1:${KIBANA_LOCAL_PORT}:5601
167+
environment:
168+
- SERVERNAME=kibana
169+
- ELASTICSEARCH_HOSTS=http://es01:9200
170+
- ELASTICSEARCH_USERNAME=kibana_system
171+
- ELASTICSEARCH_PASSWORD=${KIBANA_LOCAL_PASSWORD}
172+
- XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=${KIBANA_ENCRYPTION_KEY}
173+
healthcheck:
174+
test:
175+
[
176+
"CMD-SHELL",
177+
"curl -s -I http://kibana:5601 | grep -q 'HTTP/1.1 302 Found'",
178+
]
179+
interval: 10s
180+
timeout: 10s
181+
retries: 120
182+
183+
volumes:
184+
dev-es01:
185+
dev-kibana:
186+
EOL
187+
188+
echo "- Created a docker-compose.yml file"
189+
190+
# Execute docker compose
191+
echo "- Running docker compose up --wait"
192+
set +e
193+
docker compose up --wait
194+
if [ $? -ne 0 ]; then
195+
echo "Error: the 'docker compose up --wait' command failed!"
196+
cleanup
197+
exit 1
198+
fi
199+
set -e
200+
201+
# Success
202+
echo "Congrats, Elasticsearch and Kibana successfully installed!"
203+
echo
204+
echo "Open the browser at http://localhost:${KIBANA_LOCAL_PORT}"
205+
echo "Use 'elastic' as username and '${ES_LOCAL_PASSWORD}' as password."
206+
echo "To connect to Elasticsearch use the URL: http://elastic:${ES_LOCAL_PASSWORD}@localhost:${ES_LOCAL_PORT}"
207+
echo
208+
echo "To stop the service: docker compose stop"
209+
echo "To run it again: docker compose up --wait"

0 commit comments

Comments
 (0)