Name:           {{product}}
Version:        {{version}}
Release:        {{release}}%{?dist}
Summary:        Scylla is a highly scalable, eventually consistent, distributed, partitioned row DB.
Group:          Applications/Databases

License:        AGPLv3
URL:            http://www.scylladb.com/
Source0:        %{name}-{{version}}-{{release}}.tar
Requires:       {{product}}-server = {{version}} {{product}}-jmx = {{version}} {{product}}-tools = {{version}} {{product}}-tools-core = {{version}} {{product}}-kernel-conf = {{version}} scylla-libgcc73 scylla-libstdc++73
Obsoletes:	scylla-server < 1.1
Obsoletes:      scylla-libgcc72
Obsoletes:      scylla-libstdc++72
Provides:       scylla-libgcc72
Provides:      scylla-libstdc++72

%description
Scylla is a highly scalable, eventually consistent, distributed,
partitioned row DB.
This package installs all required packages for ScyllaDB,  including
{{product}}-server, {{product}}-jmx, {{product}}-tools, {{product}}-tools-core.

# this is needed to prevent python compilation error on CentOS (#2235)
%if 0%{?rhel}
%global __os_install_post    \
    /usr/lib/rpm/redhat/brp-compress \
    %{!?__debug_package:\
    /usr/lib/rpm/redhat/brp-strip %{__strip} \
    /usr/lib/rpm/redhat/brp-strip-comment-note %{__strip} %{__objdump} \
    } \
    /usr/lib/rpm/redhat/brp-strip-static-archive %{__strip} \
    %{!?__jar_repack:/usr/lib/rpm/redhat/brp-java-repack-jars} \
%{nil}
%else
%global __os_install_post    \
    /usr/lib/rpm/brp-compress \
    %{!?__debug_package:\
    /usr/lib/rpm/brp-strip %{__strip} \
    /usr/lib/rpm/brp-strip-comment-note %{__strip} %{__objdump} \
    } \
    /usr/lib/rpm/brp-strip-static-archive %{__strip} \
    /usr/lib/rpm/brp-python-hardlink \
%{nil}
%endif

%files
%defattr(-,root,root)

%prep
%setup -q

%package        server
Group:          Applications/Databases
Summary:        The Scylla database server
License:        AGPLv3
URL:            http://www.scylladb.com/
BuildRequires:  libaio-devel libstdc++-devel cryptopp-devel hwloc-devel numactl-devel libpciaccess-devel libxml2-devel zlib-devel thrift-devel yaml-cpp-devel lz4-devel snappy-devel jsoncpp-devel systemd-devel xz-devel pcre-devel elfutils-libelf-devel bzip2-devel keyutils-libs-devel xfsprogs-devel make gnutls-devel systemd-devel lksctp-tools-devel protobuf-devel protobuf-compiler systemtap-sdt-devel ninja-build cmake python ragel grep kernel-headers
%{?fedora:BuildRequires: boost-devel antlr3-tool antlr3-C++-devel python3 gcc-c++ libasan libubsan python3-pyparsing dnf-yum python2-pystache}
%{?rhel:BuildRequires: scylla-libstdc++73-static scylla-libatomic73-static scylla-boost163-devel scylla-boost163-static scylla-antlr35-tool scylla-antlr35-C++-devel python34 scylla-gcc73-c++, scylla-python34-pyparsing20 yaml-cpp-static pystache python-setuptools}
Requires:       {{product}}-conf systemd-libs hwloc PyYAML python-urwid pciutils pyparsing python-requests curl util-linux python-setuptools pciutils python3-pyudev mdadm xfsprogs
%{?rhel:Requires: python34 python34-PyYAML kernel >= 3.10.0-514}
%{?fedora:Requires: python3 python3-PyYAML}
Conflicts:      abrt
%ifarch x86_64
%define configure_opt --enable-dpdk
%endif

%description server
This package contains ScyllaDB server.

%if 0%{?fedora} >= 27
# Does not able to build with redifning __debug_install_post, disable it
%else
%define __debug_install_post   \
    %{_rpmconfigdir}/find-debuginfo.sh %{?_missing_build_ids_terminate_build:--strict-build-id} %{?_find_debuginfo_opts} "%{_builddir}/%{?buildsubdir}";\
    cp scylla-gdb.py ${RPM_BUILD_ROOT}/usr/src/debug/%{name}-%{version}/;\
    %{nil}
%endif

%build

defines=()

# We want a package compiled with old kernel headers to
# support nowait aio if the user upgrades their kernel
if ! grep -qwr RWF_NOWAIT /usr/include/linux; then
    defines+=("-DRWF_NOWAIT=8")
fi
if ! grep -qwr aio_rw_flags /usr/include/linux; then
    defines+=("-Daio_rw_flags=aio_reserved1")
