Skip to content

Commit 2e7f6d4

Browse files
committed
Fix bug with -i option (#51)
* Fix bug with -i option * Improve error handling
1 parent 0369a4e commit 2e7f6d4

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

scripts/indexer-ism-init.sh

+54-18
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,15 @@ function generate_rollover_template() {
8585
function load_templates() {
8686
# Note: the wazuh-template.json could also be loaded here.
8787
for alias in "${aliases[@]}"; do
88-
echo "TEMPLATES AND POLICIES - Uploading ${alias} template"
89-
generate_rollover_template "${alias}" | curl -s -k ${C_AUTH} \
90-
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" -o /dev/null \
91-
-H 'Content-Type: application/json' -d @-
88+
generate_rollover_template "${alias}" |
89+
if ! curl -s -k ${C_AUTH} \
90+
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" -o /dev/null \
91+
-H 'Content-Type: application/json' -d @-; then
92+
echo "Error uploading ${alias} template"
93+
return 1
94+
else
95+
echo "${alias} template uploaded"
96+
fi
9297
done
9398
}
9499

@@ -110,15 +115,21 @@ function upload_rollover_policy() {
110115

111116
# Check if the ${POLICY_NAME} ISM policy was loaded (404 error if not found)
112117
if [[ "${policy_exists}" == "404" ]]; then
113-
echo "TEMPLATES AND POLICIES - Uploading ${POLICY_NAME} ISM policy"
114-
curl -s -k ${C_AUTH} -o /dev/null \
118+
if ! curl -s -k ${C_AUTH} -o /dev/null \
115119
-X PUT "${INDEXER_URL}/_plugins/_ism/policies/${POLICY_NAME}" \
116-
-H 'Content-Type: application/json' -d "$(generate_rollover_policy)"
120+
-H 'Content-Type: application/json' \
121+
-d "$(generate_rollover_policy)"; then
122+
echo "Error uploading ${POLICY_NAME} policy"
123+
return 1
124+
else
125+
echo "${POLICY_NAME} policy uploaded"
126+
fi
117127
else
118128
if [[ "${policy_exists}" == "200" ]]; then
119-
echo "TEMPLATES AND POLICIES - ${POLICY_NAME} policy already exists"
129+
echo "${POLICY_NAME} policy already exists"
120130
else
121-
echo "TEMPLATES AND POLICIES - Error uploading ${POLICY_NAME} policy"
131+
echo "Error checking if ${POLICY_NAME} exists"
132+
return 1
122133
fi
123134
fi
124135
}
@@ -158,9 +169,15 @@ function generate_write_index_alias() {
158169
# 1. The alias. String.
159170
#########################################################################
160171
function create_write_index() {
161-
curl -s -k ${C_AUTH} -o /dev/null \
172+
if ! curl -s -k ${C_AUTH} -o /dev/null \
162173
-X PUT "$INDEXER_URL/%3C${1}-4.x-%7Bnow%2Fd%7D-000001%3E?pretty" \
163-
-H 'Content-Type: application/json' -d "$(generate_write_index_alias "${1}")"
174+
-H 'Content-Type: application/json' \
175+
-d "$(generate_write_index_alias "${1}")"; then
176+
echo "Error creating ${1} write index"
177+
exit 1
178+
else
179+
echo "${1} write index created"
180+
fi
164181
}
165182

166183
#########################################################################
@@ -169,7 +186,6 @@ function create_write_index() {
169186
# 1. List of aliases to initialize.
170187
#########################################################################
171188
function create_indices() {
172-
echo "TEMPLATES AND POLICIES - Creating write indices"
173189
for alias in "${aliases[@]}"; do
174190
# Check if there are any write indices for the current alias
175191
write_index_exists=$(check_for_write_index "${alias}")
@@ -181,7 +197,6 @@ function create_indices() {
181197
done
182198
}
183199

184-
185200
#########################################################################
186201
# Shows usage help.
187202
#########################################################################
@@ -209,9 +224,15 @@ function show_help() {
209224
echo -e " -p, --indexer-password <password>"
210225
echo -e " Specifies the Wazuh indexer admin user password."
211226
echo -e ""
227+
echo -e " -P, --priority <priority>"
228+
echo -e " Specifies the policy's priority."
229+
echo -e ""
212230
echo -e " -s, --min-shard-size <shard-size>"
213231
echo -e " Set the minimum shard size in GB. By default 25."
214232
echo -e ""
233+
echo -e " -v, --verbose"
234+
echo -e " Set verbose mode. Prints more information."
235+
echo -e ""
215236
exit 1
216237
}
217238

@@ -252,6 +273,7 @@ function main() {
252273
show_help
253274
else
254275
INDEXER_HOSTNAME="${2}"
276+
INDEXER_URL="https://${INDEXER_HOSTNAME}:9200"
255277
shift 2
256278
fi
257279
;;
@@ -274,6 +296,19 @@ function main() {
274296
shift 2
275297
fi
276298
;;
299+
"-P" | "--priority")
300+
if [ -z "${2}" ]; then
301+
echo "Error on arguments. Probably missing <priority> after -P|--priority"
302+
show_help
303+
else
304+
ISM_PRIORITY="${2}"
305+
shift 2
306+
fi
307+
;;
308+
"-v" | "--verbose")
309+
set -x
310+
shift
311+
;;
277312
*)
278313
echo "Unknow option: ${1}"
279314
show_help
@@ -282,13 +317,14 @@ function main() {
282317
done
283318

284319
# Load the Wazuh Indexer templates
285-
load_templates
286-
287320
# Upload the rollover policy
288-
upload_rollover_policy
289-
290321
# Create the initial write indices
291-
create_indices "${aliases[@]}"
322+
if load_templates && upload_rollover_policy && create_indices "${aliases[@]}"; then
323+
echo "Indexer ISM initialization finished successfully"
324+
else
325+
echo "Indexer ISM initialization failed"
326+
exit 1
327+
fi
292328
}
293329

294330
main "$@"

0 commit comments

Comments
 (0)