diff --git a/TESTING.asciidoc b/TESTING.asciidoc index 1bcbce4ca47..aa14bc39e7b 100644 --- a/TESTING.asciidoc +++ b/TESTING.asciidoc @@ -298,3 +298,23 @@ You can also skip this by using the "dev" profile: --------------------------------------------------------------------------- mvn test -Pdev --------------------------------------------------------------------------- + +== Testing scripts + +Shell scripts can be tested with the Bash Automate Testing System tool available +at https://github.com/sstephenson/bats. Once the tool is installed, you can +execute a .bats test file with the following command: + +--------------------------------------------------------------------------- +bats test_file.bats +--------------------------------------------------------------------------- + +When executing the test files located in the `/packaging/scripts` folder, +it's possible to add the flag `ES_CLEAN_BEFORE_TEST=true` to clean the test +environment before the tests are executed: + +--------------------------------------------------------------------------- +ES_CLEAN_BEFORE_TEST=true bats 30_deb_package.bats +--------------------------------------------------------------------------- + + diff --git a/pom.xml b/pom.xml index 1418049897d..005e66b836c 100644 --- a/pom.xml +++ b/pom.xml @@ -50,12 +50,15 @@ /usr/share/elasticsearch + /usr/share/elasticsearch/bin /etc/elasticsearch /var/lib/elasticsearch elasticsearch elasticsearch - /var/run/elasticsearch + /tmp/elasticsearch /var/log/elasticsearch + ${packaging.elasticsearch.home.dir}/plugins + /var/run/elasticsearch @@ -1068,7 +1071,7 @@ 1.4 ${project.build.directory}/releases/${project.artifactId}-${project.version}.deb - ${project.build.directory}/generated-packaging/deb/control + ${project.build.directory}/generated-packaging/deb/scripts @@ -1078,31 +1081,44 @@ + - ${project.basedir}/ - *.txt, *.textile - LICENSE.txt, .DS_Store - directory - - perm - ${packaging.elasticsearch.home.dir} - root - root - - - - ${project.build.directory}/generated-packaging/deb/bin directory - *.bat, .DS_Store, *.exe + elasticsearch,elasticsearch.in.sh,plugin perm - ${packaging.elasticsearch.home.dir}/bin + ${packaging.elasticsearch.bin.dir} 755 root root + + + ${project.basedir}/config + directory + .DS_Store + + perm + ${packaging.elasticsearch.conf.dir} + root + root + + + + + ${project.build.directory}/generated-packaging/deb/env/elasticsearch + file + + perm + /etc/default + 644 + root + root + + + ${project.build.directory}/ ${project.build.finalName}.jar @@ -1136,17 +1152,7 @@ root - - ${project.build.directory}/generated-packaging/deb/default/ - directory - .DS_Store - - perm - /etc/default - root - root - - + ${project.build.directory}/generated-packaging/deb/init.d/ directory @@ -1159,22 +1165,13 @@ root + ${project.build.directory}/generated-packaging/deb/systemd/elasticsearch.service /usr/lib/systemd/system/elasticsearch.service file - - ${project.basedir}/config - directory - .DS_Store - - perm - ${packaging.elasticsearch.conf.dir} - root - root - - + ${project.build.directory}/generated-packaging/deb/lintian directory @@ -1186,6 +1183,20 @@ root + + + ${project.basedir}/ + *.txt, *.textile + LICENSE.txt, .DS_Store + directory + + perm + ${packaging.elasticsearch.home.dir} + root + root + + + ${project.build.directory}/generated-packaging/deb/copyright /usr/share/doc/elasticsearch/copyright @@ -1198,6 +1209,8 @@ ${packaging.elasticsearch.data.dir} ${packaging.elasticsearch.log.dir} ${packaging.elasticsearch.work.dir} + ${packaging.elasticsearch.plugins.dir} + ${packaging.elasticsearch.pid.dir} perm @@ -1233,40 +1246,101 @@ root root + + + ${packaging.elasticsearch.bin.dir}/ + 755 + + + ${project.build.directory}/generated-packaging/rpm/bin + + elasticsearch + elasticsearch.in.sh + plugin + + + + + ${packaging.elasticsearch.conf.dir}/ noreplace - config/ + ${project.basedir}/config/ *.yml + /etc/sysconfig/ + false noreplace - ${project.build.directory}/generated-packaging/rpm/sysconfig + ${project.build.directory}/generated-packaging/rpm/env/ elasticsearch + - /etc/rc.d/init.d/ + ${packaging.elasticsearch.home.dir}/lib + + + target/lib/ + + lucene* + *log4j* + jna* + spatial4j* + jts* + groovy* + antlr-runtime* + asm* + + + + ${project.build.directory}/ + + ${project.build.finalName}.jar + + + + + + ${packaging.elasticsearch.home.dir}/lib/sigar + + + lib/sigar + + sigar*.jar + libsigar-*-linux.* + + + + + + + /etc/init.d + false 755 true - ${project.build.directory}/generated-packaging/rpm/init.d/elasticsearch + ${project.build.directory}/generated-packaging/rpm/init.d + + elasticsearch + + /usr/lib/systemd/system/ 755 @@ -1305,74 +1379,7 @@ - - ${packaging.elasticsearch.work.dir}/ - 755 - ${packaging.elasticsearch.user} - ${packaging.elasticsearch.group} - - - ${packaging.elasticsearch.data.dir}/ - 755 - ${packaging.elasticsearch.user} - ${packaging.elasticsearch.group} - - - ${packaging.elasticsearch.log.dir}/ - 755 - ${packaging.elasticsearch.user} - ${packaging.elasticsearch.group} - - - ${packaging.elasticsearch.home.dir}/bin/ - 755 - - - ${project.build.directory}/generated-packaging/rpm/bin - - elasticsearch - elasticsearch.in.sh - plugin - - - - - - ${packaging.elasticsearch.home.dir}/lib - - - target/lib/ - - lucene* - *log4j* - jna* - spatial4j* - jts* - groovy* - antlr-runtime* - asm* - - - - ${project.build.directory}/ - - ${project.build.finalName}.jar - - - - - - ${packaging.elasticsearch.home.dir}/lib/sigar - - - lib/sigar - - sigar*.jar - libsigar-*-linux.* - - - - + ${packaging.elasticsearch.home.dir} @@ -1405,21 +1412,33 @@ ${packaging.elasticsearch.user} ${packaging.elasticsearch.group} + + ${packaging.elasticsearch.plugins.dir} + 755 + ${packaging.elasticsearch.user} + ${packaging.elasticsearch.group} + + + ${packaging.elasticsearch.pid.dir} + 755 + ${packaging.elasticsearch.user} + ${packaging.elasticsearch.group} + - ${project.build.directory}/generated-packaging/rpm/scripts/preinstall + ${project.build.directory}/generated-packaging/rpm/scripts/preinst utf-8 - ${project.build.directory}/generated-packaging/rpm/scripts/postinstall + ${project.build.directory}/generated-packaging/rpm/scripts/postinst utf-8 - ${project.build.directory}/generated-packaging/rpm/scripts/preremove + ${project.build.directory}/generated-packaging/rpm/scripts/prerm utf-8 - ${project.build.directory}/generated-packaging/rpm/scripts/postremove + ${project.build.directory}/generated-packaging/rpm/scripts/postrm utf-8 diff --git a/src/packaging/common/env/elasticsearch b/src/packaging/common/env/elasticsearch new file mode 100644 index 00000000000..8b6d88ac4ae --- /dev/null +++ b/src/packaging/common/env/elasticsearch @@ -0,0 +1,81 @@ +################################ +# Elasticsearch +################################ + +# Elasticsearch home directory +ES_HOME=${packaging.elasticsearch.home.dir} + +# Elasticsearch configuration directory +CONF_DIR=${packaging.elasticsearch.conf.dir} + +# Elasticsearch configuration file +CONF_FILE=$CONF_DIR/elasticsearch.yml + +# Elasticsearch data directory +DATA_DIR=${packaging.elasticsearch.data.dir} + +# Elasticsearch logs directory +LOG_DIR=${packaging.elasticsearch.log.dir} + +# Elasticsearch work directory +WORK_DIR=${packaging.elasticsearch.work.dir} + +# Elasticsearch plugins directory +PLUGINS_DIR=${packaging.elasticsearch.plugins.dir} + +# Elasticsearch PID directory +PID_DIR=${packaging.elasticsearch.pid.dir} + +# Heap size defaults to ${packaging.elasticsearch.heap.min} min, ${packaging.elasticsearch.heap.max} 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= + +# 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=${packaging.elasticsearch.log.dir}/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 '${packaging.elasticsearch.user}' 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 ${packaging.elasticsearch.systemd.dir}/elasticsearch.service +ES_USER=${packaging.elasticsearch.user} +ES_GROUP=${packaging.elasticsearch.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 +# ${packaging.elasticsearch.systemd.dir}/elasticsearch.service takes precedence +MAX_OPEN_FILES=${packaging.os.max.open.files} + +# 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 ${packaging.elasticsearch.systemd.dir}/elasticsearch.service +#MAX_LOCKED_MEMORY=unlimited + +# 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 ${packaging.elasticsearch.systemd.sysctl.dir}/elasticsearch.conf +MAX_MAP_COUNT=${packaging.os.max.map.count} diff --git a/src/packaging/common/packaging.properties b/src/packaging/common/packaging.properties index 7c9293fcb35..6693dc224ea 100644 --- a/src/packaging/common/packaging.properties +++ b/src/packaging/common/packaging.properties @@ -3,9 +3,22 @@ # Properties defined here can be overridden with specific settings, # like in rpm/packaging.properties and deb/packaging.properties. +# Environment file +packaging.env.file= + # Default values for min/max heap memory allocated to elasticsearch java process packaging.elasticsearch.heap.min=256m packaging.elasticsearch.heap.max=1g +# Specifies the maximum file descriptor number +packaging.os.max.open.files=65535 + +# Maximum number of VMA (Virtual Memory Areas) a process can own +packaging.os.max.map.count=262144 + # Simple marker to check that properties are correctly overridden packaging.type=tar.gz,gzip + +# Custom header for package scripts +packaging.scripts.header= +packaging.scripts.footer= diff --git a/src/packaging/common/scripts/postinst b/src/packaging/common/scripts/postinst new file mode 100644 index 00000000000..93432c3dc34 --- /dev/null +++ b/src/packaging/common/scripts/postinst @@ -0,0 +1,103 @@ +${packaging.scripts.header} + +# +# This script is executed in the post-installation phase +# +# On Debian, +# $1=configure : is set to 'configure' and if $2 is set, it is an upgrade +# +# On RedHat, +# $1=0 : indicates a removal +# $1=1 : indicates an upgrade + + + +# Source the default env file +ES_ENV_FILE="${packaging.env.file}" +if [ -f "$ES_ENV_FILE" ]; then + . "$ES_ENV_FILE" +else + echo "Unable to source environment file $ES_ENV_FILE" >&2 +fi + +IS_UPGRADE=false + +case "$1" in + + # Debian #################################################### + configure) + + # If $1=configure and $2 is set, this is an upgrade + if [ -n $2 ]; then + IS_UPGRADE=true + fi + ;; + abort-upgrade|abort-remove|abort-deconfigure) + ;; + + # RedHat #################################################### + 1) + # If $1=1 this is an install + IS_UPGRADE=false + ;; + 2) + # If $1=1 this is an upgrade + IS_UPGRADE=true + ;; + + *) + echo "post install script called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# Use default user and group +[ -z "$ES_USER" ] && ES_USER="${packaging.elasticsearch.user}" +[ -z "$ES_GROUP" ] && ES_GROUP="${packaging.elasticsearch.group}" + +if [ "x$IS_UPGRADE" != "xtrue" ]; then + if command -v systemctl >/dev/null; then + echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd" + echo " sudo systemctl daemon-reload" + echo " sudo systemctl enable elasticsearch.service" + echo "### You can start elasticsearch service by executing" + echo " sudo systemctl start elasticsearch.service" + + elif command -v chkconfig >/dev/null; then + echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using chkconfig" + echo " sudo chkconfig --add elasticsearch" + echo "### You can start elasticsearch service by executing" + echo " sudo service elasticsearch start" + + elif command -v update-rc.d >/dev/null; then + echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using chkconfig" + echo " sudo update-rc.d elasticsearch defaults 95 10" + echo "### You can start elasticsearch service by executing" + echo " sudo /etc/init.d/elasticsearch start" + fi +elif [ "$RESTART_ON_UPGRADE" = "true" ]; then + + echo -n "Restarting elasticsearch service..." + if command -v systemctl >/dev/null; then + systemctl daemon-reload + systemctl restart elasticsearch.service || true + + elif [ -x /etc/init.d/elasticsearch ]; then + if command -v invoke-rc.d >/dev/null; then + invoke-rc.d elasticsearch stop || true + invoke-rc.d elasticsearch start || true + else + /etc/init.d/elasticsearch restart || true + fi + + # older suse linux distributions do not ship with systemd + # but do not have an /etc/init.d/ directory + # this tries to start the elasticsearch service on these + # as well without failing this script + elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then + /etc/rc.d/init.d/elasticsearch restart || true + fi + echo " OK" +fi + +${packaging.scripts.footer} diff --git a/src/packaging/common/scripts/postrm b/src/packaging/common/scripts/postrm new file mode 100644 index 00000000000..8e4823380c8 --- /dev/null +++ b/src/packaging/common/scripts/postrm @@ -0,0 +1,129 @@ +${packaging.scripts.header} + +# +# This script is executed in the post-removal phase +# +# On Debian, +# $1=remove : indicates a removal +# $1=purge : indicates an upgrade +# +# On RedHat, +# $1=1 : indicates an new install +# $1=2 : indicates an upgrade + + + +SOURCE_ENV_FILE=true +REMOVE_DIRS=false +REMOVE_SERVICE=false +REMOVE_USER_AND_GROUP=false + +case "$1" in + + # Debian #################################################### + remove) + REMOVE_DIRS=true + REMOVE_SERVICE=true + ;; + + purge) + REMOVE_USER_AND_GROUP=true + SOURCE_ENV_FILE=false + ;; + failed-upgrade|abort-install|abort-upgrade|disappear|upgrade|disappear) + ;; + + # RedHat #################################################### + 0) + REMOVE_DIRS=true + REMOVE_SERVICE=true + REMOVE_USER_AND_GROUP=true + ;; + 2) + # If $1=1 this is an upgrade + IS_UPGRADE=true + ;; + + *) + echo "post remove script called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# Source the default env file +if [ "$SOURCE_ENV_FILE" = "true" ]; then + ES_ENV_FILE="${packaging.env.file}" + if [ -f "$ES_ENV_FILE" ]; then + . "$ES_ENV_FILE" + else + echo "Unable to source environment file $ES_ENV_FILE" >&2 + fi +fi + +if [ "$REMOVE_SERVICE" = "true" ]; then + if command -v systemctl >/dev/null; then + systemctl --no-reload disable elasticsearch.service > /dev/null 2>&1 || true + fi + + if command -v chkconfig >/dev/null; then + chkconfig --del elasticsearch 2> /dev/null || true + fi + + if command -v update-rc.d >/dev/null; then + update-rc.d elasticsearch remove >/dev/null || true + fi +fi + +if [ "$REMOVE_DIRS" = "true" ]; then + + [ -z "$LOG_DIR" ] && LOG_DIR="${packaging.elasticsearch.log.dir}" + + if [ -d "$LOG_DIR" ]; then + echo -n "Deleting log directory..." + rm -rf "$LOG_DIR" + echo " OK" + fi + + [ -z "$WORK_DIR" ] && WORK_DIR="${packaging.elasticsearch.work.dir}" + + if [ -d "$WORK_DIR" ]; then + echo -n "Deleting work directory..." + rm -rf "$WORK_DIR" + echo " OK" + fi + + [ -z "$PLUGINS_DIR" ] && PLUGINS_DIR="${packaging.elasticsearch.plugins.dir}" + + if [ -d "$PLUGINS_DIR" ]; then + echo -n "Deleting plugins directory..." + rm -rf "$PLUGINS_DIR" + echo " OK" + fi + + [ -z "$PID_DIR" ] && PID_DIR="${packaging.elasticsearch.pid.dir}" + + if [ -d "$PID_DIR" ]; then + echo -n "Deleting PID directory..." + rm -rf "$PID_DIR" + echo " OK" + fi + + [ -z "$DATA_DIR" ] && DATA_DIR="${packaging.elasticsearch.data.dir}" + + # Delete the data directory if and only if empty + if [ -d "$DATA_DIR" ]; then + rmdir --ignore-fail-on-non-empty "$DATA_DIR" + fi +fi + +if [ "$REMOVE_USER_AND_GROUP" = "true" ]; then + if id $ES_USER > /dev/null 2>&1 ; then + userdel "${packaging.elasticsearch.user}" + fi + + if getent group "$ES_GROUP" > /dev/null 2>&1 ; then + groupdel "${packaging.elasticsearch.group}" + fi +fi + +${packaging.scripts.footer} diff --git a/src/packaging/common/scripts/preinst b/src/packaging/common/scripts/preinst new file mode 100644 index 00000000000..f9ad39a11ff --- /dev/null +++ b/src/packaging/common/scripts/preinst @@ -0,0 +1,77 @@ +${packaging.scripts.header} + +# +# This script is executed in the pre-installation phase +# +# On Debian, +# $1=install : indicates an new install +# $1=upgrade : indicates an upgrade +# +# On RedHat, +# $1=1 : indicates an new install +# $1=2 : indicates an upgrade + + + +# Define default user and group +[ -z "$ES_USER" ] && ES_USER="${packaging.elasticsearch.user}" +[ -z "$ES_GROUP" ] && ES_GROUP="${packaging.elasticsearch.group}" + +case "$1" in + + # Debian #################################################### + install|upgrade) + + # Create elasticsearch group if not existing + if ! getent group "$ES_GROUP" > /dev/null 2>&1 ; then + echo -n "Creating $ES_GROUP group..." + addgroup --quiet --system "$ES_GROUP" + echo " OK" + fi + + # Create elasticsearch user if not existing + if ! id $ES_USER > /dev/null 2>&1 ; then + echo -n "Creating $ES_USER user..." + adduser --quiet \ + --system \ + --no-create-home \ + --ingroup "$ES_GROUP" \ + --disabled-password \ + --shell /bin/false \ + "$ES_USER" + echo " OK" + fi + ;; + abort-deconfigure|abort-upgrade|abort-remove) + ;; + + # RedHat #################################################### + 1|2) + + # Create elasticsearch group if not existing + if ! getent group "$ES_GROUP" > /dev/null 2>&1 ; then + echo -n "Creating $ES_GROUP group..." + groupadd -r "$ES_GROUP" + echo " OK" + fi + + # Create elasticsearch user if not existing + if ! id $ES_USER > /dev/null 2>&1 ; then + echo -n "Creating $ES_USER user..." + useradd --system \ + -M \ + --gid "$ES_GROUP" \ + --shell /sbin/nologin \ + --comment "elasticsearch user" \ + "$ES_USER" + echo " OK" + fi + ;; + + *) + echo "pre install script called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +${packaging.scripts.footer} diff --git a/src/packaging/common/scripts/prerm b/src/packaging/common/scripts/prerm new file mode 100644 index 00000000000..046cf3c7b73 --- /dev/null +++ b/src/packaging/common/scripts/prerm @@ -0,0 +1,77 @@ +${packaging.scripts.header} + +# +# This script is executed in the pre-remove phase +# +# On Debian, +# $1=remove : indicates a removal +# $1=upgrade : indicates an upgrade +# +# On RedHat, +# $1=0 : indicates a removal +# $1=1 : indicates an upgrade + + + +# Source the default env file +ES_ENV_FILE="${packaging.env.file}" +if [ -f "$ES_ENV_FILE" ]; then + . "$ES_ENV_FILE" +else + echo "Unable to source environment file $ES_ENV_FILE" >&2 +fi + +STOP_REQUIRED=false + +case "$1" in + + # Debian #################################################### + remove) + STOP_REQUIRED=true + ;; + upgrade) + if [ "$RESTART_ON_UPGRADE" = "true" ]; then + STOP_REQUIRED=true + fi + ;; + deconfigure|failed-upgrade) + ;; + + # RedHat #################################################### + 0) + STOP_REQUIRED=true + ;; + 1) + # Dont do anything on upgrade, because the preun script in redhat gets executed after the postinst (madness!) + ;; + + *) + echo "pre remove script called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# Stops the service +if [ "$STOP_REQUIRED" = "true" ]; then + echo -n "Stopping elasticsearch service..." + if command -v systemctl >/dev/null; then + systemctl --no-reload stop elasticsearch.service > /dev/null 2>&1 || true + + elif [ -x /etc/init.d/elasticsearch ]; then + if command -v invoke-rc.d >/dev/null; then + invoke-rc.d elasticsearch stop || true + else + /etc/init.d/elasticsearch stop || true + fi + + # older suse linux distributions do not ship with systemd + # but do not have an /etc/init.d/ directory + # this tries to start the elasticsearch service on these + # as well without failing this script + elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then + /etc/rc.d/init.d/elasticsearch stop || true + fi + echo " OK" +fi + +${packaging.scripts.footer} diff --git a/src/packaging/deb/control/conffiles b/src/packaging/deb/control/conffiles deleted file mode 100644 index d77241e3934..00000000000 --- a/src/packaging/deb/control/conffiles +++ /dev/null @@ -1,5 +0,0 @@ -/etc/init.d/elasticsearch -/etc/default/elasticsearch -/etc/elasticsearch/logging.yml -/etc/elasticsearch/elasticsearch.yml -/usr/lib/systemd/system/elasticsearch.service diff --git a/src/packaging/deb/control/postinst b/src/packaging/deb/control/postinst deleted file mode 100755 index 8c76fe916f7..00000000000 --- a/src/packaging/deb/control/postinst +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/sh -set -e - -[ -f /etc/default/elasticsearch ] && . /etc/default/elasticsearch - -startElasticsearch() { - if [ -x /bin/systemctl ] ; then - /bin/systemctl daemon-reload - /bin/systemctl start elasticsearch.service - elif [ -x "/etc/init.d/elasticsearch" ]; then - if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then - invoke-rc.d elasticsearch start || true - else - /etc/init.d/elasticsearch start || true - fi - fi -} - -case "$1" in - configure) - [ -z "$ES_USER" ] && ES_USER="elasticsearch" - [ -z "$ES_GROUP" ] && ES_GROUP="elasticsearch" - if ! getent group "$ES_GROUP" > /dev/null 2>&1 ; then - addgroup --system "$ES_GROUP" --quiet - fi - if ! id $ES_USER > /dev/null 2>&1 ; then - adduser --system --home /usr/share/elasticsearch --no-create-home \ - --ingroup "$ES_GROUP" --disabled-password --shell /bin/false \ - "$ES_USER" - fi - - # Set user permissions on /var/log/elasticsearch, /var/lib/elasticsearch, - # and /usr/share/elasticsearch/plugins - mkdir -p /var/log/elasticsearch /var/lib/elasticsearch /usr/share/elasticsearch/plugins - chown -R $ES_USER:$ES_GROUP /var/log/elasticsearch /var/lib/elasticsearch /usr/share/elasticsearch/plugins - chmod 755 /var/log/elasticsearch /var/lib/elasticsearch - - # configuration files should not be modifiable by elasticsearch user, as this can be a security issue - chown -Rh root:root /etc/elasticsearch/* - chmod 755 /etc/elasticsearch - find /etc/elasticsearch -type f -exec chmod 644 {} ';' - find /etc/elasticsearch -type d -exec chmod 755 {} ';' - - # if $2 is set, this is an upgrade - if ( [ -n $2 ] && [ "$RESTART_ON_UPGRADE" = "true" ] ) ; then - startElasticsearch - # this is a fresh installation - elif [ -z $2 ] ; then - if [ -x /bin/systemctl ] ; then - echo "### NOT starting on installation, please execute the following statements to configure elasticsearch to start automatically using systemd" - echo " sudo /bin/systemctl daemon-reload" - echo " sudo /bin/systemctl enable elasticsearch.service" - echo "### You can start elasticsearch by executing" - echo " sudo /bin/systemctl start elasticsearch.service" - - elif [ -x /usr/sbin/update-rc.d ] ; then - echo "### NOT starting elasticsearch by default on bootup, please execute" - echo " sudo update-rc.d elasticsearch defaults 95 10" - echo "### In order to start elasticsearch, execute" - echo " sudo /etc/init.d/elasticsearch start" - fi - fi - ;; -esac - diff --git a/src/packaging/deb/control/postrm b/src/packaging/deb/control/postrm deleted file mode 100755 index ac0428a083a..00000000000 --- a/src/packaging/deb/control/postrm +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/sh -set -e - -case "$1" in - remove) - # Remove logs - rm -rf /var/log/elasticsearch - - # disable elasticsearch service on systemd systems - if [ -x /bin/systemctl ] ; then - /bin/systemctl --no-reload disable elasticsearch.service > /dev/null 2>&1 || : - fi - - # Remove plugin directory and all plugins - rm -rf /usr/share/elasticsearch/plugins - - # Remove **only** empty data dir - if [ -d /var/lib/elasticsearch ]; then - rmdir --ignore-fail-on-non-empty /var/lib/elasticsearch - fi - ;; - - purge) - # Remove service - # disable elasticsearch service on systemd systems - if [ -x /bin/systemctl ] ; then - /bin/systemctl --no-reload disable elasticsearch.service > /dev/null 2>&1 || : - else - update-rc.d elasticsearch remove >/dev/null || true - fi - - # Remove logs, data and plugins - rm -rf /var/log/elasticsearch /var/lib/elasticsearch /usr/share/elasticsearch/plugins - - # Remove user/group - deluser elasticsearch || true - delgroup elasticsearch || true - ;; - - upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) - # Nothing to do here - ;; - - *) - echo "$0 called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac diff --git a/src/packaging/deb/control/prerm b/src/packaging/deb/control/prerm deleted file mode 100755 index c613e0667b1..00000000000 --- a/src/packaging/deb/control/prerm +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh -set -e - -[ -f /etc/default/elasticsearch ] && . /etc/default/elasticsearch - -stopElasticsearch() { - if [ -x /bin/systemctl ] ; then - /bin/systemctl --no-reload stop elasticsearch.service > /dev/null 2>&1 || : - elif [ -x "/etc/init.d/elasticsearch" ]; then - if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then - invoke-rc.d elasticsearch stop || true - else - /etc/init.d/elasticsearch stop || true - fi - fi -} - -case "$1" in - upgrade) - if [ "$RESTART_ON_UPGRADE" = "true" ] ; then - stopElasticsearch - fi - ;; - remove) - stopElasticsearch - ;; -esac - diff --git a/src/packaging/deb/copyright b/src/packaging/deb/copyright index 032b8f40942..d93b550af95 100644 --- a/src/packaging/deb/copyright +++ b/src/packaging/deb/copyright @@ -1,4 +1,4 @@ -Copyright 2013-2015 Elasticsearch +Copyright 2013-2015 Elasticsearch License: Apache-2.0 Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/packaging/deb/default/elasticsearch b/src/packaging/deb/default/elasticsearch deleted file mode 100644 index 00ab1129a02..00000000000 --- a/src/packaging/deb/default/elasticsearch +++ /dev/null @@ -1,47 +0,0 @@ -# Run Elasticsearch as this user ID and group ID -#ES_USER=elasticsearch -#ES_GROUP=elasticsearch - -# Heap Size (defaults to 256m min, 1g max) -#ES_HEAP_SIZE=2g - -# Heap new generation -#ES_HEAP_NEWSIZE= - -# max direct memory -#ES_DIRECT_SIZE= - -# Maximum number of open files, defaults to 65535. -#MAX_OPEN_FILES=65535 - -# Maximum locked memory size. Set to "unlimited" if you use the -# bootstrap.mlockall option in elasticsearch.yml. You must also set -# ES_HEAP_SIZE. -#MAX_LOCKED_MEMORY=unlimited - -# Maximum number of VMA (Virtual Memory Areas) a process can own -#MAX_MAP_COUNT=262144 - -# Elasticsearch log directory -#LOG_DIR=/var/log/elasticsearch - -# Elasticsearch data directory -#DATA_DIR=/var/lib/elasticsearch - -# Elasticsearch work directory -#WORK_DIR=/tmp/elasticsearch - -# Elasticsearch configuration directory -#CONF_DIR=/etc/elasticsearch - -# Elasticsearch configuration file (elasticsearch.yml) -#CONF_FILE=/etc/elasticsearch/elasticsearch.yml - -# Additional Java OPTS -#ES_JAVA_OPTS= - -# Configure restart on package upgrade (true, every other setting will lead to not restarting) -#RESTART_ON_UPGRADE=true - -# Path to the GC log file -#ES_GC_LOG_FILE=/var/log/elasticsearch/gc.log diff --git a/src/packaging/deb/packaging.properties b/src/packaging/deb/packaging.properties index 402f79d9cdc..22dc96a9a73 100644 --- a/src/packaging/deb/packaging.properties +++ b/src/packaging/deb/packaging.properties @@ -1,5 +1,12 @@ # Properties used to build to the DEB package # +# Environment file +packaging.env.file=/etc/default/elasticsearch + # Simple marker to check that properties are correctly overridden packaging.type=deb + +# Custom header for package scripts +packaging.scripts.header=#!/bin/sh${line.separator}set -e +packaging.scripts.footer=exit 0${line.separator}# Built for ${project.name}-${project.version} (${packaging.type}) diff --git a/src/packaging/deb/scripts/conffiles b/src/packaging/deb/scripts/conffiles new file mode 100644 index 00000000000..9f658416784 --- /dev/null +++ b/src/packaging/deb/scripts/conffiles @@ -0,0 +1,5 @@ +${packaging.env.file} +${packaging.elasticsearch.conf.dir}/elasticsearch.yml +${packaging.elasticsearch.conf.dir}/logging.yml +/etc/init.d/elasticsearch +/usr/lib/systemd/system/elasticsearch.service diff --git a/src/packaging/deb/control/control b/src/packaging/deb/scripts/control similarity index 93% rename from src/packaging/deb/control/control rename to src/packaging/deb/scripts/control index 584873b97c0..b98ce2066c0 100644 --- a/src/packaging/deb/control/control +++ b/src/packaging/deb/scripts/control @@ -1,11 +1,11 @@ Package: elasticsearch Version: [[version]] Architecture: all -Maintainer: Elasticsearch Team +Maintainer: Elasticsearch Team Depends: libc6, adduser Section: web Priority: optional -Homepage: http://www.elasticsearch.org/ +Homepage: https://www.elastic.co/ Description: Open Source, Distributed, RESTful Search Engine Elasticsearch is a distributed RESTful search engine built for the cloud. . diff --git a/src/packaging/rpm/packaging.properties b/src/packaging/rpm/packaging.properties index b6b902a7e83..f630c9cddbf 100644 --- a/src/packaging/rpm/packaging.properties +++ b/src/packaging/rpm/packaging.properties @@ -1,5 +1,12 @@ # Properties used to build to the RPM package # +# Environment file +packaging.env.file=/etc/sysconfig/elasticsearch + # Simple marker to check that properties are correctly overridden packaging.type=rpm + +# Custom header for package scripts +packaging.scripts.header= +packaging.scripts.footer=# Built for ${project.name}-${project.version} (${packaging.type}) diff --git a/src/packaging/rpm/scripts/postinstall b/src/packaging/rpm/scripts/postinstall deleted file mode 100644 index 55d1dbe3a1f..00000000000 --- a/src/packaging/rpm/scripts/postinstall +++ /dev/null @@ -1,54 +0,0 @@ - -[ -f /etc/sysconfig/elasticsearch ] && . /etc/sysconfig/elasticsearch - -# Generate ES plugin directory and hand over ownership to ES user -mkdir -p /usr/share/elasticsearch/plugins -chown elasticsearch:elasticsearch /usr/share/elasticsearch/plugins - -startElasticsearch() { - if [ -x /bin/systemctl ] ; then - /bin/systemctl start elasticsearch.service - elif [ -x /etc/init.d/elasticsearch ] ; then - /etc/init.d/elasticsearch start - # older suse linux distributions do not ship with systemd - # but do not have an /etc/init.d/ directory - # this tries to start elasticsearch on these as well without failing this script - elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then - /etc/rc.d/init.d/elasticsearch start - fi -} - -stopElasticsearch() { - if [ -x /bin/systemctl ] ; then - /bin/systemctl stop elasticsearch.service > /dev/null 2>&1 || : - elif [ -x /etc/init.d/elasticsearch ] ; then - /etc/init.d/elasticsearch stop - elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then - /etc/rc.d/init.d/elasticsearch stop - fi -} - -# Initial installation: $1 == 1 -# Upgrade: $1 == 2, and configured to restart on upgrade -if [ $1 -eq 1 ] ; then - - if [ -x /bin/systemctl ] ; then - echo "### NOT starting on installation, please execute the following statements to configure elasticsearch to start automatically using systemd" - echo " sudo /bin/systemctl daemon-reload" - echo " sudo /bin/systemctl enable elasticsearch.service" - echo "### You can start elasticsearch by executing" - echo " sudo /bin/systemctl start elasticsearch.service" - - - elif [ -x /sbin/chkconfig ] ; then - echo "### NOT starting on installation, please execute the following statements to configure elasticsearch to start automatically using chkconfig" - echo " sudo /sbin/chkconfig --add elasticsearch" - echo "### You can start elasticsearch by executing" - echo " sudo service elasticsearch start" - fi - -elif [ $1 -ge 2 -a "$RESTART_ON_UPGRADE" == "true" ] ; then - stopElasticsearch - startElasticsearch -fi - diff --git a/src/packaging/rpm/scripts/postremove b/src/packaging/rpm/scripts/postremove deleted file mode 100644 index 52fd0e0b1c1..00000000000 --- a/src/packaging/rpm/scripts/postremove +++ /dev/null @@ -1,18 +0,0 @@ -# only execute in case of package removal, not on upgrade -if [ $1 -eq 0 ] ; then - - getent passwd elasticsearch > /dev/null - if [ "$?" == "0" ] ; then - userdel elasticsearch - fi - - getent group elasticsearch >/dev/null - if [ "$?" == "0" ] ; then - groupdel elasticsearch - fi - - # Remove plugin directory and all plugins - rm -rf /usr/share/elasticsearch/plugins -fi - -exit diff --git a/src/packaging/rpm/scripts/preinstall b/src/packaging/rpm/scripts/preinstall deleted file mode 100644 index 327c8d63e39..00000000000 --- a/src/packaging/rpm/scripts/preinstall +++ /dev/null @@ -1,4 +0,0 @@ -getent group elasticsearch >/dev/null || groupadd -r elasticsearch -getent passwd elasticsearch >/dev/null || \ - useradd -r -g elasticsearch -d /usr/share/elasticsearch -s /sbin/nologin \ - -c "elasticsearch user" elasticsearch diff --git a/src/packaging/rpm/scripts/preremove b/src/packaging/rpm/scripts/preremove deleted file mode 100644 index 1627c19c7aa..00000000000 --- a/src/packaging/rpm/scripts/preremove +++ /dev/null @@ -1,29 +0,0 @@ - -[ -f /etc/sysconfig/elasticsearch ] && . /etc/sysconfig/elasticsearch - -stopElasticsearch() { - if [ -x /bin/systemctl ] ; then - /bin/systemctl stop elasticsearch.service > /dev/null 2>&1 || : - elif [ -x /etc/init.d/elasticsearch ] ; then - /etc/init.d/elasticsearch stop - elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then - /etc/rc.d/init.d/elasticsearch stop - fi -} - -# Removal: $1 == 0 -# Dont do anything on upgrade, because the preun script in redhat gets executed after the postinst (madness!) -if [ $1 -eq 0 ] ; then - - if [ -x /bin/systemctl ] ; then - /bin/systemctl --no-reload disable elasticsearch.service > /dev/null 2>&1 || : - fi - - if [ -x /sbin/chkconfig ] ; then - /sbin/chkconfig --del elasticsearch 2> /dev/null - fi - - stopElasticsearch -fi - -exit 0 diff --git a/src/packaging/rpm/sysconfig/elasticsearch b/src/packaging/rpm/sysconfig/elasticsearch deleted file mode 100644 index 7e0776c6231..00000000000 --- a/src/packaging/rpm/sysconfig/elasticsearch +++ /dev/null @@ -1,49 +0,0 @@ -# Directory where the Elasticsearch binary distribution resides -ES_HOME=/usr/share/elasticsearch - -# Heap Size (defaults to 256m min, 1g max) -#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= - -# Maximum number of VMA (Virtual Memory Areas) a process can own -MAX_MAP_COUNT=262144 - -# Elasticsearch log directory -LOG_DIR=/var/log/elasticsearch - -# Elasticsearch data directory -DATA_DIR=/var/lib/elasticsearch - -# Elasticsearch work directory -WORK_DIR=/tmp/elasticsearch - -# Elasticsearch conf directory -CONF_DIR=/etc/elasticsearch - -# Elasticsearch configuration file (elasticsearch.yml) -CONF_FILE=/etc/elasticsearch/elasticsearch.yml - -# User to run as, change this to a specific elasticsearch user if possible -# Also make sure, this user can write into the log directories in case you change them -# This setting only works for the init script, but has to be configured separately for systemd startup -ES_USER=elasticsearch - -# Configure restart on package upgrade (true, every other setting will lead to not restarting) -#RESTART_ON_UPGRADE=true - -# Path to the GC log file -#ES_GC_LOG_FILE=/var/log/elasticsearch/gc.log diff --git a/src/test/resources/packaging/scripts/30_deb_package.bats b/src/test/resources/packaging/scripts/30_deb_package.bats new file mode 100644 index 00000000000..7130d275d8c --- /dev/null +++ b/src/test/resources/packaging/scripts/30_deb_package.bats @@ -0,0 +1,177 @@ +#!/usr/bin/env bats + +# This file is used to test the installation and removal +# of a Debian package. + +# WARNING: This testing file must be executed as root and can +# dramatically change your system. It removes the 'elasticsearch' +# user/group and also many directories. Do not execute this file +# unless you know exactly what you are doing. + +# The test case can be executed with the Bash Automated +# Testing System tool available at https://github.com/sstephenson/bats +# Thanks to Sam Stephenson! + +# Licensed to Elasticsearch under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Load test utilities +load packaging_test_utils + +# Cleans everything for the 1st execution +setup() { + if [ "$BATS_TEST_NUMBER" -eq 1 ]; then + clean_before_test + fi +} + +################################## +# Install DEB package +################################## +@test "[DEB] dpkg command is available" { + skip_not_dpkg + run dpkg --version + [ "$status" -eq 0 ] +} + +@test "[DEB] package is available" { + skip_not_dpkg + count=$(find . -type f -name 'elastic*.deb' | wc -l) + [ "$count" -eq 1 ] +} + +@test "[DEB] package is not installed" { + skip_not_dpkg + run dpkg -s 'elasticsearch' >&2 + [ "$status" -eq 1 ] +} + +@test "[DEB] install package" { + skip_not_dpkg + run dpkg -i elasticsearch*.deb >&2 + [ "$status" -eq 0 ] +} + +@test "[DEB] package is installed" { + skip_not_dpkg + run dpkg -s 'elasticsearch' >&2 + [ "$status" -eq 0 ] +} + +################################## +# Check that the package is correctly installed +################################## +@test "[DEB] verify package installation" { + skip_not_dpkg + + verify_package_installation +} + +################################## +# Check that Elasticsearch is working +################################## +@test "[TEST] test elasticsearch" { + skip_not_dpkg + + start_elasticsearch_service + + run_elasticsearch_tests +} + +################################## +# Uninstall DEB package +################################## +@test "[DEB] remove package" { + skip_not_dpkg + run dpkg -r 'elasticsearch' >&2 + [ "$status" -eq 0 ] +} + +@test "[DEB] package has been removed" { + skip_not_dpkg + run dpkg -s 'elasticsearch' >&2 + [ "$status" -eq 0 ] + echo "$output" | grep -i "status" | grep -i "deinstall ok" +} + +@test "[DEB] verify package removal" { + skip_not_dpkg + + # The removal must stop the service + count=$(ps | grep Elasticsearch | wc -l) + [ "$count" -eq 0 ] + + # The removal must disable the service + # see prerm file + if is_systemd; then + run systemctl status elasticsearch.service + [ "$status" -eq 3 ] + + run systemctl is-enabled elasticsearch.service + [ "$status" -eq 1 ] + fi + + # Those directories are deleted when removing the package + # see postrm file + assert_file_not_exist "/var/log/elasticsearch" + assert_file_not_exist "/tmp/elasticsearch" + assert_file_not_exist "/usr/share/elasticsearch/plugins" + assert_file_not_exist "/var/run/elasticsearch" + + # The configuration files are still here + assert_file_exist "/etc/elasticsearch" + assert_file_exist "/etc/elasticsearch/elasticsearch.yml" + assert_file_exist "/etc/elasticsearch/logging.yml" + + # The env file is still here + assert_file_exist "/etc/default/elasticsearch" + + # The service files are still here + assert_file_exist "/etc/init.d/elasticsearch" + assert_file_exist "/usr/lib/systemd/system/elasticsearch.service" +} + +@test "[DEB] purge package" { + skip_not_dpkg + run dpkg --purge 'elasticsearch' >&2 + [ "$status" -eq 0 ] +} + +@test "[DEB] verify package purge" { + skip_not_dpkg + + # all remaining files are deleted by the purge + assert_file_not_exist "/etc/elasticsearch" + assert_file_not_exist "/etc/elasticsearch/elasticsearch.yml" + assert_file_not_exist "/etc/elasticsearch/logging.yml" + + assert_file_not_exist "/etc/default/elasticsearch" + + assert_file_not_exist "/etc/init.d/elasticsearch" + assert_file_not_exist "/usr/lib/systemd/system/elasticsearch.service" + + assert_file_not_exist "/usr/share/elasticsearch" + + assert_file_not_exist "/usr/share/doc/elasticsearch" + assert_file_not_exist "/usr/share/doc/elasticsearch/copyright" +} + +@test "[DEB] package has been completly removed" { + skip_not_dpkg + run dpkg -s 'elasticsearch' >&2 + [ "$status" -eq 1 ] +} diff --git a/src/test/resources/packaging/scripts/40_rpm_package.bats b/src/test/resources/packaging/scripts/40_rpm_package.bats new file mode 100644 index 00000000000..6be482867f9 --- /dev/null +++ b/src/test/resources/packaging/scripts/40_rpm_package.bats @@ -0,0 +1,141 @@ +#!/usr/bin/env bats + +# This file is used to test the installation of a RPM package. + +# WARNING: This testing file must be executed as root and can +# dramatically change your system. It removes the 'elasticsearch' +# user/group and also many directories. Do not execute this file +# unless you know exactly what you are doing. + +# The test case can be executed with the Bash Automated +# Testing System tool available at https://github.com/sstephenson/bats +# Thanks to Sam Stephenson! + +# Licensed to Elasticsearch under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Load test utilities +load packaging_test_utils + +# Cleans everything for the 1st execution +setup() { + if [ "$BATS_TEST_NUMBER" -eq 1 ]; then + clean_before_test + fi +} + +################################## +# Install RPM package +################################## +@test "[RPM] rpm command is available" { + skip_not_rpm + run rpm --version + [ "$status" -eq 0 ] +} + +@test "[RPM] package is available" { + skip_not_rpm + count=$(find . -type f -name 'elastic*.rpm' | wc -l) + [ "$count" -eq 1 ] +} + +@test "[RPM] package is not installed" { + skip_not_rpm + run rpm -qe 'elasticsearch' >&2 + [ "$status" -eq 1 ] +} + +@test "[RPM] install package" { + skip_not_rpm + run rpm -i elasticsearch*.rpm >&2 + [ "$status" -eq 0 ] +} + +@test "[RPM] package is installed" { + skip_not_rpm + run rpm -qe 'elasticsearch' >&2 + [ "$status" -eq 0 ] +} + +################################## +# Check that the package is correctly installed +################################## +@test "[RPM] verify package installation" { + skip_not_rpm + + verify_package_installation +} + +################################## +# Check that Elasticsearch is working +################################## +@test "[TEST] test elasticsearch" { + skip_not_rpm + + start_elasticsearch_service + + run_elasticsearch_tests +} + +################################## +# Uninstall RPM package +################################## +@test "[RPM] remove package" { + skip_not_rpm + run rpm -e 'elasticsearch' >&2 + [ "$status" -eq 0 ] +} + +@test "[RPM] package has been removed" { + skip_not_rpm + run rpm -qe 'elasticsearch' >&2 + [ "$status" -eq 1 ] +} + +@test "[RPM] verify package removal" { + skip_not_rpm + + # The removal must stop the service + count=$(ps | grep Elasticsearch | wc -l) + [ "$count" -eq 0 ] + + # The removal must disable the service + # see prerm file + if is_systemd; then + run systemctl status elasticsearch.service + echo "$output" | grep "Active:" | grep 'inactive\|failed' + + run systemctl is-enabled elasticsearch.service + [ "$status" -eq 1 ] + fi + + # Those directories are deleted when removing the package + # see postrm file + assert_file_not_exist "/var/log/elasticsearch" + assert_file_not_exist "/tmp/elasticsearch" + assert_file_not_exist "/usr/share/elasticsearch/plugins" + assert_file_not_exist "/var/run/elasticsearch" + + assert_file_not_exist "/etc/elasticsearch" + assert_file_not_exist "/etc/elasticsearch/elasticsearch.yml" + assert_file_not_exist "/etc/elasticsearch/logging.yml" + + assert_file_not_exist "/etc/init.d/elasticsearch" + assert_file_not_exist "/usr/lib/systemd/system/elasticsearch.service" + + assert_file_not_exist "/etc/sysconfig/elasticsearch" +} diff --git a/src/test/resources/packaging/scripts/packaging_test_utils.bash b/src/test/resources/packaging/scripts/packaging_test_utils.bash new file mode 100644 index 00000000000..6ef1874c4ab --- /dev/null +++ b/src/test/resources/packaging/scripts/packaging_test_utils.bash @@ -0,0 +1,371 @@ +#!/bin/sh + +# This file contains some utilities to test the elasticsearch scripts, +# the .deb/.rpm packages and the SysV/Systemd scripts. + +# WARNING: This testing file must be executed as root and can +# dramatically change your system. It removes the 'elasticsearch' +# user/group and also many directories. Do not execute this file +# unless you know exactly what you are doing. + +# Licensed to Elasticsearch under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# Checks if necessary commands are available to run the tests + +if [ ! -x /usr/bin/which ]; then + echo "'which' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which wget 2>/dev/null`" ]; then + echo "'wget' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which curl 2>/dev/null`" ]; then + echo "'curl' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which pgrep 2>/dev/null`" ]; then + echo "'pgrep' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which unzip 2>/dev/null`" ]; then + echo "'unzip' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which tar 2>/dev/null`" ]; then + echo "'tar' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which unzip 2>/dev/null`" ]; then + echo "'unzip' command is mandatory to run the tests" + exit 1 +fi + +if [ ! -x "`which java 2>/dev/null`" ]; then + echo "'java' command is mandatory to run the tests" + exit 1 +fi + +# Returns 0 if the 'dpkg' command is available +is_dpkg() { + [ -x "`which dpkg 2>/dev/null`" ] +} + +# Returns 0 if the 'rpm' command is available +is_rpm() { + [ -x "`which rpm 2>/dev/null`" ] +} + +# Skip test if the 'dpkg' command is not supported +skip_not_dpkg() { + if [ ! -x "`which dpkg 2>/dev/null`" ]; then + skip "dpkg is not supported" + fi +} + +# Skip test if the 'rpm' command is not supported +skip_not_rpm() { + if [ ! -x "`which rpm 2>/dev/null`" ]; then + skip "rpm is not supported" + fi +} + +# Returns 0 if the system supports Systemd +is_systemd() { + [ -x /bin/systemctl ] +} + +# Skip test if Systemd is not supported +skip_not_systemd() { + if [ ! -x /bin/systemctl ]; then + skip "systemd is not supported" + fi +} + +# Returns 0 if the system supports SysV +is_sysvinit() { + [ -x "`which service 2>/dev/null`" ] +} + +# Skip test if SysV is not supported +skip_not_sysvinit() { + if [ -x "`which service 2>/dev/null`" ] && is_systemd; then + skip "sysvinit is supported, but systemd too" + fi + if [ ! -x "`which service 2>/dev/null`" ]; then + skip "sysvinit is not supported" + fi +} + +# Skip if tar is not supported +skip_not_tar_gz() { + if [ ! -x "`which tar 2>/dev/null`" ]; then + skip "tar is not supported" + fi +} + +# Skip if unzip is not supported +skip_not_zip() { + if [ ! -x "`which unzip 2>/dev/null`" ]; then + skip "unzip is not supported" + fi +} + +assert_file_exist() { + [ -e "$1" ] +} + +assert_file_not_exist() { + [ ! -e "$1" ] +} + +assert_file() { + local file=$1 + local type=$2 + local user=$3 + local privileges=$4 + + [ -n "$file" ] && [ -e "$file" ] + + if [ "$type" = "d" ]; then + [ -d "$file" ] + else + [ -f "$file" ] + fi + + if [ "x$user" != "x" ]; then + realuser=$(ls -ld "$file" | awk '{print $3}') + [ "$realuser" = "$user" ] + fi + + if [ "x$privileges" != "x" ]; then + realprivileges=$(find "$file" -maxdepth 0 -printf "%m") + [ "$realprivileges" = "$privileges" ] + fi +} + +assert_output() { + echo "$output" | grep -E "$1" +} + +# Checks that all directories & files are correctly installed +verify_package_installation() { + + run id elasticsearch + [ "$status" -eq 0 ] + + run getent group elasticsearch + [ "$status" -eq 0 ] + + # Home dir + assert_file "/usr/share/elasticsearch" d root 755 + # Bin dir + assert_file "/usr/share/elasticsearch/bin" d root 755 + assert_file "/usr/share/elasticsearch/lib" d root 755 + # Conf dir + assert_file "/etc/elasticsearch" d root 755 + assert_file "/etc/elasticsearch/elasticsearch.yml" f root 644 + assert_file "/etc/elasticsearch/logging.yml" f root 644 + # Data dir + assert_file "/var/lib/elasticsearch" d elasticsearch 755 + # Log dir + assert_file "/var/log/elasticsearch" d elasticsearch 755 + # Work dir + assert_file "/tmp/elasticsearch" d elasticsearch 755 + # Plugins dir + assert_file "/usr/share/elasticsearch/plugins" d elasticsearch 755 + # PID dir + assert_file "/var/run/elasticsearch" d elasticsearch 755 + # Readme files + assert_file "/usr/share/elasticsearch/NOTICE.txt" f root 644 + assert_file "/usr/share/elasticsearch/README.textile" f root 644 + + if is_dpkg; then + # Env file + assert_file "/etc/default/elasticsearch" f root 644 + + # Doc files + assert_file "/usr/share/doc/elasticsearch" d root 755 + assert_file "/usr/share/doc/elasticsearch/copyright" f root 644 + + fi + + if is_rpm; then + # Env file + assert_file "/etc/sysconfig/elasticsearch" f root 644 + # License file + assert_file "/usr/share/elasticsearch/LICENSE.txt" f root 644 + fi +} + +# Deletes everything before running a test file +clean_before_test() { + + # List of files to be deleted + ELASTICSEARCH_TEST_FILES=("/usr/share/elasticsearch" \ + "/etc/elasticsearch" \ + "/var/lib/elasticsearch" \ + "/var/log/elasticsearch" \ + "/tmp/elasticsearch" \ + "/etc/default/elasticsearch" \ + "/etc/sysconfig/elasticsearch" \ + "/var/run/elasticsearch" \ + "/usr/share/doc/elasticsearch") + + if [ "$ES_CLEAN_BEFORE_TEST" = "true" ]; then + # Kills all processes of user elasticsearch + if id elasticsearch > /dev/null 2>&1; then + pkill -u elasticsearch 2>/dev/null || true + fi + + # Removes RPM package + if is_rpm; then + rpm --quiet -e elasticsearch 2>/dev/null || true + fi + + if [ -x "`which yum 2>/dev/null`" ]; then + yum remove -y elasticsearch 2>/dev/null || true + fi + + # Removes DEB package + if is_dpkg; then + dpkg --purge elasticsearch > /dev/null 2>&1 || true + fi + + if [ -x "`which apt-get 2>/dev/null`" ]; then + apt-get --quiet --yes purge elasticsearch > /dev/null 2>&1 || true + fi + + # Removes user & group + userdel elasticsearch > /dev/null 2>&1 || true + groupdel elasticsearch > /dev/null 2>&1 || true + + + # Removes all files + for d in "${ELASTICSEARCH_TEST_FILES[@]}"; do + if [ -e "$d" ]; then + rm -rf "$d" + fi + done + fi + + # Checks that all files are deleted + for d in "${ELASTICSEARCH_TEST_FILES[@]}"; do + if [ -e "$d" ]; then + echo "$d should not exist before running the tests" >&2 + exit 1 + fi + done +} + +start_elasticsearch_service() { + + if is_systemd; then + run systemctl daemon-reload + [ "$status" -eq 0 ] + + run systemctl enable elasticsearch.service + [ "$status" -eq 0 ] + + run systemctl is-enabled elasticsearch.service + [ "$status" -eq 0 ] + + run systemctl start elasticsearch.service + [ "$status" -eq 0 ] + + elif is_sysvinit; then + run service elasticsearch start + [ "$status" -eq 0 ] + fi + + wait_for_elasticsearch_status + + if is_systemd; then + run systemctl is-active elasticsearch.service + [ "$status" -eq 0 ] + + run systemctl status elasticsearch.service + [ "$status" -eq 0 ] + + elif is_sysvinit; then + run service elasticsearch status + [ "$status" -eq 0 ] + fi +} + +stop_elasticsearch_service() { + + if is_systemd; then + run systemctl stop elasticsearch.service + [ "$status" -eq 0 ] + + run systemctl is-active elasticsearch.service + [ "$status" -eq 3 ] + [ "$output" = "inactive" ] + + elif is_sysvinit; then + run service elasticsearch stop + [ "$status" -eq 0 ] + + run service elasticsearch status + [ "$status" -ne 0 ] + fi +} + +# Waits for Elasticsearch to reach a given status (defaults to "green") +wait_for_elasticsearch_status() { + local status="green" + if [ "x$1" != "x" ]; then + status="$1" + fi + + # Try to connect to elasticsearch and wait for expected status + wget --quiet --retry-connrefused --waitretry=1 --timeout=20 \ + --output-document=/dev/null "http://localhost:9200/_cluster/health?wait_for_status=$status&timeout=20s" + + # Checks the cluster health + curl -XGET 'http://localhost:9200/_cat/health?h=status&v=false' + if [ $? -ne 0 ]; then + echo "error when checking cluster health" >&2 + exit 1 + fi +} + +# Executes some very basic Elasticsearch tests +run_elasticsearch_tests() { + run curl -XGET 'http://localhost:9200/_cat/health?h=status&v=false' + [ "$status" -eq 0 ] + echo "$output" | grep -w "green" + + run curl -XPOST 'http://localhost:9200/library/book/1?refresh=true' -d '{"title": "Elasticsearch - The Definitive Guide"}' 2>&1 + [ "$status" -eq 0 ] + + run curl -XGET 'http://localhost:9200/_cat/count?h=count&v=false' + [ "$status" -eq 0 ] + echo "$output" | grep -w "1" + + run curl -XDELETE 'http://localhost:9200/_all' + [ "$status" -eq 0 ] +}