Skip to content

Commit

Permalink
Add cross-compilation Makefile targets and tar-based releases.
Browse files Browse the repository at this point in the history
Revamp the build system to be more inline with other Prometheus exporters.
Notably add Darwin and Windows build targets, and add support for releases
using tar files.
  • Loading branch information
wrouesnel committed Nov 30, 2017
1 parent 61b93a1 commit 5b9fea0
Show file tree
Hide file tree
Showing 98 changed files with 10,581 additions and 1,469 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ postgres_exporter_integration_test
cover.out
cover.*.out
.coverage
bin
release
*.prom
.metrics.*.*.prom
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ script:
- make docker
- make test-integration
- make cover.out
- make release
- $HOME/gopath/bin/goveralls -coverprofile=cover.out -service=travis-ci
after_success:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
Expand All @@ -38,7 +39,8 @@ deploy:
provider: releases
api_key:
secure: rwlge/Rs3wnWyfKRhD9fd5GviVe0foYUp20DY3AjKdDjhtwScA1EeR9QHOkB3raze52en0+KkpqlLCWbt3q4CRT7+ku1DNKhd6VWALdTZ1RPJYvNlU6CKJdRnWUJsECmSBsShXlbiYR8axqNVedzFPFGKzS9gYlFN6rr7pez/JZhxqucopZ6I+TkRHMELrFXyQK7/Y2bNRCLC4a+rGsjKeLLtYXbRXCmS0G4BSJEBRk7d69fIRzBApCMfrcLftgHzPuPth616yyUusQSCQYvaZ5tlwrPP8/E0wG3SVJVeDCMuDOSBZ9M6vNzR8W8VR/hxQamegn1OQgC5kNOaLZCTcJ5xguRouqb+FNFBqrd/Zi6vESo7RiVLULawzwxkh9sIPa3WZYDb3VK/Z/cpggUeR7wAu0S5ZYEvJHRefIZpqofZEHzDE3Blqp5yErz05e/zmjpd6HHK3f/UHmRRYfbulkvGT3aL/dlq5GcFvuxVC/vTL2VPvg9cGbqtf7PakC5IhoHpDs35tOyLxifOBLHvkwtGSxEfsCohIG8Hz2XFD83EsxgOiKSXVPLNd6yxjdqZj7OeAKFFU3bzGndnRbDIXaf987IN1imgUtP6wegfImoRStqxN4gEwwIMFsZCF86Ug4eLhlajLbWhudriDxDPBM/F9950aVxLwmWh9l5cRI=
file: postgres_exporter
file_glob: true
file: release/*
on:
tags: true
branch: master
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM scratch

COPY postgres_exporter /postgres_exporter
ARG binary

COPY $binary /postgres_exporter

EXPOSE 9187

Expand Down
112 changes: 73 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,78 @@

COVERDIR = .coverage
TOOLDIR = tools
BINDIR = bin
RELEASEDIR = release

GO_SRC := $(shell find . -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' )
GO_DIRS := $(shell find . -type d -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' )
DIRS = $(BINDIR) $(RELEASEDIR)

GO_SRC := $(shell find . -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' ! -path 'bin/*' ! -path 'release/*' )
GO_DIRS := $(shell find . -type d -name '*.go' ! -path '*/vendor/*' ! -path 'tools/*' ! -path 'bin/*' ! -path 'release/*' )
GO_PKGS := $(shell go list ./... | grep -v '/vendor/')

CONTAINER_NAME ?= wrouesnel/postgres_exporter:latest
VERSION ?= $(shell git describe --dirty)
BINARY := $(shell basename $(shell pwd))
VERSION ?= $(shell git describe --dirty 2>/dev/null)
VERSION_SHORT ?= $(shell git describe --abbrev=0 2>/dev/null)

ifeq ($(VERSION),)
VERSION := v0.0.0
endif

ifeq ($(VERSION_SHORT),)
VERSION_SHORT := v0.0.0
endif

# By default this list is filtered down to some common platforms.
platforms := $(subst /,-,$(shell go tool dist list | grep -e linux -e windows -e darwin | grep -e 386 -e amd64))
PLATFORM_BINS := $(patsubst %,$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%/$(BINARY),$(platforms))
PLATFORM_DIRS := $(patsubst %,$(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%,$(platforms))
PLATFORM_TARS := $(patsubst %,$(RELEASEDIR)/$(BINARY)_$(VERSION_SHORT)_%.tar.gz,$(platforms))

# These are evaluated on use, and so will have the correct values in the build
# rule (https://vic.demuzere.be/articles/golang-makefile-crosscompile/)
PLATFORMS_TEMP = $(subst -, ,$(patsubst $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_%/$(BINARY),%,$@))
GOOS = $(word 1, $(PLATFORMS_TEMP))
GOARCH = $(word 2, $(PLATFORMS_TEMP))

CURRENT_PLATFORM := $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_$(shell go env GOOS)-$(shell go env GOARCH)/$(BINARY)

CONCURRENT_LINTERS ?=
ifeq ($(CONCURRENT_LINTERS),)
CONCURRENT_LINTERS = $(shell gometalinter --help | grep -o 'concurrency=\w*' | cut -d= -f2 | cut -d' ' -f1)
endif

CONCURRENT_LINTERS ?= $(shell cat /proc/cpuinfo | grep processor | wc -l)
LINTER_DEADLINE ?= 30s

$(shell mkdir -p $(DIRS))

export PATH := $(TOOLDIR)/bin:$(PATH)
SHELL := env PATH=$(PATH) /bin/bash

all: style lint test postgres_exporter
all: style lint test binary

# Cross compilation (e.g. if you are on a Mac)
cross: docker-build docker
binary: $(BINARY)

# Simple go build
postgres_exporter: $(GO_SRC)
CGO_ENABLED=0 go build -a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" -o postgres_exporter .
$(BINARY): $(CURRENT_PLATFORM)
ln -sf $< $@

postgres_exporter_integration_test: $(GO_SRC)
CGO_ENABLED=0 go test -c -tags integration \
-a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" -o postgres_exporter_integration_test -cover -covermode count .
$(PLATFORM_BINS): $(GO_SRC)
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -a \
-ldflags "-extldflags '-static' -X main.Version=$(VERSION)" \
-o $@ .

$(PLATFORM_DIRS): $(PLATFORM_BINS)

$(PLATFORM_TARS): $(RELEASEDIR)/%.tar.gz : $(BINDIR)/%
tar -czf $@ -C $(BINDIR) $$(basename $<)

release-bin: $(PLATFORM_BINS)

release: $(PLATFORM_TARS)

# Take a go build and turn it into a minimal container
docker: postgres_exporter
docker build -t $(CONTAINER_NAME) .
docker: $(CURRENT_PLATFORM)
docker build --build-arg=binary=$(CURRENT_PLATFORM) -t $(CONTAINER_NAME) .

style: tools
gometalinter --disable-all --enable=gofmt --vendor
Expand All @@ -42,14 +84,17 @@ lint: tools
fmt: tools
gofmt -s -w $(GO_SRC)

run-tests: tools
mkdir -p $(COVERDIR)
rm -f $(COVERDIR)/*
postgres_exporter_integration_test: $(GO_SRC)
CGO_ENABLED=0 go test -c -tags integration \
-a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" \
-o postgres_exporter_integration_test -cover -covermode count .

test: tools
@mkdir -p $(COVERDIR)
@rm -f $(COVERDIR)/*
for pkg in $(GO_PKGS) ; do \
go test -v -covermode count -coverprofile=$(COVERDIR)/$$(echo $$pkg | tr '/' '-').out $$pkg || exit 1 ; \
done

test: run-tests
gocovmerge $(shell find $(COVERDIR) -name '*.out') > cover.test.out

test-integration: postgres_exporter postgres_exporter_integration_test
Expand All @@ -58,24 +103,13 @@ test-integration: postgres_exporter postgres_exporter_integration_test
cover.out: tools
gocovmerge cover.*.out > cover.out

# Do a self-contained docker build - we pull the official upstream container
# and do a self-contained build.
docker-build:
docker run -v $(shell pwd):/go/src/github.com/wrouesnel/postgres_exporter \
-v $(shell pwd):/real_src \
-e SHELL_UID=$(shell id -u) -e SHELL_GID=$(shell id -g) \
-w /go/src/github.com/wrouesnel/postgres_exporter \
golang:1.9-wheezy \
/bin/bash -c "make >&2 && chown $$SHELL_UID:$$SHELL_GID ./postgres_exporter"
docker build -t $(CONTAINER_NAME) .

push:
docker push $(CONTAINER_NAME)

clean:
[ ! -z $(BINDIR) ] && [ -e $(BINDIR) ] && find $(BINDIR) -print -delete || /bin/true
[ ! -z $(COVERDIR) ] && [ -e $(COVERDIR) ] && find $(COVERDIR) -print -delete || /bin/true
[ ! -z $(RELEASEDIR) ] && [ -e $(RELEASEDIR) ] && find $(RELEASEDIR) -print -delete || /bin/true
rm -f postgres_exporter postgres_exporter_integration_test

tools:
$(MAKE) -C $(TOOLDIR)

clean:
rm -rf postgres_exporter postgres_exporter_integration_test $(COVERDIR)

.PHONY: tools docker-build docker lint fmt test vet push cross clean

.PHONY: tools style fmt test all release binary clean
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,11 @@ GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
> ```
> DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable
> ```
# Hacking
* The build system is currently only supported for Linux-like platforms. It
depends on GNU Make.
* To build a copy for your current architecture run `make binary` or just `make`
This will create a symlink to the just built binary in the root directory.
* To build release tar balls run `make release`.
4 changes: 3 additions & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ SHELL := env PATH=$(PATH) /bin/bash
THIS_FILE := $(lastword $(MAKEFILE_LIST))

# This function is used to get the linters used by metalinter
get_metalinters := gometalinter --help | grep -oP ' \w+ \(.+\)' | tr -s ' ' | cut -d' ' -f3 | grep -oP '[^()]+'
get_metalinters := gometalinter --help | grep -oP '\s+\w+:\s*\(.+\)' | tr -s ' ' | cut -d' ' -f3 | grep -oP '[^()]+'

# This is a list of external tools we want to vendor
TOOL_SRCS := github.com/kardianos/govendor \
github.com/wadey/gocovmerge \
github.com/mattn/goveralls \
github.com/alecthomas/gometalinter

# This is populated by imported dependencies from gometalinter
METATOOL_SRCS :=

GO_SRC := $(shell find $(SOURCEDIR) -name '*.go')
Expand Down
23 changes: 23 additions & 0 deletions tools/vendor/github.com/GoASTScanner/gas/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tools/vendor/github.com/GoASTScanner/gas/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 37 additions & 21 deletions tools/vendor/github.com/alecthomas/gometalinter/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b9fea0

Please sign in to comment.