Skip to content

Commit 3d61822

Browse files
committed
feat(helm): Cogstack CE - provision opensearch dashboards on install
1 parent 78aacb8 commit 3d61822

3 files changed

Lines changed: 138 additions & 36 deletions

File tree

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
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
53
log() {
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

1721
wait_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"

deployment/kubernetes/charts/cogstack-helm-ce/templates/_helpers.tpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,32 @@ Create the name of the service account to use
6060
{{- default "default" .Values.serviceAccount.name }}
6161
{{- end }}
6262
{{- end }}
63+
64+
{{/*
65+
Dependency URLs
66+
*/}}
67+
68+
{{- define "opensearch.url" -}}
69+
{{- $serviceName := include "opensearch.serviceName" (index .Subcharts "opensearch") -}}
70+
{{- $scheme := default "https" .Values.opensearch.protocol -}}
71+
{{- $port := default 9200 .Values.opensearch.httpPort -}}
72+
{{- printf "%s://%s:%v" $scheme $serviceName $port -}}
73+
{{- end }}
74+
75+
{{- define "opensearch-dashboards.url" -}}
76+
{{- $serviceName := include "opensearch-dashboards.fullname" (index .Subcharts "opensearch-dashboards") -}}
77+
{{- $scheme := "http" -}}
78+
{{- $port := default 5601 (index .Values "opensearch-dashboards" "service" "port") -}}
79+
{{- printf "%s://%s:%v" $scheme $serviceName $port -}}
80+
{{- end }}
81+
82+
{{- define "opensearch.initialAdminPassword" -}}
83+
{{- $val := "" -}}
84+
{{- range .Values.opensearch.extraEnvs }}
85+
{{- if eq .name "OPENSEARCH_INITIAL_ADMIN_PASSWORD" }}
86+
{{- $val = .value -}}
87+
{{- end }}
88+
{{- end }}
89+
{{- $val -}}
90+
{{- end }}
91+

deployment/kubernetes/charts/cogstack-helm-ce/templates/opensearch-provisioning-post-install.yaml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@ apiVersion: v1
22
kind: Pod
33
metadata:
44
labels:
5-
app.kubernetes.io/name: opensearch-provisioning
5+
{{- include "cogstack-helm-ce.labels" . | nindent 4 }}
66
annotations:
77
"helm.sh/hook": post-install,post-upgrade
88
"helm.sh/hook-weight": "1"
9-
#"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
10-
"helm.sh/hook-delete-policy": before-hook-creation
11-
name: opensearch-provisioning
9+
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
10+
#"helm.sh/hook-delete-policy": before-hook-creation
11+
name: {{ include "cogstack-helm-ce.fullname" . }}-opensearch-provisioning
1212
spec:
1313
restartPolicy: OnFailure
1414
containers:
15-
- image: busybox:1.28
16-
name: opensearch-provisioning
17-
command: ["bash", "/etc/config/opensearch-provisioning-config/opensearch-provisioning.sh"]
18-
env:
19-
- name: OPENSEARCH_URL
20-
value: https://localhost:9200
21-
- name: OPENSEARCH_DASHBOARD_URL
22-
value: http://localhost:5601
23-
- name: OPENSEARCH_INITIAL_ADMIN_PASSWORD
24-
value: "opensearch-312$A"
25-
volumeMounts:
26-
- name: opensearch-provisioning-config
27-
mountPath: /etc/config/opensearch-provisioning-config
15+
- image: curlimages/curl
16+
name: opensearch-provisioning
17+
command: ["/bin/sh", "/etc/config/opensearch-provisioning-config/opensearch-provisioning.sh"]
18+
env:
19+
- name: OPENSEARCH_URL
20+
value: {{ include "opensearch.url" . }}
21+
- name: OPENSEARCH_DASHBOARD_URL
22+
value: {{ include "opensearch-dashboards.url" . }}
23+
- name: OPENSEARCH_USERNAME
24+
value: admin
25+
- name: OPENSEARCH_PASSWORD
26+
value: {{ include "opensearch.initialAdminPassword" . }}
27+
- name: CONFIG_DIR
28+
value: /etc/config/opensearch-provisioning-config
29+
- name: PROVISION_OPENSEARCH_DASHBOARDS_ENABLED
30+
value: "true"
31+
- name: PROVISION_OPENSEARCH_INDEX_TEMPLATE_ENABLED
32+
value: "true"
33+
- name: PROVISION_OPENSEARCH_EXAMPLE_DOCUMENTS_ENABLED
34+
value: "true"
35+
volumeMounts:
36+
- name: opensearch-provisioning-config
37+
mountPath: /etc/config/opensearch-provisioning-config
2838
volumes:
2939
- name: opensearch-provisioning-config
3040
configMap:

0 commit comments

Comments
 (0)