|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# 等待 Elasticsearch 啟動 |
4 |
| -until curl -u "elastic:${ELASTIC_PASSWORD}" -s http://es01:9200 >/dev/null; do |
5 |
| - echo "等待 Elasticsearch 啟動..." |
6 |
| - sleep 5 |
7 |
| -done |
| 3 | +# unset http proxy which maybe set by docker daemon |
| 4 | +export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY="" |
8 | 5 |
|
| 6 | +echo "Elasticsearch built-in user: elastic:${ELASTIC_PASSWORD}" |
9 | 7 |
|
10 |
| -echo "使用者: elastic:${ELASTIC_PASSWORD}" |
| 8 | +# Wait Elasticsearch be healthy |
| 9 | +while true; do |
| 10 | + response=$(curl -s -v -w "\n%{http_code}" -u "elastic:${ELASTIC_PASSWORD}" "http://es01:9200") |
| 11 | + exit_code=$? |
| 12 | + status=$(echo "$response" | tail -n1) |
| 13 | + if [ $exit_code -eq 0 ] && [ "$status" = "200" ]; then |
| 14 | + echo "Elasticsearch is healthy" |
| 15 | + break |
| 16 | + else |
| 17 | + echo "Elasticsearch is unhealthy: $exit_code $status" |
| 18 | + echo "$response" |
| 19 | + sleep 5 |
| 20 | + fi |
| 21 | +done |
11 | 22 |
|
| 23 | +# Create new role with all privileges to all indices |
| 24 | +# https://www.elastic.co/guide/en/elasticsearch/reference/current/security-privileges.html#privileges-list-indices |
| 25 | +echo "Going to create Elasticsearch role own_indices with all privileges to all indices" |
| 26 | +while true; do |
| 27 | + response=$(curl -s -v -w "\n%{http_code}" -u "elastic:${ELASTIC_PASSWORD}" -X POST http://es01:9200/_security/role/own_indices -H 'Content-Type: application/json' -d '{"indices": [{"names": ["*"], "privileges": ["all"]}]}') |
| 28 | + exit_code=$? |
| 29 | + status=$(echo "$response" | tail -n1) |
| 30 | + if [ $exit_code -eq 0 ] && [ "$status" = "200" ]; then |
| 31 | + echo "Elasticsearch role own_indices created" |
| 32 | + break |
| 33 | + else |
| 34 | + echo "Elasticsearch role own_indices failure: $exit_code $status" |
| 35 | + echo "$response" |
| 36 | + sleep 5 |
| 37 | + fi |
| 38 | +done |
12 | 39 |
|
| 40 | +echo "Elasticsearch role own_indices:" |
| 41 | +curl -u "elastic:${ELASTIC_PASSWORD}" -X GET "http://es01:9200/_security/role/own_indices" |
| 42 | +echo "" |
13 | 43 |
|
14 |
| -PAYLOAD="{ |
15 |
| - \"password\" : \"${KIBANA_PASSWORD}\", |
16 |
| - \"roles\" : [ \"kibana_admin\",\"kibana_system\" ], |
17 |
| - \"full_name\" : \"${KIBANA_USER}\", |
18 |
| - \"email\" : \"${KIBANA_USER}@example.com\" |
19 |
| -}" |
20 |
| -echo "新用戶帳戶: $PAYLOAD" |
| 44 | +PAYLOAD="{\"password\": \"${KIBANA_PASSWORD}\", \"roles\": [\"kibana_admin\", \"kibana_system\", \"own_indices\"], \"full_name\": \"${KIBANA_USER}\", \"email\": \"${KIBANA_USER}@example.com\"}" |
21 | 45 |
|
22 |
| -# 創建新用戶帳戶 |
23 |
| -curl -X POST "http://es01:9200/_security/user/${KIBANA_USER}" \ |
24 |
| --u "elastic:${ELASTIC_PASSWORD}" \ |
25 |
| --H "Content-Type: application/json" \ |
26 |
| --d "$PAYLOAD"s |
| 46 | +echo "Going to create Elasticsearch user ${KIBANA_USER}: ${PAYLOAD}" |
| 47 | + |
| 48 | +# Create new user |
| 49 | +while true; do |
| 50 | + response=$(curl -s -v -w "\n%{http_code}" -u "elastic:${ELASTIC_PASSWORD}" -X POST http://es01:9200/_security/user/${KIBANA_USER} -H "Content-Type: application/json" -d "${PAYLOAD}") |
| 51 | + exit_code=$? |
| 52 | + status=$(echo "$response" | tail -n1) |
| 53 | + if [ $exit_code -eq 0 ] && [ "$status" = "200" ]; then |
| 54 | + echo "Elasticsearch user ${KIBANA_USER} created" |
| 55 | + break |
| 56 | + else |
| 57 | + echo "Elasticsearch user ${KIBANA_USER} failure: $exit_code $status" |
| 58 | + echo "$response" |
| 59 | + sleep 5 |
| 60 | + fi |
| 61 | +done |
27 | 62 |
|
28 |
| -echo "新用戶帳戶已創建" |
| 63 | +echo "Elasticsearch user ${KIBANA_USER}:" |
| 64 | +curl -u "elastic:${ELASTIC_PASSWORD}" -X GET "http://es01:9200/_security/user/${KIBANA_USER}" |
| 65 | +echo "" |
29 | 66 |
|
30 | 67 | exit 0
|
0 commit comments