Standardization of packages structure and install

The existing DEB/RPM packages have a lot of differences: they don't execute the same actions when installing or removing the package. They also don't declare exactly the same environment variables at the same place. At the end of the day the global behavior and configuration is *almost* the same but it's very difficult to maintain the scripts.

This commits unifies the package behavior:
- DEB/RPM use the same package scripts (pre installation, post installation etc) in order to execute exactly the same actions
- Use of a unique environment vars file that declares everything needed by scripts (the goal is to delete vars declaration in init.d and systemd scripts, this will be done in another PR)
- Variables like directory paths are centralized and replaced according to the target platform (using #10330)
- Move /etc/rc.d/init.d to standard /etc/init.d (RPM only)
- Add PID_DIR env var
- Always set ES_USER, ES_GROUP,MAX_MAP_COUNT and MAX_OPEN_FILES in env vars file
- Create log, data, work and plugins directories with DEB/RPM packaging system
- Change to elastic.co domain in copyright and control files
- Add Bats files to automate testing of DEB and RPM packages
- Update TESTING.asciidoc

More info on Bats here:  https://github.com/sstephenson/bats
This commit is contained in:
Tanguy Leroux 2015-04-14 16:22:37 +02:00
parent 2b0440368c
commit 867955188e
26 changed files with 1345 additions and 465 deletions

View File

@ -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
---------------------------------------------------------------------------

249
pom.xml
View File

@ -50,12 +50,15 @@
<!-- Properties used for building RPM & DEB packages (see common/packaging.properties) -->
<packaging.elasticsearch.home.dir>/usr/share/elasticsearch</packaging.elasticsearch.home.dir>
<packaging.elasticsearch.bin.dir>/usr/share/elasticsearch/bin</packaging.elasticsearch.bin.dir>
<packaging.elasticsearch.conf.dir>/etc/elasticsearch</packaging.elasticsearch.conf.dir>
<packaging.elasticsearch.data.dir>/var/lib/elasticsearch</packaging.elasticsearch.data.dir>
<packaging.elasticsearch.user>elasticsearch</packaging.elasticsearch.user>
<packaging.elasticsearch.group>elasticsearch</packaging.elasticsearch.group>
<packaging.elasticsearch.work.dir>/var/run/elasticsearch</packaging.elasticsearch.work.dir>
<packaging.elasticsearch.work.dir>/tmp/elasticsearch</packaging.elasticsearch.work.dir>
<packaging.elasticsearch.log.dir>/var/log/elasticsearch</packaging.elasticsearch.log.dir>
<packaging.elasticsearch.plugins.dir>${packaging.elasticsearch.home.dir}/plugins</packaging.elasticsearch.plugins.dir>
<packaging.elasticsearch.pid.dir>/var/run/elasticsearch</packaging.elasticsearch.pid.dir>
</properties>
@ -1068,7 +1071,7 @@
<version>1.4</version>
<configuration>
<deb>${project.build.directory}/releases/${project.artifactId}-${project.version}.deb</deb>
<controlDir>${project.build.directory}/generated-packaging/deb/control</controlDir>
<controlDir>${project.build.directory}/generated-packaging/deb/scripts</controlDir>
</configuration>
<executions>
<execution>
@ -1078,31 +1081,44 @@
</goals>
<configuration>
<dataSet>
<!-- Add bin directory -->
<data>
<src>${project.basedir}/</src>
<includes>*.txt, *.textile</includes>
<excludes>LICENSE.txt, .DS_Store</excludes>
<type>directory</type>
<mapper>
<type>perm</type>
<prefix>${packaging.elasticsearch.home.dir}</prefix>
<user>root</user>
<group>root</group>
</mapper>
</data>
<data>
<!-- use the filtered one from the resources plugin -->
<src>${project.build.directory}/generated-packaging/deb/bin</src>
<type>directory</type>
<excludes>*.bat, .DS_Store, *.exe</excludes>
<includes>elasticsearch,elasticsearch.in.sh,plugin</includes>
<mapper>
<type>perm</type>
<prefix>${packaging.elasticsearch.home.dir}/bin</prefix>
<prefix>${packaging.elasticsearch.bin.dir}</prefix>
<filemode>755</filemode>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add configuration files -->
<data>
<src>${project.basedir}/config</src>
<type>directory</type>
<excludes>.DS_Store</excludes>
<mapper>
<type>perm</type>
<prefix>${packaging.elasticsearch.conf.dir}</prefix>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add environment vars file -->
<data>
<src>${project.build.directory}/generated-packaging/deb/env/elasticsearch</src>
<type>file</type>
<mapper>
<type>perm</type>
<prefix>/etc/default</prefix>
<filemode>644</filemode>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add libs -->
<data>
<src>${project.build.directory}/</src>
<includes>${project.build.finalName}.jar</includes>
@ -1136,17 +1152,7 @@
<group>root</group>
</mapper>
</data>
<data>
<src>${project.build.directory}/generated-packaging/deb/default/</src>
<type>directory</type>
<excludes>.DS_Store</excludes>
<mapper>
<type>perm</type>
<prefix>/etc/default</prefix>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add init.d files -->
<data>
<src>${project.build.directory}/generated-packaging/deb/init.d/</src>
<type>directory</type>
@ -1159,22 +1165,13 @@
<group>root</group>
</mapper>
</data>
<!-- Adds systemd file -->
<data>
<src>${project.build.directory}/generated-packaging/deb/systemd/elasticsearch.service</src>
<dst>/usr/lib/systemd/system/elasticsearch.service</dst>
<type>file</type>
</data>
<data>
<src>${project.basedir}/config</src>
<type>directory</type>
<excludes>.DS_Store</excludes>
<mapper>
<type>perm</type>
<prefix>${packaging.elasticsearch.conf.dir}</prefix>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add lintian files -->
<data>
<src>${project.build.directory}/generated-packaging/deb/lintian</src>
<type>directory</type>
@ -1186,6 +1183,20 @@
<group>root</group>
</mapper>
</data>
<!-- Add readme files -->
<data>
<src>${project.basedir}/</src>
<includes>*.txt, *.textile</includes>
<excludes>LICENSE.txt, .DS_Store</excludes>
<type>directory</type>
<mapper>
<type>perm</type>
<prefix>${packaging.elasticsearch.home.dir}</prefix>
<user>root</user>
<group>root</group>
</mapper>
</data>
<!-- Add license files -->
<data>
<src>${project.build.directory}/generated-packaging/deb/copyright</src>
<dst>/usr/share/doc/elasticsearch/copyright</dst>
@ -1198,6 +1209,8 @@
<path>${packaging.elasticsearch.data.dir}</path>
<path>${packaging.elasticsearch.log.dir}</path>
<path>${packaging.elasticsearch.work.dir}</path>
<path>${packaging.elasticsearch.plugins.dir}</path>
<path>${packaging.elasticsearch.pid.dir}</path>
</paths>
<mapper>
<type>perm</type>
@ -1233,40 +1246,101 @@
<defaultUsername>root</defaultUsername>
<defaultGroupname>root</defaultGroupname>
<mappings>
<!-- Add bin directory -->
<mapping>
<directory>${packaging.elasticsearch.bin.dir}/</directory>
<filemode>755</filemode>
<sources>
<source>
<location>${project.build.directory}/generated-packaging/rpm/bin</location>
<includes>
<include>elasticsearch</include>
<include>elasticsearch.in.sh</include>
<include>plugin</include>
</includes>
</source>
</sources>
</mapping>
<!-- Add configuration files -->
<mapping>
<directory>${packaging.elasticsearch.conf.dir}/</directory>
<configuration>noreplace</configuration>
<sources>
<source>
<location>config/</location>
<location>${project.basedir}/config/</location>
<includes>
<include>*.yml</include>
</includes>
</source>
</sources>
</mapping>
<!-- Add environment vars file -->
<mapping>
<directory>/etc/sysconfig/</directory>
<directoryIncluded>false</directoryIncluded>
<configuration>noreplace</configuration>
<sources>
<source>
<location>${project.build.directory}/generated-packaging/rpm/sysconfig</location>
<location>${project.build.directory}/generated-packaging/rpm/env/</location>
<includes>
<include>elasticsearch</include>
</includes>
</source>
</sources>
</mapping>
<!-- Add libs -->
<mapping>
<directory>/etc/rc.d/init.d/</directory>
<directory>${packaging.elasticsearch.home.dir}/lib</directory>
<sources>
<source>
<location>target/lib/</location>
<includes>
<include>lucene*</include>
<include>*log4j*</include>
<include>jna*</include>
<include>spatial4j*</include>
<include>jts*</include>
<include>groovy*</include>
<include>antlr-runtime*</include>
<include>asm*</include>
</includes>
</source>
<source>
<location>${project.build.directory}/</location>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</source>
</sources>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.home.dir}/lib/sigar</directory>
<sources>
<source>
<location>lib/sigar</location>
<includes>
<include>sigar*.jar</include>
<include>libsigar-*-linux.*</include>
</includes>
</source>
</sources>
</mapping>
<!-- Add init.d files -->
<mapping>
<directory>/etc/init.d</directory>
<directoryIncluded>false</directoryIncluded>
<filemode>755</filemode>
<configuration>true</configuration>
<sources>
<source>
<location>${project.build.directory}/generated-packaging/rpm/init.d/elasticsearch</location>
<location>${project.build.directory}/generated-packaging/rpm/init.d</location>
<includes>
<include>elasticsearch</include>
</includes>
</source>
</sources>
</mapping>
<!-- Adds systemd file -->
<mapping>
<directory>/usr/lib/systemd/system/</directory>
<filemode>755</filemode>
@ -1305,74 +1379,7 @@
</source>
</sources>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.work.dir}/</directory>
<filemode>755</filemode>
<username>${packaging.elasticsearch.user}</username>
<username>${packaging.elasticsearch.group}</username>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.data.dir}/</directory>
<filemode>755</filemode>
<username>${packaging.elasticsearch.user}</username>
<username>${packaging.elasticsearch.group}</username>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.log.dir}/</directory>
<filemode>755</filemode>
<username>${packaging.elasticsearch.user}</username>
<username>${packaging.elasticsearch.group}</username>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.home.dir}/bin/</directory>
<filemode>755</filemode>
<sources>
<source>
<location>${project.build.directory}/generated-packaging/rpm/bin</location>
<includes>
<include>elasticsearch</include>
<include>elasticsearch.in.sh</include>
<include>plugin</include>
</includes>
</source>
</sources>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.home.dir}/lib</directory>
<sources>
<source>
<location>target/lib/</location>
<includes>
<include>lucene*</include>
<include>*log4j*</include>
<include>jna*</include>
<include>spatial4j*</include>
<include>jts*</include>
<include>groovy*</include>
<include>antlr-runtime*</include>
<include>asm*</include>
</includes>
</source>
<source>
<location>${project.build.directory}/</location>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</source>
</sources>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.home.dir}/lib/sigar</directory>
<sources>
<source>
<location>lib/sigar</location>
<includes>
<include>sigar*.jar</include>
<include>libsigar-*-linux.*</include>
</includes>
</source>
</sources>
</mapping>
<!-- Add readme files -->
<mapping>
<directory>${packaging.elasticsearch.home.dir}</directory>
<sources>
@ -1405,21 +1412,33 @@
<username>${packaging.elasticsearch.user}</username>
<groupname>${packaging.elasticsearch.group}</groupname>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.plugins.dir}</directory>
<filemode>755</filemode>
<username>${packaging.elasticsearch.user}</username>
<groupname>${packaging.elasticsearch.group}</groupname>
</mapping>
<mapping>
<directory>${packaging.elasticsearch.pid.dir}</directory>
<filemode>755</filemode>
<username>${packaging.elasticsearch.user}</username>
<groupname>${packaging.elasticsearch.group}</groupname>
</mapping>
</mappings>
<preinstallScriptlet>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/preinstall</scriptFile>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/preinst</scriptFile>
<fileEncoding>utf-8</fileEncoding>
</preinstallScriptlet>
<postinstallScriptlet>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/postinstall</scriptFile>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/postinst</scriptFile>
<fileEncoding>utf-8</fileEncoding>
</postinstallScriptlet>
<preremoveScriptlet>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/preremove</scriptFile>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/prerm</scriptFile>
<fileEncoding>utf-8</fileEncoding>
</preremoveScriptlet>
<postremoveScriptlet>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/postremove</scriptFile>
<scriptFile>${project.build.directory}/generated-packaging/rpm/scripts/postrm</scriptFile>
<fileEncoding>utf-8</fileEncoding>
</postremoveScriptlet>
</configuration>

81
src/packaging/common/env/elasticsearch vendored Normal file
View File

@ -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}

View File

@ -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=

View File

@ -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}

View File

@ -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}

View File

@ -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}

View File

@ -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}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,4 @@
Copyright 2013-2015 Elasticsearch <info@elasticsearch.org>
Copyright 2013-2015 Elasticsearch <info@elastic.co>
License: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -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

View File

@ -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})

View File

@ -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

View File

@ -1,11 +1,11 @@
Package: elasticsearch
Version: [[version]]
Architecture: all
Maintainer: Elasticsearch Team <info@elasticsearch.com>
Maintainer: Elasticsearch Team <info@elastic.co>
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.
.

View File

@ -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})

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ]
}

View File

@ -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"
}

View File

@ -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 ]
}