Skip to content
Snippets Groups Projects
Commit a6fb5611 authored by Will Rouesnel's avatar Will Rouesnel
Browse files

Add cross-compilation Makefile targets and tar-based releases.

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.
parent 61b93a17
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -9,5 +9,7 @@ postgres_exporter_integration_test
cover.out
cover.*.out
.coverage
bin
release
*.prom
.metrics.*.*.prom
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
FROM scratch
 
COPY postgres_exporter /postgres_exporter
ARG binary
COPY $binary /postgres_exporter
 
EXPOSE 9187
 
Loading
Loading
 
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),$(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 -, ,$(subst /, ,$@))
GOOS = $(word 2, $(PLATFORMS_TEMP))
GOARCH = $(word 3, $(PLATFORMS_TEMP))
CURRENT_PLATFORM := $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_$(shell go env GOOS)-$(shell go env GOARCH)/$(BINARY)
 
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
binary: $(CURRENT_PLATFORM) $(BINARY)
$(BINARY):
ln -sf $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_$(shell go env GOOS)-$(shell go env GOARCH)/$(BINARY) $@
 
# Cross compilation (e.g. if you are on a Mac)
cross: docker-build docker
$(PLATFORM_BINS): $(GO_SRC)
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -a \
-ldflags "-extldflags '-static' -X main.Version=$(VERSION)" \
-o $(BINDIR)/$(BINARY)_$(VERSION_SHORT)_$(GOOS)-$(GOARCH)/$(BINARY) .
 
# Simple go build
postgres_exporter: $(GO_SRC)
CGO_ENABLED=0 go build -a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" -o postgres_exporter .
$(PLATFORM_DIRS): $(PLATFORM_BINS)
 
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_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
Loading
Loading
@@ -42,14 +80,12 @@ lint: tools
fmt: tools
gofmt -s -w $(GO_SRC)
 
run-tests: tools
mkdir -p $(COVERDIR)
rm -f $(COVERDIR)/*
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
Loading
Loading
@@ -58,24 +94,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
Loading
Loading
@@ -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`.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment