From 37447d1618b85b36f4ac40b3e56249cb5cd233ea Mon Sep 17 00:00:00 2001 From: Dale McDiarmid Date: Fri, 27 Nov 2015 18:12:59 +0000 Subject: [PATCH] Templates everything + Systemd improvements+fixes + Notifications for restart + Idempotent checks for multiple runs --- handlers/main.yml | 4 + tasks/elasticsearch-config.yml | 155 +++------------- tasks/elasticsearch.yml | 9 +- templates/elasticsearch.j2 | 84 +++++++++ templates/init/debian/elasticsearch.j2 | 237 +++++++++++++++++++++++++ templates/init/redhat/elasticsearch.j2 | 185 +++++++++++++++++++ templates/logging.yml.j2 | 68 +++++++ templates/systemd/elasticsearch.j2 | 50 ++++++ vars/main.yml | 3 +- 9 files changed, 663 insertions(+), 132 deletions(-) create mode 100644 handlers/main.yml create mode 100644 templates/elasticsearch.j2 create mode 100755 templates/init/debian/elasticsearch.j2 create mode 100755 templates/init/redhat/elasticsearch.j2 create mode 100644 templates/logging.yml.j2 create mode 100644 templates/systemd/elasticsearch.j2 diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..3501e12 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,4 @@ + +- name: restart elasticsearch + service: name={{instance_init_script | basename}} state=restarted enabled=yes + when: es_start_service and not elasticsearch_started.changed \ No newline at end of file diff --git a/tasks/elasticsearch-config.yml b/tasks/elasticsearch-config.yml index b525771..9f9d117 100644 --- a/tasks/elasticsearch-config.yml +++ b/tasks/elasticsearch-config.yml @@ -1,6 +1,7 @@ --- # Configure Elasticsearch Node +#For directories we also use the {{inventory_hostname}}-{{ es_instance_name }} - this helps if we have a shared SAN. - set_fact: pid_dir={{es_pid_dir}}/{{inventory_hostname}}-{{ es_instance_name }} @@ -10,151 +11,49 @@ - set_fact: work_dir={{ es_work_dir }}/{{inventory_hostname}}-{{ es_instance_name }} - -# Create an instance specific default file -- name: Copy Default File for Instance - command: creates={{instance_default_file}} cp "{{default_file}}" "{{instance_default_file}}" - when: instance_default_file != default_file - -# Create an instance specific init file -- name: Copy Init File for Instance - command: creates={{instance_init_script}} cp "{{init_script}}" "{{instance_init_script}}" - when: instance_init_script != init_script - -#Create Config directory -- name: Create Config Directory - file: path={{ instance_config_directory }} state=directory owner={{ es_user }} group={{ es_group }} - -#Copy the config template -- name: Copy configuration file - template: src=elasticsearch.yml.j2 dest={{instance_config_directory}}/elasticsearch.yml owner={{ es_user }} group={{ es_group }} mode=0644 force=yes - -# Apply changes to the default file for this instance -- name: Configure config directory - lineinfile: dest={{instance_default_file}} regexp="^CONF_DIR" insertafter="^#CONF_DIR" line="CONF_DIR={{ instance_config_directory }}" - register: elasticsearch_configure - -- name: Configure config file - lineinfile: dest={{instance_default_file}} regexp="^CONF_FILE" insertafter="^#CONF_FILE" line="CONF_FILE={{ instance_config_directory }}/elasticsearch.yml" - register: elasticsearch_configure - -- name: Configure memory in defaults - lineinfile: dest={{instance_default_file}} regexp="^ES_HEAP_SIZE" insertafter="^#ES_HEAP_SIZE" line="ES_HEAP_SIZE={{ es_heap_size }}" - when: es_heap_size is defined - register: elasticsearch_configure - -- name: Remove memory in defaults - lineinfile: dest={{instance_default_file}} regexp="^ES_HEAP_SIZE" line="ES_HEAP_SIZE=" state=absent - when: es_heap_size is not defined - register: elasticsearch_configure - -- debug: msg="{{elasticsearch_configure}}" - -#We only have to set these if they are specified. The start scripts will by default set this value. -- name: Configure max open files - lineinfile: dest={{instance_default_file}} regexp="^MAX_OPEN_FILES" insertafter="^#MAX_OPEN_FILES" line="MAX_OPEN_FILES={{ es_max_open_files }}" - when: es_max_open_files is defined - register: elasticsearch_configure - -#If the setting is not specified we remove it if its present. This will mean it reverts to the value in the init script. -- name: Remove max open files - lineinfile: dest={{instance_default_file}} regexp="^MAX_OPEN_FILES" line="MAX_OPEN_FILES=" state=absent - when: es_max_open_files is not defined - register: elasticsearch_configure - -#For directories we also use the {{inventory_hostname}}-{{ es_instance_name }} - this helps if we have a shared SAN. - -#Create PID directory +#Create required directories - name: Create PID Directory file: path={{ pid_dir }} state=directory owner={{ es_user }} group={{ es_group }} -- name: Configure PID directory - lineinfile: dest={{instance_default_file}} regexp="^PID_DIR" insertafter="^#PID_DIR" line="PID_DIR={{ pid_dir }}" - register: elasticsearch_configure - -#include the host name as potentially shared SAN - name: Create data dir file: state=directory path={{ data_dir }} owner={{ es_user }} group={{ es_group }} -- name: Configure data store - lineinfile: dest={{instance_default_file}} regexp="^DATA_DIR" insertafter="^#DATA_DIR" line="DATA_DIR={{ data_dir }}" - register: elasticsearch_configure - - name: Create work dir file: state=directory path={{ work_dir }} owner={{ es_user }} group={{ es_group }} -- name: Configure work directory - lineinfile: dest={{instance_default_file}} regexp="^WORK_DIR" insertafter="^#WORK_DIR" line="WORK_DIR={{ work_dir }}" - register: elasticsearch_configure - - name: Create log dir file: state=directory path={{ log_dir }} owner={{ es_user }} group={{ es_group }} -- name: Configure log directory - lineinfile: dest={{instance_default_file}} regexp="^LOG_DIR" insertafter="^#LOG_DIR" line="LOG_DIR={{ log_dir }}" - register: elasticsearch_configure +- name: Create Config Directory + file: path={{ instance_config_directory }} state=directory owner={{ es_user }} group={{ es_group }} -#required so that the ES_HOME does not change between instances +#Copy the config template +- name: Copy Configuration File + template: src=elasticsearch.yml.j2 dest={{instance_config_directory}}/elasticsearch.yml owner={{ es_user }} group={{ es_group }} mode=0644 force=yes + notify: restart elasticsearch -- name: Configure elasticsearch home - lineinfile: dest={{instance_default_file}} regexp="^ES_HOME" insertafter="^#ES_HOME" line="ES_HOME={{es_home}}" - register: elasticsearch_configure +#Copy the instance specific default file +- name: Copy Default File for Instance + template: src=elasticsearch.j2 dest={{instance_default_file}} mode=0644 force=yes + notify: restart elasticsearch -- name: Configure elasticsearch user - lineinfile: dest={{instance_default_file}} regexp="^ES_USER" insertafter="^#ES_USER" line="ES_USER={{ es_user }}" - when: es_user is defined - register: elasticsearch_configure - -- name: Configure elasticsearch group - lineinfile: dest={{instance_default_file}} regexp="^ES_GROUP" insertafter="^#ES_GROUP" line="ES_GROUP={{ es_group }}" - when: es_group is defined - register: elasticsearch_configure - -#Apply changes to init script - NAME can be changed in Debian start script -- name: Name Node in Init Script - lineinfile: dest={{instance_init_script}} regexp="^NAME" line="NAME={{es_instance_name}}_{{default_file | basename}}" - register: elasticsearch_configure +#Copy the instance specific init file +- name: Copy Debian Init File for Instance + template: src=init/debian/elasticsearch.j2 dest={{instance_init_script}} mode=0755 force=yes when: ansible_os_family == 'Debian' -- debug: msg="For {{ instance_init_script }} using default {{ instance_default_file }}" +#Copy the instance specific init file +- name: Copy Redhat Init File for Instance + template: src=init/redhat/elasticsearch.j2 dest={{instance_init_script}} mode=0755 force=yes + when: ansible_os_family == 'Redhat' -- name: Environment in Init Script - lineinfile: dest={{instance_init_script}} regexp="^ES_ENV_FILE" line="ES_ENV_FILE={{instance_default_file}}" - register: elasticsearch_configure - when: ansible_os_family == 'RedHat' +#Copy the systemd specific file if systemd is installed +- name: Copy Systemd File for Instance + template: src=systemd/elasticsearch.j2 dest={{instance_sysd_script}} mode=0644 force=yes + when: systemd_service + notify: restart elasticsearch #Copy the logging.yml -- name: Copy Logging.yml for Instance - command: creates={{instance_config_directory}}/logging.yml cp "{{ es_conf_dir }}/logging.yml" "{{instance_config_directory}}/logging.yml" - when: instance_init_script != init_script - -- file: path={{instance_config_directory}}/logging.yml state=touch owner={{ es_user }} group={{ es_group }} - changed_when: false - -- set_fact: m_lock_enabled={{ es_config['bootstrap.mlockall'] is defined and es_config['bootstrap.mlockall'] == True }} - -#Configure mlock if enabled -- name: Configure mlock - lineinfile: dest={{instance_default_file}} regexp="^MAX_LOCKED_MEMORY" insertafter="^#MAX_LOCKED_MEMORY" line="MAX_LOCKED_MEMORY=unlimited" - when: m_lock_enabled - register: elasticsearch_configure - -#Removed mlock if not enabled and present -- name: Remove mlock - lineinfile: dest={{instance_default_file}} regexp="^MAX_LOCKED_MEMORY" line="MAX_LOCKED_MEMORY=unlimited" state=absent - when: not m_lock_enabled - -- stat: path=/usr/lib/systemd/system/elasticsearch.service - register: systemd_service - when: m_lock_enabled - -#Change for systemd setting to ensure mlock is achieved -- name: Configure systemd - lineinfile: dest=/usr/lib/systemd/system/elasticsearch.service regexp="^LimitMEMLOCK" insertafter="^#LimitMEMLOCK" line="LimitMEMLOCK=infinity" - when: m_lock_enabled and systemd_service.stat.exists == True - register: elasticsearch_configure - -#Remove setting if mlock is disabled -- name: Remove systemd - lineinfile: dest=/usr/lib/systemd/system/elasticsearch.service regexp="^LimitMEMLOCK" line="LimitMEMLOCK=infinity" state=absent - when: not m_lock_enabled \ No newline at end of file +- name: Copy Systemd File for Instance + template: src=logging.yml.j2 dest={{instance_config_directory}}/logging.yml owner={{ es_user }} group={{ es_group }} mode=0644 force=yes + notify: restart elasticsearch \ No newline at end of file diff --git a/tasks/elasticsearch.yml b/tasks/elasticsearch.yml index 8e54a8a..e626ddc 100644 --- a/tasks/elasticsearch.yml +++ b/tasks/elasticsearch.yml @@ -3,6 +3,11 @@ - set_fact: instance_default_file={{default_file | dirname}}/{{es_instance_name}}_{{default_file | basename}} - set_fact: instance_init_script={{init_script | dirname }}/{{es_instance_name}}_{{init_script | basename}} - set_fact: instance_config_directory={{ es_conf_dir }}/{{es_instance_name}} +- stat: path={{sysd_script}} + register: systemd_service +- set_fact: instance_sysd_script={{sysd_script | dirname }}/{{es_instance_name}}_{{sysd_script | basename}} + when: systemd_service +- set_fact: m_lock_enabled={{ es_config['bootstrap.mlockall'] is defined and es_config['bootstrap.mlockall'] == True }} - debug: msg="Node configuration {{ es_config }} " @@ -28,6 +33,4 @@ - name: Restart elasticsearch service if new version installed service: name={{instance_init_script | basename}} state=restarted enabled=yes - when: es_start_service and - ( elasticsearch_install.changed or elasticsearch_configure.changed ) - and not elasticsearch_started.changed + when: es_start_service and elasticsearch_install.changed and not elasticsearch_started.changed \ No newline at end of file diff --git a/templates/elasticsearch.j2 b/templates/elasticsearch.j2 new file mode 100644 index 0000000..9c577b5 --- /dev/null +++ b/templates/elasticsearch.j2 @@ -0,0 +1,84 @@ +################################ +# Elasticsearch +################################ + +# Elasticsearch home directory +ES_HOME={{es_home}} + +# Elasticsearch configuration directory +CONF_DIR={{instance_config_directory}} + +# Elasticsearch configuration file +CONF_FILE=$CONF_DIR/elasticsearch.yml + +# Elasticsearch data directory +DATA_DIR={{data_dir}} + +# Elasticsearch logs directory +LOG_DIR={{log_dir}} + +# Elasticsearch work directory +WORK_DIR={{work_dir}} + +# Elasticsearch PID directory +PID_DIR={{pid_dir}} + +# Heap size defaults to 256m min, 1g max +# Set ES_HEAP_SIZE to 50% of available RAM, but no more than 31g +{% if es_heap_size is defined %} +ES_HEAP_SIZE={{es_heap_size}} +{% endif %} + +# Heap new generation +#ES_HEAP_NEWSIZE= + +# Maximum direct memory +#ES_DIRECT_SIZE= + +# Additional Java OPTS +#ES_JAVA_OPTS= + +# Configure restart on package upgrade (true, every other setting will lead to not restarting) +#ES_RESTART_ON_UPGRADE=true + +# Path to the GC log file +#ES_GC_LOG_FILE=/var/log/elasticsearch/gc.log + +################################ +# Elasticsearch service +################################ + +# SysV init.d +# +# When executing the init script, this user will be used to run the elasticsearch service. +# The default value is 'elasticsearch' and is declared in the init.d file. +# Note that this setting is only used by the init script. If changed, make sure that +# the configured user can read and write into the data, work, plugins and log directories. +# For systemd service, the user is usually configured in file /usr/lib/systemd/system/elasticsearch.service +ES_USER={{es_user}} +ES_GROUP={{es_group}} + +################################ +# System properties +################################ + +# Specifies the maximum file descriptor number that can be opened by this process +# When using Systemd, this setting is ignored and the LimitNOFILE defined in +# /usr/lib/systemd/system/elasticsearch.service takes precedence +{% if es_max_open_files is defined %} +MAX_OPEN_FILES={{es_max_open_files}} +{% endif %} + +# The maximum number of bytes of memory that may be locked into RAM +# Set to "unlimited" if you use the 'bootstrap.mlockall: true' option +# in elasticsearch.yml (ES_HEAP_SIZE must also be set). +# When using Systemd, the LimitMEMLOCK property must be set +# in /usr/lib/systemd/system/elasticsearch.service +{% if m_lock_enabled %} +MAX_LOCKED_MEMORY=unlimited +{% endif %} + +# Maximum number of VMA (Virtual Memory Areas) a process can own +# When using Systemd, this setting is ignored and the 'vm.max_map_count' +# property is set at boot time in /usr/lib/sysctl.d/elasticsearch.conf +#MAX_MAP_COUNT=262144 diff --git a/templates/init/debian/elasticsearch.j2 b/templates/init/debian/elasticsearch.j2 new file mode 100755 index 0000000..b9e65c7 --- /dev/null +++ b/templates/init/debian/elasticsearch.j2 @@ -0,0 +1,237 @@ +#!/bin/sh +# +# /etc/init.d/elasticsearch -- startup script for Elasticsearch +# +# Written by Miquel van Smoorenburg . +# Modified for Debian GNU/Linux by Ian Murdock . +# Modified for Tomcat by Stefan Gybas . +# Modified for Tomcat6 by Thierry Carrez . +# Additional improvements by Jason Brittain . +# Modified by Nicolas Huray for Elasticsearch . +# +### BEGIN INIT INFO +# Provides: elasticsearch +# Required-Start: $network $remote_fs $named +# Required-Stop: $network $remote_fs $named +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Starts elasticsearch +# Description: Starts elasticsearch using start-stop-daemon +### END INIT INFO + +PATH=/bin:/usr/bin:/sbin:/usr/sbin +NAME={{es_instance_name}}_{{default_file | basename}} +{% if es_config['node.name'] is defined %} +DESC="Elasticsearch Server - {{es_config['node.name']}}" +{% else %} +DESC="Elasticsearch Server - {{es_instance_name}}" +{% endif %} +DEFAULT=/etc/default/$NAME + +if [ `id -u` -ne 0 ]; then + echo "You need root privileges to run this script" + exit 1 +fi + + +. /lib/lsb/init-functions + +if [ -r /etc/default/rcS ]; then + . /etc/default/rcS +fi + +# The following variables can be overwritten in $DEFAULT + +# Run Elasticsearch as this user ID and group ID +ES_USER=elasticsearch +ES_GROUP=elasticsearch + +# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT) +JDK_DIRS="/usr/lib/jvm/java-8-oracle/ /usr/lib/jvm/j2sdk1.8-oracle/ /usr/lib/jvm/jdk-7-oracle-x64 /usr/lib/jvm/java-7-oracle /usr/lib/jvm/j2sdk1.7-oracle/ /usr/lib/jvm/java-7-openjdk /usr/lib/jvm/java-7-openjdk-amd64/ /usr/lib/jvm/java-7-openjdk-armhf /usr/lib/jvm/java-7-openjdk-i386/ /usr/lib/jvm/default-java" + +# Look for the right JVM to use +for jdir in $JDK_DIRS; do + if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then + JAVA_HOME="$jdir" + fi +done +export JAVA_HOME + +# Directory where the Elasticsearch binary distribution resides +ES_HOME=/usr/share/$NAME + +# Heap size defaults to 256m min, 1g max +# Set ES_HEAP_SIZE to 50% of available RAM, but no more than 31g +#ES_HEAP_SIZE=2g + +# Heap new generation +#ES_HEAP_NEWSIZE= + +# max direct memory +#ES_DIRECT_SIZE= + +# Additional Java OPTS +#ES_JAVA_OPTS= + +# Maximum number of open files +MAX_OPEN_FILES=65535 + +# Maximum amount of locked memory +#MAX_LOCKED_MEMORY= + +# Elasticsearch log directory +LOG_DIR=/var/log/$NAME + +# Elasticsearch data directory +DATA_DIR=/var/lib/$NAME + +# Elasticsearch work directory +WORK_DIR=/tmp/$NAME + +# Elasticsearch configuration directory +CONF_DIR=/etc/$NAME + +# Elasticsearch configuration file (elasticsearch.yml) +CONF_FILE=$CONF_DIR/elasticsearch.yml + +# Maximum number of VMA (Virtual Memory Areas) a process can own +MAX_MAP_COUNT=262144 + +# Path to the GC log file +#ES_GC_LOG_FILE=/var/log/elasticsearch/gc.log + +# Elasticsearch PID file directory +PID_DIR="/var/run/elasticsearch" + +# End of variables that can be overwritten in $DEFAULT + +# overwrite settings from default file +if [ -f "$DEFAULT" ]; then + . "$DEFAULT" +fi + +# Define other required variables +PID_FILE="$PID_DIR/$NAME.pid" +DAEMON=$ES_HOME/bin/elasticsearch +DAEMON_OPTS="-d -p $PID_FILE --default.config=$CONF_FILE --default.path.home=$ES_HOME --default.path.logs=$LOG_DIR --default.path.data=$DATA_DIR --default.path.work=$WORK_DIR --default.path.conf=$CONF_DIR" + +export ES_HEAP_SIZE +export ES_HEAP_NEWSIZE +export ES_DIRECT_SIZE +export ES_JAVA_OPTS + +# Check DAEMON exists +test -x $DAEMON || exit 0 + +checkJava() { + if [ -x "$JAVA_HOME/bin/java" ]; then + JAVA="$JAVA_HOME/bin/java" + else + JAVA=`which java` + fi + + if [ ! -x "$JAVA" ]; then + echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME" + exit 1 + fi +} + +case "$1" in + start) + checkJava + + if [ -n "$MAX_LOCKED_MEMORY" -a -z "$ES_HEAP_SIZE" ]; then + log_failure_msg "MAX_LOCKED_MEMORY is set - ES_HEAP_SIZE must also be set" + exit 1 + fi + + log_daemon_msg "Starting $DESC" + + pid=`pidofproc -p $PID_FILE elasticsearch` + if [ -n "$pid" ] ; then + log_begin_msg "Already running." + log_end_msg 0 + exit 0 + fi + + # Prepare environment + mkdir -p "$LOG_DIR" "$DATA_DIR" "$WORK_DIR" && chown "$ES_USER":"$ES_GROUP" "$LOG_DIR" "$DATA_DIR" "$WORK_DIR" + + # Ensure that the PID_DIR exists (it is cleaned at OS startup time) + if [ -n "$PID_DIR" ] && [ ! -e "$PID_DIR" ]; then + mkdir -p "$PID_DIR" && chown "$ES_USER":"$ES_GROUP" "$PID_DIR" + fi + if [ -n "$PID_FILE" ] && [ ! -e "$PID_FILE" ]; then + touch "$PID_FILE" && chown "$ES_USER":"$ES_GROUP" "$PID_FILE" + fi + + if [ -n "$MAX_OPEN_FILES" ]; then + ulimit -n $MAX_OPEN_FILES + fi + + if [ -n "$MAX_LOCKED_MEMORY" ]; then + ulimit -l $MAX_LOCKED_MEMORY + fi + + if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then + sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT + fi + + # Start Daemon + start-stop-daemon --start -b --user "$ES_USER" -c "$ES_USER" --pidfile "$PID_FILE" --exec $DAEMON -- $DAEMON_OPTS + return=$? + if [ $return -eq 0 ] + then + i=0 + timeout=10 + # Wait for the process to be properly started before exiting + until { cat "$PID_FILE" | xargs kill -0; } >/dev/null 2>&1 + do + sleep 1 + i=$(($i + 1)) + if [ $i -gt $timeout ]; then + log_end_msg 1 + exit 1 + fi + done + else + log_end_msg $return + fi + ;; + stop) + log_daemon_msg "Stopping $DESC" + + if [ -f "$PID_FILE" ]; then + start-stop-daemon --stop --pidfile "$PID_FILE" \ + --user "$ES_USER" \ + --retry=TERM/20/KILL/5 >/dev/null + if [ $? -eq 1 ]; then + log_progress_msg "$DESC is not running but pid file exists, cleaning up" + elif [ $? -eq 3 ]; then + PID="`cat $PID_FILE`" + log_failure_msg "Failed to stop $DESC (pid $PID)" + exit 1 + fi + rm -f "$PID_FILE" + else + log_progress_msg "(not running)" + fi + log_end_msg 0 + ;; + status) + status_of_proc -p $PID_FILE elasticsearch elasticsearch && exit 0 || exit $? + ;; + restart|force-reload) + if [ -f "$PID_FILE" ]; then + $0 stop + sleep 1 + fi + $0 start + ;; + *) + log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" + exit 1 + ;; +esac + +exit 0 diff --git a/templates/init/redhat/elasticsearch.j2 b/templates/init/redhat/elasticsearch.j2 new file mode 100755 index 0000000..fab94b3 --- /dev/null +++ b/templates/init/redhat/elasticsearch.j2 @@ -0,0 +1,185 @@ +#!/bin/sh +# +# elasticsearch +# +# chkconfig: 2345 80 20 +# description: Starts and stops a single elasticsearch instance on this system +# + +### BEGIN INIT INFO +# Provides: Elasticsearch +# Required-Start: $network $named +# Required-Stop: $network $named +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: This service manages the elasticsearch daemon +# Description: Elasticsearch is a very scalable, schema-free and high-performance search solution supporting multi-tenancy and near realtime search. +### END INIT INFO + +# +# init.d / servicectl compatibility (openSUSE) +# +if [ -f /etc/rc.status ]; then + . /etc/rc.status + rc_reset +fi + +# +# Source function library. +# +if [ -f /etc/rc.d/init.d/functions ]; then + . /etc/rc.d/init.d/functions +fi + +# Sets the default values for elasticsearch variables used in this script +ES_USER="elasticsearch" +ES_GROUP="elasticsearch" +ES_HOME="/usr/share/elasticsearch" +MAX_OPEN_FILES=65535 +MAX_MAP_COUNT=262144 +LOG_DIR="/var/log/elasticsearch" +DATA_DIR="/var/lib/elasticsearch" +WORK_DIR="/tmp/elasticsearch" +CONF_DIR="/etc/elasticsearch" +CONF_FILE="/etc/elasticsearch/elasticsearch.yml" +PID_DIR="/var/run/elasticsearch" + +# Source the default env file +ES_ENV_FILE="{{instance_default_file}}" +if [ -f "$ES_ENV_FILE" ]; then + . "$ES_ENV_FILE" +fi + +exec="$ES_HOME/bin/elasticsearch" +prog="elasticsearch" +pidfile="$PID_DIR/${prog}.pid" + +export ES_HEAP_SIZE +export ES_HEAP_NEWSIZE +export ES_DIRECT_SIZE +export ES_JAVA_OPTS +export JAVA_HOME + +lockfile=/var/lock/subsys/$prog + +# backwards compatibility for old config sysconfig files, pre 0.90.1 +if [ -n $USER ] && [ -z $ES_USER ] ; then + ES_USER=$USER +fi + +checkJava() { + if [ -x "$JAVA_HOME/bin/java" ]; then + JAVA="$JAVA_HOME/bin/java" + else + JAVA=`which java` + fi + + if [ ! -x "$JAVA" ]; then + echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME" + exit 1 + fi +} + +start() { + checkJava + [ -x $exec ] || exit 5 + [ -f $CONF_FILE ] || exit 6 + if [ -n "$MAX_LOCKED_MEMORY" -a -z "$ES_HEAP_SIZE" ]; then + echo "MAX_LOCKED_MEMORY is set - ES_HEAP_SIZE must also be set" + return 7 + fi + if [ -n "$MAX_OPEN_FILES" ]; then + ulimit -n $MAX_OPEN_FILES + fi + if [ -n "$MAX_LOCKED_MEMORY" ]; then + ulimit -l $MAX_LOCKED_MEMORY + fi + if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then + sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT + fi + if [ -n "$WORK_DIR" ]; then + mkdir -p "$WORK_DIR" + chown "$ES_USER":"$ES_GROUP" "$WORK_DIR" + fi + + # Ensure that the PID_DIR exists (it is cleaned at OS startup time) + if [ -n "$PID_DIR" ] && [ ! -e "$PID_DIR" ]; then + mkdir -p "$PID_DIR" && chown "$ES_USER":"$ES_GROUP" "$PID_DIR" + fi + if [ -n "$pidfile" ] && [ ! -e "$pidfile" ]; then + touch "$pidfile" && chown "$ES_USER":"$ES_GROUP" "$pidfile" + fi + + echo -n $"Starting $prog: " + # if not running, start it up here, usually something like "daemon $exec" + daemon --user $ES_USER --pidfile $pidfile $exec -p $pidfile -d -Des.default.path.home=$ES_HOME -Des.default.path.logs=$LOG_DIR -Des.default.path.data=$DATA_DIR -Des.default.path.work=$WORK_DIR -Des.default.path.conf=$CONF_DIR + retval=$? + echo + [ $retval -eq 0 ] && touch $lockfile + return $retval +} + +stop() { + echo -n $"Stopping $prog: " + # stop it here, often "killproc $prog" + killproc -p $pidfile -d 20 $prog + retval=$? + echo + [ $retval -eq 0 ] && rm -f $lockfile + return $retval +} + +restart() { + stop + start +} + +reload() { + restart +} + +force_reload() { + restart +} + +rh_status() { + # run checks to determine if the service is running or use generic status + status -p $pidfile $prog +} + +rh_status_q() { + rh_status >/dev/null 2>&1 +} + + +case "$1" in + start) + rh_status_q && exit 0 + $1 + ;; + stop) + rh_status_q || exit 0 + $1 + ;; + restart) + $1 + ;; + reload) + rh_status_q || exit 7 + $1 + ;; + force-reload) + force_reload + ;; + status) + rh_status + ;; + condrestart|try-restart) + rh_status_q || exit 0 + restart + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" + exit 2 +esac +exit $? diff --git a/templates/logging.yml.j2 b/templates/logging.yml.j2 new file mode 100644 index 0000000..538c9bd --- /dev/null +++ b/templates/logging.yml.j2 @@ -0,0 +1,68 @@ +# you can override this using by setting a system property, for example -Des.logger.level=DEBUG +es.logger.level: INFO +rootLogger: ${es.logger.level}, console, file +logger: + # log action execution errors for easier debugging + action: DEBUG + # reduce the logging for aws, too much is logged under the default INFO + com.amazonaws: WARN + org.apache.http: INFO + + # gateway + #gateway: DEBUG + #index.gateway: DEBUG + + # peer shard recovery + #indices.recovery: DEBUG + + # discovery + #discovery: TRACE + + index.search.slowlog: TRACE, index_search_slow_log_file + index.indexing.slowlog: TRACE, index_indexing_slow_log_file + +additivity: + index.search.slowlog: false + index.indexing.slowlog: false + +appender: + console: + type: console + layout: + type: consolePattern + conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n" + + file: + type: dailyRollingFile + file: ${path.logs}/${cluster.name}.log + datePattern: "'.'yyyy-MM-dd" + layout: + type: pattern + conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n" + + # Use the following log4j-extras RollingFileAppender to enable gzip compression of log files. + # For more information see https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html + #file: + #type: extrasRollingFile + #file: ${path.logs}/${cluster.name}.log + #rollingPolicy: timeBased + #rollingPolicy.FileNamePattern: ${path.logs}/${cluster.name}.log.%d{yyyy-MM-dd}.gz + #layout: + #type: pattern + #conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n" + + index_search_slow_log_file: + type: dailyRollingFile + file: ${path.logs}/${cluster.name}_index_search_slowlog.log + datePattern: "'.'yyyy-MM-dd" + layout: + type: pattern + conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n" + + index_indexing_slow_log_file: + type: dailyRollingFile + file: ${path.logs}/${cluster.name}_index_indexing_slowlog.log + datePattern: "'.'yyyy-MM-dd" + layout: + type: pattern + conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n" \ No newline at end of file diff --git a/templates/systemd/elasticsearch.j2 b/templates/systemd/elasticsearch.j2 new file mode 100644 index 0000000..5e19d0e --- /dev/null +++ b/templates/systemd/elasticsearch.j2 @@ -0,0 +1,50 @@ +[Unit] +Description=Elasticsearch +Documentation=http://www.elastic.co +Wants=network-online.target +After=network-online.target + +[Service] +Environment=ES_HOME={{es_home}} +Environment=CONF_DIR={{instance_config_directory}} +Environment=CONF_FILE={{instance_config_directory}}/elasticsearch.yml +Environment=DATA_DIR={{data_dir}} +Environment=LOG_DIR={{log_dir}} +Environment=PID_DIR={{pid_dir}} +EnvironmentFile=-{{instance_default_file}} + +User={{es_user}} +Group={{es_group}} + +ExecStart={{es_home}}/bin/elasticsearch \ + -Des.pidfile=$PID_DIR/elasticsearch.pid \ + -Des.default.path.home=$ES_HOME \ + -Des.default.path.logs=$LOG_DIR \ + -Des.default.path.data=$DATA_DIR \ + -Des.default.config=$CONF_FILE \ + -Des.default.path.conf=$CONF_DIR + +# Connects standard output to /dev/null +StandardOutput=null + +# Connects standard error to journal +StandardError=journal + +# When a JVM receives a SIGTERM signal it exits with code 143 +SuccessExitStatus=143 + +# Specifies the maximum file descriptor number that can be opened by this process +LimitNOFILE=65535 + +# Specifies the maximum number of bytes of memory that may be locked into RAM +# Set to "infinity" if you use the 'bootstrap.mlockall: true' option +# in elasticsearch.yml and 'MAX_LOCKED_MEMORY=unlimited' in {{instance_default_file}} +{% if m_lock_enabled %} +LimitMEMLOCK=infinity +{% endif %} + +# Shutdown delay in seconds, before process is tried to be killed with KILL (if configured) +TimeoutStopSec=20 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index 1c232fa..b2f74d9 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,4 @@ --- es_package_url: "https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch" -es_conf_dir: "/etc/elasticsearch" \ No newline at end of file +es_conf_dir: "/etc/elasticsearch" +sysd_script: "/usr/lib/systemd/system/elasticsearch.service" \ No newline at end of file