|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Launch one or more Elasticsearch nodes via the Docker image, |
| 4 | +# to form a cluster suitable for running the REST API tests. |
| 5 | +# |
| 6 | +# Export the ELASTICSEARCH_VERSION variable, eg. 'elasticsearch:8.0.0-SNAPSHOT'. |
| 7 | + |
| 8 | +# Version 1.0.2 |
| 9 | +# - Initial version of the run-elasticsearch.sh script |
| 10 | +# - Deleting the volume should not dependent on the container still running |
| 11 | +# - Fixed `ES_JAVA_OPTS` config |
| 12 | + |
| 13 | +if [[ -z "$ELASTICSEARCH_VERSION" ]]; then |
| 14 | + echo -e "\033[31;1mERROR:\033[0m Required environment variable [ELASTICSEARCH_VERSION] not set\033[0m" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +set -euxo pipefail |
| 19 | + |
| 20 | +SCRIPT_PATH=$(dirname $(realpath -s $0)) |
| 21 | + |
| 22 | +moniker=$(echo "$ELASTICSEARCH_VERSION" | tr -C "[:alnum:]" '-') |
| 23 | +suffix=rest-test |
| 24 | + |
| 25 | +NODE_NAME=${NODE_NAME-${moniker}node1} |
| 26 | +MASTER_NODE_NAME=${MASTER_NODE_NAME-${NODE_NAME}} |
| 27 | +CLUSTER_NAME=${CLUSTER_NAME-${moniker}${suffix}} |
| 28 | +HTTP_PORT=${HTTP_PORT-9200} |
| 29 | + |
| 30 | +ELASTIC_PASSWORD=${ELASTIC_PASSWORD-changeme} |
| 31 | +SSL_CERT=${SSL_CERT-"${SCRIPT_PATH}/certs/testnode.crt"} |
| 32 | +SSL_KEY=${SSL_KEY-"${SCRIPT_PATH}/certs/testnode.key"} |
| 33 | +SSL_CA=${SSL_CA-"${SCRIPT_PATH}/certs/ca.crt"} |
| 34 | +SSL_CA_PEM=${SSL_CA-"${SCRIPT_PATH}/certs/ca.pem"} |
| 35 | + |
| 36 | +DETACH=${DETACH-false} |
| 37 | +CLEANUP=${CLEANUP-false} |
| 38 | + |
| 39 | +volume_name=${NODE_NAME}-${suffix}-data |
| 40 | +network_default=${moniker}${suffix} |
| 41 | +NETWORK_NAME=${NETWORK_NAME-"$network_default"} |
| 42 | + |
| 43 | +set +x |
| 44 | + |
| 45 | +function cleanup_volume { |
| 46 | + if [[ "$(docker volume ls -q -f name=$1)" ]]; then |
| 47 | + echo -e "\033[34;1mINFO:\033[0m Removing volume $1\033[0m" |
| 48 | + (docker volume rm "$1") || true |
| 49 | + fi |
| 50 | +} |
| 51 | +function container_running { |
| 52 | + if [[ "$(docker ps -q -f name=$1)" ]]; then |
| 53 | + return 0; |
| 54 | + else return 1; |
| 55 | + fi |
| 56 | +} |
| 57 | +function cleanup_node { |
| 58 | + if container_running "$1"; then |
| 59 | + echo -e "\033[34;1mINFO:\033[0m Removing container $1\033[0m" |
| 60 | + (docker container rm --force --volumes "$1") || true |
| 61 | + fi |
| 62 | + echo -e "\033[34;1mINFO:\033[0m Removing volume $1-${suffix}-data\033[0m" |
| 63 | + cleanup_volume "$1-${suffix}-data" |
| 64 | +} |
| 65 | +function cleanup_network { |
| 66 | + if [[ "$(docker network ls -q -f name=$1)" ]]; then |
| 67 | + echo -e "\033[34;1mINFO:\033[0m Removing network $1\033[0m" |
| 68 | + (docker network rm "$1") || true |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +function cleanup { |
| 73 | + if [[ "$DETACH" != "true" ]] || [[ "$1" == "1" ]]; then |
| 74 | + echo -e "\033[34;1mINFO:\033[0m clean the node and volume on startup (1) OR on exit if not detached\033[0m" |
| 75 | + cleanup_node "$NODE_NAME" |
| 76 | + fi |
| 77 | + if [[ "$DETACH" != "true" ]]; then |
| 78 | + echo -e "\033[34;1mINFO:\033[0m clean the network if not detached (start and exit)\033[0m" |
| 79 | + cleanup_network "$NETWORK_NAME" |
| 80 | + fi |
| 81 | +}; |
| 82 | +trap "cleanup 0" EXIT |
| 83 | + |
| 84 | +if [[ "$CLEANUP" == "true" ]]; then |
| 85 | + trap - EXIT |
| 86 | + if [[ -z "$(docker network ls -q -f name=${NETWORK_NAME})" ]]; then |
| 87 | + echo -e "\033[34;1mINFO:\033[0m $NETWORK_NAME is already deleted\033[0m" |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | + containers=$(docker network inspect -f '{{ range $key, $value := .Containers }}{{ printf "%s\n" .Name}}{{ end }}' ${NETWORK_NAME}) |
| 91 | + while read -r container; do |
| 92 | + cleanup_node "$container" |
| 93 | + done <<< "$containers" |
| 94 | + cleanup_network "$NETWORK_NAME" |
| 95 | + echo -e "\033[32;1mSUCCESS:\033[0m Cleaned up and exiting\033[0m" |
| 96 | + exit 0 |
| 97 | +fi |
| 98 | + |
| 99 | +echo -e "\033[34;1mINFO:\033[0m Making sure previous run leftover infrastructure is removed \033[0m" |
| 100 | +cleanup 1 |
| 101 | + |
| 102 | +echo -e "\033[34;1mINFO:\033[0m Creating network $NETWORK_NAME if it does not exist already \033[0m" |
| 103 | +docker network inspect "$NETWORK_NAME" > /dev/null 2>&1 || docker network create "$NETWORK_NAME" |
| 104 | + |
| 105 | +environment=($(cat <<-END |
| 106 | + --env node.name=$NODE_NAME |
| 107 | + --env cluster.name=$CLUSTER_NAME |
| 108 | + --env cluster.initial_master_nodes=$MASTER_NODE_NAME |
| 109 | + --env discovery.seed_hosts=$MASTER_NODE_NAME |
| 110 | + --env cluster.routing.allocation.disk.threshold_enabled=false |
| 111 | + --env bootstrap.memory_lock=true |
| 112 | + --env node.attr.testattr=test |
| 113 | + --env path.repo=/tmp |
| 114 | + --env repositories.url.allowed_urls=http://snapshot.test* |
| 115 | +END |
| 116 | +)) |
| 117 | + |
| 118 | +volumes=($(cat <<-END |
| 119 | + --volume $volume_name:/usr/share/elasticsearch/data |
| 120 | +END |
| 121 | +)) |
| 122 | + |
| 123 | +if [[ "$ELASTICSEARCH_VERSION" != *oss* ]]; then |
| 124 | + environment+=($(cat <<-END |
| 125 | + --env ELASTIC_PASSWORD=$ELASTIC_PASSWORD |
| 126 | + --env xpack.license.self_generated.type=trial |
| 127 | + --env xpack.security.enabled=true |
| 128 | + --env xpack.security.http.ssl.enabled=true |
| 129 | + --env xpack.security.http.ssl.verification_mode=certificate |
| 130 | + --env xpack.security.http.ssl.key=certs/testnode.key |
| 131 | + --env xpack.security.http.ssl.certificate=certs/testnode.crt |
| 132 | + --env xpack.security.http.ssl.certificate_authorities=certs/ca.crt |
| 133 | + --env xpack.security.transport.ssl.enabled=true |
| 134 | + --env xpack.security.transport.ssl.key=certs/testnode.key |
| 135 | + --env xpack.security.transport.ssl.certificate=certs/testnode.crt |
| 136 | + --env xpack.security.transport.ssl.certificate_authorities=certs/ca.crt |
| 137 | +END |
| 138 | +)) |
| 139 | + volumes+=($(cat <<-END |
| 140 | + --volume $SSL_CERT:/usr/share/elasticsearch/config/certs/testnode.crt |
| 141 | + --volume $SSL_KEY:/usr/share/elasticsearch/config/certs/testnode.key |
| 142 | + --volume $SSL_CA:/usr/share/elasticsearch/config/certs/ca.crt |
| 143 | + --volume $SSL_CA_PEM:/usr/share/elasticsearch/config/certs/ca.pem |
| 144 | +END |
| 145 | +)) |
| 146 | +fi |
| 147 | + |
| 148 | +url="http://$NODE_NAME" |
| 149 | +if [[ "$ELASTICSEARCH_VERSION" != *oss* ]]; then |
| 150 | + url="https://elastic:$ELASTIC_PASSWORD@$NODE_NAME" |
| 151 | +fi |
| 152 | + |
| 153 | +cert_validation_flags="--insecure" |
| 154 | +if [[ "$NODE_NAME" == "instance" ]]; then |
| 155 | + cert_validation_flags="--cacert /usr/share/elasticsearch/config/certs/ca.pem --resolve ${NODE_NAME}:443:127.0.0.1" |
| 156 | +fi |
| 157 | + |
| 158 | +echo -e "\033[34;1mINFO:\033[0m Starting container $NODE_NAME \033[0m" |
| 159 | + |
| 160 | +set -x |
| 161 | +docker run \ |
| 162 | + --name "$NODE_NAME" \ |
| 163 | + --network "$NETWORK_NAME" \ |
| 164 | + --env "ES_JAVA_OPTS=-Xms1g -Xmx1g" \ |
| 165 | + "${environment[@]}" \ |
| 166 | + "${volumes[@]}" \ |
| 167 | + --publish "$HTTP_PORT":9200 \ |
| 168 | + --ulimit nofile=65536:65536 \ |
| 169 | + --ulimit memlock=-1:-1 \ |
| 170 | + --detach="$DETACH" \ |
| 171 | + --health-cmd="curl $cert_validation_flags --fail $url:9200/_cluster/health || exit 1" \ |
| 172 | + --health-interval=2s \ |
| 173 | + --health-retries=20 \ |
| 174 | + --health-timeout=2s \ |
| 175 | + --rm \ |
| 176 | + docker.elastic.co/elasticsearch/"$ELASTICSEARCH_VERSION"; |
| 177 | +set +x |
| 178 | + |
| 179 | +if [[ "$DETACH" == "true" ]]; then |
| 180 | + until ! container_running "$NODE_NAME" || (container_running "$NODE_NAME" && [[ "$(docker inspect -f "{{.State.Health.Status}}" ${NODE_NAME})" != "starting" ]]); do |
| 181 | + echo "" |
| 182 | + docker inspect -f "{{range .State.Health.Log}}{{.Output}}{{end}}" ${NODE_NAME} |
| 183 | + echo -e "\033[34;1mINFO:\033[0m waiting for node $NODE_NAME to be up\033[0m" |
| 184 | + sleep 2; |
| 185 | + done; |
| 186 | + |
| 187 | + # Always show logs if the container is running, this is very useful both on CI as well as while developing |
| 188 | + if container_running $NODE_NAME; then |
| 189 | + docker logs $NODE_NAME |
| 190 | + fi |
| 191 | + |
| 192 | + if ! container_running $NODE_NAME || [[ "$(docker inspect -f "{{.State.Health.Status}}" ${NODE_NAME})" != "healthy" ]]; then |
| 193 | + cleanup 1 |
| 194 | + echo |
| 195 | + echo -e "\033[31;1mERROR:\033[0m Failed to start ${ELASTICSEARCH_VERSION} in detached mode beyond health checks\033[0m" |
| 196 | + echo -e "\033[31;1mERROR:\033[0m dumped the docker log before shutting the node down\033[0m" |
| 197 | + exit 1 |
| 198 | + else |
| 199 | + echo |
| 200 | + echo -e "\033[32;1mSUCCESS:\033[0m Detached and healthy: ${NODE_NAME} on docker network: ${NETWORK_NAME}\033[0m" |
| 201 | + echo -e "\033[32;1mSUCCESS:\033[0m Running on: ${url/$NODE_NAME/localhost}:${HTTP_PORT}\033[0m" |
| 202 | + exit 0 |
| 203 | + fi |
| 204 | +fi |
0 commit comments