Created
August 10, 2021 08:21
-
-
Save fteychene/f76709b778edd2eb955e349067a395bc to your computer and use it in GitHub Desktop.
Local Elastic/Kibana stack script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
elasticsearch: | |
image: elasticsearch:7.14.0 | |
hostname: elasticsearch | |
ports: | |
- 9200:9200 | |
- 9300:9300 | |
environment: | |
discovery.type: single-node | |
networks: | |
- elastic | |
volumes: | |
- elastic-data:/usr/share/elasticsearch/data | |
kibana: | |
image: kibana:7.14.0 | |
ports: | |
- 5601:5601 | |
environment: | |
ELASTICSEARCH_HOSTS: http://elasticsearch:9200 | |
networks: | |
- elastic | |
networks: | |
elastic: | |
driver: bridge | |
volumes: | |
elastic-data: | |
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -Eeuo pipefail | |
trap cleanup SIGINT SIGTERM ERR EXIT | |
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) | |
usage() { | |
cat <<EOF | |
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] command | |
Local elasticsearch environment script. | |
Manage a elasticsearch and a kibana. | |
Available options: | |
-h, --help Print this help and exit | |
-v, --verbose Print script debug info | |
-t, --technology Target container runtime (default: docker-compose) | |
Available: docker, docker-compose | |
Commands: | |
start Start the stack | |
stop Stop the stack | |
clean Cleanup the stack | |
status Check this status of the stack | |
EOF | |
exit | |
} | |
cleanup() { | |
trap - SIGINT SIGTERM ERR EXIT | |
# script cleanup here | |
} | |
setup_colors() { | |
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then | |
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m' | |
else | |
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW='' | |
fi | |
} | |
msg() { | |
echo >&2 -e "${1-}" | |
} | |
die() { | |
local msg=$1 | |
local code=${2-1} # default exit status 1 | |
msg "$msg" | |
exit "$code" | |
} | |
parse_params() { | |
technology='docker-compose' | |
while :; do | |
case "${1-}" in | |
-h | --help) usage ;; | |
-v | --verbose) set -x ;; | |
--no-color) NO_COLOR=1 ;; | |
-t | --technology) | |
technology="${2-}" | |
shift | |
;; | |
-?*) die "Unknown option: $1" ;; | |
*) break ;; | |
esac | |
shift | |
done | |
args=("$@") | |
[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments" | |
return 0 | |
} | |
parse_params "$@" | |
setup_colors | |
technology_command() { | |
local command=$1 | |
case "$technology" in | |
docker) ${command}_docker ;; | |
docker-compose) ${command}_docker-compose ;; | |
*) die "Invalid technology $technology. Must be one of the following : docker-compose" ;; | |
esac | |
} | |
DOCKER_CMD="docker" | |
start_docker() { | |
if [ ! "$(${DOCKER_CMD} volume ls -q -f name=local_elastic_data)" ]; then | |
${DOCKER_CMD} volume create -d "local" local_elastic_data | |
fi | |
if [ ! "$(${DOCKER_CMD} network ls -q -f name=local_elastic_network)" ]; then | |
${DOCKER_CMD} network create -d bridge local_elastic_network | |
fi | |
if [ ! "$(${DOCKER_CMD} container ps -aq -f name=local_elastic_elasticsearch)" ]; then | |
${DOCKER_CMD} container create \ | |
--name local_elastic_elasticsearch \ | |
--hostname elasticsearch \ | |
--publish 0.0.0.0:9200:9200 \ | |
--publish 0.0.0.0:9300:9300 \ | |
--env 'discovery.type=single-node' \ | |
--network local_elastic_network \ | |
--volume local_elastic_data:/usr/share/elasticsearch/data \ | |
--label 'stack=local_elastic' \ | |
elasticsearch:7.14.0 | |
fi | |
if [ ! "$(${DOCKER_CMD} container ps -aq -f name=local_elastic_kibana)" ]; then | |
${DOCKER_CMD} container create \ | |
--name local_elastic_kibana \ | |
--publish 0.0.0.0:5601:5601 \ | |
--env 'ELASTICSEARCH_HOSTS=http://elasticsearch:9200' \ | |
--network local_elastic_network \ | |
--label 'stack=local_elastic' \ | |
kibana:7.14.0 | |
fi | |
${DOCKER_CMD} container start local_elastic_elasticsearch | |
${DOCKER_CMD} container start local_elastic_kibana | |
} | |
stop_docker() { | |
${DOCKER_CMD} container stop local_elastic_kibana | |
${DOCKER_CMD} container stop local_elastic_elasticsearch | |
} | |
clean_docker() { | |
${DOCKER_CMD} container rm -f local_elastic_kibana | |
${DOCKER_CMD} container rm -f local_elastic_elasticsearch | |
${DOCKER_CMD} network rm local_elastic_network | |
${DOCKER_CMD} volume rm local_elastic_data | |
} | |
status_docker() { | |
${DOCKER_CMD} container ps -a -f 'label=stack=local_elastic' | |
} | |
DOCKER_COMPOSE_CMD="docker-compose -f ${script_dir}/docker-compose-elastic.yml -p \"local_elastic\"" | |
start_docker-compose() { | |
$DOCKER_COMPOSE_CMD up -d | |
if [ "$?" -eq 0 ]; then | |
echo -e "${GREEN}Local elastic stack is starting${NOFORMAT}" | |
echo -e "\tElasticsearch will be available at http://localhost:9200 and http://localhost:9300" | |
echo -e "\tKibana will be available at http://localhost:5601" | |
else | |
die "${RED}Error starting elastic stack${NOFORMAT}"} | |
fi | |
} | |
stop_docker-compose() { | |
$DOCKER_COMPOSE_CMD stop | |
if [ "$?" -eq 0 ]; then | |
echo -e "${GREEN}Local elastic stack is stopped${NOFORMAT}" | |
else | |
die "${RED}Error stopping elastic stack${NOFORMAT}"} | |
fi | |
} | |
clean_docker-compose() { | |
$DOCKER_COMPOSE_CMD down | |
if [ "$?" -eq 0 ]; then | |
echo -e "${GREEN}Cleanup local elastic stack${NOFORMAT}" | |
else | |
die "${RED}Error cleanup elastic stack${NOFORMAT}"} | |
fi | |
} | |
status_docker-compose() { | |
$DOCKER_COMPOSE_CMD ps | |
if [ "$?" -ne 0 ]; then | |
die "${RED}Error getting elastic stack status, check docker dcaemon directly${NOFORMAT}"} | |
fi | |
} | |
case "${args[0]}" in | |
start) technology_command start ;; | |
stop) technology_command stop ;; | |
clean) technology_command clean ;; | |
status) technology_command status ;; | |
*) usage ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment