File elasticsearch.spec of Package elasticsearch

# spec file for package elasticsearch
# 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

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

# "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:        6.0.0
Release:        0
Summary:        Open Source, Distributed, RESTful Search Engine
License:        Apache-2.0
Group:          Productivity/Databases/Tools
Patch1:         0001-Local-maven-repo.patch
Source1:        init.gradle
Source2:        %{name}.logrotate
Source5:        %{name}.tmpfiles.d
Source7:        %{name}.SuSEfirewall2
Source8:        %{name}.init
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
BuildRequires:  git
# gradle > 4 is needed for openjdk 9
BuildRequires:  gradle > 4
BuildRequires:  libjnidispatch
BuildRequires:  libnative-platform0

%if 0%{?suse_version} > 1010
BuildRequires:  fdupes
# Java 1.7 triggers build problems
BuildRequires:  java-devel >= 1.8
# %%{version}
BuildRequires:  elasticsearch-kit == %{version}

%if 0%{?has_systemd}
BuildRequires:  systemd

# 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
Requires(post): %insserv_prereq  %fillup_prereq

Requires:       java >= 1.8
Requires:       logrotate
# for sysctl:
Requires:       procps
# for elasticsearch-plugin:
Requires:       hostname
Requires:       which
# mkdir, chown in %%pre
Requires(pre):  coreutils

Elasticsearch is a distributed RESTful search engine built for the
cloud. Reference documentation can be found at
and the 'Elasticsearch: The Definitive Guide' book can be found at

%setup -q -c -n src
rm -rf ../kit
cp -Rf %{_datadir}/tetra ../kit
cd %{name}-%{version}
%patch1 -p1

cd ../..
rm -rf ~/.gradle
ln -s `pwd`/kit/gradle ~/.gradle

cd ../src/%{name}-%{version}
export GRADLE_OPTS="-Xmx1024m"
#don't set GRADLE_USER_HOME, forces it to m2 cache
#export GRADLE_USER_HOME=/home/abuild/rpmbuild/BUILD/kit/gradle
gradle \
  --init-script %{S:1} \
  -Dbuild.snapshot=false \
  --offline \
  --no-daemon \

rm -rf ~/.gradle

export BUILD_VCS_NUMBER=%{version}
cd %{name}-%{version}

# bin

install -d %{buildroot}%{_datadir}
tar -C %{buildroot}%{_datadir} -xf distribution/tar/build/distributions/%{name}-%{version}.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}
%{__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}

# tmpfiles.d
%{__install} -d -m 755 %{buildroot}%{_tmpfilesdir}
%{__install} -m 644 %{S:5} %{buildroot}%{_tmpfilesdir}/%{name}.conf

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

# init scripts / systemd

%if 0%{?has_systemd}
%{__install} -D -m 644 distribution/rpm/build/packaging/systemd/elasticsearch.service $RPM_BUILD_ROOT%{_unitdir}/%{name}.service
%{__install} -D -m 644 distribution/src/main/packaging/systemd/sysctl/%{name}.conf $RPM_BUILD_ROOT%{_sysconfdir}/sysctl.d/%{name}.conf

# rc%%{name}
ln -sf %{_sbindir}/service $RPM_BUILD_ROOT%{_sbindir}/rc%{name}
mkdir -p $RPM_BUILD_ROOT%{_initddir}
%{__install} -m 755 %{S:8} $RPM_BUILD_ROOT%{_initddir}/%{name}
ln -sf %{_initddir}/%{name} $RPM_BUILD_ROOT%{_sbindir}/rc%{name}

# logrotate

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

# SuSEfirewall2
%{__install} -D -m 644 %{S:7} %{buildroot}%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}

# /etc/elasticsearch
%{__install} -d -m 755 %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 distribution/rpm/build/packaging/etc/elasticsearch/%{name}.yml %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 distribution/rpm/build/packaging/etc/elasticsearch/jvm.options %{buildroot}%{_sysconfdir}/%{name}
%{__install} -m 644 distribution/rpm/build/packaging/etc/elasticsearch/ %{buildroot}%{_sysconfdir}/%{name}
%{__install} -d -m 755 %{buildroot}%{_sysconfdir}/%{name}/scripts

# sysconfig template
%{__mkdir} -p %{buildroot}%{_localstatedir}/adm/fillup-templates
%{__install} -m 644 distribution/rpm/build/packaging/env/%{name} %{buildroot}%{_localstatedir}/adm/fillup-templates/sysconfig.%{name}

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

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

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

%{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
test -d %{_rundir}/%{name} || mkdir -m 755 %{_rundir}/%{name} && chown %{name}.%{name} %{_rundir}/%{name}

%if 0%{?has_systemd}
%service_del_preun %{name}.service

## no auto restart on update

%if 0%{?has_systemd}
%service_del_postun %{name}.service

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

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


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

%dir %{_datadir}/%{name}

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


%config %{_sysconfdir}/sysctl.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%config %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/%{name}

%dir %{_datadir}/%{name}

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


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