File alertmanager.spec of Package alertmanager

%define realname alertmanager
%define realver  0.28.0
%define srcext   tar.gz

%define promu_ver 0.17.0

%define prometheus_user  prometheus
%define prometheus_group prometheus

%if %{defined suse_version}
%if ! %{defined _fillupdir}
%define _fillupdir /var/adm/fillup-templates
%endif
%define sysconffile %{_fillupdir}/sysconfig.alertmanager
%else
%define sysconffile %{_sysconfdir}/sysconfig/alertmanager
%endif

# Common info
Name:          %{realname}
Version:       %{realver}
Release:       wiz%{?extraver:0.}1%{?dist}
License:       Apache-2.0
Group:         System/Monitoring
URL:           https://prometheus.io/docs/alerting/alertmanager/
Summary:       Prometheus Alertmanager

# Build-time parameters
BuildRequires: go >= 1.16
BuildRequires: npm
BuildRoot:     %{_tmppath}/%{name}-root
Source0:       https://codeload.github.com/prometheus/alertmanager/%{srcext}/refs/tags/v%{realver}#/%{realname}-%{realver}%{?extraver}.%{srcext}
Source1:       https://codeload.github.com/prometheus/promu/%{srcext}/refs/tags/v%{promu_ver}#/promu-%{promu_ver}.%{srcext}
# Deps
# cd alertmanager; go mod vendor -v && tar -Jcvf ../vendor-alertmanager.tar.xz vendor && rm -rf vendor/; cd -
Source2:       vendor-alertmanager.tar.xz
# cd promu; go mod vendor -v && tar -Jcvf ../vendor-promu.tar.xz vendor && rm -rf vendor/; cd -
Source3:       vendor-promu.tar.xz
# Configs
Source11:      alertmanager.yml
Source12:      alertmanager.sysconfig
Source13:      alertmanager.service
# WebUI static assets
Source21:      https://github.com/prometheus/alertmanager/releases/download/v%{realver}/alertmanager-web-ui-%{realver}.tar.gz
# react node_modules
# cd alertmanager/ui/react-app; npm install --no-save; cd ../..; tar -Jcvf ../react-node-modules.tar.xz ui/react-app/node_modules; cd ..
Source22:      react-node-modules.tar.xz

%description
The Alertmanager handles alerts sent by client applications such as
the Prometheus server. It takes care of deduplicating, grouping, and
routing them to the correct receiver integrations such as email,
PagerDuty, or OpsGenie. It also takes care of silencing and inhibition
of alerts.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-%{realver}%{?extraver} -a1 -a2
# promu deps
tar -C promu-%{promu_ver} -Jxf %{S:3}
# WebUI static assets
tar -zxf %{S:21}
# react node_modules
tar -Jxf %{S:22}

# 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'
# Add flags to skip debug symbols
%{__sed} -ri \
 -e '/^go:/ a\    cgo: true' \
 -e '/^build:/ a\    static: false' \
 -e '/^\s*flags:/ s|$| -buildmode=pie -gcflags=all=-dwarf=false -v|' \
 -e "/^\s*ldflags:/ a\        -s\n        -w\n        -linkmode external\n        -extldflags '$_LDFLAGS'" \
 .promu.yml
# 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_LDFLAGS="$_LDFLAGS"
# Build promu
cd promu-%{promu_ver}
go build -v -mod=vendor -o ../bin/promu
cd ..
# Build alermanager
%{__make} %{?_smp_mflags} build \
  PROMU=$(pwd -P)/bin/promu

# Install built stuff
%install
# Install binaries
%{__install} -D -m0755 alertmanager %{buildroot}%{_bindir}/alertmanager
%{__install} -D -m0755 amtool       %{buildroot}%{_bindir}/amtool
# Install main config
%{__install} -D -m0660 %{S:11} %{buildroot}%{_sysconfdir}/prometheus/alertmanager.yml
# Install sysconfig file
_SYSCONFFILE=%{sysconffile}
%{__install} -d -m0755 %{buildroot}${_SYSCONFFILE%/*}
%{__sed} \
 -e 's|%%{_sysconfdir}|%{_sysconfdir}|' \
 -e 's|%%{_localstatedir}|%{_localstatedir}|' \
 %{S:12} > %{buildroot}%{sysconffile}
# Systemd unit file
%{__install} -D -m0644 %{S:13} %{buildroot}%{_unitdir}/alertmanager.service
# Storage directory
%{__install} -d -m0755 %{buildroot}%{_localstatedir}/lib/alertmanager

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

# Files list
%files
%defattr(-,root,root)
%doc CHANGELOG.md LICENSE MAINTAINERS.md NOTICE README.md
%doc doc/examples/simple.yml
%{_bindir}/*
%dir %{_sysconfdir}/prometheus
%config(noreplace) %attr(0660,%{prometheus_user},%{prometheus_group}) %{_sysconfdir}/prometheus/alertmanager.yml
%config(noreplace) %{sysconffile}
%{_unitdir}/alertmanager.service
%dir %attr(0770,%{prometheus_user},%{prometheus_group}) %{_localstatedir}/lib/alertmanager

%pre
/usr/sbin/groupadd -r %{prometheus_group} &>/dev/null ||:
/usr/sbin/useradd  -g %{prometheus_group} -s /bin/false -r -M \
  -c "Prometheus monitoring" -d %{_localstatedir}/lib/prometheus %{prometheus_user} &>/dev/null ||:
%if "%{expand:%_vendor}" == "suse"
%{service_add_pre alertmanager.service}
%endif

%if "%{expand:%_vendor}" == "suse"
%post
%{fillup_only}
%{service_add_post alertmanager.service}

%preun
%{service_del_preun alertmanager.service}
%{stop_on_removal alertmanager.service}

%postun
%{service_del_postun alertmanager.service}
%{restart_on_update alertmanager.service}

%endif

%changelog
openSUSE Build Service is sponsored by