# python bluechi module is enabled by default, it can be disabled passing `--define "with_python 0"` option to rpmbuild
%if 0%{!?with_python:1}
%global with_python 1
%endif

# coverage collection is disabled by default , it can be enabled passing `--define "with_coverage 1"` option to rpmbuild
%if 0%{?with_coverage}
%global coverage_flags -Dwith_coverage=true
%endif


Name:		bluechi
Version:	0.10.0
Release:	0.202412141410.gitde238cc%{?dist}
Summary:	A systemd service controller for multi-nodes environments
License:	LGPL-2.1-or-later AND CC0-1.0
URL:		https://github.com/eclipse-bluechi/bluechi
Source0:	bluechi-0.10.0.tar.gz

# Required to apply the patch
BuildRequires:	git-core
BuildRequires:	gcc
# Meson needs to detect C++, because part of inih library (which we don't use) provides C++ functionality
BuildRequires:	gcc-c++
BuildRequires:	meson
BuildRequires:	systemd-devel
BuildRequires:	systemd-rpm-macros
BuildRequires:	golang-github-cpuguy83-md2man

%if 0%{?with_coverage}
BuildRequires:	lcov
BuildRequires: sed
%endif

%description
BlueChi is a systemd service controller for multi-nodes environements with a
predefined number of nodes and with a focus on highly regulated environment
such as those requiring functional safety (for example in cars).


##########################
### bluechi-controller ###
##########################

%package	controller
Summary:	BlueChi service controller
Requires:	systemd
Recommends:	bluechi-selinux

%if 0%{?with_coverage}
Requires:	bluechi-coverage = %{version}-%{release}
%endif

Obsoletes:	hirte < 0.6.0
Provides:	hirte = %{version}-%{release}
Obsoletes:	bluechi < 0.7.0
Provides:	bluechi = %{version}-%{release}

%description controller
BlueChi is a systemd service controller for multi-nodes environements with a
predefined number of nodes and with a focus on highly regulated environment
such as those requiring functional safety (for example in cars).
This package contains the controller service.

%post controller
%systemd_post bluechi-controller.service

%preun controller
%systemd_preun bluechi-controller.service

%postun controller
%systemd_postun_with_restart bluechi-controller.service

%files controller
%ghost %{_sysconfdir}/bluechi/controller.conf
%dir %{_sysconfdir}/bluechi
%dir %{_sysconfdir}/bluechi/controller.conf.d
%dir %{_rundir}/bluechi
%doc README.md
%doc README.developer.md
%license LICENSE
%{_libexecdir}/bluechi-controller
%{_datadir}/dbus-1/interfaces/org.eclipse.bluechi.Job.xml
%{_datadir}/dbus-1/interfaces/org.eclipse.bluechi.Controller.xml
%{_datadir}/dbus-1/interfaces/org.eclipse.bluechi.Monitor.xml
%{_datadir}/dbus-1/interfaces/org.eclipse.bluechi.Node.xml
%{_datadir}/dbus-1/system.d/org.eclipse.bluechi.conf
%{_datadir}/bluechi/config/controller.conf
%{_mandir}/man1/bluechi-controller.*
%{_mandir}/man5/bluechi-controller.conf.*
%{_sysconfdir}/bluechi/controller.conf.d/README.md
%{_unitdir}/bluechi-controller.service
%{_unitdir}/bluechi-controller.socket


#####################
### bluechi-agent ###
#####################

%package agent
Summary:	BlueChi service controller agent
Requires:	systemd
Recommends:	bluechi-selinux

%if 0%{?with_coverage}
Requires:	bluechi-coverage = %{version}-%{release}
%endif

Obsoletes:	hirte-agent < 0.6.0
Provides:	hirte-agent = %{version}-%{release}

%description agent
BlueChi is a systemd service controller for multi-nodes environements with a
predefined number of nodes and with a focus on highly regulated environment
such as those requiring functional safety (for example in cars).
This package contains the node agent.

%post agent
%systemd_post bluechi-agent.service

%preun agent
%systemd_preun bluechi-agent.service

%postun agent
%systemd_postun_with_restart bluechi-agent.service

%files agent
%ghost %{_sysconfdir}/bluechi/agent.conf
%dir %{_sysconfdir}/bluechi
%dir %{_sysconfdir}/bluechi/agent.conf.d
%doc README.md
%license LICENSE
%{_libexecdir}/bluechi-agent
%{_libexecdir}/bluechi-proxy
%{_datadir}/dbus-1/system.d/org.eclipse.bluechi.Agent.conf
%{_datadir}/bluechi-agent/config/agent.conf
%{_datadir}/dbus-1/interfaces/org.eclipse.bluechi.Agent.xml
%{_mandir}/man1/bluechi-agent.*
%{_mandir}/man1/bluechi-proxy.*
%{_mandir}/man5/bluechi-agent.conf.*
%{_sysconfdir}/bluechi/agent.conf.d/README.md
%{_unitdir}/bluechi-agent.service
%{_userunitdir}/bluechi-agent.service
%{_unitdir}/bluechi-proxy@.service
%{_userunitdir}/bluechi-proxy@.service
%{_unitdir}/bluechi-dep@.service
%{_userunitdir}/bluechi-dep@.service


