Skip to content

Commit 677f234

Browse files
authored
Merge pull request #62 from klihub/packaging/debian
packaging: add ubuntu and debian packaging
2 parents 954e981 + ddb5ab7 commit 677f234

File tree

10 files changed

+256
-27
lines changed

10 files changed

+256
-27
lines changed

Makefile

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ GO_CILINT := golangci-lint
1010
GO_CILINT_CHECKERS := -D unused,staticcheck,errcheck,deadcode,structcheck,gosimple -E golint,gofmt
1111

1212
# Protoc compiler and protobuf definitions we might need to recompile.
13-
PROTOC := protoc
13+
PROTOC := $(shell command -v protoc || echo echo 'WARNING: no protoc, cannot run protoc ')
1414
PROTOBUFS = $(shell find cmd pkg -name \*.proto)
1515
PROTOCODE := $(patsubst %.proto,%.pb.go,$(PROTOBUFS))
1616

@@ -52,13 +52,32 @@ TAR_UPDATE := $(TAR) -uf
5252
TAR_COMPRESS := bzip2
5353
TAR_SUFFIX := bz2
5454

55+
# Metadata for packages, changelog, etc.
56+
USER_NAME ?= $(shell git config user.name)
57+
USER_EMAIL ?= $(shell git config user.email)
58+
BUILD_DATE ?= $(shell date -R)
59+
60+
HOST_DISTRO ?= $(shell [ -f /etc/os-release ] && eval `cat /etc/os-release`; echo $$ID)
61+
DEB_DISTRO ?= ubuntu
62+
5563
# RPM spec files we might want to generate.
5664
SPEC_FILES = $(shell find packaging -name \*.spec.in | sed 's/.spec.in/.spec/g' | uniq)
5765

5866
# Systemd collateral.
5967
SYSTEMD_DIRS = $(shell find cmd -name \*.service -o -name \*.socket | sed 's:cmd/::g;s:/.*::g'|uniq)
6068
SYSCONF_DIRS = $(shell find cmd -name \*.sysconf | sed 's:cmd/::g;s:/.*::g' | uniq)
6169

70+
# Extra options to pass to docker (for instance --network host).
71+
DOCKER_OPTIONS =
72+
73+
# Docker boilerplate/commands to build debian/ubuntu packages.
74+
DOCKER_DEB_BUILD := mkdir -p /build && cd /build && \
75+
git clone /input/cri-resource-manager && cd /build/cri-resource-manager && \
76+
make BUILD_DIRS=cri-resmgr deb
77+
78+
# Where to leave built packages, if/when we build them in containers.
79+
PACKAGES_DIR = packages
80+
6281
# Be quiet by default but let folks override it with Q= on the command line.
6382
Q := @
6483

@@ -71,9 +90,9 @@ all: build
7190

7291
build: $(BUILD_BINS)
7392

74-
install: $(BUILD_BINS) $(foreach dir,$(BUILD_DIRS),install-$(dir)) \
75-
$(foreach dir,$(SYSTEMD_DIRS),install-systemd-$(dir)) \
76-
$(foreach dir,$(SYSCONF_DIRS),install-sysconf-$(dir))
93+
install: $(BUILD_BINS) $(foreach dir,$(BUILD_DIRS),install-bin-$(dir)) \
94+
$(foreach dir,$(BUILD_DIRS),install-systemd-$(dir)) \
95+
$(foreach dir,$(BUILD_DIRS),install-sysconf-$(dir))
7796

7897
clean: $(foreach dir,$(BUILD_DIRS),clean-$(dir)) clean-spec
7998

@@ -96,17 +115,14 @@ bin/%:
96115
cd $$src && \
97116
$(GO_BUILD) $(LDFLAGS) -o ../../bin/$$bin
98117

99-
install-%: bin/%
100-
$(Q)bin=$(patsubst install-%,%,$@); dir=cmd/$$bin; \
118+
install-bin-%: bin/%
119+
$(Q)bin=$(patsubst install-bin-%,%,$@); dir=cmd/$$bin; \
101120
echo "Installing $$bin in $(DESTDIR)$(BINDIR)..."; \
102121
$(INSTALL) -d $(DESTDIR)$(BINDIR) && \
103-
$(INSTALL) -m 0755 -t $(DESTDIR)$(BINDIR) bin/$$bin
122+
$(INSTALL) -m 0755 -t $(DESTDIR)$(BINDIR) bin/$$bin; \
104123

105124
install-systemd-%:
106125
$(Q)bin=$(patsubst install-systemd-%,%,$@); dir=cmd/$$bin; \
107-
if [ ! -f bin/$$bin ]; then \
108-
exit 0; \
109-
fi; \
110126
echo "Installing systemd collateral for $$bin..."; \
111127
$(INSTALL) -d $(DESTDIR)$(UNITDIR) && \
112128
for f in $(shell find $(dir) -name \*.service -o -name \*.socket); do \
@@ -116,9 +132,6 @@ install-systemd-%:
116132

