1- #! /usr/bin/env bash
2- # Apply base_index_settings.json as index template. Run from this directory.
3- # See: https://docs.opensearch.org/latest/im-plugin/index-templates/
4- #! /usr/bin/env bash
1+ #! /usr/bin/sh
2+ set -e
53log () {
64 local ts
75 ts=$( date +" %Y-%m-%d %H:%M:%S" )
86 echo " [$ts ] $* "
97}
108
11- # : "${OPENSEARCH_URL:?OPENSEARCH_URL is required. Include scheme, port and /api eg http://localhost:9200/ }"
12- # : "${OPENSEARCH_DASHBOARD_URL:?OPENSEARCH_DASHBOARD_URL is required. Include scheme, port and /api eg http://localhost:5601/api }"
9+ : " ${OPENSEARCH_URL:? OPENSEARCH_URL is required. Include scheme, port and / api eg http:// localhost: 9200/ } "
10+ : " ${OPENSEARCH_DASHBOARD_URL:? OPENSEARCH_DASHBOARD_URL is required. Include scheme, port and / api eg http:// localhost: 5601/ api } "
11+ : " ${OPENSEARCH_USERNAME:? OPENSEARCH_USERNAME is required. } "
12+ : " ${OPENSEARCH_PASSWORD:? OPENSEARCH_PASSWORD is required. } "
1313
14- OPENSEARCH_URL=https://localhost:9200
15- OPENSEARCH_DASHBOARD_URL=http://localhost:5601
14+ : " ${CONFIG_DIR:? CONFIG_DIR is required. } "
15+ : " ${CURL_BODY_FILE:=/ tmp/ curl_body.$$ } "
16+ # OPENSEARCH_URL=https://localhost:9200
17+ # OPENSEARCH_DASHBOARD_URL=http://localhost:5601
18+ # OPENSEARCH_USERNAME=admin
19+ # OPENSEARCH_PASSWORD=opensearch-312$A
1620
1721wait_for_service () {
1822 local service_name=" $1 "
@@ -24,10 +28,10 @@ wait_for_service() {
2428
2529 while true ; do
2630 if curl -sf --insecure $curl_extra_args " $url " > /dev/null; then
27- log " $service_name is running ✅ "
31+ log " $service_name is running"
2832 return 0
2933 else
30- log " $service_name is not running at $url ❌ "
34+ log " $service_name is not running at $url "
3135 if [ " $elapsed " -ge " $max_wait " ]; then
3236 log " Timed out waiting for $service_name to become healthy"
3337 return 1
@@ -42,14 +46,42 @@ wait_for_service() {
4246 fi
4347 done
4448}
45- wait_for_service " OpenSearch Dashboard" " $OPENSEARCH_DASHBOARD_URL " || exit 1
46- wait_for_service " OpenSearch" " $OPENSEARCH_URL " ' -u admin:opensearch-312$A' || exit 1
4749
48- log " Creating index template"
49- curl -X PUT " https://localhost:9200/_index_template/base_index_template" -H " Content-Type: application/json" -u ' admin:opensearch-312$A' -k -d @base_index_settings.json
50+ OPENSEARCH_AUTH=" $OPENSEARCH_USERNAME :$OPENSEARCH_PASSWORD "
5051
51- log " Creating example admissions document"
52- curl -X POST " https://localhost:9200/admissions/_doc" -H " Content-Type: application/json" -u ' admin:opensearch-312$A' -k -d ' {
52+ if [ " $PROVISION_OPENSEARCH_INDEX_TEMPLATE_ENABLED " = " true" ]; then
53+ wait_for_service " OpenSearch" " $OPENSEARCH_URL " " -u $OPENSEARCH_AUTH " || exit 1
54+ # See: https://docs.opensearch.org/latest/im-plugin/index-templates/
55+ log " Creating index template - PUT $OPENSEARCH_URL /_index_template/base_index_template"
56+ os_status=" $( curl -sS \
57+ -o " $CURL_BODY_FILE " \
58+ -w " %{http_code}" \
59+ -X PUT " $OPENSEARCH_URL /_index_template/base_index_template" \
60+ -H " Content-Type: application/json" \
61+ -u " $OPENSEARCH_AUTH " \
62+ -k \
63+ -d @" ${CONFIG_DIR} /base_index_settings.json" ) "
64+ if [ " $os_status " != " 200" ] && [ " $os_status " != " 201" ]; then
65+ log " Failed to create index template (http_status=$os_status )"
66+ if [ -s " $CURL_BODY_FILE " ]; then
67+ log " Response body:"
68+ sed ' s/^/ /' " $CURL_BODY_FILE "
69+ fi
70+ exit 1
71+ fi
72+ fi
73+
74+ if [ " $PROVISION_OPENSEARCH_EXAMPLE_DOCUMENTS_ENABLED " = " true" ]; then
75+ wait_for_service " OpenSearch" " $OPENSEARCH_URL " " -u $OPENSEARCH_AUTH " || exit 1
76+ log " Creating example admissions document - POST $OPENSEARCH_URL /admissions/_doc"
77+ os_status=" $( curl -sS \
78+ -o " $CURL_BODY_FILE " \
79+ -w " %{http_code}" \
80+ -X POST " $OPENSEARCH_URL /admissions/_doc" \
81+ -H " Content-Type: application/json" \
82+ -u " $OPENSEARCH_AUTH " \
83+ -k \
84+ -d ' {
5385 "subject_id": 10000032,
5486 "hadm_id": 22595853,
5587 "admittime": "2180-05-06 22:23:00",
@@ -65,7 +97,38 @@ curl -X POST "https://localhost:9200/admissions/_doc" -H "Content-Type: applicat
6597 "edregtime": "2180-05-06 19:17:00",
6698 "edouttime": "2180-05-06 23:30:00",
6799 "hospital_expire_flag": 0
68- }'
100+ }' ) "
101+ if [ " $os_status " != " 200" ] && [ " $os_status " != " 201" ]; then
102+ log " Failed to create example admissions document (http_status=$os_status )"
103+ if [ -s " $CURL_BODY_FILE " ]; then
104+ log " Response body:"
105+ sed ' s/^/ /' " $CURL_BODY_FILE "
106+ fi
107+ exit 1
108+ fi
109+ fi
110+
111+ if [ " $PROVISION_OPENSEARCH_DASHBOARDS_ENABLED " = " true" ]; then
112+ log " Provisioning OpenSearch Dashboards"
113+ wait_for_service " OpenSearch Dashboard" " $OPENSEARCH_DASHBOARD_URL " || exit 1
114+
115+ log " Importing dashboards - POST $OPENSEARCH_DASHBOARD_URL /api/saved_objects/_import?overwrite=true"
116+ osd_status=" $( curl -sS \
117+ -o " $CURL_BODY_FILE " \
118+ -w " %{http_code}" \
119+ -X POST " $OPENSEARCH_DASHBOARD_URL /api/saved_objects/_import?overwrite=true" \
120+ -H " osd-xsrf: true" \
121+ --form " file=@${CONFIG_DIR} /dashboards.ndjson" \
122+ -u " $OPENSEARCH_AUTH " \
123+ --insecure) "
124+ if [ " $osd_status " != " 200" ] && [ " $osd_status " != " 201" ]; then
125+ log " Failed to import dashboards (http_status=$osd_status )"
126+ if [ -s " $CURL_BODY_FILE " ]; then
127+ log " Response body:"
128+ sed ' s/^/ /' " $CURL_BODY_FILE "
129+ fi
130+ exit 1
131+ fi
132+ fi
69133
70- log " Importing dashboards"
71- curl -X POST " http://localhost:5601/api/saved_objects/_import?overwrite=true" -H " osd-xsrf: true" --form file=@dashboards.ndjson -u ' admin:opensearch-312$A' --insecure
134+ log " Provisioning completed"
0 commit comments