github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/GNUmakefile (about)

     1  SHELL = bash
     2  PROJECT_ROOT := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
     3  THIS_OS := $(shell uname | cut -d- -f1)
     4  THIS_ARCH := $(shell uname -m)
     5  
     6  GIT_COMMIT := $(shell git rev-parse HEAD)
     7  GIT_DIRTY := $(if $(shell git status --porcelain),+CHANGES)
     8  
     9  GO_LDFLAGS := "-X github.com/hashicorp/nomad/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)"
    10  
    11  ifneq (MSYS_NT,$(THIS_OS))
    12  # GOPATH supports PATH style multi-paths; assume the first entry is favorable.
    13  # Necessary because new Circle images override GOPATH with multiple values.
    14  # See: https://discuss.circleci.com/t/gopath-is-set-to-multiple-directories/7174
    15  GOPATH := $(shell go env GOPATH | cut -d: -f1)
    16  endif
    17  
    18  # Respect $GOBIN if set in environment or via $GOENV file.
    19  BIN := $(shell go env GOBIN)
    20  ifndef BIN
    21  BIN := $(GOPATH)/bin
    22  endif
    23  
    24  GO_TAGS := $(GO_TAGS)
    25  
    26  ifeq ($(CI),true)
    27  GO_TAGS := codegen_generated $(GO_TAGS)
    28  endif
    29  
    30  # Don't embed the Nomad UI when the NOMAD_NO_UI env var is set.
    31  ifndef NOMAD_NO_UI
    32  GO_TAGS := ui $(GO_TAGS)
    33  endif
    34  
    35  # tag corresponding to latest release we maintain backward compatibility with
    36  PROTO_COMPARE_TAG ?= v1.0.3$(if $(findstring ent,$(GO_TAGS)),+ent,)
    37  
    38  # LAST_RELEASE is the git sha of the latest release corresponding to this branch. main should have the latest
    39  # published release, and release branches should point to the latest published release in the X.Y release line.
    40  LAST_RELEASE ?= v1.4.3
    41  
    42  default: help
    43  
    44  ifeq (Linux,$(THIS_OS))
    45  ALL_TARGETS = linux_386 \
    46  	linux_amd64 \
    47  	linux_arm \
    48  	linux_arm64 \
    49  	windows_386 \
    50  	windows_amd64
    51  endif
    52  
    53  ifeq (s390x,$(THIS_ARCH))
    54  ALL_TARGETS = linux_s390x
    55  endif
    56  
    57  ifeq (Darwin,$(THIS_OS))
    58  ALL_TARGETS = darwin_amd64 \
    59  	darwin_arm64
    60  endif
    61  
    62  ifeq (FreeBSD,$(THIS_OS))
    63  ALL_TARGETS = freebsd_amd64
    64  endif
    65  
    66  # Allow overriding ALL_TARGETS via $TARGETS
    67  ifdef TARGETS
    68  ALL_TARGETS = $(TARGETS)
    69  endif
    70  
    71  SUPPORTED_OSES = Darwin Linux FreeBSD Windows MSYS_NT
    72  
    73  CGO_ENABLED = 1
    74  
    75  # include per-user customization after all variables are defined
    76  -include GNUMakefile.local
    77  
    78  pkg/%/nomad: GO_OUT ?= $@
    79  pkg/%/nomad: CC ?= $(shell go env CC)
    80  pkg/%/nomad: ## Build Nomad for GOOS_GOARCH, e.g. pkg/linux_amd64/nomad
    81  ifeq (,$(findstring $(THIS_OS),$(SUPPORTED_OSES)))
    82  	$(warning WARNING: Building Nomad is only supported on $(SUPPORTED_OSES); not $(THIS_OS))
    83  endif
    84  	@echo "==> Building $@ with tags $(GO_TAGS)..."
    85  	@CGO_ENABLED=$(CGO_ENABLED) \
    86  		GOOS=$(firstword $(subst _, ,$*)) \
    87  		GOARCH=$(lastword $(subst _, ,$*)) \
    88  		CC=$(CC) \
    89  		go build -trimpath -ldflags $(GO_LDFLAGS) -tags "$(GO_TAGS)" -o $(GO_OUT)
    90  
    91  ifneq (armv7l,$(THIS_ARCH))
    92  pkg/linux_arm/nomad: CC = arm-linux-gnueabihf-gcc
    93  endif
    94  
    95  ifneq (aarch64,$(THIS_ARCH))
    96  pkg/linux_arm64/nomad: CC = aarch64-linux-gnu-gcc
    97  endif
    98  
    99  ifeq (Darwin,$(THIS_OS))
   100  pkg/linux_%/nomad: CGO_ENABLED = 0
   101  endif
   102  
   103  pkg/windows_%/nomad: GO_OUT = $@.exe
   104  
   105  # Define package targets for each of the build targets we actually have on this system
   106  define makePackageTarget
   107  
   108  pkg/$(1).zip: pkg/$(1)/nomad
   109  	@echo "==> Packaging for $(1)..."
   110  	@zip -j pkg/$(1).zip pkg/$(1)/*
   111  
   112  endef
   113  
   114  # Reify the package targets
   115  $(foreach t,$(ALL_TARGETS),$(eval $(call makePackageTarget,$(t))))
   116  
   117  .PHONY: bootstrap
   118  bootstrap: deps lint-deps git-hooks # Install all dependencies
   119  
   120  .PHONY: deps
   121  deps:  ## Install build and development dependencies
   122  	@echo "==> Updating build dependencies..."
   123  	go install github.com/hashicorp/go-bindata/go-bindata@bf7910af899725e4938903fb32048c7c0b15f12e
   124  	go install github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs@234c15e7648ff35458026de92b34c637bae5e6f7
   125  	go install github.com/a8m/tree/cmd/tree@fce18e2a750ea4e7f53ee706b1c3d9cbb22de79c
   126  	go install gotest.tools/gotestsum@v1.8.2
   127  	go install github.com/hashicorp/hcl/v2/cmd/hclfmt@d0c4fa8b0bbc2e4eeccd1ed2a32c2089ed8c5cf1
   128  	go install github.com/golang/protobuf/protoc-gen-go@v1.3.4
   129  	go install github.com/hashicorp/go-msgpack/codec/codecgen@v1.1.5
   130  	go install github.com/bufbuild/buf/cmd/buf@v0.36.0
   131  	go install github.com/hashicorp/go-changelog/cmd/changelog-build@latest
   132  	go install golang.org/x/tools/cmd/stringer@v0.1.12
   133  	go install github.com/hashicorp/hc-install/cmd/hc-install@4487b02cbcbb92204e3416cef9852b6ad44487b2
   134  
   135  .PHONY: lint-deps
   136  lint-deps: ## Install linter dependencies
   137  ## Keep versions in sync with tools/go.mod (see https://github.com/golang/go/issues/30515)
   138  	@echo "==> Updating linter dependencies..."
   139  	go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
   140  	go install github.com/client9/misspell/cmd/misspell@v0.3.4
   141  	go install github.com/hashicorp/go-hclog/hclogvet@v0.1.5
   142  
   143  .PHONY: git-hooks
   144  git-dir = $(shell git rev-parse --git-dir)
   145  git-hooks: $(git-dir)/hooks/pre-push
   146  $(git-dir)/hooks/%: dev/hooks/%
   147  	cp $^ $@
   148  	chmod 755 $@
   149  
   150  .PHONY: check
   151  check: ## Lint the source code
   152  	@echo "==> Linting source code..."
   153  	@golangci-lint run
   154  
   155  	@echo "==> Linting hclog statements..."
   156  	@hclogvet .
   157  
   158  	@echo "==> Spell checking website..."
   159  	@misspell -error -source=text website/pages/
   160  
   161  	@echo "==> Checking for breaking changes in protos..."
   162  	@buf breaking --config tools/buf/buf.yaml --against-config tools/buf/buf.yaml --against .git#tag=$(PROTO_COMPARE_TAG)
   163  
   164  	@echo "==> Check proto files are in-sync..."
   165  	@$(MAKE) proto
   166  	@if (git status -s | grep -q .pb.go); then echo the following proto files are out of sync; git status -s | grep .pb.go; exit 1; fi
   167  
   168  	@echo "==> Check format of jobspecs and HCL files..."
   169  	@$(MAKE) hclfmt
   170  	@if (git status -s | grep -q -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'); then echo the following HCL files are out of sync; git status -s | grep -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'; exit 1; fi
   171  
   172  	@echo "==> Check API package is isolated from rest"
   173  	@cd ./api && if go list --test -f '{{ join .Deps "\n" }}' . | grep github.com/hashicorp/nomad/ | grep -v -e /nomad/api/ -e nomad/api.test; then echo "  /api package depends the ^^ above internal nomad packages.  Remove such dependency"; exit 1; fi
   174  
   175  	@echo "==> Check command package does not import structs"
   176  	@cd ./command && if go list -f '{{ join .Imports "\n" }}' . | grep github.com/hashicorp/nomad/nomad/structs; then echo "  /command package imports the structs pkg. Remove such import"; exit 1; fi
   177  
   178  	@echo "==> Checking Go mod.."
   179  	@GO111MODULE=on $(MAKE) tidy
   180  	@if (git status --porcelain | grep -Eq "go\.(mod|sum)"); then \
   181  		echo go.mod or go.sum needs updating; \
   182  		git --no-pager diff go.mod; \
   183  		git --no-pager diff go.sum; \
   184  		exit 1; fi
   185  
   186  	@echo "==> Check raft util msg type mapping are in-sync..."
   187  	@go generate ./helper/raftutil/
   188  	@if (git status -s ./helper/raftutil| grep -q .go); then echo "raftutil helper message type mapping is out of sync. Run go generate ./... and push."; exit 1; fi
   189  
   190  .PHONY: checkscripts
   191  checkscripts: ## Lint shell scripts
   192  	@echo "==> Linting scripts..."
   193  	@find scripts -type f -name '*.sh' | xargs shellcheck
   194  
   195  .PHONY: checkproto
   196  checkproto: ## Lint protobuf files
   197  	@echo "==> Lint proto files..."
   198  	@buf check lint --config tools/buf/buf.yaml
   199  
   200  	@echo "==> Checking for breaking changes in protos..."
   201  	@buf check breaking --config tools/buf/buf.yaml --against-config tools/buf/buf.yaml --against .git#tag=$(PROTO_COMPARE_TAG)
   202  
   203  .PHONY: generate-all
   204  generate-all: generate-structs proto generate-examples ## Generate structs, protobufs, examples
   205  
   206  .PHONY: generate-structs
   207  generate-structs: LOCAL_PACKAGES = $(shell go list ./...)
   208  generate-structs: ## Update generated code
   209  	@echo "==> Running go generate..."
   210  	@go generate $(LOCAL_PACKAGES)
   211  
   212  .PHONY: proto
   213  proto: ## Generate protobuf bindings
   214  	@echo "==> Generating proto bindings..."
   215  	@buf --config tools/buf/buf.yaml --template tools/buf/buf.gen.yaml generate
   216  
   217  .PHONY: generate-examples
   218  generate-examples: command/job_init.bindata_assetfs.go
   219  
   220  command/job_init.bindata_assetfs.go: command/assets/*
   221  	go-bindata-assetfs -pkg command -o command/job_init.bindata_assetfs.go ./command/assets/...
   222  
   223  changelog: ## Generate changelog from entries
   224  	@changelog-build -last-release $(LAST_RELEASE) -this-release HEAD \
   225  		-entries-dir .changelog/ -changelog-template ./.changelog/changelog.tmpl -note-template ./.changelog/note.tmpl
   226  
   227  ## We skip the terraform directory as there are templated hcl configurations
   228  ## that do not successfully compile without rendering
   229  .PHONY: hclfmt
   230  hclfmt: ## Format HCL files with hclfmt
   231  	@echo "==> Formatting HCL"
   232  	@find . -name '.terraform' -prune \
   233  	        -o -name 'upstart.nomad' -prune \
   234  	        -o -name '.git' -prune \
   235  	        -o -name 'node_modules' -prune \
   236  	        -o -name '.next' -prune \
   237  	        -o -path './ui/dist' -prune \
   238  	        -o -path './website/out' -prune \
   239  	        -o -path './command/testdata' -prune \
   240  	        -o \( -name '*.nomad' -o -name '*.hcl' -o -name '*.tf' \) \
   241  	      -print0 | xargs -0 hclfmt -w
   242  
   243  .PHONY: tidy
   244  tidy: ## Tidy up the go mod files
   245  	@echo "==> Tidy up submodules"
   246  	@cd tools && go mod tidy
   247  	@cd api && go mod tidy
   248  	@echo "==> Tidy nomad module"
   249  	@go mod tidy
   250  
   251  .PHONY: dev
   252  dev: GOOS=$(shell go env GOOS)
   253  dev: GOARCH=$(shell go env GOARCH)
   254  dev: DEV_TARGET=pkg/$(GOOS)_$(GOARCH)/nomad
   255  dev: hclfmt ## Build for the current development platform
   256  	@echo "==> Removing old development build..."
   257  	@rm -f $(PROJECT_ROOT)/$(DEV_TARGET)
   258  	@rm -f $(PROJECT_ROOT)/bin/nomad
   259  	@rm -f $(BIN)/nomad
   260  	@if [ -d vendor ]; then echo -e "==> WARNING: Found vendor directory.  This may cause build errors, consider running 'rm -r vendor' or 'make clean' to remove.\n"; fi
   261  	@$(MAKE) --no-print-directory \
   262  		$(DEV_TARGET) \
   263  		GO_TAGS="$(GO_TAGS) $(NOMAD_UI_TAG)"
   264  	@mkdir -p $(PROJECT_ROOT)/bin
   265  	@mkdir -p $(BIN)
   266  	@cp $(PROJECT_ROOT)/$(DEV_TARGET) $(PROJECT_ROOT)/bin/
   267  	@cp $(PROJECT_ROOT)/$(DEV_TARGET) $(BIN)
   268  
   269  .PHONY: prerelease
   270  prerelease: GO_TAGS=ui codegen_generated release
   271  prerelease: generate-all ember-dist static-assets ## Generate all the static assets for a Nomad release
   272  
   273  .PHONY: release
   274  release: GO_TAGS=ui codegen_generated release
   275  release: clean $(foreach t,$(ALL_TARGETS),pkg/$(t).zip) ## Build all release packages which can be built on this platform.
   276  	@echo "==> Results:"
   277  	@tree --dirsfirst $(PROJECT_ROOT)/pkg
   278  
   279  .PHONY: test-nomad
   280  test-nomad: GOTEST_PKGS=$(shell go run -modfile=tools/go.mod tools/missing/main.go ci/test-core.json $(GOTEST_GROUP))
   281  test-nomad: # dev ## Run Nomad unit tests
   282  	@echo "==> Running Nomad unit tests $(GOTEST_GROUP)"
   283  	@echo "==> with packages $(GOTEST_PKGS)"
   284  	gotestsum --format=testname --rerun-fails=3 --packages="$(GOTEST_PKGS)" -- \
   285  		-cover \
   286  		-timeout=20m \
   287  		-count=1 \
   288  		-tags "$(GO_TAGS)" \
   289  		$(GOTEST_PKGS)
   290  
   291  .PHONY: test-nomad-module
   292  test-nomad-module: dev ## Run Nomad unit tests on sub-module
   293  	@echo "==> Running Nomad unit tests on sub-module $(GOTEST_MOD)"
   294  	cd $(GOTEST_MOD); gotestsum --format=testname --rerun-fails=3 --packages=./... -- \
   295  		-cover \
   296  		-timeout=20m \
   297  		-count=1 \
   298  		-tags "$(GO_TAGS)" \
   299  		./...
   300  
   301  .PHONY: e2e-test
   302  e2e-test: dev ## Run the Nomad e2e test suite
   303  	@echo "==> Running Nomad E2E test suites:"
   304  	go test \
   305  		$(if $(ENABLE_RACE),-race) $(if $(VERBOSE),-v) \
   306  		-timeout=900s \
   307  		-tags "$(GO_TAGS)" \
   308  		github.com/hashicorp/nomad/e2e
   309  
   310  .PHONY: integration-test
   311  integration-test: dev ## Run Nomad integration tests
   312  	@echo "==> Running Nomad integration test suites:"
   313  	go test \
   314  		$(if $(ENABLE_RACE),-race) $(if $(VERBOSE),-v) \
   315  		-cover \
   316  		-timeout=900s \
   317  		-tags "$(GO_TAGS)" \
   318  		github.com/hashicorp/nomad/e2e/vaultcompat/ \
   319  		-integration
   320  
   321  .PHONY: clean
   322  clean: GOPATH=$(shell go env GOPATH)
   323  clean: ## Remove build artifacts
   324  	@echo "==> Cleaning build artifacts..."
   325  	@rm -rf "$(PROJECT_ROOT)/bin/"
   326  	@rm -rf "$(PROJECT_ROOT)/pkg/"
   327  	@rm -rf "$(PROJECT_ROOT)/vendor/"
   328  	@rm -f "$(BIN)/nomad"
   329  
   330  .PHONY: testcluster
   331  testcluster: ## Bring up a Linux test cluster using Vagrant. Set PROVIDER if necessary.
   332  	vagrant up nomad-server01 \
   333  		nomad-server02 \
   334  		nomad-server03 \
   335  		nomad-client01 \
   336  		nomad-client02 \
   337  		nomad-client03 \
   338  		$(if $(PROVIDER),--provider $(PROVIDER))
   339  
   340  .PHONY: static-assets
   341  static-assets: ## Compile the static routes to serve alongside the API
   342  	@echo "==> Generating static assets"
   343  	@go-bindata-assetfs -pkg agent -prefix ui -modtime 1480000000 -tags ui -o bindata_assetfs.go ./ui/dist/...
   344  	@mv bindata_assetfs.go command/agent
   345  
   346  .PHONY: test-ui
   347  test-ui: ## Run Nomad UI test suite
   348  	@echo "==> Installing JavaScript assets"
   349  	@cd ui && npm rebuild node-sass
   350  	@cd ui && yarn install
   351  	@echo "==> Running ember tests"
   352  	@cd ui && npm test
   353  
   354  .PHONY: ember-dist
   355  ember-dist: ## Build the static UI assets from source
   356  	@echo "==> Installing JavaScript assets"
   357  	@cd ui && yarn install --silent --network-timeout 300000
   358  	@cd ui && npm rebuild node-sass
   359  	@echo "==> Building Ember application"
   360  	@cd ui && npm run build
   361  
   362  .PHONY: dev-ui
   363  dev-ui: ember-dist static-assets ## Build a dev UI binary
   364  	@$(MAKE) NOMAD_UI_TAG="ui" dev ## Build a dev binary with the UI baked in
   365  
   366  HELP_FORMAT="    \033[36m%-25s\033[0m %s\n"
   367  .PHONY: help
   368  help: ## Display this usage information
   369  	@echo "Valid targets:"
   370  	@grep -E '^[^ ]+:.*?## .*$$' $(MAKEFILE_LIST) | \
   371  		sort | \
   372  		awk 'BEGIN {FS = ":.*?## "}; \
   373  			{printf $(HELP_FORMAT), $$1, $$2}'
   374  	@echo ""
   375  	@echo "This host will build the following targets if 'make release' is invoked:"
   376  	@echo $(ALL_TARGETS) | sed 's/^/    /'
   377  
   378  .PHONY: ui-screenshots
   379  ui-screenshots: ## Collect  UI screenshots
   380  	@echo "==> Collecting UI screenshots..."
   381          # Build the screenshots image if it doesn't exist yet
   382  	@if [[ "$$(docker images -q nomad-ui-screenshots 2> /dev/null)" == "" ]]; then \
   383  		docker build --tag="nomad-ui-screenshots" ./scripts/screenshots; \
   384  	fi
   385  	@docker run \
   386  		--rm \
   387  		--volume "$(shell pwd)/scripts/screenshots/screenshots:/screenshots" \
   388  		nomad-ui-screenshots
   389  
   390  .PHONY: ui-screenshots-local
   391  ui-screenshots-local: ## Collect UI screenshots (local)
   392  	@echo "==> Collecting UI screenshots (local)..."
   393  	@cd scripts/screenshots/src && SCREENSHOTS_DIR="../screenshots" node index.js
   394  
   395  .PHONY: version
   396  version: ## Lookup the current build version
   397  ifneq (,$(wildcard version/version_ent.go))
   398  	@$(CURDIR)/scripts/version.sh version/version.go version/version_ent.go
   399  else
   400  	@$(CURDIR)/scripts/version.sh version/version.go version/version.go
   401  endif
   402  
   403  .PHONY: missing
   404  missing: ## Check for packages not being tested
   405  	@echo "==> Checking for packages not being tested ..."
   406  	@go run -modfile tools/go.mod tools/missing/main.go ci/test-core.json
   407  
   408  .PHONY: ec2info
   409  ec2info: ## Generate AWS EC2 CPU specification table
   410  	@echo "==> Generating AWS EC2 specifications ..."
   411  	@go run -modfile tools/go.mod tools/ec2info/main.go
   412  
   413  .PHONY: cl
   414  cl: ## Create a new Changelog entry
   415  	@go run -modfile tools/go.mod tools/cl-entry/main.go
   416