117133
install-sysconf-%:
118134
$(Q)bin=$(patsubst install-sysconf-%,%,$@); dir=cmd/$$bin; \
119-
if [ ! -f bin/$$bin ]; then \
120-
exit 0; \
121-
fi; \
122135
echo "Installing sysconf collateral for $$bin..."; \
123136
$(INSTALL) -d $(DESTDIR)$(SYSCONFDIR)/sysconfig && \
124137
for f in $(shell find $(dir) -name \*.sysconf); do \
@@ -144,7 +157,7 @@ image-%:
144157
fi; \
145158
echo "Vendoring dependencies..."; \
146159
go mod vendor && \
147-
scripts/build/docker-build --network=host $$buildopts $$src; \
160+
scripts/build/docker-build $(DOCKER_OPTIONS) $$buildopts $$src; \
148161
rc=$$?; \
149162
rm -fr vendor; \
150163
exit $$rc
@@ -191,18 +204,17 @@ test:
191204
$(GO_MODULES)
192205

193206
#
194-
# Rules for building dist-tarballs, SPEC-files and RPMs.
207+
# Rule for building dist-tarballs, SPEC files, RPMs, debian collateral, deb's.
195208
#
196209

197210
dist:
198211
$(Q)eval `$(GIT_ID) .` && \
199-
tarid=`echo $$gitversion | tr '+-' '_'` && \
200-
tardir=cri-resource-manager-$$tarid; \
201-
tarball=cri-resource-manager-$$tarid.tar; \
212+
tardir=cri-resource-manager-$$gitversion; \
213+
tarball=cri-resource-manager-$$gitversion.tar; \
202214
echo "Creating $$tarball.$(TAR_SUFFIX)..."; \
203215
rm -fr $$tardir $$tarball* && \
204216
git archive --format=tar --prefix=$$tardir/ HEAD > $$tarball && \
205-
mkdir -p $$tardir && cp git-{version,buildid} $$tardir && \
217+
mkdir -p $$tardir && cp git-version git-buildid $$tardir && \
206218
$(TAR_UPDATE) $$tarball $$tardir && \
207219
$(TAR_COMPRESS) $$tarball && \
208220
rm -fr $$tardir
@@ -211,10 +223,11 @@ spec: clean-spec $(SPEC_FILES)
211223

212224
%.spec:
213225
$(Q)echo "Generating RPM spec file $@..."; \
214-
eval `$(GIT_ID)`; \
215-
tarid=`echo $$gitversion | tr '+-' '_'`; \
216-
cat $@.in | sed "s/__VERSION__/$$tarid/g;s/__BUILDID__/$$gitbuildid/g" \
217-
> $@
226+
eval `$(GIT_ID) .` && \
227+
cp $@.in $@ && \
228+
sed -E -i -e "s/__VERSION__/$$rpmversion/g" \
229+
-e "s/__TARVERSION__/$$gitversion/g" \
230+
-e "s/__BUILDID__/$$gitbuildid/g" $@
218231

219232
clean-spec:
220233
$(Q)rm -f $(SPEC_FILES)
@@ -231,6 +244,59 @@ src.rpm source-rpm: spec dist
231244
cp cri-resource-manager*.tar.bz2 ~/rpmbuild/SOURCES && \
232245
rpmbuild -bs ~/rpmbuild/SPECS/cri-resource-manager.spec
233246