#######################
### bluechi-selinux ###
#######################

%package selinux
Summary:	BlueChi SELinux policy
BuildArch:	noarch
BuildRequires:	checkpolicy
BuildRequires:	selinux-policy-devel

%if "%{_selinux_policy_version}" != ""
Requires:	selinux-policy >= %{_selinux_policy_version}
%endif

Requires(post):	policycoreutils

Obsoletes:	hirte-selinux < 0.6.0
Provides:	hirte-selinux = %{version}-%{release}

%global selinuxtype targeted

%description selinux
SELinux policy associated with the bluechi and bluechi-agent daemons

%files selinux
%{_datadir}/selinux/devel/include/services/bluechi.if
%{_datadir}/selinux/packages/bluechi.pp.bz2
%{_mandir}/man8/bluechi*selinux.*

%post selinux
# Remove hirte policy
if [ $1 -eq 1 ]; then
   semodule -N -X 200 -r hirte 2>/dev/null || true
fi
%selinux_modules_install -s %{selinuxtype} %{_datadir}/selinux/packages/bluechi.pp.bz2
restorecon -R %{_bindir}/bluechi* &> /dev/null || :

%postun selinux
if [ $1 -eq 0 ]; then
   %selinux_modules_uninstall -s %{selinuxtype} bluechi
   restorecon -R %{_bindir}/bluechi* &> /dev/null || :
fi


###################
### bluechi-ctl ###
###################

%package ctl
Summary:	BlueChi service controller command line tool
Requires:	%{name} = %{version}-%{release}

%if 0%{?with_coverage}
Requires:	bluechi-coverage = %{version}-%{release}
%endif

Obsoletes:	hirte-ctl < 0.6.0
Provides:	hirte-ctl = %{version}-%{release}

%description ctl
BlueChi is a systemd service controller for multi-nodes environements with a
predefined number of nodes and with a focus on highly regulated environment
such as those requiring functional safety (for example in cars).
This package contains the service controller command line tool.

%files ctl
%doc README.md
%license LICENSE
%{_bindir}/bluechictl
%{_mandir}/man1/bluechictl.*


#########################
### bluechi-is-online ###
#########################

%package is-online
Summary:	Command line tool to monitor the connection state of BlueChi's components
Recommends:	bluechi-controller = %{version}-%{release}
Recommends:	bluechi-agent = %{version}-%{release}

%if 0%{?with_coverage}
Requires:	bluechi-coverage = %{version}-%{release}
%endif

%description is-online
BlueChi is a systemd service controller for multi-nodes environements with a
predefined number of nodes and with a focus on highly regulated environment
such as those requiring functional safety (for example in cars).
This package contains a command line tool for checking and monitoring the
connection state of BlueChi's core components.

%files is-online
%doc README.md
%license LICENSE
%{_bindir}/bluechi-is-online
%{_mandir}/man1/bluechi-is-online.*


#######################
### python3-bluechi ###
#######################

%if %{with_python}
%package -n python3-bluechi
Summary:	Python bindings for BlueChi
BuildArch:	noarch
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
Requires:	python3-dasbus

Obsoletes:	python3-hirte < 0.6.0
Provides:	python3-hirte = %{version}-%{release}

%description -n python3-bluechi
bluechi is a python module to access the public D-Bus API of BlueChi project.
It contains typed python code that is auto-generated from BlueChi's
API description and manually written code to simplify recurring tasks.

%files -n python3-bluechi
%license LICENSE
%doc README.md
%{python3_sitelib}/bluechi-*.egg-info/
%{python3_sitelib}/bluechi/
%endif


########################
### bluechi-coverage ###
########################

%if 0%{?with_coverage}
%package coverage
Summary:	Code coverage files for BlueChi

%description coverage
This package contains code coverage files created during the build. Those files
will be used during integration tests when creating code coverage report.

