# Ugly, but we need headers from a kernel to rebuild against
%define kernel %(rpm -q kernel-devel --qf '%{RPMTAG_VERSION}-%{RPMTAG_RELEASE}\\n' 2>/dev/null | head -1)

Summary: HA monitor built upon LVS, VRRP and services poller
Name: keepalived
Version: @VERSION@
Release: 1%{?dist}
License: GPL-2.0-or-later
Group: Applications/System
URL: http://www.keepalived.org/

Source0: http://www.keepalived.org/software/keepalived-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

BuildRequires: openssl-devel
# We need both of these for proper LVS support
BuildRequires: kernel-headers
BuildRequires: autoconf automake pkgconfig
BuildRequires: kmod-devel
@SNMP_TRUE@BuildRequires: net-snmp-devel
@WITH_DBUS_TRUE@BuildRequires: glib2-devel
@IPTABLES_TRUE@BuildRequires: iptables-devel
@LIBIPSET_TRUE@BuildRequires: ipset-devel
@LIBNL1_TRUE@BuildRequires: libnl-devel
@LIBNL3_TRUE@BuildRequires: libnl3-devel
@NFTABLES_TRUE@BuildRequires: libnftnl-devel libmnl-devel
@INIT_SYSV_TRUE@Requires(post): /sbin/chkconfig
@INIT_SYSV_TRUE@Requires(preun): /sbin/service, /sbin/chkconfig
@INIT_SYSV_TRUE@Requires(postun): /sbin/service
@INIT_SYSTEMD_TRUE@BuildRequires: systemd-units
@INIT_SYSTEMD_TRUE@BuildRequires: systemd-devel
@WITH_REGEX_TRUE@BuildRequires: pcre2-devel
@MAGIC_TRUE@BuildRequires: file-devel

Requires: openssl
Requires: kmod-libs
@SNMP_TRUE@Requires: net-snmp
@WITH_DBUS_TRUE@Requires: glib2
@IPTABLES_TRUE@%if 0%{?rhel} <= 7
@IPTABLES_TRUE@Requires: iptables
@IPTABLES_TRUE@%else
@IPTABLES_TRUE@Requires: iptables-libs
@IPTABLES_TRUE@%endif
@LIBIPSET_TRUE@Requires: ipset-libs
@LIBNL1_TRUE@Requires: libnl
@LIBNL3_TRUE@Requires: libnl3
@NFTABLES_TRUE@Requires: libnftnl libmnl
@WITH_REGEX_TRUE@Requires: pcre2

%description
The main goal of the keepalived project is to add a strong & robust keepalive
facility to the Linux Virtual Server project. This project is written in C with
multilayer TCP/IP stack checks. Keepalived implements a framework based on
three family checks : Layer3, Layer4 & Layer5/7. This framework gives the
daemon the ability to check the state of an LVS server pool. When one of the
servers of the LVS server pool is down, keepalived informs the linux kernel via
a setsockopt call to remove this server entry from the LVS topology. In
addition keepalived implements an independent VRRPv2 stack to handle director
failover. So in short keepalived is a userspace daemon for LVS cluster nodes
healthchecks and LVS directors failover.


%prep
%setup


%build
CONFIG_OPTS=
@DEBUG_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-debug"
@PROFILE_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-profile"
@WITH_VRRP_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-vrrp"
@WITH_IPVS_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-lvs"
@NFTABLES_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-nftables"
@IPTABLES_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-iptables"
@IPTABLES_TRUE@@LIBIPSET_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-libipset"
@LIBIPTC_DYNAMIC_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-libiptc-dynamic"
@LIBIPSET_TRUE@@LIBIPSET_DYNAMIC_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-libipset-dynamic"
@LIBNL_DYNAMIC_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-libnl-dynamic"
@SNMP_KEEPALIVED_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp"
@SNMP_KEEPALIVED_FALSE@@SNMP_VRRP_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp-vrrp"
@SNMP_KEEPALIVED_FALSE@@SNMP_CHECKER_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp-checker"
@SNMP_RFC_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp-rfc"
@SNMP_RFC_FALSE@@SNMP_RFCV2_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp-rfcv2"
@SNMP_RFC_FALSE@@SNMP_RFCV3_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-snmp-rfcv3"
@SNMP_RFC_TRUE@@SNMP_REPLY_V3_FOR_V2_FALSE@CONFIG_OPTS="$CONFIG_OPTS --disable-snmp-reply-v3-for-v2"
@WITH_DBUS_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-dbus"
@WITH_JSON_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-json"
@INIT_UPSTART_TRUE@CONFIG_OPTS="$CONFIG_OPTS --with-init=upstart"
@INIT_SYSTEMD_TRUE@CONFIG_OPTS="$CONFIG_OPTS --with-init=systemd"
@INIT_SYSV_TRUE@CONFIG_OPTS="$CONFIG_OPTS --with-init=SYSV"
@INIT_SUSE_TRUE@CONFIG_OPTS="$CONFIG_OPTS --with-init=SUSE"
@INIT_OPENRC_TRUE@CONFIG_OPTS="$CONFIG_OPTS --with-init=openrc"
@WITH_REGEX_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-regex"
@WITH_BFD_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-bfd"
@WITH_STRICT_CONFIG_CHECKS_TRUE@CONFIG_OPTS="$CONFIG_OPTS --enable-strict-config-checks"

