From 7381253ee0dbf190933e724cc8d3b83dbfc1fafd Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Mon, 30 Dec 2019 20:47:02 +0100 Subject: [PATCH] build: add an option to compile in a container This makes it possible to build on any platform that supports Linux containers. The container image used for building is created once, or on updating the `scripts/Dockerfile.build` and is cached afterwards. To build the executable in a container, use `make containerized-build` and everything will be done automatically. The executable will also be available on the usual location. Signed-off-by: Niels de Vos --- .gitignore | 3 +++ Makefile | 17 +++++++++++++++++ docs/development-guide.md | 5 ++++- scripts/Dockerfile.devel | 11 +++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 scripts/Dockerfile.devel diff --git a/.gitignore b/.gitignore index 23596df10..1456465ef 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ _output # docker build deploy/cephcsi/image/cephcsi +# build container +.devel-container-id + # git merge files *.orig *.patch diff --git a/Makefile b/Makefile index 05cf33e09..d61c58270 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,11 @@ ifeq ($(origin GOARCH), undefined) GOARCH := $(shell go env GOARCH) endif +SELINUX := $(shell getenforce 2>/dev/null) +ifeq ($(SELINUX),Enforcing) + SELINUX_VOL_FLAG = :z +endif + all: cephcsi test: go-test static-check mod-check @@ -60,6 +65,16 @@ cephcsi: if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi GOOS=linux GOARCH=$(GOARCH) go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/ +.PHONY: containerized-build +containerized-build: .devel-container-id + $(CONTAINER_CMD) run --rm -v $(PWD):/go/src/github.com/ceph/ceph-csi$(SELINUX_VOL_FLAG) $(CSI_IMAGE_NAME):devel make -C /go/src/github.com/ceph/ceph-csi cephcsi + +# create a (cached) container image with dependencied for building cephcsi +.devel-container-id: scripts/Dockerfile.devel + [ ! -f .devel-container-id ] || $(CONTAINER_CMD) rmi $(CSI_IMAGE_NAME):devel + $(CONTAINER_CMD) build -t $(CSI_IMAGE_NAME):devel -f ./scripts/Dockerfile.devel . + $(CONTAINER_CMD) inspect -f '{{.Id}}' $(CSI_IMAGE_NAME):devel > .devel-container-id + image-cephcsi: cephcsi cp _output/cephcsi deploy/cephcsi/image/cephcsi chmod +x deploy/cephcsi/image/cephcsi @@ -75,3 +90,5 @@ clean: go clean -r -x rm -f deploy/cephcsi/image/cephcsi rm -f _output/cephcsi + [ ! -f .devel-container-id ] || $(CONTAINER_CMD) rmi $(CSI_IMAGE_NAME):devel + $(RM) .devel-container-id diff --git a/docs/development-guide.md b/docs/development-guide.md index 7f07fa739..763981232 100644 --- a/docs/development-guide.md +++ b/docs/development-guide.md @@ -36,9 +36,12 @@ it is **highly** encouraged to: ### Building Ceph-CSI -To build ceph-csi run: +To build ceph-csi locally run: `$ make` +To build ceph-csi in a container: +`$ make containerized-build` + The built binary will be present under `_output/` directory. ### Code contribution workflow diff --git a/scripts/Dockerfile.devel b/scripts/Dockerfile.devel new file mode 100644 index 000000000..71c4946c1 --- /dev/null +++ b/scripts/Dockerfile.devel @@ -0,0 +1,11 @@ +FROM ceph/ceph:v14 + +ENV GOPATH=/go + +RUN yum -y install \ + golang \ + make \ + librados-devel \ + librbd-devel \ + && yum -y update \ + && true