Name:           pbench-server
Version:        1.0.0
Release:        1gc01043cd4
Summary:        The pbench server bits, including the Pbench Dashboard UI

License:        GPLv3+
URL:            http://github.com/distributed-systems-analysis/pbench
Source0:        pbench-server-%{version}.tar.gz
Buildarch:      noarch


Requires: python3 python3-devel cronie

# policycoreutils for semanage and restorecon - used in pbench-server-activate-create-results-dir
Requires:       policycoreutils
%if 0%{?rhel} != 7
Requires: policycoreutils-python-utils
Requires: libselinux-python3 python3-psycopg2
%else
Requires: policycoreutils-python
Requires: python3-libselinux
%endif

Requires: npm

Requires: python3-alembic python3-aniso8601 python3-boto3 python3-click
Requires: python3-dateutil python3-elasticsearch python3-email-validator
Requires: python3-flask python3-flask-cors

# The following is available on recent Fedora versions, but not in RHEL8,
# so we exclude it on RHEL8: we'll pick it up through `pip3 install'
# as a Pypi package.
%if 0%{?rhel} != 8
Requires:  python3-flask-httpauth
%endif

Requires: python3-flask-migrate python3-flask-restful python3-flask-sqlalchemy
# Requires: python3-Bcrypt-Flask flask-jwt-extended  # Not available
Requires: python3-gunicorn python3-humanize python3-psycopg2 python3-requests
# Requires: pyesbulk>=2.0.1 PyJwt  # Not available
Requires: python3-sqlalchemy

# The following is available on recent Fedora versions, but not in RHEL8,
# so we exclude it on RHEL8: we'll pick it up through `pip3 install'
# as a Pypi package.
%if 0%{?rhel} != 8
Requires: python3-sqlalchemy-utils
%endif

# The following are indirect dependencies -- dependencies of dependencies that
# we currently install via pip -- we require them here so that they will
# already be satisfied during the pip install.  (certifi is required by
# elasticsearch; bcrypt by Bcrypt-Flask; and greenlet by sqlalchemy.)  If/when
# the packages requiring these are installed via RPM, these requirements should
# no longer be necessary.
Requires: python3-certifi python3-bcrypt python3-greenlet

# installdir has to agree with the definition of install-dir in
# pbench-server.cfg, but we can't go out and pluck it from there,
# because we don't know where the config file is. Note that we omit
# the initial / - it is added in every use below.  IMO, that's more
# readable since it appears in the middle of the path in all cases,
# *except* in the %files section (and one instance in the %post
# and %postun sections).

%define installdir opt/pbench-server
%define installdir_db var/www/html/dashboard
%define static html/static

%define __python python3

BuildRequires: yarn nodejs

%description
The pbench Server scripts and Dashboard UI.

%prep

%setup

%build
( cd dashboard && yarn install && yarn build )

%pre
getent group pbench > /dev/null || groupadd pbench
getent passwd pbench > /dev/null || useradd -g pbench -d /home/pbench -c "Pbench user" pbench

%install
rm -rf %{buildroot}

mkdir -p %{buildroot}/%{installdir}
cp -a ./server/* %{buildroot}/%{installdir}

mkdir -p %{buildroot}/%{installdir}/%{static}
cp -a ./web-server/* %{buildroot}/%{installdir}/%{static}

mkdir -p %{buildroot}/%{installdir_db}
cp -a ./dashboard/build/* %{buildroot}/%{installdir_db}

# for the npm install below
mv %{buildroot}/%{installdir}/%{static}/package.json %{buildroot}/%{installdir}

# The %dir directive creates this directory on install, but needs a source
mkdir -p %{buildroot}/var/log/pbench-server/

%post
# Install python dependencies as pbench user into user's site-packages

# First, get an up-to-date pip3: the one installed through the python3 package
# is not always capable of dealing with the options that we pass it below.
# N.B. We redirect both stdout and stderr into the log here.
python3 -m pip install --upgrade pip 2>&1 > /%{installdir}/pip3-install.log

# The newly installed pip3 should be able to deal with the following.
# N.B. We redirect both stdout and stderr into the log here but append the output to
# the already existing file.
su pbench -c "python3 -m pip --no-cache-dir install --user --no-warn-script-location -r /%{installdir}/requirements.txt" >> /%{installdir}/pip3-install.log 2>&1

# The `site` package is Python magic; it runs automatically when Python starts,
# and builds `sys.path`.
#
# For our purposes, this includes looking in the per-user site library and
# adding the site-packages paths plus adding directories captured in *.pth
# files it finds in the "user site" directory.
#
# The following line captures our install path `/${installdir}/lib` in our own
# pbench.pth file for `site` to find when the pbench user runs Python.
#
# That `pbench.pth` file needs to be in the right place, and the command
# 'python3 -m site --user-site` gives us the proper location. The `su pbench`
# ensures that this is run as "pbench" and therefore gives us the proper
# path where `pbench.pth` will be found, without making arbitrary assumptions
# about `site` package behavior.
#
# Now, when the pbench user runs Python, without any PYTHONPATH environment
# variable, the normal Python startup will include both the pbench user's
# site-library paths, and /opt/pbench-server/lib.
su pbench -c "echo /%{installdir}/lib > \$(python3 -m site --user-site)/pbench.pth"

# install node.js modules under /%{installdir}
cd /%{installdir}
rm -rf node_modules
echo 'package-lock=false' >> .npmrc
npm install

# this only handles v0.3
# AFAIK, we don't need all the other modules that package.json includes.
jslist="node_modules/d3/d3.min.js
        node_modules/d3-queue/build/d3-queue.min.js
        node_modules/save-svg-as-png/lib/saveSvgAsPng.js"

# Copy them to the appropriate directory for v0.3. That
# directory should exist already because of the `cp -a'
# of the web-server stuff above, but add an explicit
# mkdir just in case, although it should be a no-op.
mkdir -p /%{installdir}/%{static}/js/v0.3
for x in ${jslist} ;do
    cp $x /%{installdir}/%{static}/js/v0.3
done

# Finally, make sure the installation directory is entirely owned
# by the pbench user.
chown -R pbench.pbench /%{installdir}

%preun

rm -rf \
    "$(python3 -m site --user-site)/pbench.pth"       \
    /%{installdir}/node_modules                       \
    /%{installdir}/.npmrc                             \
    /%{installdir}/package-lock.json                  \
    /%{installdir}/pip3-install.log                   \
    /%{installdir}/%{static}/js/v0.3/d3.min.js        \
    /%{installdir}/%{static}/js/v0.3/d3-queue.min.js  \
    /%{installdir}/%{static}/js/v0.3/saveSvgAsPng.js

%postun
# if uninstalling, rather than updating, remove everything
if [ $1 -eq 0 ] ;then
    crontab=/%{installdir}/lib/crontab/crontab
    if [ -f $crontab ] ;then
        crontab -u pbench -r
    fi
fi

%posttrans

%files

%defattr(644, pbench, pbench, 755)
%dir /var/log/pbench-server

/%{installdir_db}

/%{installdir}
%attr(755, pbench, pbench) /%{installdir}/bin
%attr(644, pbench, pbench) /%{installdir}/bin/pbench-base.sh

%doc /%{installdir}/lib/pbench/server/s3backup/README
%doc /%{installdir}/lib/pbench/common/AUTHORS.log_formatter
%doc /%{installdir}/lib/pbench/common/LICENSE.log_formatter

%license /%{installdir}/%{static}/css/v0.3/LICENSE.TXT
%license /%{installdir}/%{static}/js/v0.3/LICENSE.TXT