autoreconf -f -i
%configure $CONFIG_OPTS
%{__make} %{?_smp_mflags} STRIP=/bin/true


%install
%{__rm} -rf %{buildroot}
%{__make} install DESTDIR=%{buildroot}
# Remove "samples", as we include them in %%doc
%{__rm} -rf %{buildroot}%{_sysconfdir}/keepalived/samples/
# Likewise remove README
%{__rm} -f %{buildroot}%{_docdir}/%{name}/README
# Copy the doc files we want, removing the .pem files
%{__mkdir} -p doc_install/samples
%{__cp} doc/keepalived.conf.SYNOPSIS doc_install
%{__cp} doc/samples/* doc_install/samples
%{__rm} -f doc_install/samples/*.pem

# Rename the SYSV init file if necessary
@INIT_SUSE_TRUE@%{__mv} %{buildroot}%{_initrddir}/keepalived.suse.init %{buildroot}%{_initrddir}/keepalived
@INIT_UPSTART_FALSE@%{__rm} -f %{buildroot}%{_sysconfdir}/init/keepalived.conf

%check
# A build could silently have LVS support disabled if the kernel includes can't
# be properly found, we need to avoid that.
if ! grep -q "#define _WITH_LVS_ *1" lib/config.h; then
    %{__echo} "ERROR: We do not want keepalived lacking LVS support."
    exit 1
fi


%clean
%{__rm} -rf %{buildroot}
%{__rm} -rf doc_install


%post
if [ $1 -eq 1 ]; then
        # Enable (but don't start) the units by default
@INIT_SYSV_TRUE@	/sbin/chkconfig --add keepalived
@INIT_SYSTEMD_TRUE@	/bin/systemctl enable keepalived.service >/dev/null 2>&1 || :
:
fi

%preun
if [ $1 -eq 0 ]; then
        # Disable and stop the units
@INIT_SYSV_TRUE@	/sbin/service keepalived stop &>/dev/null || :
@INIT_SYSV_TRUE@	/sbin/chkconfig --del keepalived
@INIT_SYSTEMD_TRUE@	/bin/systemctl disable keepalived.service >/dev/null 2>&1 || :
@INIT_SYSTEMD_TRUE@	/bin/systemctl stop keepalived.service >/dev/null 2>&1 || :
@INIT_UPSTART_TRUE@	/sbin/stop keepalived >/dev/null 2>&1 || :
:
fi

%postun
if [ $1 -ge 1 ]; then
@INIT_SYSV_TRUE@	/sbin/service keepalived condrestart &>/dev/null || :
	# On upgrade, reload init system configuration if we changed unit files
	# and restart the daemon
@INIT_SYSTEMD_TRUE@	/bin/systemctl daemon-reload >/dev/null 2>&1 || :
@INIT_SYSTEMD_TRUE@	/bin/systemctl try-restart keepalived.service >/dev/null 2>&1 || :
:
fi

%files
%defattr(-, root, root, -)
%doc AUTHOR ChangeLog CONTRIBUTORS COPYING README TODO
%doc doc_install/*
%dir %{_sysconfdir}/keepalived/
%attr(0600,root,root) %{_sysconfdir}/keepalived/keepalived.conf.sample
%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/sysconfig/keepalived
@INIT_SYSTEMD_TRUE@%{_unitdir}/keepalived.service
@INIT_SYSV_TRUE@%{_initrddir}/keepalived
@INIT_UPSTART_TRUE@%{_sysconfdir}/init/keepalived.conf
@SNMP_TRUE@%{_datadir}/snmp/mibs/KEEPALIVED-MIB.txt
@SNMP_RFCV2_TRUE@%{_datadir}/snmp/mibs/VRRP-MIB.txt
@SNMP_RFCV3_TRUE@%{_datadir}/snmp/mibs/VRRPv3-MIB.txt
@WITH_IPVS_TRUE@%{_bindir}/genhash
%attr(0755,root,root) %{_sbindir}/keepalived
@WITH_IPVS_TRUE@%{_mandir}/man1/genhash.1*
%{_mandir}/man5/keepalived.conf.5*
%{_mandir}/man8/keepalived.8*
@WITH_DBUS_TRUE@%attr(0644,root,root) %{_sysconfdir}/dbus-1/system.d/org.keepalived.Vrrp1.conf
@WITH_DBUS_TRUE@%attr(0644,root,root) %{_datarootdir}/dbus-1/interfaces/org.keepalived.Vrrp1.Instance.xml
@WITH_DBUS_TRUE@%attr(0644,root,root) %{_datarootdir}/dbus-1/interfaces/org.keepalived.Vrrp1.Vrrp.xml

%changelog
* Mon Jan 24 2022 Quentin Armitage <quentin@armitage.org.uk> 2.2.7-2
- RedHat has kmod-lib and kmod-devel rather than libkmod and libkmod-devel

* Mon Jan 24 2022 Quentin Armitage <quentin@armitage.org.uk> 2.2.7-1
- Change to install /etc/keepalived/keepalived.conf.sample instead of keepalived.conf

* Wed Nov  1 2017 Quentin Armitage <quentin@armitage.org.uk> 1.3.9-1
- Fix installation of keepalived.service

* Tue Oct 17 2017 Quentin Armitage <quentin@armitage.org.uk> 1.3.8-1
- Handle Fedora and CentOS differences for %{_docdir_fmt}

* Fri Sep 16 2016 Quentin Armitage <quentin@armitage.org.uk> 1.2.24-2
- Fixes to allow building on a systemd based system

* Wed Sep 14 2016 Quentin Armitage <quentin@armitage.org.uk> 1.2.24-1
- Add more BuildRequires

* Tue Sep 13 2016 Quentin Armitage <quentin@armitage.org.uk> 1.2.24
- Update for changed format due of config.log due to using automake
- Add support for systemd and upstart based systems

* Thu Sep 13 2007 Alexandre Cassen <acassen@linux-vs.org> 1.1.14
- Merge work done by freshrpms.net... Thanks guys !!! ;)

* Wed Feb 14 2007 Matthias Saou <http://freshrpms.net/> 1.1.13-5
- Add missing scriplet requirements.

* Tue Feb 13 2007 Matthias Saou <http://freshrpms.net/> 1.1.13-4
- Add missing \n to the kernel define, for when multiple kernels are installed.
- Pass STRIP=/bin/true to "make" in order to get a useful debuginfo package.

* Tue Feb 13 2007 Matthias Saou <http://freshrpms.net/> 1.1.13-3
- Add %%check section to make sure any build without LVS support will fail.

* Mon Feb  5 2007 Matthias Saou <http://freshrpms.net/> 1.1.13-2
- Use our own init script, include a sysconfig entry used by it for options.

* Thu Jan 25 2007 Matthias Saou <http://freshrpms.net/> 1.1.13-1
- Update to 1.1.13.
- Change mode of configuration file to 0600.
- Don't include all of "doc" since it meant re-including all man pages.
- Don't include samples in the main configuration path, they're in %%doc.
- Include patch to add an optional label to interfaces.

* Sat Apr 08 2006 Dries Verachtert <dries@ulyssis.org> - 1.1.12-1.2
- Rebuild for Fedora Core 5.

* Sun Mar 12 2006 Dag Wieers <dag@wieers.com> - 1.1.12-1
- Updated to release 1.1.12.

* Fri Mar 04 2005 Dag Wieers <dag@wieers.com> - 1.1.11-1
- Updated to release 1.1.11.

* Wed Feb 23 2005 Dag Wieers <dag@wieers.com> - 1.1.10-2
- Fixed IPVS/LVS support. (Joe Sauer)

* Tue Feb 15 2005 Dag Wieers <dag@wieers.com> - 1.1.10-1
- Updated to release 1.1.10.

* Mon Feb 07 2005 Dag Wieers <dag@wieers.com> - 1.1.9-1
- Updated to release 1.1.9.

* Sun Oct 17 2004 Dag Wieers <dag@wieers.com> - 1.1.7-2
- Fixes to build with kernel IPVS support. (Tim Verhoeven)

* Fri Sep 24 2004 Dag Wieers <dag@wieers.com> - 1.1.7-1
- Updated to release 1.1.7. (Mathieu Lubrano)

* Mon Feb 23 2004 Dag Wieers <dag@wieers.com> - 1.1.6-0
- Updated to release 1.1.6.

* Mon Jan 26 2004 Dag Wieers <dag@wieers.com> - 1.1.5-0
- Updated to release 1.1.5.

* Mon Dec 29 2003 Dag Wieers <dag@wieers.com> - 1.1.4-0
- Updated to release 1.1.4.

* Fri Jun 06 2003 Dag Wieers <dag@wieers.com> - 1.0.3-0
- Initial package. (using DAR)

