File grafana.spec of Package grafana

%define realname grafana
%define realver  12.0.1
%define srcext   tar.gz

# turn off the generation of debuginfo rpm  (RH9) ??
%global debug_package %{nil}

# Common info
Name:          %{realname}
Version:       %{realver}
Release:       wiz%{?extraver:0.}1%{?dist}
License:       AGPL-3.0
Group:         System/Monitoring
URL:           https://grafana.com/
Summary:       The open and composable observability and data visualization platform

# Build-time parameters
BuildRequires: go >= 1.23.5
BuildRequires: yarn
Source0:       https://codeload.github.com/grafana/grafana/%{srcext}/refs/tags/v%{realver}#/%{realname}-%{realver}%{?extraver}.%{srcext}
# Deps
# cd grafana; PWD=$(pwd -P); GOPATH="$PWD/gomodules" GOCACHE="$PWD/.gocache" go work vendor -v && tar -Jcvf ../go-modules.tar.xz vendor/; rm -rf vendor/; cd -
Source1:       go-modules.tar.xz
# cd grafana; yarn install --immutable && tar -Jcvf ../node-modules.tar.xz node_modules && rm -rf node_modules/; cd -
Source2:       node-modules.tar.xz
# Config(s)
Source11:      grafana.sysconfig
Source12:      grafana.tmpfiles

%description
The open-source platform for monitoring and observability

Grafana allows you to query, visualize, alert on and understand your metrics no
matter where they are stored. Create, explore, and share dashboards with your
team and foster a data-driven culture:

  * Visualizations: Fast and flexible client side graphs with a multitude of
    options. Panel plugins offer many different ways to visualize metrics
    and logs.
  * Dynamic Dashboards: Create dynamic & reusable dashboards with template
    variables that appear as dropdowns at the top of the dashboard.
  * Explore Metrics: Explore your data through ad-hoc queries and dynamic
    drilldown. Split view and compare different time ranges, queries and data
    sources side by side.
  * Explore Logs: Experience the magic of switching from metrics to logs with
    preserved label filters. Quickly search through all your logs or streaming
    them live.
  * Alerting: Visually define alert rules for your most important metrics.
    Grafana will continuously evaluate and send notifications to systems like
    Slack, PagerDuty, VictorOps, OpsGenie.
  * Mixed Data Sources: Mix different data sources in the same graph! You can
    specify a data source on a per-query basis. This works for even
    custom datasources.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-%{realver}%{?extraver} -a1 -a2
# Disable go work sync
%{__sed} -ri '/^build-go:/ s/ update-workspace//' Makefile

# Build step (compile/build binaries, documentation, etc)
%build
# Note: cgo does not work with LTO
_CFLAGS='%{optflags}'
_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro'
# Set go build variables
export GOPATH=$(pwd -P)/gomodules
export GOCACHE=$(pwd -P)/.gocache
export GOTRACEBACK=crash
export CGO_ENABLED=1
export CGO_CFLAGS="$_CFLAGS"
export CGO_CXXFLAGS="$_CFLAGS"
export CGO_LDFLAGS="$_LDFLAGS"
export GOFLAGS="-a -v -mod=vendor -buildmode=pie -gcflags=all=-dwarf=false -ldflags=all=-s"
# Build alermanager
yarn run build
yarn run plugins:build-bundled
%{__make} %{?_smp_mflags} build-go

# Install built stuff
%install
BINDIR=bin/$(go env GOOS GOARCH | tr \\n - | sed 's/-$//')
# Install binaries
%{__install} -D -m0755 $BINDIR/grafana %{buildroot}%{_bindir}/grafana
# Install config(s)
%{__install} -D -m0644 %{S:11} %{buildroot}%{_sysconfdir}/sysconfig/grafana-server
%{__install} -D -m0644 %{S:12} %{buildroot}%{_sysconfdir}/tmpfiles.d/grafana-run-dir.conf
%{__install} -D -m0644 conf/sample.ini %{buildroot}%{_sysconfdir}/grafana/grafana.ini
%{__install} -D -m0640 conf/ldap.toml  %{buildroot}%{_sysconfdir}/grafana/ldap.toml
%{__install} -d -m0755 %{buildroot}%{_unitdir}
%{__sed} -r \
    -e '/^WorkingDirectory/ s|/usr/share/grafana|/var/lib/grafana|' \
    -e '/^ExecStart/ s|/usr/share/grafana/bin|%{_bindir}|' \
    packaging/rpm/systemd/grafana-server.service > %{buildroot}%{_unitdir}/grafana-server.service
%{__install} -D -m0644 packaging/autocomplete/bash_autocomplete %{buildroot}%{_datadir}/bash-completion/completions/grafana
# Create directories
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/grafana/provisioning/access-control
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/grafana/provisioning/alerting
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/grafana/provisioning/dashboards
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/grafana/provisioning/datasources
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/grafana/provisioning/plugins
%{__install} -d -m0755 %{buildroot}/var/lib/grafana
%{__install} -d -m0755 %{buildroot}/var/log/grafana

# Cleanup
%clean
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}

# Files list
%files
%defattr(-,root,root)
%license LICENSE LICENSING.md
%doc CHANGELOG.md NOTICE.md README.md SECURITY.md SUPPORT.md
%doc conf/
%{_bindir}/grafana
%config(noreplace) %{_sysconfdir}/sysconfig/grafana-server
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning/access-control
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning/alerting
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning/dashboards
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning/datasources
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning/plugins
%dir %attr(0755,grafana,grafana) %{_sysconfdir}/grafana/provisioning
%dir %attr(0750,grafana,grafana) %{_sysconfdir}/grafana
%config(noreplace) %attr(0644,grafana,grafana) %{_sysconfdir}/grafana/grafana.ini
%config(noreplace) %attr(0640,grafana,grafana) %{_sysconfdir}/grafana/ldap.toml
%config %{_sysconfdir}/tmpfiles.d/grafana-run-dir.conf
%{_unitdir}/grafana-server.service
%dir %{_datadir}/bash-completion/completions
%dir %{_datadir}/bash-completion
%{_datadir}/bash-completion/completions/grafana
%dir %attr(0755,grafana,grafana) /var/lib/grafana
%dir %attr(0755,grafana,grafana) /var/log/grafana

%pre
getent group  grafana > /dev/null 2>&1 || groupadd -r grafana
getent passwd grafana > /dev/null 2>&1 || useradd -r -g grafana -d /var/lib/grafana -s /sbin/nologin -c "Grafana user" grafana

%post
systemd-tmpfiles --create %{_sysconfdir}/tmpfiles.d/grafana-run-dir.conf

%changelog
openSUSE Build Service is sponsored by