Name:           pbench-agent
Version:        0.71.0
Release:        3g85910732a
Summary:        The Pbench Agent CLI & default configuration.

License:        GPLv3+
URL:            https://github.com/distributed-system-analysis/pbench
Source0:        pbench-agent-%{version}.tar.gz
Buildarch:      noarch


# RHEL7, RHEL9 (and CentOS-Stream-9) and the Fedoras provide a python3 package
# through the standard distro repos.
# RHEL8 (and CentOS-Stream-8) provide python36, python38 and python39 as modules.
# We stick with python39 for them for now.
# N.B. The condition catches both RHEL8 and CentOS-Stream-8)
%if 0%{?rhel} == 8

Requires:  python39
# This is used by the shebang processor
%define __python3 /usr/bin/python3.9
%define __python_name python39

%else

Requires:  python3
%define __python_name python3

%endif


# RHEL7 is a special case: it does not conform to the (slightly) more
# general pattern of the rest

%if 0%{?rhel} == 7

# RPMs for modules in requirements.txt
Requires: python3-pip, python3-requests
# RPMS for module dependencies
Requires: python3-psutil

# RHEL7 also does not define __python3 - we need it for installing
# requirements through pip so we define it here
%define __python3 /usr/bin/python3

%else

# RPMs for modules in requirements.txt
Requires: %{__python_name}-pip, %{__python_name}-cffi, %{__python_name}-requests
# RPMS for module dependencies
Requires: %{__python_name}-psutil

%endif

# docutils is not available for RHEL7 - it is also *only* available as
# `python3-docutil's on everything else, which is not good for
# RHEL8/CentOS Stream 8 where we would want `python39-docutils', but
# this does not exist. We need to handle it specially: check for
# RHEL9/CentOS Stream 9 OR Fedora

%if 0%{?rhel} > 8 || 0%{?fedora} != 0
Requires: python3-docutils
%endif

# additional packages that Fedora builds but the RHELs don't
%if 0%{?fedora} != 0
# RPMs for modules in requirements.txt
Requires:  python3-bottle, python3-click, python3-daemon
Requires:  python3-jinja2, python3-redis, python3-sh
%endif

%if 0%{?fedora} >= 36
Requires: python3-ifaddr
%endif

# Common requirements
Requires:  perl, perl-Data-UUID, perl-JSON, perl-JSON-XS
Requires:  perl-Time-HiRes

Requires:  bc, bzip2, hostname, iproute, iputils, net-tools, numactl
Requires:  openssh-clients, openssh-server, procps-ng, psmisc, redis
Requires:  rpmdevtools, rsync, screen, sos, tar, xz

# The condition here should be 7 OR 8.4 OR 8.5 but we don't have a way to deal with minor versions.
# The resulting RPM will NOT work on RHEL8.[45].
%if 0%{?rhel} == 7
Requires:  ansible
%else
Requires:  ansible-core
%endif

Obsoletes: pbench <= 0.34
Conflicts: pbench <= 0.34
# configtools is packaged with pbench-agent, so we specifically do NOT want
# the configtools RPM installed.
Conflicts: configtools

Patch0: stockpile-shebang.patch

%define installdir opt/pbench-agent

%description
The Pbench Agent - wraps benchmark workloads to collect specified tool and configuration data.

%prep

%setup
%patch0 -p1

%build

%install
rm -rf %{buildroot}

mkdir -p %{buildroot}/%{installdir}

cp -a agent/* %{?buildroot}/%{installdir}/

%pre
# This RPM conflicts with a configtools RPM, but we may have a PyPI
# configtools installed: zap it.
if pip3 show configtools > /dev/null 2>&1 ;then pip3 uninstall -y configtools ;fi

%post

# Install python dependencies
%{__python3} -m pip --no-cache-dir install --prefix=/%{installdir} -r /%{installdir}/requirements.txt > /%{installdir}/pip3-install.log 2>&1

# link the pbench profile, so it'll automatically be sourced on login
ln -sf /%{installdir}/profile /etc/profile.d/pbench-agent.sh

%preun
# If uninstalling, rather than updating, delete the link
if [ $1 -eq 0 ] ;then
    rm -f /etc/profile.d/pbench-agent.sh
fi

%postun
# If uninstalling, rather than updating, delete the Python3 pip3 install location.
if [ $1 -eq 0 ] ;then
    rm -rf /%{installdir}/bin /%{installdir}/lib /%{installdir}/lib64 /%{installdir}/pip3-install.log

    if [ -e /%{installdir}/id_rsa ] ;then
        echo "WARNING: beware /%{installdir}/id_rsa left behind after uninstall of the pbench-agent RPM." >&2
    fi
fi

%posttrans

%files
%defattr(444,root,root,555)
/%{installdir}/ansible
/%{installdir}/config
/%{installdir}/lib
/%{installdir}/templates

/%{installdir}/base
/%{installdir}/VERSION
/%{installdir}/SEQNO
/%{installdir}/SHA1
/%{installdir}/profile
/%{installdir}/requirements.txt

%config(noreplace) %attr(0664,root,root) /%{installdir}/config/pbench-agent.cfg

%defattr(555,root,root,555)
/%{installdir}/util-scripts
%attr(444,root,root) /%{installdir}/util-scripts/README.md
/%{installdir}/tool-scripts
%attr(444,root,root) /%{installdir}/tool-scripts/meta.json
%attr(444,root,root) /%{installdir}/tool-scripts/perf.README
%attr(444,root,root) /%{installdir}/tool-scripts/README.md
/%{installdir}/bench-scripts
%attr(444,root,root) /%{installdir}/bench-scripts/pbench-fio.md
%attr(444,root,root) /%{installdir}/bench-scripts/pbench-uperf.md
%attr(444,root,root) /%{installdir}/bench-scripts/postprocess/BenchPostprocess.pm
%attr(444,root,root) /%{installdir}/bench-scripts/templates/fio.job
%attr(444,root,root) /%{installdir}/bench-scripts/templates/fio-shared-fs.job

# stockpile
%defattr(444,root,root,555)
/%{installdir}/stockpile
%attr(555,root,root) /%{installdir}/stockpile/import_roles.sh
%attr(555,root,root) /%{installdir}/stockpile/roles/openshift-cluster-topology/files/openshift_config_scraper.py
%attr(555,root,root) /%{installdir}/stockpile/roles/openstack_common/files/openstack-config-parser.py