File logstash.spec of Package logstash

#
# spec file for package logstash
#
# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2015 Thomas Neuburger, t.neuburger@telekom.de
# Copyright 2017 Fujitsu LIMITED
#
# 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/
#


%define _binaries_in_noarch_packages_terminate_build 0
%global _binaries_in_noarch_packages_terminate_build 0

#Compat macro for new _fillupdir macro introduced in Nov 2017
%if ! %{defined _fillupdir}
  %define _fillupdir /var/adm/fillup-templates
%endif

Name:           logstash
Version:        2.4.1
Release:        0
Summary:        A tool for managing events and logs
License:        Apache-2.0
Group:          System/Monitoring
Url:            https://github.com/elastic/logstash
Source:         https://download.elastic.co/logstash/logstash/%{name}-%{version}.tar.gz
Source1:        logstash.service
Source2:        logstash.logrotate
Source3:        logstash.sysconfig
Source4:        logstash.init
Source5:        logstash.conf
Source6:        logstash-rpmlintrc
Patch1:         cve-2018-3817.patch
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
Requires:       java >= 1.7.0
Requires:       logrotate
Requires(pre):  pwdutils
BuildRequires:  fdupes

# needed for brp-check-bytecode-version (jar, fastjar would do as well)
BuildRequires:  unzip

%if 0%{?suse_version} >= 1210
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%bcond_without  systemd
%else
%bcond_with     systemd
%endif

%description
logstash is a tool for managing events and logs. You can use it to collect logs,
parse them, and store them for later use (like, for searching).

%prep
%setup -q -n %{name}-%{version}
%patch1 -p 1

%build

%install
export NO_BRP_CHECK_BYTECODE_VERSION=true

## usr
%{__install} -d -m 755 %{buildroot}/%{_sbindir}

## etc
%{__install} -d -m 755 %{buildroot}%{_sysconfdir}/%{name}/conf.d/
%{__install} -D -m 644 %{S:5} %{buildroot}%{_sysconfdir}/%{name}/sample.conf
%{__install} -D -m 644 %{S:2} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}

## opt
%{__install} -d -m 755 %{buildroot}/opt/%{name}/
%{__install} -m 644 Gemfile %{buildroot}/opt/%{name}/
%{__install} -m 644 Gemfile.jruby-1.9.lock %{buildroot}/opt/%{name}/
%{__install} -m 644 LICENSE %{buildroot}/opt/%{name}/
%{__install} -m 644 CHANGELOG.md %{buildroot}/opt/%{name}/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/bin
%{__install} -m 755 bin/logstash %{buildroot}/opt/%{name}/bin/
%{__install} -m 755 bin/logstash.lib.sh %{buildroot}/opt/%{name}/bin/
%{__install} -m 755 bin/plugin %{buildroot}/opt/%{name}/bin/
%{__install} -m 755 bin/logstash-plugin %{buildroot}/opt/%{name}/bin/
%{__install} -m 755 bin/rspec %{buildroot}/opt/%{name}/bin/
%ifarch x86_64
%{__install} -d -m 755 %{buildroot}/opt/%{name}/lib
cp -rp lib/* %{buildroot}/opt/%{name}/lib/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/jruby/bin
%{__install} -m 755 vendor/jruby/bin/jruby %{buildroot}/opt/%{name}/vendor/jruby/bin/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/jruby/lib
%{__install} -m 644 vendor/jruby/lib/jruby.jar %{buildroot}/opt/%{name}/vendor/jruby/lib/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/jruby/lib/jni
%ifarch x86_64
cp -rp vendor/jruby/lib/jni/x86_64-Linux %{buildroot}/opt/%{name}/vendor/jruby/lib/jni/
%endif
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/jruby/lib/ruby
cp -rp vendor/jruby/lib/ruby/* %{buildroot}/opt/%{name}/vendor/jruby/lib/ruby/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/bundle/jruby/1.9/gems
cp -rp vendor/bundle/jruby/1.9/gems/* %{buildroot}/opt/%{name}/vendor/bundle/jruby/1.9/gems/
rm -rf %{buildroot}/opt/%{name}/vendor/bundle/jruby/1.9/gems/rack-1.6.4/test/
%{__install} -d -m 755 %{buildroot}/opt/%{name}/vendor/bundle/jruby/1.9/specifications
cp -rp vendor/bundle/jruby/1.9/specifications/* %{buildroot}/opt/%{name}/vendor/bundle/jruby/1.9/specifications/
%endif

## var
%{__install} -d -m 755 %{buildroot}/var/log/%{name}/
%{__install} -d -m 755 %{buildroot}/var/lib/%{name}/

## sysconfig template
%{__mkdir} -p %{buildroot}%{_fillupdir}/
%{__install} -m 644 %{S:3} %{buildroot}%{_fillupdir}/sysconfig.%{name}

## service (systemd or sysVinit)
%if %{with systemd}
%{__mkdir} -p %{buildroot}%{_unitdir}
%{__install} -m 444 %{S:1} %{buildroot}%{_unitdir}/%{name}.service
ln -sf /usr/sbin/service %{buildroot}%{_sbindir}/rc%{name}
%else
%{__install} -D -m 755 %{S:4} %{buildroot}%{_initrddir}/%{name}
%{__mkdir} -p %{buildroot}%{_sbindir}
ln -sf %{_initrddir}/%{name} %{buildroot}%{_sbindir}/rc%{name}
%endif

## delet zero files from logstash
find %{buildroot} -size 0 -delete

## finds duplicate files in a given set of directories
%fdupes $RPM_BUILD_ROOT

%pre
## Register service systemd
%if %{with systemd}
%service_add_pre %{name}.service
%endif

## create logstash group and user
getent group logstash >/dev/null || groupadd -r logstash
getent passwd logstash >/dev/null || useradd -r -g logstash -d /var/lib/empty -s /sbin/nologin -c "service user for logstash" logstash
exit 0

%post
## fill up sysconfig file
%{fillup_and_insserv -n -y %{name}}

## Register service systemd
%if %{with systemd}
%service_add_post %{name}.service
%endif

cat <<EOF

================================================================================
Please create logstash pipeline config files in:
    /etc/logstash/conf.d/
Here is a sample pipeline config file: /etc/logstash/sample.conf

Maybe you also have to adjust: /etc/sysconfig/logstash
================================================================================

EOF

%preun
## Stop service (systemd or sysVinit)
%if %{with systemd}
%service_del_preun %{name}.service
%else
%stop_on_removal
%endif

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

## Unregister service (systemd or sysVinit)
%if %{with 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 logstash > /dev/null
    if [ "$?" == "0" ] ; then
        userdel logstash
    fi

    getent group logstash >/dev/null
    if [ "$?" == "0" ] ; then
        groupdel logstash
    fi
fi

%files
%defattr(-,root,root)

%if %{with systemd}
%{_unitdir}/%{name}.service
%else
%{_initrddir}/%{name}
%endif

%{_sbindir}/rc%{name}
%dir /opt/%{name}
/opt/%{name}/bin
/opt/%{name}/lib
/opt/%{name}/vendor
/opt/%{name}/Gemfile.jruby-1.9.lock
%dir /etc/%{name}/
%dir /etc/%{name}/conf.d
%doc /etc/%{name}/sample.conf
%{_fillupdir}/sysconfig.%{name}
%config(noreplace) /etc/logrotate.d/%{name}
%config(noreplace) /opt/%{name}/Gemfile

%license /opt/%{name}/LICENSE
%doc /opt/%{name}/CHANGELOG.md

%defattr(-,logstash,logstash)
%dir /var/log/%{name}
%dir /var/lib/%{name}

%changelog