File elasticsearch.spec of Package elasticsearch

#
# spec file for package elasticsearch
#
# Copyright (c) 2018 Samu Voutilainen
# Copyright (c) 2017 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2016 kkaempf
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via http://bugs.opensuse.org/
#


%if 0%{?suse_version} > 1140 || 0%{?fedora_version} > 14
%define has_systemd 1
%else
%define has_systemd 0
%endif

%define javaversion 11

%if ! %{defined _fillupdir}
%define _fillupdir /var/adm/fillup-templates
%endif

# "backport"
%{!?_initddir:    %{expand: %%global _initddir    %{_initrddir}}}
%{!?_rundir:      %{expand: %%global _rundir      /run}}
# these two are broken on SLE 11... so just "correct" them enough to work...
%{!?_tmpfilesdir: %{expand: %%global _tmpfilesdir %{_prefix}/lib/tmpfiles.d}}
%{!?_sysctldir:   %{expand: %%global _sysctldir   %{_prefix}/lib/sysctl.d}}

Name:           elasticsearch
Version:        7.10.2
Release:        0
Summary:        Open Source, Distributed, RESTful Search Engine
License:        Apache-2.0
Group:          Productivity/Databases/Tools
Url:            https://github.com/elastic/elasticsearch
Source0:        https://github.com/elastic/elasticsearch/archive/v%{version}/%{name}-%{version}.tar.gz

Source2:        %{name}.logrotate
Source7:        %{name}.SuSEfirewall2

Patch1:         0001-provide-SUSE-environment.patch
Patch2:         0002-systemd-service-type-simple.patch
Patch3:         0003-var-run-run.patch

Obsoletes:      elasticsearch5 < 6
Provides:       elasticsearch5
Obsoletes:      elasticsearch6 < 7
Provides:       elasticsearch6

BuildRoot:      %{_tmppath}/%{name}-%{version}-build
BuildRequires:  git
BuildRequires:  gradle6 >= 6.2.1
%ifarch x86_64
BuildRequires:  libjnidispatch
%endif

%if 0%{?suse_version} > 1010
BuildRequires:  fdupes
%endif

BuildRequires:  java-devel = %{javaversion}
BuildRequires:  java = %{javaversion}


BuildRequires:  elasticsearch-kit = %{version}

%if 0%{?has_systemd}
BuildRequires:  systemd
%{?systemd_requires}
%endif

# SLE_12 and Leap 42 need this:
BuildRequires:  mozilla-nss

BuildArch:      noarch
Provides:       mvn(org.elasticsearch:core) == %{version}
Provides:       mvn(org.elasticsearch:dev-tools) == %{version}
Provides:       mvn(org.elasticsearch:distribution) == %{version}
Provides:       mvn(org.elasticsearch:modules) == %{version}
Provides:       mvn(org.elasticsearch:plugins) == %{version}
Provides:       mvn(org.elasticsearch:qa) == %{version}
Provides:       mvn(org.elasticsearch:rest-api-spec) == %{version}
Provides:       mvn(org:elasticsearch) == %{version}

%if 0%{?has_systemd}
Requires(post): %fillup_prereq
%else
Requires(post): %insserv_prereq  %fillup_prereq
%endif

Requires:       java-headless = %{javaversion}

Requires:       logrotate
# for sysctl:
Requires:       procps
# for elasticsearch-plugin:
Requires:       hostname
Requires:       which
# mkdir, chown in %%pre
Requires(pre):  coreutils
# useradd, groupadd in %%pre
Requires(pre):  shadow

%description
Elasticsearch is a distributed RESTful search engine built for the
cloud. Reference documentation can be found at
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
and the 'Elasticsearch: The Definitive Guide' book can be found at
https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html

%prep
%setup -q -c -n src
cd %{name}-%{version}
%patch1 -p1
%patch2 -p1
%patch3 -p1

if [ -d %{_datadir}/tetra/gradle ]; then
  rm -rf /tmp/gradle*
  cp -Rf %{_datadir}/tetra/gradle* /tmp
else
  cp -Rf %{_datadir}/tetra kit
fi

