File telegraf.spec of Package telegraf

%define realname telegraf
%define realver  1.34.3
%define srcext   tar.gz

# Common info
Name:          %{realname}
Version:       %{realver}
Release:       wiz%{?extraver:0.}1%{?dist}
License:       MIT
Group:         System/Monitoring
URL:           https://influxdata.com/telegraf
Summary:       Agent for collecting, processing, aggregating, and writing metrics and logs

# Build-time parameters
BuildRequires: go >= 1.24.0
Source0:       https://codeload.github.com/influxdata/telegraf/%{srcext}/refs/tags/v%{realver}#/%{realname}-%{version}.%{srcext}
# cd telegraf; go mod vendor -v && tar -Jcvf ../vendor.tar.xz vendor && rm -rf vendor; cd ..
Source1:       vendor.tar.xz
Source99:      telegraf.service

%description
Telegraf, a server-based agent, collects and sends metrics and events from
databases, systems, and IoT sensors. Written in Go, Telegraf compiles into
a single binary with no external dependencies–requiring very minimal memory.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-%{version}%{?extraver} -a1

# Build step (compile/build binaries, documentation, etc)
%build
export GOPATH=$(pwd -P)/.gomodules
export GOCACHE=$(pwd -P)/.gocache
export GOTRACEBACK=crash
export CGO_ENABLED=1
export CGO_CFLAGS='%{optflags} %{?gcc_lto}'
export CGO_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro %{?gcc_lto}'
INTERNAL_PKG=github.com/influxdata/telegraf/internal
# Build binary
go build \
    -a -v -mod=vendor -buildmode=pie -trimpath \
    -gcflags=all="-c=$(nproc) -dwarf=false" \
    -ldflags=all="-s -w -X $INTERNAL_PKG.Commit=refs/tags/v%{version} -X $INTERNAL_PKG.Branch=master -X $INTERNAL_PKG.Version=%{version}" \
    -tags="goplugin" \
    -o telegraf \
    ./cmd/telegraf

# Install built stuff
%install
%{__install} -D -m0755 telegraf %{buildroot}%{_bindir}/telegraf
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/telegraf/telegraf.d/
./telegraf config > %{buildroot}%{_sysconfdir}/telegraf/telegraf.conf
%{__install} -d -m0755 %{buildroot}/var/lib/telegraf
# Systemd unit
%{__install} -D -m0644 %{S:99} %{buildroot}%{_unitdir}/%{name}.service

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

# Files list
%files
%defattr(-,root,root)
%license LICENSE
%doc CHANGELOG.md EXTERNAL_PLUGINS.md README.md SECURITY.md
%doc docs/*
%{_bindir}/telegraf
%dir %{_sysconfdir}/telegraf/telegraf.d/
%dir %{_sysconfdir}/telegraf/
%config(noreplace) %{_sysconfdir}/telegraf/telegraf.conf
%{_unitdir}/%{name}.service
%dir /var/lib/telegraf

%pre
%if "%{expand:%_vendor}" == "suse"
%{service_add_pre %{name}.service}
%endif

%post
%if "%{expand:%_vendor}" == "suse"
%{fillup_only}
%{service_add_post %{name}.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_post %{name}.service}
%endif

%preun
%if "%{expand:%_vendor}" == "suse"
%{service_del_preun %{name}.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_preun %{name}.service}
%endif

%postun
%if "%{expand:%_vendor}" == "suse"
%{service_del_postun %{name}.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_postun %{name}.service}
%endif

%changelog
openSUSE Build Service is sponsored by