|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright OpenSearch Contributors |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# This script specify the entrypoint startup actions for opensearch |
| 7 | +# It will start both opensearch and performance analyzer plugin cli |
| 8 | +# If either process failed, the entire docker container will be removed |
| 9 | +# in favor of a newly started container |
| 10 | + |
| 11 | +# Export OpenSearch Home |
| 12 | +export OPENSEARCH_HOME=/usr/share/opensearch |
| 13 | +export OPENSEARCH_PATH_CONF=$OPENSEARCH_HOME/config |
| 14 | + |
| 15 | +# The virtual file /proc/self/cgroup should list the current cgroup |
| 16 | +# membership. For each hierarchy, you can follow the cgroup path from |
| 17 | +# this file to the cgroup filesystem (usually /sys/fs/cgroup/) and |
| 18 | +# introspect the statistics for the cgroup for the given |
| 19 | +# hierarchy. Alas, Docker breaks this by mounting the container |
| 20 | +# statistics at the root while leaving the cgroup paths as the actual |
| 21 | +# paths. Therefore, OpenSearch provides a mechanism to override |
| 22 | +# reading the cgroup path from /proc/self/cgroup and instead uses the |
| 23 | +# cgroup path defined the JVM system property |
| 24 | +# opensearch.cgroups.hierarchy.override. Therefore, we set this value here so |
| 25 | +# that cgroup statistics are available for the container this process |
| 26 | +# will run in. |
| 27 | +export OPENSEARCH_JAVA_OPTS="-Dopensearch.cgroups.hierarchy.override=/ $OPENSEARCH_JAVA_OPTS" |
| 28 | + |
| 29 | +# Security Plugin |
| 30 | +function setupSecurityPlugin { |
| 31 | + SECURITY_PLUGIN="opensearch-security" |
| 32 | + |
| 33 | + if [ -d "$OPENSEARCH_HOME/plugins/$SECURITY_PLUGIN" ]; then |
| 34 | + if [ "$DISABLE_SECURITY_PLUGIN" = "true" ]; then |
| 35 | + echo "Disabling OpenSearch Security Plugin" |
| 36 | + opensearch_opt="-Eplugins.security.disabled=true" |
| 37 | + opensearch_opts+=("${opensearch_opt}") |
| 38 | + else |
| 39 | + echo "Enabling OpenSearch Security Plugin" |
| 40 | + if [ "$DISABLE_INSTALL_DEMO_CONFIG" = "true" ]; then |
| 41 | + echo "Disabling execution of install_demo_configuration.sh for OpenSearch Security Plugin" |
| 42 | + else |
| 43 | + echo -e "Enabling execution of install_demo_configuration.sh for OpenSearch Security Plugin \nOpenSearch 2.12.0 onwards, the OpenSearch Security Plugin a change that requires an initial password for 'admin' user. \nPlease define an environment variable 'OPENSEARCH_INITIAL_ADMIN_PASSWORD' with a strong password string. \nIf a password is not provided, the setup will quit. \n For more details, please visit: https://opensearch.org/docs/latest/install-and-configure/install-opensearch/docker/" |
| 44 | + bash $OPENSEARCH_HOME/plugins/$SECURITY_PLUGIN/tools/install_demo_configuration.sh -y -i -s || exit 1 |
| 45 | + fi |
| 46 | + fi |
| 47 | + else |
| 48 | + echo "OpenSearch Security Plugin does not exist, disable by default" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# Performance Analyzer Plugin |
| 53 | +function setupPerformanceAnalyzerPlugin { |
| 54 | + PERFORMANCE_ANALYZER_PLUGIN="opensearch-performance-analyzer" |
| 55 | + if [ -d "$OPENSEARCH_HOME/plugins/$PERFORMANCE_ANALYZER_PLUGIN" ]; then |
| 56 | + if [ "$DISABLE_PERFORMANCE_ANALYZER_AGENT_CLI" = "true" ]; then |
| 57 | + echo "Disabling execution of $OPENSEARCH_HOME/bin/$PERFORMANCE_ANALYZER_PLUGIN/performance-analyzer-agent-cli for OpenSearch Performance Analyzer Plugin" |
| 58 | + else |
| 59 | + echo "Enabling execution of OPENSEARCH_HOME/bin/$PERFORMANCE_ANALYZER_PLUGIN/performance-analyzer-agent-cli for OpenSearch Performance Analyzer Plugin" |
| 60 | + $OPENSEARCH_HOME/bin/opensearch-performance-analyzer/performance-analyzer-agent-cli > $OPENSEARCH_HOME/logs/PerformanceAnalyzer.log 2>&1 & disown |
| 61 | + fi |
| 62 | + else |
| 63 | + echo "OpenSearch Performance Analyzer Plugin does not exist, disable by default" |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# Start up the opensearch and performance analyzer agent processes. |
| 68 | +# When either of them halts, this script exits, or we receive a SIGTERM or SIGINT signal then we want to kill both these processes. |
| 69 | +function runOpensearch { |
| 70 | + # Files created by OpenSearch should always be group writable too |
| 71 | + umask 0002 |
| 72 | + |
| 73 | + if [[ "$(id -u)" == "0" ]]; then |
| 74 | + echo "OpenSearch cannot run as root. Please start your container as another user." |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + # Parse Docker env vars to customize OpenSearch |
| 79 | + # |
| 80 | + # e.g. Setting the env var cluster.name=testcluster |
| 81 | + # will cause OpenSearch to be invoked with -Ecluster.name=testcluster |
| 82 | + opensearch_opts=() |
| 83 | + while IFS='=' read -r envvar_key envvar_value |
| 84 | + do |
| 85 | + # OpenSearch settings need to have at least two dot separated lowercase |
| 86 | + # words, e.g. `cluster.name`, except for `processors` which we handle |
| 87 | + # specially |
| 88 | + if [[ "$envvar_key" =~ ^[a-z0-9_]+\.[a-z0-9_]+ || "$envvar_key" == "processors" ]]; then |
| 89 | + if [[ ! -z $envvar_value ]]; then |
| 90 | + opensearch_opt="-E${envvar_key}=${envvar_value}" |
| 91 | + opensearch_opts+=("${opensearch_opt}") |
| 92 | + fi |
| 93 | + fi |
| 94 | + done < <(env) |
| 95 | + |
| 96 | + setupSecurityPlugin |
| 97 | + setupPerformanceAnalyzerPlugin |
| 98 | + |
| 99 | + # Start opensearch |
| 100 | + "$@" "${opensearch_opts[@]}" |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +# Prepend "opensearch" command if no argument was provided or if the first |
| 105 | +# argument looks like a flag (i.e. starts with a dash). |
| 106 | +if [ $# -eq 0 ] || [ "${1:0:1}" = '-' ]; then |
| 107 | + set -- opensearch "$@" |
| 108 | +fi |
| 109 | + |
| 110 | +if [ "$1" = "opensearch" ]; then |
| 111 | + # If the first argument is opensearch, then run the setup script. |
| 112 | + runOpensearch "$@" |
| 113 | +else |
| 114 | + # Otherwise, just exec the command. |
| 115 | + exec "$@" |
| 116 | +fi |
0 commit comments