%build
cd ../src/%{name}-%{version}
export LANG=en_US.UTF-8
export GRADLE_OPTS="-Xmx1024m"
export GRADLE_USER_HOME=/tmp/gradle
export JAVA_HOME=%{_libdir}/jvm/java-%{javaversion}-openjdk-%{javaversion}

# build    -x :distribution:archives:buildOssNoJdkLinuxTar \
gradle \
  --gradle-user-home $GRADLE_USER_HOME \
  --project-cache-dir /tmp/gradle-cache \
  --continue \
  -Dbuild.snapshot=false \
  -Dlicense.key=x-pack/plugin/core/snapshot.key \
  -Dfile.encoding=UTF-8 \
  -Dxpack.ml.enabled=false \
  -Drepos.mavenLocal=true \
  -x :x-pack:plugin:security:generatePomFileForNebulaPublication \
  --offline \
  --no-daemon \
  --no-parallel \
  :distribution:archives:buildOssNoJdkLinuxTar

%clean
rm -rf ~/.gradle

%install
export BUILD_VCS_NUMBER=%{version}
export NO_BRP_CHECK_BYTECODE_VERSION=true
cd %{name}-%{version}


#
# bin

install -d %{buildroot}%{_datadir}
tar -C %{buildroot}%{_datadir} -xf distribution/archives/oss-no-jdk-linux-tar/build/distributions/%{name}-oss-%{version}-no-jdk-linux-x86_64.tar.gz

mv %{buildroot}%{_datadir}/%{name}-%{version} %{buildroot}%{_datadir}/%{name}

# handled as %%doc
rm -f %{buildroot}%{_datadir}/%{name}/README.textile
rm -f %{buildroot}%{_datadir}/%{name}/LICENSE.txt
rm -f %{buildroot}%{_datadir}/%{name}/NOTICE.txt

