packer-cn/Makefile

172 lines
7.2 KiB
Makefile
Raw Normal View History

TEST?=$(shell go list ./...)
COUNT?=1
VET?=$(shell go list ./...)
2020-12-11 16:37:03 -05:00
ACC_TEST_BUILDERS?=all
ACC_TEST_PROVISIONERS?=all
# Get the current full sha from git
GITSHA:=$(shell git rev-parse HEAD)
# Get the current local branch name from git (if we can, this may be blank)
GITBRANCH:=$(shell git symbolic-ref --short HEAD 2>/dev/null)
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)
GOPATH=$(shell go env GOPATH)
2019-01-20 10:43:47 -05:00
EXECUTABLE_FILES=$(shell find . -type f -executable | egrep -v '^\./(website/[vendor|tmp]|vendor/|\.git|bin/|scripts/|pkg/)' | egrep -v '.*(\.sh|\.bats|\.git)' | egrep -v './provisioner/(ansible|inspec)/test-fixtures/exit1')
# Get the git commit
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
GIT_COMMIT=$(shell git rev-parse --short HEAD)
GIT_IMPORT=github.com/hashicorp/packer/version
fix builds on linux (#9031) * fix builds on linux * Build: Move to CGO_ENABLED=0 (#9057) After further investigation on cross-compiling Go bins on Linux. I found that statically linking against GCC (for libc) failed to build for ARM and introduced a possible licensing issue as our bins would essentially be bundling libc into the bin. Diving further into cross compiling on Linux I found that the defacto solution is to compile with CGO disabled - this was also found to be the case for other HashiCorp products. Disabling CGO has the limitation of not allowing the use of any pkg that calls out to C (net, os), but in looking into the Packer code base and the relevant Go code base it appears that the latest versions of Go have pure Go implementations of the said packages so I believe we are good to go. I should also point out that CGO is disabled by default when cross compiling via `go build`. However, the GOX tool will enable it if it is not explicitly disabled. Below are three test cases executed to validate the compile bins work as expected. Build results after change ``` ⇶ make bin WARN: 'make bin' is for debug / test builds only. Use 'make release' for release builds. ==> Checking for necessary tools... ==> Entering Packer source dir... ==> Ensuring output directories are present... ==> Removing old builds... ==> Building... Number of parallel builds: 7 --> windows/amd64: github.com/hashicorp/packer --> linux/arm64: github.com/hashicorp/packer --> linux/386: github.com/hashicorp/packer --> linux/arm: github.com/hashicorp/packer --> darwin/amd64: github.com/hashicorp/packer --> windows/386: github.com/hashicorp/packer --> linux/amd64: github.com/hashicorp/packer --> darwin/386: github.com/hashicorp/packer ==> Copying binaries for this platform... './pkg/linux_amd64/packer' -> 'bin/packer' './pkg/linux_amd64/packer' -> '/home/wilken/Development/go/bin/packer' ==> Results: total 111M -rwxr-xr-x 1 wilken wilken 111M Apr 13 12:29 packer ``` Packer executed on ARM based machine ``` ubuntu@ip-172-31-10-18:~$ ./packer version Packer v1.5.6-dev (314ac5b65+CHANGES ubuntu@ip-172-31-10-18:~$ uname -a Linux ip-172-31-10-18 4.15.0-1054-aws #56-Ubuntu SMP Thu Nov 7 16:18:50 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux ubuntu@ip-172-31-10-18:~$ ./packer build build.json null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell170248556 null: UUID from Packer: 79cc8532-6114-925d-2a79-33ef6ce281cd Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` Custom Docker image with updated bin ``` ⇶ docker run packertest:latest version Packer v1.5.6-dev (314ac5b65+CHANGES) ⇶ docker run packertest:latest build build.json null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell065599452 null: UUID from Packer: 852f0604-2be4-9e16-99af-6d7df972ac2e Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` Windows AMI ``` [...] ==> amazon-ebs: Launching a source AWS instance... ==> amazon-ebs: Adding tags to source instance amazon-ebs: Adding tag: "Name": "Packer Builder" amazon-ebs: Instance ID: i-04387545cf3e2acd3 ==> amazon-ebs: Waiting for instance (i-04387545cf3e2acd3) to become ready... ==> amazon-ebs: Skipping waiting for password since WinRM password set... ==> amazon-ebs: Using winrm communicator to connect: 18.206.100.104 ==> amazon-ebs: Waiting for WinRM to become available... amazon-ebs: WinRM connected. ==> amazon-ebs: Connected to WinRM! ==> amazon-ebs: Uploading packertest => c:/Windows/Temp ==> amazon-ebs: Provisioning with Powershell... ==> amazon-ebs: Provisioning with powershell script: /tmp/powershell-provisioner173180945 amazon-ebs: Packer v1.5.6-dev (314ac5b65+CHANGES) amazon-ebs: null: output will be in this color. amazon-ebs: ``` Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>
2020-04-14 14:48:50 -04:00
UNAME_S := $(shell uname -s)
LDFLAGS=-s -w
GOLDFLAGS=-X $(GIT_IMPORT).GitCommit=$(GIT_COMMIT)$(GIT_DIRTY) $(LDFLAGS)
export GOLDFLAGS
.PHONY: bin checkversion ci ci-lint default install-build-deps install-gen-deps fmt fmt-docs fmt-examples generate install-lint-deps lint \
releasebin test testacc testrace
2018-10-16 08:59:16 -04:00
default: install-build-deps install-gen-deps generate dev
2013-03-23 03:48:20 -04:00
ci: testrace ## Test in continuous integration
release: install-build-deps test releasebin package ## Build a release build
bin: install-build-deps ## Build debug/test build
@echo "WARN: 'make bin' is for debug / test builds only. Use 'make release' for release builds."
@GO111MODULE=auto sh -c "$(CURDIR)/scripts/build.sh"
releasebin: install-build-deps
@grep 'const VersionPrerelease = "dev"' version/version.go > /dev/null ; if [ $$? -eq 0 ]; then \
echo "ERROR: You must remove prerelease tags from version/version.go prior to release."; \
exit 1; \
fi
@GO111MODULE=auto sh -c "$(CURDIR)/scripts/build.sh"
2013-09-17 07:35:07 -04:00
package:
$(if $(VERSION),,@echo 'VERSION= needed to release; Use make package skip compilation'; exit 1)
@sh -c "$(CURDIR)/scripts/dist.sh $(VERSION)"
install-build-deps: ## Install dependencies for bin build
2021-03-31 11:53:41 -04:00
@go install github.com/mitchellh/gox@v1.0.1
install-gen-deps: ## Install dependencies for code generation
2019-09-05 11:19:50 -04:00
# to avoid having to tidy our go deps, we `go get` our binaries from a temp
# dir. `go get` will change our deps and the following deps are not part of
# out code dependencies; so a go mod tidy will remove them again. `go
# install` seems to install the last tagged version and we want to install
# master.
2019-09-06 06:15:48 -04:00
@(cd $(TEMPDIR) && GO111MODULE=on go get github.com/alvaroloes/enumer@master)
@go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@latest
install-lint-deps: ## Install linter dependencies
# Pinning golangci-lint at v1.23.8 as --new-from-rev seems to work properly; the latest 1.24.0 has caused issues with memory consumption
@echo "==> Updating linter dependencies..."
2020-03-12 05:36:10 -04:00
@curl -sSfL -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.23.8
2020-12-10 17:38:16 -05:00
dev: ## Build and install a development build
@grep 'const VersionPrerelease = ""' version/version.go > /dev/null ; if [ $$? -eq 0 ]; then \
echo "ERROR: You must add prerelease tags to version/version.go prior to making a dev build."; \
exit 1; \
fi
@mkdir -p pkg/$(GOOS)_$(GOARCH)
@mkdir -p bin
@go install -ldflags '$(GOLDFLAGS)'
@cp $(GOPATH)/bin/packer bin/packer
@cp $(GOPATH)/bin/packer pkg/$(GOOS)_$(GOARCH)
2013-08-13 21:42:08 -04:00
lint: install-lint-deps ## Lint Go code
@if [ ! -z $(PKG_NAME) ]; then \
echo "golangci-lint run ./$(PKG_NAME)/..."; \
golangci-lint run ./$(PKG_NAME)/...; \
else \
echo "golangci-lint run ./..."; \
golangci-lint run ./...; \
fi
ci-lint: install-lint-deps ## On ci only lint newly added Go source files
@echo "==> Running linter on newly added Go source files..."
GO111MODULE=on golangci-lint run --new-from-rev=$(shell git merge-base origin/master HEAD) ./...
2016-08-14 09:28:29 -04:00
fmt: ## Format Go code
@go fmt ./...
2016-11-01 16:48:10 -04:00
fmt-check: fmt ## Check go code formatting
@echo "==> Checking that code complies with go fmt requirements..."
@git diff --exit-code; if [ $$? -eq 1 ]; then \
echo "Found files that are not fmt'ed."; \
2018-05-01 23:39:48 -04:00
echo "You can use the command: \`make fmt\` to reformat code."; \
exit 1; \
fi
2016-02-12 14:37:49 -05:00
mode-check: ## Check that only certain files are executable
@echo "==> Checking that only certain files are executable..."
@if [ ! -z "$(EXECUTABLE_FILES)" ]; then \
echo "These files should not be executable or they must be white listed in the Makefile:"; \
echo "$(EXECUTABLE_FILES)" | xargs -n1; \
exit 1; \
else \
echo "Check passed."; \
fi
2017-06-14 21:04:16 -04:00
fmt-docs:
@find ./website/pages/docs -name "*.md" -exec pandoc --wrap auto --columns 79 --atx-headers -s -f "markdown_github+yaml_metadata_block" -t "markdown_github+yaml_metadata_block" {} -o {} \;
2017-06-14 21:04:16 -04:00
2016-03-10 20:41:28 -05:00
# Install js-beautify with npm install -g js-beautify
fmt-examples:
find examples -name *.json | xargs js-beautify -r -s 2 -n -eol "\n"
2015-05-26 16:26:22 -04:00
# generate runs `go generate` to build the dynamically generated
# source files.
generate: install-gen-deps ## Generate dynamically generated code
@echo "==> removing autogenerated markdown..." # but don't remove partials generated in the SDK and copied over.
@find website/pages -path website/pages/partials/packer-plugin-sdk -prune -o -type f | xargs grep -l '^<!-- Code generated' | xargs rm -f
@echo "==> removing autogenerated code..."
@find post-processor helper builder provisioner -type f | xargs grep -l '^// Code generated' | xargs rm -f
2020-12-11 16:37:03 -05:00
PROJECT_ROOT="$(shell pwd)" go generate $(shell go list ./... | grep -v packer-plugin-sdk)
generate-check: generate ## Check go code generation is on par
@echo "==> Checking that auto-generated code is not changed..."
@git diff --exit-code; if [ $$? -eq 1 ]; then \
echo "Found diffs in go generated code."; \
echo "You can use the command: \`make generate\` to reformat code."; \
exit 1; \
fi
2015-05-26 16:26:22 -04:00
test: mode-check vet ## Run unit tests
@go test -count $(COUNT) $(TEST) $(TESTARGS) -timeout=3m
2013-07-18 10:26:36 -04:00
# acctest runs provisioners acceptance tests
provisioners-acctest: #install-build-deps generate
2020-12-07 18:06:46 -05:00
ACC_TEST_BUILDERS=$(ACC_TEST_BUILDERS) go test $(TEST) $(TESTARGS) -timeout=1h
2015-05-26 16:26:22 -04:00
# testacc runs acceptance tests
testacc: # install-build-deps generate ## Run acceptance tests
@echo "WARN: Acceptance tests will take a long time to run and may cost money. Ctrl-C if you want to cancel."
PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) $(TESTARGS) -timeout=120m
2015-05-26 16:26:22 -04:00
testrace: mode-check vet ## Test with race detection enabled
@GO111MODULE=off go test -count $(COUNT) -race $(TEST) $(TESTARGS) -timeout=3m -p=8
2013-03-24 17:47:59 -04:00
2020-03-12 05:36:10 -04:00
# Runs code coverage and open a html page with report
cover:
go test -count $(COUNT) $(TEST) $(TESTARGS) -timeout=3m -coverprofile=coverage.out
2020-03-12 05:36:10 -04:00
go tool cover -html=coverage.out
rm coverage.out
check-vendor-vs-mod: ## Check that go modules and vendored code are on par
@GO111MODULE=on go mod vendor
@git diff --exit-code --ignore-space-change --ignore-space-at-eol -- vendor ; if [ $$? -eq 1 ]; then \
echo "ERROR: vendor dir is not on par with go modules definition." && \
exit 1; \
fi
vet: ## Vet Go code
@go vet $(VET) ; if [ $$? -eq 1 ]; then \
echo "ERROR: Vet found problems in the code."; \
exit 1; \
fi
2016-08-14 09:28:29 -04:00
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'