Name:           pbench-agent
Version:        0.71.0
Release:        1g9831f77f3
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

%if 0%{?rhel} < 9
Requires:  ansible
%else
Requires:  ansible-core
%endif

%if 0%{?rhel} == 7
Requires:  python3, python3-pip
# For RHEL boxen we need the python development environment in order for the
# pip3 installs for some modules to be successful.
Requires:  gcc python3-devel
%endif

%if 0%{?rhel} == 8
Requires:  python36, python3-pip
# RPMs for modules in requirements.txt
Requires:  python3-cffi, python3-click, python3-requests
# RPMs for module dependencies
Requires:  python3-docutils, python3-psutil
%endif

%if 0%{?rhel} == 9
Requires:  python3-pip
# RPMs for modules in requirements.txt
Requires:  python3-cffi, python3-requests
# RPMs for module dependencies
Requires:  python3-docutils, python3-psutil
%endif

%if 0%{?fedora} != 0
Requires:  python3, python3-pip
# RPMs for modules in requirements.txt
Requires:  python3-bottle, python3-cffi, python3-click, python3-daemon
Requires:  python3-jinja2, python3-redis, python3-requests, python3-sh
# RPMs for module dependencies
Requires:  python3-psutil
%endif

%if 0%{?rhel} >= 7
%define prefixjsonxs pbench-
%endif
Requires:  perl, perl-Data-UUID, perl-JSON, %{?prefixjsonxs}perl-JSON-XS
Requires:  perl-Time-HiRes

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

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
%if 0%{?rhel} == 7
# This is a very ugly work-around for RHEL 7 / CentOS 7 environments. The
# version of `pip3` that is available in those environments no longer works
# to install the latest Python3 modules, which we need. So we have to fetch
# our own copy of `pip3`, as described at:
#   https://pip.pypa.io/en/latest/installing/#installing-with-get-pip-py 
# We then install it separately from the existing version and set `PATH` and
# `PYTHONPATH` so that we use this updated version when we install our module
# dependencies below.
curl -X GET -o /%{installdir}/get-pip.py https://bootstrap.pypa.io/get-pip.py > /%{installdir}/pip3-update.log 2>&1
python3 /%{installdir}/get-pip.py --prefix=/%{installdir} >> /%{installdir}/pip3-update.log 2>&1
export PYTHONPATH=/%{installdir}/lib/python3.6/site-packages:${PYTHONPATH}
export PATH=/%{installdir}/bin:${PATH}
%endif

# Install python dependencies
pip3 --no-cache-dir install --prefix=/%{installdir} -r /%{installdir}/requirements.txt > /%{installdir}/pip3-install.log

# 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