rm -f %{buildroot}%{_datadir}/%{name}/bin/*.exe
rm -f %{buildroot}%{_datadir}/%{name}/bin/*.bat

#
# var

%{__install} -d -m 755 %{buildroot}%{_localstatedir}/log/%{name}
rmdir %{buildroot}%{_datadir}/%{name}/logs
ln -sf %{_localstatedir}/log/%{name} %{buildroot}%{_datadir}/%{name}/logs
%{__install} -d -m 755 %{buildroot}%{_localstatedir}/lib/%{name}
%{__install} -d -m 755 %{buildroot}%{_localstatedir}/lib/%{name}/data
%{__install} -d -m 755 %{buildroot}%{_localstatedir}/lib/%{name}/work
%{__install} -d -m 755 %{buildroot}%{_rundir}/%{name}

#
# /usr/share
%{__install} -d %{buildroot}%{_datadir}/%{name}

# data

%{__install} -d -m 775 %{buildroot}%{_datadir}/%{name}/data

#
# tmpfiles.d
%{__install} -d -m 755 %{buildroot}%{_tmpfilesdir}
%{__install} -m 644 distribution/packages/src/common/systemd/%{name}.conf %{buildroot}%{_tmpfilesdir}

#
# sbin
%{__install} -d %{buildroot}%{_sbindir}

ln -sf %{_datadir}/%{name}/bin/%{name}-keystore %{buildroot}%{_sbindir}
ln -sf %{_datadir}/%{name}/bin/%{name}-cli %{buildroot}%{_sbindir}

#
# init scripts / systemd

%if 0%{?has_systemd}
%{__install} -D -m 644 distribution/packages/src/common/systemd/elasticsearch.service %{buildroot}%{_unitdir}/%{name}.service
%{__install} -D -m 644 distribution/packages/src/common/systemd/sysctl/%{name}.conf %{buildroot}%{_sysconfdir}/sysctl.d/%{name}.conf
%{__install} -D -m 755 distribution/packages/src/common/systemd/systemd-entrypoint %{buildroot}%{_datadir}/%{name}/bin

# rc%%{name}
ln -sf %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
%else
mkdir -p %{buildroot}%{_initddir}
%{__install} -m 755 distribution/packages/src/rpm/init.d/%{name} %{buildroot}%{_initddir}
ln -sf %{_initddir}/%{name} %{buildroot}%{_sbindir}/rc%{name}
%endif

#
# logrotate

%{__install} -D -m 644 %{S:2} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}

%if 0%{suse_version} < 1500
# only for pre-firewalld distributions
#
# SuSEfirewall2
%{__install} -D -m 644 %{S:7} %{buildroot}%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}
%endif

#
# /etc/elasticsearch
%{__install} -d -m 775 %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 %{buildroot}%{_datadir}/%{name}/config/%{name}.yml %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 %{buildroot}%{_datadir}/%{name}/config/jvm.options %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 %{buildroot}%{_datadir}/%{name}/config/log4j2.properties %{buildroot}%{_sysconfdir}/%{name}
rm -rf %{buildroot}%{_datadir}/%{name}/config
%{__install} -d -m 755 %{buildroot}%{_sysconfdir}/%{name}/scripts

#
# sysconfig template
%{__install} -d -m 755 %{buildroot}%{_fillupdir}
%{__install} -m 644 distribution/packages/src/common/env/%{name} %{buildroot}%{_fillupdir}/sysconfig.%{name}

%if 0%{?suse_version} > 1010
%fdupes %{buildroot}%{_datadir}/%{name}
%endif

%pre
%if 0%{?has_systemd}
%service_add_pre %{name}.service
%endif

## create %%{name} group and user
getent group %{name} >/dev/null || groupadd -r %{name}
getent passwd %{name} >/dev/null || useradd -r -g %{name} -d %{_localstatedir}/lib/%{name} -s /sbin/nologin -c "service user for elasticsearch" %{name}
exit 0

%post
%{fillup_and_insserv -n -y %{name}}
%service_add_post %{name}.service

# rpm is kinda stupid ...
# Create our dirs immediatly, after a manual package install.
# After a reboot systemd/aaa_base will take care.
%if 0%{?has_systemd}
systemd-tmpfiles --create %{_tmpfilesdir}/%{name}.conf
%else
test -d %{_rundir}/%{name} || mkdir -m 755 %{_rundir}/%{name} && chown %{name}.%{name} %{_rundir}/%{name}
%endif

%preun
%if 0%{?has_systemd}
%service_del_preun %{name}.service
%else
%stop_on_removal
%endif

%postun
## no auto restart on update
export DISABLE_RESTART_ON_UPDATE=1

%if 0%{?has_systemd}
%service_del_postun %{name}.service
%else
%insserv_cleanup
%endif

# only execute in case of package removal, not on upgrade
if [ $1 -eq 0 ] ; then
  getent passwd %{name} > /dev/null
  if [ "$?" == "0" ] ; then
    userdel %{name}
  fi

  getent group %{name} >/dev/null
  if [ "$?" == "0" ] ; then
    groupdel %{name}
  fi
fi

%files
%defattr(-,root,root)

%doc %{name}-%{version}/LICENSE.txt
%doc %{name}-%{version}/NOTICE.txt

%dir %{_datadir}/%{name}

%attr(775,root,elasticsearch) %dir %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/%{name}/scripts
%config(noreplace) %attr(644,root,%{name}) %{_sysconfdir}/%{name}/*

%dir %{_fillupdir}
%{_fillupdir}/sysconfig.%{name}

%config %{_sysconfdir}/sysctl.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%if 0%{suse_version} < 1500
%config %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}
%endif

%dir %{_datadir}/%{name}
%{_datadir}/%{name}/*

%if 0%{?has_systemd}
%{_unitdir}/%{name}.service
%{_tmpfilesdir}/%{name}.conf
%else
%attr(755,root,root) %{_initddir}/%{name}
%exclude %{_tmpfilesdir}/%{name}.conf
%dir %{_sysctldir}
%{_sysctldir}/%{name}.conf
%endif

%{_sbindir}/rc%{name}
%{_sbindir}/%{name}-*

%dir %attr(755,%{name},%{name}) %{_localstatedir}/lib/%{name}
%dir %attr(755,%{name},%{name}) %{_localstatedir}/log/%{name}
%dir %attr(755,%{name},%{name}) %{_datadir}/%{name}/plugins
%ghost %dir %attr(755,%{name},%{name}) %{_rundir}/%{name}

%changelog
openSUSE Build Service is sponsored by