fi

cflags="--cflags=${defines[*]}"

%if 0%{?fedora}
./configure.py %{?configure_opt} --with=scylla --with=iotune --mode=release "$cflags"
%endif
%if 0%{?rhel}
. /etc/profile.d/scylla.sh
python3.4 ./configure.py %{?configure_opt} --with=scylla --with=iotune --mode=release "$cflags" --static-boost --static-yaml-cpp --compiler=/opt/scylladb/bin/g++-7.3 --c-compiler=/opt/scylladb/bin/gcc-7.3 --python python3.4 --ldflag=-Wl,-rpath=/opt/scylladb/lib64
%endif
ninja-build %{?_smp_mflags} build/release/scylla build/release/iotune

%install
{{#housekeeping}}
./install.sh --root "$RPM_BUILD_ROOT" --housekeeping
{{/housekeeping}}
{{^housekeeping}}
./install.sh --root "$RPM_BUILD_ROOT"
{{/housekeeping}}

%pre server
getent group scylla || /usr/sbin/groupadd scylla 2> /dev/null || :
getent passwd scylla || /usr/sbin/useradd -g scylla -s /sbin/nologin -r -d %{_sharedstatedir}/scylla scylla 2> /dev/null || :

%post server
# Upgrade coredump settings
if [ -f /etc/systemd/coredump.conf ];then
    /usr/lib/scylla/scylla_coredump_setup
fi

# Install capabilities.conf when AmbientCapabilities supported
SYSTEMD_VER=`rpm -q systemd --qf %%{version}`
SYSTEMD_REL=`rpm -q systemd --qf %%{release}|sed -n "s/\([0-9]*\).*/\1/p"`
AMB_SUPPORT=`grep -c ^CapAmb: /proc/self/status`
# AmbientCapabilities supported from v229 but it backported to v219-33 on RHEL7
if [ $SYSTEMD_VER -ge 229 ] || [ $SYSTEMD_VER -eq 219 ] && [ $SYSTEMD_REL -ge 33 ]; then
    if [ $AMB_SUPPORT -eq 1 ]; then
        mkdir -p /etc/systemd/system/scylla-server.service.d/
        cat << EOS > /etc/systemd/system/scylla-server.service.d/capabilities.conf
[Service]
AmbientCapabilities=CAP_SYS_NICE
EOS
    fi
fi
%systemd_post scylla-server.service

%preun server
%systemd_preun scylla-server.service

%postun server
%systemd_postun

%posttrans server
if  [ -d /tmp/%{name}-%{version}-%{release} ]; then
    cp -a /tmp/%{name}-%{version}-%{release}/* /etc/scylla/
    rm -rf /tmp/%{name}-%{version}-%{release}/
fi
ln -sfT /etc/scylla /var/lib/scylla/conf

%clean
rm -rf $RPM_BUILD_ROOT

%files server
%defattr(-,root,root)

%config(noreplace) %{_sysconfdir}/sysconfig/scylla-server
%{_sysconfdir}/security/limits.d/scylla.conf
%attr(0755,root,root) %dir %{_sysconfdir}/scylla.d
%config(noreplace) %{_sysconfdir}/scylla.d/*.conf
%{_docdir}/scylla/README.md
%{_docdir}/scylla/README-DPDK.md
%{_docdir}/scylla/NOTICE.txt
%{_docdir}/scylla/ORIGIN
%{_docdir}/scylla/licenses/
%{_unitdir}/*.service
%{_unitdir}/*.timer
%{_bindir}/scylla
%{_bindir}/iotune
%{_bindir}/scyllatop
%{_sbindir}/scylla*
%{_prefix}/lib/scylla/scylla-blocktune
%{_prefix}/lib/scylla/scylla_blocktune.py
%{_prefix}/lib/scylla/scylla-housekeeping
%{_prefix}/lib/scylla/scylla_prepare
%{_prefix}/lib/scylla/scylla_stop
%{_prefix}/lib/scylla/scylla_setup
%{_prefix}/lib/scylla/node_exporter_install
%{_sbindir}/node_exporter_install
%{_prefix}/lib/scylla/node_health_check
%{_sbindir}/node_health_check
%{_prefix}/lib/scylla/scylla_coredump_setup
%{_prefix}/lib/scylla/scylla_raid_setup
%{_prefix}/lib/scylla/scylla_sysconfig_setup
%{_prefix}/lib/scylla/scylla_bootparam_setup
%{_prefix}/lib/scylla/scylla_ntp_setup
%{_prefix}/lib/scylla/scylla_selinux_setup
%{_prefix}/lib/scylla/scylla_io_setup
%{_prefix}/lib/scylla/scylla_dev_mode_setup
%{_prefix}/lib/scylla/scylla_ec2_check
%{_prefix}/lib/scylla/scylla_kernel_check
%{_prefix}/lib/scylla/scylla_cpuset_setup
%{_prefix}/lib/scylla/scylla_cpuscaling_setup
%{_prefix}/lib/scylla/scylla_fstrim
%{_prefix}/lib/scylla/scylla_fstrim_setup
%{_prefix}/lib/scylla/perftune.py
%{_prefix}/lib/scylla/dpdk-devbind.py
%{_prefix}/lib/scylla/hex2list.py
%{_prefix}/lib/scylla/swagger-ui/dist/*
%{_prefix}/lib/scylla/api/api-doc/*
%{_prefix}/lib/scylla/scyllatop/*
%{_prefix}/lib/scylla/scylla_config_get.py
%{_prefix}/lib/scylla/scylla_util.py
%if 0%{?fedora} >= 27
%{_prefix}/lib/scylla/scylla-gdb.py
%endif
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/data
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/commitlog
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/hints
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/view_hints
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla/coredump
%attr(0755,scylla,scylla) %dir %{_sharedstatedir}/scylla-housekeeping
%ghost /etc/systemd/system/scylla-server.service.d/
%ghost /etc/systemd/system/scylla-server.service.d/capabilities.conf
%ghost /etc/systemd/system/scylla-server.service.d/mounts.conf

%package conf
Group:          Applications/Databases
Summary:        Scylla configuration package
License:        AGPLv3
URL:            http://www.scylladb.com/
Obsoletes:	scylla-server < 1.1

%description conf
This package contains the main scylla configuration file.

%post conf
grep -v api_ui_dir /etc/scylla/scylla.yaml | grep -v api_doc_dir > /tmp/scylla.yaml
echo "api_ui_dir: /usr/lib/scylla/swagger-ui/dist/" >> /tmp/scylla.yaml
echo "api_doc_dir: /usr/lib/scylla/api/api-doc/" >> /tmp/scylla.yaml
mv /tmp/scylla.yaml /etc/scylla/scylla.yaml

# we need to refuse upgrade if current scylla < 1.7.3 && commitlog remains
%pretrans conf
ver=$(rpm -qi scylla-server | grep Version | awk '{print $3}')
if [ -n "$ver" ]; then
    ver_fmt=$(echo $ver | awk -F. '{printf "%d%02d%02d", $1,$2,$3}')
    if [ $ver_fmt -lt 10703 ]; then
        # for <scylla-1.2
        if [ ! -f /usr/lib/scylla/scylla_config_get.py ]; then
            echo
            echo "Error: Upgrading from scylla-$ver to scylla-{{version}} is not supported."
            echo "Please upgrade to scylla-1.7.3 or later, before upgrade to {{version}}."
            echo
            exit 1
        fi
        commitlog_directory=$(/usr/lib/scylla/scylla_config_get.py -g commitlog_directory)
        commitlog_files=$(ls $commitlog_directory | wc -l)
        if [ $commitlog_files -ne 0 ]; then
            echo
            echo "Error: Upgrading from scylla-$ver to scylla-{{version}} is not supported when commitlog is not clean."
            echo "Please upgrade to scylla-1.7.3 or later, before upgrade to {{version}}."
            echo "Also make sure $commitlog_directory is empty."
            echo
            exit 1
        fi
    fi
fi

%files conf
%defattr(-,root,root)
%attr(0755,root,root) %dir %{_sysconfdir}/scylla
%config(noreplace) %{_sysconfdir}/scylla/scylla.yaml
%config(noreplace) %{_sysconfdir}/scylla/cassandra-rackdc.properties
{{#housekeeping}}
%config(noreplace) %{_sysconfdir}/scylla.d/housekeeping.cfg
{{/housekeeping}}


%package kernel-conf
Group:          Applications/Databases
Summary:        Scylla configuration package for the Linux kernel
License:        AGPLv3
URL:            http://www.scylladb.com/
Requires:       kmod

%description kernel-conf
This package contains Linux kernel configuration changes for the Scylla database.  Install this package
if Scylla is the main application on your server and you wish to optimize its latency and throughput.

%post kernel-conf
# We cannot use the sysctl_apply rpm macro because it is not present in 7.0
# following is a "manual" expansion
/usr/lib/systemd/systemd-sysctl 99-scylla-sched.conf >/dev/null 2>&1 || :
/usr/lib/systemd/systemd-sysctl 99-scylla-aio.conf >/dev/null 2>&1 || :

%files kernel-conf
%defattr(-,root,root)
%{_sysctldir}/*.conf

%changelog
* Tue Jul 21 2015 Takuya ASADA <syuu@cloudius-systems.com>
- inital version of scylla.spec
