From b89f7fa1d648abe6753e18eacebbb374b369700b Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Mon, 4 May 2020 10:28:40 +0200 Subject: [PATCH] build: add check for functional environment Updates: #1000 Reported-by: Madhu Rajanna Signed-off-by: Niels de Vos --- Makefile | 13 ++++++++----- scripts/check-env.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100755 scripts/check-env.sh diff --git a/Makefile b/Makefile index 5de0d33c6..e52fba9c2 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -.PHONY: all cephcsi +.PHONY: all cephcsi check-env CONTAINER_CMD?=$(shell docker version >/dev/null 2>&1 && echo docker) ifeq ($(CONTAINER_CMD),) @@ -62,12 +62,12 @@ all: cephcsi .PHONY: go-test static-check mod-check go-lint go-lint-text gosec test: go-test static-check mod-check -static-check: go-lint go-lint-text gosec +static-check: check-env go-lint go-lint-text gosec -go-test: +go-test: check-env ./scripts/test-go.sh -mod-check: +mod-check: check-env @echo 'running: go mod verify' @go mod verify && [ "$(shell sha512sum go.mod)" = "`sha512sum go.mod`" ] || ( echo "ERROR: go.mod was modified by 'go mod verify'" && false ) @@ -83,8 +83,11 @@ gosec: func-test: go test -mod=vendor github.com/ceph/ceph-csi/e2e $(TESTOPTIONS) +check-env: + @./scripts/check-env.sh + .PHONY: cephcsi -cephcsi: +cephcsi: check-env if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi GOOS=linux go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/ diff --git a/scripts/check-env.sh b/scripts/check-env.sh new file mode 100755 index 000000000..e530362b8 --- /dev/null +++ b/scripts/check-env.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# +# Check the environment for dependencies and configuration +# + +# count errors, run script to the end before exiting +ERRORS=0 +fail() { + echo "${*}" > /dev/stderr + ERRORS=$((ERRORS+1)) +} + +# check if 'go' is available +[ -n "$(command -v go)" ] \ + || fail "could not find 'go' executable" + +# parse the Golang version, return the digit passed as argument +# 1.13.9 -> go_version 1 -> 1 +# 1.13.9 -> go_version 2 -> 13 +# 1.13.9 -> go_version 3 -> 9 +go_version() { + go version | cut -d' ' -f3 | sed 's/^go//' | cut -d'.' -f"${1}" +} + +# Golang needs to be > 1.13 +GO_MAJOR=$(go_version 1) +GO_MINOR=$(go_version 2) +if ! [ "${GO_MAJOR}" -gt 1 ] +then + if ! { [ "${GO_MAJOR}" -eq 1 ] && [ "${GO_MINOR}" -ge 13 ]; } + then + fail "go version needs to be >= 1.13" + fi +fi + +# we're building with modules, so GO111MODULE needs to be 'on' or 'auto' +# some versions of Go return nothing when GO111MODULE is not explicitly +# configured +[ "$(go env GO111MODULE 2>&1)" = '' ] || [ "$(go env GO111MODULE)" = 'on' ] || [ "$(go env GO111MODULE)" = 'auto' ] \ + || fail "GO111MODULE should be set to 'on' or 'auto'" + +# CGO needs to be enabled, we build with go-ceph language bindings +[ "$(go env CGO_ENABLED)" = '1' ] \ + || fail "CGO_ENABLED should be set to '1'" + +exit ${ERRORS}