%global service trove
%{!?upstream_version: %global upstream_version %{version}%{?milestone}}

%global common_desc OpenStack DBaaS (codename %{service}) provisioning service.

%global with_doc 1

Name:             openstack-%{service}
Version:          12.1.0
Release:          1
Summary:          OpenStack DBaaS (%{service})

License:          ASL 2.0
URL:              https://wiki.openstack.org/wiki/Trove
Source0:          https://tarballs.openstack.org/%{service}/%{service}-%{upstream_version}.tar.gz

#

Source1:          %{service}.logrotate
Source2:          guest_info

Source10:         %{name}-api.service
Source11:         %{name}-taskmanager.service
Source12:         %{name}-conductor.service
Source13:         %{name}-guestagent.service

BuildArch:        noarch
BuildRequires:    python3-devel
BuildRequires:    python3-setuptools
BuildRequires:    python3-pbr
# BuildRequires:    python3-d2to1
BuildRequires:    python3-sphinx
BuildRequires:    python3-crudini
BuildRequires:    intltool
BuildRequires:    openstack-macros

# to build default config files
BuildRequires:    python3-keystoneauth1
BuildRequires:    python3-keystonemiddleware
BuildRequires:    python3-oslo-config
BuildRequires:    python3-oslo-log
BuildRequires:    python3-oslo-messaging
BuildRequires:    python3-oslo-middleware
BuildRequires:    python3-oslo-policy
BuildRequires:    python3-osprofiler

Requires:         %{name}-api = %{version}-%{release}
Requires:         %{name}-taskmanager = %{version}-%{release}
Requires:         %{name}-conductor = %{version}-%{release}


%description
%{common_desc}

%package common
Summary:          Components common to all OpenStack %{service} services

Requires:         python3-%{service} = %{version}-%{release}

%{?systemd_requires}
BuildRequires:    systemd

Requires(pre):    shadow-utils
Requires:         python3-pbr

%description common
%{common_desc}

This package contains scripts, config and dependencies shared
between all the OpenStack %{service} services.


%package api
Summary:          OpenStack %{service} API service
Requires:         %{name}-common = %{version}-%{release}

%description api
%{common_desc}

This package contains the %{service} interface daemon.


%package taskmanager
Summary:          OpenStack %{service} taskmanager service
Requires:         %{name}-common = %{version}-%{release}

%description taskmanager
%{common_desc}

This package contains the %{service} taskmanager service.


%package conductor
Summary:          OpenStack %{service} conductor service
Requires:         %{name}-common = %{version}-%{release}

%description conductor
%{common_desc}

This package contains the %{service} conductor service.


%package guestagent
Summary:          OpenStack %{service} guest agent
Requires:         python3-pexpect

Requires:         %{name}-common = %{version}-%{release}

%description guestagent
%{common_desc}

This package contains the %{service} guest agent service
that runs within the database VM instance.


%package -n       python3-%{service}
Summary:          Python libraries for %{service}
Requires:         python3-PyMySQL

Requires:         python3-kombu

Requires:         python3-cryptography
Requires:         python3-enum34
Requires:         python3-eventlet
Requires:         python3-iso8601
Requires:         python3-netaddr
Requires:         python3-lxml
Requires:         python3-six
Requires:         python3-stevedore
Requires:         python3-xmltodict

Requires:         python3-webob

# python-migrate instead by python-sqlalchemy-migrate
# Requires:         python-migrate

Requires:         python3-sqlalchemy
Requires:         python3-paste
Requires:         python3-paste-deploy
Requires:         python3-routes

Requires:         python3-troveclient
Requires:         python3-cinderclient
Requires:         python3-designateclient
Requires:         python3-glanceclient
Requires:         python3-heatclient
Requires:         python3-keystoneclient
Requires:         python3-keystonemiddleware
Requires:         python3-neutronclient
Requires:         python3-novaclient
Requires:         python3-swiftclient

Requires:         python3-oslo-concurrency
Requires:         python3-oslo-config
Requires:         python3-oslo-context
Requires:         python3-oslo-db
Requires:         python3-oslo-i18n
Requires:         python3-oslo-log
Requires:         python3-oslo-messaging
Requires:         python3-oslo-middleware
Requires:         python3-oslo-policy
Requires:         python3-oslo-serialization
Requires:         python3-oslo-service
Requires:         python3-oslo-utils

