Skip to content

Commit

Permalink
build: rpm
Browse files Browse the repository at this point in the history
Add rpm spec and Makefile targets to install and run host-metering from
RPM.

The Makefile targets allow to build it directly on current host (rpm) or
via mock in several build targets.

Signed-off-by: Petr Vobornik <[email protected]>
  • Loading branch information
pvoborni committed Oct 4, 2023
1 parent d1123a0 commit 2d6f5bc
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Build / Release
dist/
vendor/
contrib/rpm/host-metering.spec

# Coverage
coverage.*
Expand Down
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/make -f

PROJECT := host-metering
RPMNAME := host-metering
VERSION := $(shell grep "Version" version/version.go | awk -F '"' '{print $$2}')
SHORT_COMMIT ?= $(shell git rev-parse --short=8 HEAD)
AUTORELEASE ?= "git$(shell date "+%Y%m%d%H%M")G$(SHORT_COMMIT)"

DISTDIR ?= $(CURDIR)/dist
RPMTOPDIR := $(DISTDIR)/rpmbuild

GO := go
TESTDIR := $(CURDIR)/test
Expand Down Expand Up @@ -59,6 +63,44 @@ tarball: distdir vendor
@echo "Compressing the tarball..."
gzip -f $(DISTDIR)/$(PROJECT)-$(VERSION).tar

# RPM build
.PHONY: rpm/spec
rpm/spec:
sed "s/#VERSION#/${VERSION}/" contrib/rpm/host-metering.spec.in > contrib/rpm/$(RPMNAME).spec
sed "s/#AUTORELEASE#/${AUTORELEASE}/" -i contrib/rpm/$(RPMNAME).spec

.PHONY: rpm/srpm
rpm/srpm: tarball rpm/spec
mkdir -p $(RPMTOPDIR)/SOURCES
cp $(DISTDIR)/$(PROJECT)-$(VERSION).tar.gz $(RPMTOPDIR)/SOURCES/
rm -rf $(RPMTOPDIR)/SRPMS/*
rpmbuild --define '_topdir $(RPMTOPDIR)' -bs contrib/rpm/$(RPMNAME).spec

.PHONY: rpm
rpm: rpm/srpm
rpmbuild --define '_topdir $(RPMTOPDIR)' -bb contrib/rpm/$(RPMNAME).spec

.PHONY: rpm/mock
rpm/mock: rpm/srpm
mkdir -p $(DISTDIR)/mock7
mock -r epel-7-x86_64 \
--resultdir=$(DISTDIR)/mock7/ \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)

.PHONY: rpm/mock-8
rpm/mock-8: rpm/srpm
mkdir -p $(DISTDIR)/mock8
mock -r centos-stream-8-x86_64 \
--resultdir=$(DISTDIR)/mock8/ \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)

.PHONY: rpm/mock-9
rpm/mock-9: rpm/srpm
mkdir -p $(DISTDIR)/mock9
mock -r centos-stream-9-x86_64 \
--resultdir=$(DISTDIR)/mock9 \
--rebuild $(RPMTOPDIR)/SRPMS/$(shell ls -1 $(RPMTOPDIR)/SRPMS)

# Clean
.PHONY: clean
clean:
Expand Down
71 changes: 71 additions & 0 deletions contrib/rpm/host-metering.spec.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%bcond_without check

%if 0%{?rhel} <= 7 && ! 0%{?fedora} && ! 0%{?centos}
%define gobuild(o:) scl enable go-toolset-1.19 -- go build -mod vendor -buildmode pie -compiler gc -tags="rpm_crashtraceback ${BUILDTAGS:-}" -ldflags "${GO_LDFLAGS:-} ${LDFLAGS:-} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld'" -a -v %{?**};
%endif
%if 0%{?rhel} <= 7 && ! 0%{?fedora} && 0%{?centos}

%define gobuild(o:) go build -mod vendor -buildmode pie -compiler gc -tags="rpm_crashtraceback ${BUILDTAGS:-}" -ldflags "${GO_LDFLAGS:-} ${LDFLAGS:-} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld'" -a -v %{?**};
%endif

%global goipath github.com/RedHatInsights/host-metering
%global forgeurl https://github.com/RedHatInsights/host-metering/
%global autorelease #AUTORELEASE#
%global gomodulesmode GO111MODULE=on


%global godocs README.md

Name: host-metering
Version: #VERSION#
Release: %{autorelease}%{?dist}
Summary: None

License: Apache-2.0
ExcludeArch: %{ix86} s390 ppc ppc64
URL: %{gourl}

Source: %{name}-%{version}.tar.gz

%if 0%{?rhel} <= 7 && ! 0%{?fedora} && ! 0%{?centos}
BuildRequires: go-toolset-1.19
%else
BuildRequires: golang >= 1.19
BuildRequires: systemd-rpm-macros
%endif
BuildRequires: git

%description
Host metering service

%gopkg

%prep
%setup -q -b 0

# for possible downstream patches
%autopatch -p1

%build
pwd
%gobuild -o $(pwd)/bin/host-metering %{goipath}

%install
install -m 0755 -vd %{buildroot}%{_bindir}
install -m 0755 -vp $(pwd)/bin/* %{buildroot}%{_bindir}/
install -m 0755 -vd %{buildroot}%{_unitdir}
install -m 644 contrib/systemd/host-metering.service %{buildroot}%{_unitdir}/%{name}.service

%if %{with check}
%check
%endif

%files
%doc README.md
%{_bindir}/*
%attr(644,root,root) %{_unitdir}/%{name}.service


%changelog
* Mon Oct 2 2023 Vobornik Petr <[email protected]> - #VERSION#-#AUTORELEASE#
- No changelog. The history is kept in Git, downstreams have own logs.

0 comments on commit 2d6f5bc

Please sign in to comment.