diff --git a/auth_server/.gitignore b/auth_server/.gitignore index 42f4739b..ea8e804c 100644 --- a/auth_server/.gitignore +++ b/auth_server/.gitignore @@ -1,3 +1,4 @@ ca-certificates.crt auth_server Godeps/ +version.* diff --git a/auth_server/Makefile b/auth_server/Makefile index 39b480f7..3338c5c1 100644 --- a/auth_server/Makefile +++ b/auth_server/Makefile @@ -2,6 +2,7 @@ MAKEFLAGS += --warn-undefined-variables IMAGE ?= cesanta/docker_auth COMPRESS_BINARY ?= false CA_BUNDLE = /etc/ssl/certs/ca-certificates.crt +VERSION = $(shell cat version.txt) BUILDER_IMAGE ?= centurylink/golang-builder BUILDER_IMAGE_EXTRA-build-cross = -cross @@ -15,12 +16,12 @@ local: build-local update-deps: go get -v -u -f github.com/tools/godep github.com/jteeuwen/go-bindata/... + go generate ./... godep: godep save build-local: update-deps - go generate ./... go build ca-certificates.crt: @@ -28,11 +29,18 @@ ca-certificates.crt: docker-build: docker run --rm -v $(PWD):/src -e COMPRESS_BINARY=$(COMPRESS_BINARY) $(BUILDER_OPTS-$@) $(BUILDER_IMAGE)$(BUILDER_IMAGE_EXTRA-$@) $(IMAGE) + @echo === Built version $(VERSION) === build build-cross: update-deps godep ca-certificates.crt docker-build +docker-tag: + docker tag $(IMAGE):latest $(IMAGE):$(VERSION) + docker-tag-%: - docker tag -f $(IMAGE):latest $(IMAGE):$* + docker tag $(IMAGE):latest $(IMAGE):$* + +docker-push: + docker push $(IMAGE):latest $(IMAGE):$(VERSION) docker-push-%: docker-tag-% docker push $(IMAGE):$* diff --git a/auth_server/gen_version.py b/auth_server/gen_version.py new file mode 100755 index 00000000..79132cd7 --- /dev/null +++ b/auth_server/gen_version.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +import datetime +import sys + +# Debian/Ubuntu: apt-get install python-git +# PIP: pip install GitPython +import git + +repo = git.Repo('.', search_parent_directories=True) + + +def get_tag_for_commit(repo, commit): + for tag in repo.tags: + if tag.commit == commit: + return tag.name + return None + + +if repo.head.is_detached: + branch_or_tag = get_tag_for_commit(repo, repo.head.commit) + if branch_or_tag is None: + branch_or_tag = '?' +else: + branch_or_tag = repo.active_branch + +dirty = repo.is_dirty() + +ts = datetime.datetime.utcnow() +build_id = '%s/%s@%s%s' % (ts.strftime('%Y%m%d-%H%M%S'), + branch_or_tag, + str(repo.head.commit)[:8], + '+' if dirty else '') + +version = None +if not dirty: + version = get_tag_for_commit(repo, repo.head.commit) +if version is None: + version = ts.strftime('%Y%m%d%H%M%S') + + +if len(sys.argv) == 1 or sys.argv[1] == '-': + f = sys.stdout +else: + f = open(sys.argv[1], 'w') + +with open('version.go', 'w') as f: + f.write("""\ +package main + +const ( +\tVersion = "{version}" +\tBuildId = "{build_id}" +) +""".format(version=version, build_id=build_id)) + +with open('version.txt', 'w') as f: + f.write(version) + +f.close() diff --git a/auth_server/main.go b/auth_server/main.go index 086393ed..caded573 100644 --- a/auth_server/main.go +++ b/auth_server/main.go @@ -14,6 +14,8 @@ limitations under the License. */ +//go:generate ./gen_version.py + package main // import "github.com/cesanta/docker_auth/auth_server" import ( @@ -158,6 +160,8 @@ func main() { rand.Seed(time.Now().UnixNano()) glog.CopyStandardLogTo("INFO") + glog.Infof("docker_auth %s build %s", Version, BuildId) + cf := flag.Arg(0) if cf == "" { glog.Exitf("Config file not specified")