Requires:         python3-osprofiler
Requires:         python3-jsonschema
Requires:         python3-babel
Requires:         python3-jinja2

Requires:         python3-httplib2
Requires:         python3-passlib

%description -n   python3-%{service}
%{common_desc}

This package contains the %{service} python library.

%package -n python3-%{service}-tests
Summary:        Trove tests
Requires:       python3-%{service} = %{version}-%{release}

%description -n python3-%{service}-tests
%{common_desc}

This package contains the Trove test files

%if 0%{?with_doc}
%package doc
Summary:          Documentation for OpenStack %{service}
BuildRequires:    python3-openstackdocstheme
BuildRequires:    python3-sphinx
%description      doc
%{common_desc}

This package contains documentation files for %{service}.
%endif

%prep
%autosetup -n %{service}-%{upstream_version}

# Avoid non-executable-script rpmlint while maintaining timestamps
find %{service} -name \*.py |
while read source; do
  if head -n1 "$source" | grep -F '/usr/bin/env'; then
    touch --ref="$source" "$source".ts
    sed -i '/\/usr\/bin\/env python/{d;q}' "$source"
    touch --ref="$source".ts "$source"
    rm "$source".ts
  fi
done

# Remove the requirements file so that pbr hooks don't add it
# to distutils requires_dist config
%py_req_cleanup

%build
%{__python3} setup.py build

%install
%{__python3} setup.py install -O1 --skip-build --root %{buildroot}

# docs generation requires everything to be installed first

%if 0%{?with_doc}
pushd doc

SPHINX_DEBUG=1 sphinx-build -b html source build/html
# Fix hidden-file-or-dir warnings
rm -fr build/html/.doctrees build/html/.buildinfo

