File loki.spec of Package loki

#
# spec file for package loki
#
# Copyright (c) 2022 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%global loki_user loki
%global loki_group %{loki_user}

#Compat macro for new _fillupdir macro introduced in Nov 2017
%if ! %{defined _fillupdir}
  %define _fillupdir /var/adm/fillup-templates
%endif

Name:           loki
Version:        1.5.0
Release:        0
Summary:        Loki: like Prometheus, but for logs.
License:        Apache-2.0
Group:          System/Monitoring
URL:            https://grafana.com/loki
Source:         %{name}-%{version}.tar.bz2
Source1:        loki.service
Source2:        promtail.service
Source3:        sysconfig.loki
Source4:        sysconfig.promtail
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
BuildRequires:  golang-packaging
BuildRequires:  systemd-devel
BuildRequires:  golang(API) = 1.13
Requires(pre):  shadow
Requires(post): %fillup_prereq
%{go_provides}

%description
Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus.

%prep
%setup -q loki-%{version}

%build
%define buildpkg github.com/grafana/loki/pkg/build
export GOFLAGS="-mod=vendor"
export GOLDFLAGS="-s -w -X %{buildpkg}.Version=%{version} -X %{buildpkg}.Revision=NA -X %{buildpkg}.Branch=NA -X %{buildpkg}.BuildUser=NA -X %{buildpkg}.BuildDate=NA"
go build -ldflags="$GOLDFLAGS" ./cmd/loki
go build -ldflags="$GOLDFLAGS" ./cmd/promtail
go build -ldflags="$GOLDFLAGS" ./cmd/logcli

%install

# Service files for Loki and promtail
install -D -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}/loki.service
install -D -m 0644 %{SOURCE2} %{buildroot}%{_unitdir}/promtail.service
install -Dm644 %{SOURCE3} %{buildroot}%{_fillupdir}/sysconfig.loki
install -Dm644 %{SOURCE4} %{buildroot}%{_fillupdir}/sysconfig.promtail
install -dm755 %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rcloki
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rcpromtail

# Config files
install -Dm644 cmd/loki/loki-local-config.yaml \
%{buildroot}%{_sysconfdir}/%{name}/loki.yaml
install -Dm644 cmd/promtail/promtail-local-config.yaml \
%{buildroot}%{_sysconfdir}/%{name}/promtail.yaml

# Binaries
install -dm755 %{buildroot}%{_bindir}
install -D -m 0755 loki %{buildroot}%{_bindir}
install -D -m 0755 promtail %{buildroot}%{_bindir}
install -D -m 0755 logcli %{buildroot}%{_bindir}

# Create user / group and services
%pre
%service_add_pre loki.service
%service_add_pre promtail.service
getent group %{loki_group} > /dev/null || groupadd -r %{loki_group}
getent passwd %{loki_user} > /dev/null || useradd -r -g %{loki_group} -s /sbin/nologin %{loki_user}

%post
%{fillup_only -n loki}
%{fillup_only -n promtail}
%service_add_post loki.service
%service_add_post promtail.service

%preun
%service_del_preun loki.service
%service_del_preun promtail.service

%postun
%service_del_postun loki.service
%service_del_postun promtail.service

%files
%defattr(-,root,root)
%license LICENSE
%doc README.md
%{_unitdir}/loki.service
%{_unitdir}/promtail.service
%{_fillupdir}/sysconfig.loki
%{_fillupdir}/sysconfig.promtail
%{_bindir}/loki
%{_bindir}/promtail
%{_bindir}/logcli
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/loki.yaml
%config(noreplace) %{_sysconfdir}/%{name}/promtail.yaml
%{_sbindir}/rcloki
%{_sbindir}/rcpromtail

%changelog
openSUSE Build Service is sponsored by