%files coverage
%license LICENSE
%{_datadir}/bluechi-coverage/bin/*
%{_datadir}/bluechi-coverage/*
%dir %{_localstatedir}/tmp/bluechi-coverage/
%endif



%prep
%autosetup -S git_am -n bluechi-0.10.0


%build
%meson -Dapi_bus=system %{?coverage_flags}
%meson_build

%if %{with_python}
pushd src/bindings/python
%py3_build
popd
%endif


%install
%meson_install

%if 0%{?with_coverage}
mkdir -p %{buildroot}/%{_datadir}/bluechi-coverage/bin
cp tests/scripts/gather-code-coverage.sh %{buildroot}/%{_datadir}/bluechi-coverage/bin
cp tests/scripts/setup-src-dir-for-coverage.sh %{buildroot}/%{_datadir}/bluechi-coverage/bin

mkdir -p %{buildroot}/%{_datadir}/bluechi-coverage/unit-test-results

mkdir -p %{buildroot}/%{_localstatedir}/tmp/bluechi-coverage/
%endif

%if %{with_python}
pushd src/bindings/python
%py3_install
popd
%endif


%check
%meson_test

%if 0%{?with_coverage}
# Generate code coverage report from unit tests execution
build-scripts/generate-unit-tests-code-coverage.sh %{_vpath_builddir} %{buildroot}/%{_datadir}
%endif


%changelog
* Tue Nov 12 2024 Michael Engel <mengel@redhat.com> - 0.9.0-1
- BlueChi's SELinux policy has been refined and changed from permissive to enforced
- New package: bluechi-is-online
- Added a configurable heartbeat and threshold from bluechi-controller to bluechi-agent
- Added a configurable threshold in bluechi-controller to actively disconnect an unresponsive bluechi-agent
- Added monotonic timestamp properties to D-Bus API
- Added GetUnitFileState method to D-Bus API and is-enabled command to bluechictl
- Added KillUnit method to D-Bus API and kill command to bluechictl
- Added ResetFailed and ResetFailedUnit to D-Bus API and respective commands to bluechictl
- Added ListUnitFiles to D-Bus API and list-unit-files command to bluechictl
- Added GetDefaultTarget and SetDefaultTarget to D-Bus API and respective commands to bluechictl
- Added specific usage functions for commands to bluechictl 
- Changed the register call of bluechi-agent to be asynchronous
- Fixed the order of disconnect and controller address changed on calling SwitchController
- Fixed updating the displayed peer IP when a node changes connection state in bluechictl
- Fixed determining the peer IP address of the agent
- Improved debug logging in bluechi-agent, bluechi-controller and bluechictl
- Updated the python bindings for BlueChi's D-Bus API

* Mon Apr 29 2024 Michael Engel <mengel@redhat.com> - 0.8.0-1
- Breaking change: Removed obsolete API method Ping
- Fixed .conf suffix check for conf.d files
- Fixed memory leak in bluechictl
- Fixed memory leaks in bluechi-agent
- Fixed bluechictl when fetching status of multiple services
- Fixed segfault when calling set-loglevel API with invalid loglevel
- Fix memory deallocation issue in node_method_set_log_level()
- Cleanup controller when start fails to prevent memory leaks
- Added watch option to bluechictl status
- Changed output of bluechictl status from stderr to stdout
- Implemented D-Bus API for cancelling a Job
- Added IP to of registered nodes as D-Bus property and to the ListUnits API
- Added D-Bus properties for LogLevel and LogTarget of bluechi-controller and bluechi-agent
- Added D-Bus on bluechi-agent to switch the bluechi-controller
- Refactored functions for loading the configuration
- Improved the output of bluechictl list-units
- Improved the formatted output of the dumped config of bluechi-controller and bluechi-agent on startup
- bluechictl monitor flushes stdout for each iteration
- Replaced the function for the D-Bus property of the node name by offsetof
- Removed unused code bluechi-controller and libbluechi
- BlueChi's SELinux policy has been extended by a tunable policy to dis-/allow any port
- BlueChi's python bindings are now formatted and ordered via unified tooling
- Updated the python bindings for BlueChi's D-Bus API

* Wed Jan 17 2024 Michael Engel <mengel@redhat.com> - 0.7.0-1
- Breaking change: Completed renaming of manager to controller in configuration, API, etc.
- Breaking change: Removed D-Bus method Shutdown from controller and agent
- Breaking change: Removed bluechictl monitor node-connection
- Added watch option to bluechictl for bluechictl status
- Added system status property on org.eclipse.bluechi.Manager
- Introduced pre-shutdown hook to ensure final signals are emitted
- Added flags to enable and disable commands in bluechictl
- Refactored bluechictl and introduced command pattern
- Added TCP KeepAlive options to configuration
- Added IP_RECV_ERR option to configuration
- Enable quicker tcp error notification by setting IP_RECV_ERR
- Extended monitor API by adding and removing peers
- Send monitor signals only to peer
- Added log statements when sending monitor signals fail
- Upgraded subproject hashmap.c to v0.7.0
- Check for proxy service list being empty instead of NULL on shutdown
- Fixed check that agent started successfully
- Fixed memory leak when assembling monitoring signals
- Added error log when at least one list units request fails
- Integration tests are run with valgrind on testing farm
- Integration tests produce a coverage report in GH workflow
- Added more integration tests for new peer API, -c option and more
- BlueChi logo has been added to the project
- Added documentation about monitoring has been added
- Added documentation about securint BlueChi with mTLS and double proxy

* Mon Nov 13 2023 Michael Engel <mengel@redhat.com> - 0.6.0-1
- Moved from containers to eclipse-bluechi organization
- Renamed bluechi to bluechi-controller for binary, rpm and documentation
- Snapshot builds are now available in the centos-sig-automotive COPR group
- Moved bluechi binaries to /usr/libexec for auto-completion
- Introduced packit for builds and running integration tests on testing farm
- Introduced initial implementation for a tool to test FFI of BlueChi
- Introduced clang/LLVM support
- Added properties and signals for connection status and disconnected timestamp to Agent's public API
- Removed duplicate NodeConnectionStateChanged signal from bluechi-controller
- CLI option for the version (-v) prints version and git commit hash for non-release builds
- Extended BlueChi's public D-Bus API specification by inline-comments
- Added EmitsChangedSignal annotation to properties in BlueChi's public D-Bus API specification
- Enhanced typed python bindings generator to use inline-comments from specification
- Enhanced typed python bindings generator to provide listener functions for property changed signals
- Fixes in the D-Bus API description
- Improved error messages returned by D-Bus API
- Added static code analysis from gcc and fixed detected issues
- Added a graceful node shutdown in bluechi-controller
- Fixed a few smaller memory leaks
- Fixed bug where configured manager address was overridden on connection failure
- Fixed bug where removing a subscription was not prevented
- Fixed race condition leading bluechi-proxy and bluechi-dep service to transition into failed state
- NULLing pointers after free for better code hygiene, preventing invalid reads
- Extended integration tests to run with valgrind to detect memory leaks
- Added more integration tests
- Reworked Getting Started documentation
- Reworked Proxy Service documentation and added examples and limitations
- Aligned and added API examples for Python, Go and Rust
- Changed the license for python bindings to MIT-0

* Mon Sep 04 2023 Michael Engel <mengel@redhat.com> - 0.5.0-1
- Changing the name of the project from hirte to BlueChi
- Changing the license of the project from GPL-2.0-or-later to LGPL-2.1-or-later
- Changing the license of API examples to CC0-1.0
- Changing the license of typed python bindings package to CC0-1.0
- New feature: freeze and thaw units via BlueChi
- Added CLI option to all binaries to print the version
- A heartbeat interval of 0 disables it instead of spamming the signal
- A disconnecting, anonymous node doesn't lead to a segfault anymore
- Fixed a few smaller memory leaks
- Increased the maximum line length and added multi-line support for configuration files
- Improved error message when parsing configuration file fails
- Python bindings use the property annotation for D-Bus properties
- Fixes in the typed python bindings generator
- Fixes in the D-Bus API description
- The default log target has been set to journald
- Updates to Mkdocs like adding GoLang and Rust example
- Added more unit and integration tests

* Mon Jul 10 2023 Michael Engel <mengel@redhat.com> - 0.4.0-1
- Defaults for hirtectl monitor defaults defined and documented
- Dedicated signal is emitted when a node changes its connection state
- Added a last seen timestamp of a node as a D-Bus property
- Added new command to hirtectl to monitor node connections
- Support of FQDN in ManagerHost
- Support of IPv6 in ManagerHost
- Added hirtectl command to get the status of a unit
- Autogenerated, typed python bindings
- New rpm package for the typed python bindings of hirte's D-Bus API
- API and hirtectl commands for setting the loglevel at runtime
- Fixes in the D-Bus API description

* Wed May 31 2023 Michael Engel <mengel@redhat.com> - 0.3.0-1
- API and hirtectl commands for enabling, disabling and reloading of systemd units added
- Glob filter option to hirtectl list-units
- Collecting metrics for unit startup time
- Improvements for building hirte RPMs
- Allow hirte to run with unconfined user
- Aligned naming of hirte config files
- Fixed for invalid type cast of ProxyMonitor
- Mkdocs-based documentation added
- Wildcard support for monitoring units

* Wed May 03 2023 Michael Engel <mengel@redhat.com> - 0.2.1-1
- RPM and unit test fixes

* Tue Apr 25 2023 Michael Engel <mengel@redhat.com> - 0.2.0-1
- Heartbeat interval configurable
- Restart for hirte and hirte-agent systemd unit added
- Logging improvements
- SELinux policy added
- Defaults for node name and manager host in code
- Fix conf.d usage and memory leaks

* Tue Mar 21 2023 Martin Perina <mperina@redhat.com> - 0.1.0-1
- Initial release