-
Notifications
You must be signed in to change notification settings - Fork 115
/
start-elasticsearch-stack.sh
executable file
·55 lines (39 loc) · 1.69 KB
/
start-elasticsearch-stack.sh
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
#!/usr/bin/env bash
set -e
SCRIPT_PATH="$(dirname "${BASH_SOURCE[0]}")"
cd $SCRIPT_PATH
set -a
if [ -f /.env ]; then
. /.env
fi
set +a
ELASTIC_USERNAME=${ELASTIC_USERNAME:="elastic"}
ELASTIC_PASSWORD=${ELASTIC_PASSWORD:="password"}
# Remove elasticsearch stack containers & volumes
docker compose -f ./docker-compose.elasticsearch.yml down -v
# Run docker compose to start elasticsearch container
docker compose -f ./docker-compose.elasticsearch.yml up -d elasticsearch
echo 'Waiting for Elasticsearch...'
sleep 30
# Generate the service token
# Ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/service-accounts.html#service-accounts-tokens
response=$(curl -s -w "\n%{http_code}\n" -X POST -u "${ELASTIC_USERNAME}":"${ELASTIC_PASSWORD}" "http://localhost:9200/_security/service/elastic/kibana/credential/token/my_kibana_token" -H 'Content-Type: application/json')
response_body=$(echo "$response" | head -n1)
status_code=$(echo "$response" | tail -n1)
if [ -z "$status_code" ]; then
echo "Error: Did not receive a status code from the server"
exit 1
fi
if [ "$status_code" -ne 200 ]; then
error_message=$(echo $response_body | jq -r '.error.root_cause')
echo -e "\nError: Failed to generate the service token: \n$error_message"
exit 1
fi
# Extract & export the token from the response
export ELASTICSEARCH_SERVICEACCOUNTTOKEN=$(echo $response_body | jq -r '.token.value')
echo 'Starting for Kibana...'
## Run docker compose to start kibana container
docker compose -f ./docker-compose.elasticsearch.yml up -d kibana
echo 'Starting APM Server...'
## Run docker compose to start apm-server container
docker compose -f ./docker-compose.elasticsearch.yml up -d apm-server