# Create dir link to avoid a sphinx-build exception
mkdir -p build/man/.doctrees/
ln -s .  build/man/.doctrees/man
SPHINX_DEBUG=1 sphinx-build -b man source build/man
mkdir -p %{buildroot}%{_mandir}/man1
install -p -D -m 644 build/man/*.1 %{buildroot}%{_mandir}/man1/

popd
%endif

# Create config file

export PYTHONPATH=$PYTHONPATH:.
oslo-config-generator --namespace trove.config --namespace oslo.messaging --namespace oslo.log --namespace oslo.log --namespace oslo.policy --output-file etc/%{service}/%{service}.conf.sample

# Setup directories
install -d -m 755 %{buildroot}%{_unitdir}
install -d -m 755 %{buildroot}%{_datadir}/%{service}
install -d -m 755 %{buildroot}%{_sharedstatedir}/%{service}
install -d -m 750 %{buildroot}%{_localstatedir}/log/%{service}

# Install Apache site file
install -p -D -m 640 etc/apache2/%{service} %{buildroot}%{_sysconfdir}/apache2/%{service}

# Install config files
install -p -D -m 640 etc/%{service}/%{service}.conf.sample %{buildroot}%{_sysconfdir}/%{service}/%{service}.conf
# Use crudini to set some configuration keys
crudini --set %{buildroot}%{_sysconfdir}/%{service}/%{service}.conf database connection mysql://trove:trove@localhost/trove
crudini --set %{buildroot}%{_sysconfdir}/%{service}/%{service}.conf DEFAULT log_file %{_localstatedir}/log/%{service}/%{service}.log
mv %{buildroot}%{_prefix}/etc/%{service}/api-paste.ini %{buildroot}%{_sysconfdir}/%{service}/api-paste.ini
# Remove duplicate config files under /usr/etc/trove
rmdir %{buildroot}%{_prefix}/etc/%{service}
install -d -m 755 %{buildroot}%{_sysconfdir}/%{service}
install -p -D -m 640 etc/%{service}/%{service}.conf.sample %{buildroot}%{_sysconfdir}/%{service}/trove-taskmanager.conf
install -p -D -m 640 etc/%{service}/%{service}.conf.sample %{buildroot}%{_sysconfdir}/%{service}/trove-conductor.conf
install -p -D -m 640 etc/%{service}/%{service}.conf.sample %{buildroot}%{_sysconfdir}/%{service}/trove-guestagent.conf
install -p -D -m 640 %{SOURCE2} %{buildroot}%{_sysconfdir}/%{service}/guest_info

# Install initscripts
install -p -m 644 %{SOURCE10} %{SOURCE11} %{SOURCE12} %{SOURCE13} %{buildroot}%{_unitdir}

# Install logrotate
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}

# Install pid directory
install -d -m 755 %{buildroot}%{_localstatedir}/run/%{service}

# Remove unneeded in production stuff
rm -fr %{buildroot}%{_bindir}/trove-fake-mode
rm -fr %{buildroot}%{python3_sitelib}/run_tests.*
%pre common
# Origin: http://fedoraproject.org/wiki/Packaging:UsersAndGroups#Dynamic_allocation
USERNAME=%{service}
GROUPNAME=$USERNAME
HOMEDIR=%{_sharedstatedir}/$USERNAME
getent group $GROUPNAME >/dev/null || groupadd -r $GROUPNAME
getent passwd $USERNAME >/dev/null || \
  useradd -r -g $GROUPNAME -G $GROUPNAME -d $HOMEDIR -s /sbin/nologin \
    -c "$USERNAME Daemons" $USERNAME
exit 0

%post api
%systemd_post openstack-trove-api.service
%post taskmanager
%systemd_post openstack-trove-taskmanager.service
%post conductor
%systemd_post openstack-trove-conductor.service
%post guestagent
%systemd_post openstack-trove-guestagent.service

%preun api
%systemd_preun openstack-trove-api.service
%preun taskmanager
%systemd_preun openstack-trove-taskmanager.service
%preun conductor
%systemd_preun openstack-trove-conductor.service
%preun guestagent
%systemd_preun openstack-trove-guestagent.service

%postun api
%systemd_postun_with_restart openstack-trove-api.service
%postun taskmanager
%systemd_postun_with_restart openstack-trove-taskmanager.service
%postun conductor
%systemd_postun_with_restart openstack-trove-conductor.service
%postun guestagent
%systemd_postun_with_restart openstack-trove-guestagent.service


%files
%license LICENSE

%files common
%license LICENSE
%dir %{_sysconfdir}/%{service}
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/%{service}/%{service}.conf
%attr(0640, root, %{service}) %{_sysconfdir}/%{service}/api-paste.ini
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}

%dir %attr(0750, %{service}, root) %{_localstatedir}/log/%{service}
%dir %attr(0755, %{service}, root) %{_localstatedir}/run/%{service}

%{_bindir}/%{service}-manage
%{_bindir}/%{service}-status
%{_bindir}/trove-mgmt-taskmanager

%{_datarootdir}/%{service}

%defattr(-, %{service}, %{service}, -)
%dir %{_sharedstatedir}/%{service}

%files api
%{_bindir}/%{service}-api
%{_unitdir}/%{name}-api.service
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/apache2/%{service}

%files taskmanager
%{_bindir}/%{service}-taskmanager
%{_unitdir}/%{name}-taskmanager.service
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/%{service}/%{service}-taskmanager.conf

%files conductor
%{_bindir}/%{service}-conductor
%{_unitdir}/%{name}-conductor.service
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/%{service}/%{service}-conductor.conf

%files guestagent
%{_bindir}/%{service}-guestagent
%{_unitdir}/%{name}-guestagent.service
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/%{service}/%{service}-guestagent.conf
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/%{service}/guest_info

%files -n python3-%{service}
%license LICENSE
%{python3_sitelib}/%{service}
%{python3_sitelib}/%{service}-%{version}*.egg-info
%exclude %{python3_sitelib}/%{service}/tests

%files -n python3-%{service}-tests
%license LICENSE
%{python3_sitelib}/%{service}/tests

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%{_mandir}/man1/*
%endif

%changelog
* Fri Nov 05 2021 wangxiyuan <wangxiyuan1007@gmail.com> - 12.1.0-1
- Support OpenStack Train release