247+
debian/%: packaging/deb.in/%
248+
$(Q)echo "Generating debian packaging file $@..."; \
249+
mkdir -p debian; \
250+
eval `$(GIT_ID) .` && \
251+
tarball=cri-resource-manager-$$gitversion.tar && \
252+
cp $< $@ && \
253+
sed -E -i -e "s/__PACKAGE__/cri-resource-manager/g" \
254+
-e "s/__TARBALL__/$$tarball/g" \
255+
-e "s/__VERSION__/$$debversion/g" \
256+
-e "s/__AUTHOR__/$(USER_NAME)/g" \
257+
-e "s/__EMAIL__/$(USER_EMAIL)/g" \
258+
-e "s/__DATE__/$(BUILD_DATE)/g" \
259+
-e "s/__BUILD_DIRS__/$(BUILD_DIRS)/g" $@
260+
261+
clean-deb:
262+
$(Q)rm -f debian
263+
264+
deb: debian/changelog debian/control debian/rules debian/compat dist
265+
$(Q)if [ -z "$$BUILD_CONTAINER" -a "$(HOST_DISTRO)" != "$(DEB_DISTRO)" ]; then \
266+
$(MAKE) deb-docker-$(DEB_DISTRO); \
267+
exit $$?; \
268+
fi; \
269+
dpkg-buildpackage -uc
270+
271+
deb-docker-%: docker/%-build
272+
$(Q)distro=$(patsubst deb-docker-%,%,$@); \
273+
builddir=build/docker/$$distro; \
274+
outdir=$(PACKAGES_DIR)/$$distro; \
275+
echo "Docker cross-building $$distro packages..."; \
276+
mkdir -p $(PACKAGES_DIR)/$$distro && \
277+
rm -fr $$builddir && mkdir -p $$builddir && \
278+
docker run --rm -ti $(DOCKER_OPTIONS) --user $(shell echo $$USER) \
279+
--env USER_NAME="$(USER_NAME)" --env USER_EMAIL=$(USER_EMAIL) \
280+
-v $$(pwd):/input/cri-resource-manager \
281+
-v $$(pwd)/$$builddir:/build \
282+
-v $$(pwd)/$$outdir:/output \
283+
$$distro-build /bin/bash -c "export DEB_DISTRO=$$distro; $(DOCKER_DEB_BUILD)" && \
284+
cp $$builddir/cri-resource-manager*.* $$outdir && \
285+
rm -fr $$builddir
286+
287+
ubuntu-packages:
288+
$(MAKE) DEB_DISTRO=ubuntu deb
289+
290+
debian-packages:
291+
$(MAKE) DEB_DISTRO=debian deb
292+
293+
# Build a docker image (for distro cross-building).
294+
docker/%: dockerfiles/Dockerfile.%
295+
$(Q)img=$(patsubst docker/%,%,$@); \
296+
docker rm $$img || : && \
297+
echo "Building cross-build docker image $$img..."; \
298+
scripts/build/docker-build-image $$img --container $(DOCKER_OPTIONS)
299+
234300
# Rule for recompiling a changed protobuf.
235301
%.pb.go: %.proto
236302
$(Q)echo "Generating go code ($@) for updated protobuf $<..."; \
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# pull in base + a minimal set of useful packages
2+
FROM debian:stretch as debian-build
3+
4+
ARG GOLANG_VERSION=1.13.3
5+
ARG GOLANG_URLDIR=https://dl.google.com/go
6+
ARG CREATE_USER="test"
7+
ARG USER_OPTIONS=""
8+
ENV PATH /go/bin:/usr/local/go/bin:$PATH
9+
10+
# pull in stuff for cgo
11+
RUN apt-get update && \
12+
apt-get install -y --no-install-recommends \
13+
build-essential fakeroot devscripts \
14+
bash git make sed debhelper wget ca-certificates && \
15+
rm -rf /var/lib/apt/lists/*
16+
17+
RUN arch="$(dpkg --print-architecture)"; \
18+
case "${arch##*-}" in \
19+
amd64) goarch=linux-amd64;; \
20+
i386) goarch=linux-386;; \
21+
armhf) goarch=linux-armv6l;; \
22+
ppc64el) goarch=linux-ppc64le;; \
23+
s390x) goaach=linux-s390x;; \
24+
esac; \
25+
\
26+
wget $GOLANG_URLDIR/go$GOLANG_VERSION.$goarch.tar.gz -O go.tgz && \
27+
tar -C /usr/local -xvzf go.tgz && rm go.tgz && \
28+
\
29+
export PATH="/usr/local/go/bin:$PATH" && \
30+
echo "PATH=/usr/local/go/bin:$PATH" > /etc/profile.d/go.sh && \
31+
go version
32+
33+
RUN [ -n "$CREATE_USER" ] && \
34+
useradd -m -s /bin/bash $CREATE_USER $(echo $USER_OPTIONS | sed 's/__/ /g')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# pull in base + a minimal set of useful packages
2+
FROM ubuntu:18.04 as ubuntu-build
3+
4+
ARG GOLANG_VERSION=1.13.3
5+
ARG GOLANG_URLDIR=https://dl.google.com/go
6+
ARG CREATE_USER="test"
7+
ARG USER_OPTIONS=""
8+
ENV PATH /go/bin:/usr/local/go/bin:$PATH
9+
10+
# pull in stuff for cgo
11+
RUN apt-get update && \
12+
apt-get install -y --no-install-recommends \
13+
build-essential fakeroot devscripts \
14+
bash git make sed debhelper wget ca-certificates && \
15+
rm -rf /var/lib/apt/lists/*
16+
17+
RUN arch="$(dpkg --print-architecture)"; \
18+
case "${arch##*-}" in \
19+
amd64) goarch=linux-amd64;; \
20+
i386) goarch=linux-386;; \
21+
armhf) goarch=linux-armv6l;; \
22+
ppc64el) goarch=linux-ppc64le;; \
23+
s390x) goaach=linux-s390x;; \
24+
esac; \
25+
\
26+
wget $GOLANG_URLDIR/go$GOLANG_VERSION.$goarch.tar.gz -O go.tgz && \
27+
tar -C /usr/local -xvzf go.tgz && rm go.tgz && \
28+
\
29+
export PATH="/usr/local/go/bin:$PATH" && \
30+
echo "PATH=/usr/local/go/bin:$PATH" > /etc/profile.d/go.sh && \
31+
go version
32+
33+
RUN [ -n "$CREATE_USER" ] && \
34+
useradd -m -s /bin/bash $CREATE_USER $(echo $USER_OPTIONS | sed 's/__/ /g')

packaging/deb.in/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__PACKAGE__ (__VERSION__) unstable; urgency=low
2+
3+
* Release build of __PACKAGE__ __VERSION__ for debian/ubuntu.
4+
5+
-- __AUTHOR__ <__EMAIL__> __DATE__

packaging/deb.in/compat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11

packaging/deb.in/control

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Source: __PACKAGE__
2+
Maintainer: __AUTHOR__ <__EMAIL__>
3+
4+
Package: __PACKAGE__
5+
Architecture: any
6+
Description: A CRI Proxy for Hardware Resource Management

packaging/deb.in/rules

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/make -f
2+
#-*- make -*-
3+
4+
DISTRIBUTION = $(shell sed -n "s/^VERSION_CODENAME=//p" /etc/os-release)
5+
VERSION = __VERSION__
6+
PACKAGEVERSION = $(VERSION)
7+
TARBALL = __TARBALL__
8+
URL = http://github.com/intel/cri-resource-manager
9+
10+
%:
11+
dh $@
12+
13+
override_dh_auto_clean:
14+
override_dh_auto_test:
15+
override_dh_auto_build:
16+
override_dh_auto_install:
17+
export PATH="$$PATH:$$(go env GOPATH)/bin"; \
18+
make BUILD_DIRS="__BUILD_DIRS__" install DESTDIR=debian/__PACKAGE__
19+
20+
override_dh_gencontrol:
21+
dh_gencontrol -- -v$(PACKAGEVERSION)

packaging/rpm/cri-resource-manager.spec.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Release: 0
44
Summary: CRI Resource Manager, a CRI proxy with various in-node workload placement policies
55
License: ASL 2.0
66
URL: https://github.com/intel/cri-resource-manager
7-
Source0: https://github.com/intel/cri-resource-manager/archive/cri-resource-manager-%{version}.tar.bz2
7+
Source0: https://github.com/intel/cri-resource-manager/archive/cri-resource-manager-__TARVERSION__.tar.bz2
88
BuildRequires: go >= 1.12, /usr/bin/make, /usr/bin/install
99

1010
%package doc
@@ -19,7 +19,7 @@ placement policies.
1919
LICENSE, README, and any other potential documentation.
2020

2121
%prep
22-
%setup -q -n cri-resource-manager-%{version}
22+
%setup -q -n cri-resource-manager-__TARVERSION__
2323

2424
%build
2525
make build BUILD_DIRS=cri-resmgr

scripts/build/docker-build-image

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
VOLUMES="-v /sys:/sys -v /home:/mnt/host/home"
4+
IMAGE=$1
5+
DOCKERFILE=$IMAGE
6+
shift
7+
8+
while [ -n "$1" ]; do
9+
case $1 in
10+
--volume|-v)
11+
VOLUMES="$VOLUMES -v $2"
12+
shift 2
13+
;;
14+
--container|-c)
15+
if [ -n "$2" -a "${2#-}" = "$2" ]; then
16+
CONTAINER="$2"
17+
shift
18+
else
19+
CONTAINER="$IMAGE"
20+
fi
21+
shift
22+
;;
23+
--dockerfile|-df)
24+
DOCKERFILE=$2
25+
shift 2
26+
;;
27+
*)
28+
PASSTHROUGH="$PASSTHROUGH $1"
29+
shift
30+
;;
31+
esac
32+
done
33+
34+
if [ ! -f $DOCKERFILE ]; then
35+
DOCKERFILE=dockerfiles/Dockerfile.$DOCKERFILE
36+
fi
37+
38+
echo "* Building docker images with"
39+
echo " - Dockerfile: $DOCKERFILE"
40+
echo " - image name: $IMAGE"
41+
echo " - container : $CONTAINER"
42+
echo " - volumes : $VOLUMES"
43+
echo " - options : $PASSTHROUGH"
44+
45+
docker build . \
46+
-f $DOCKERFILE -t $IMAGE \
47+
--build-arg CREATE_USER=$USER \
48+
--build-arg USER_OPTIONS="-u__$(id -u)" \
49+
$PASSTHROUGH || exit 1
50+
51+
docker create --name $CONTAINER $VOLUMES $IMAGE

0 commit comments

Comments
 (0)