From 276938350309e71564836acd29634c97f532af2f Mon Sep 17 00:00:00 2001 From: Kaushal M Date: Thu, 22 Sep 2016 14:31:15 +0530 Subject: [PATCH 1/2] Add a script to generate version strings Taken from the GlusterFS source (${glusterfs-src}/build-aux/pkg-version). --- scripts/pkg-version | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 scripts/pkg-version diff --git a/scripts/pkg-version b/scripts/pkg-version new file mode 100755 index 000000000..83d4a5f91 --- /dev/null +++ b/scripts/pkg-version @@ -0,0 +1,52 @@ +#!/bin/sh + +# To override version/release from git, +# create VERSION file containing text with version/release +# eg. v3.4.0-1 +PKG_VERSION=`cat VERSION 2> /dev/null || git describe --tags --match "v[0-9]*"` + +get_version() +{ + # tags and output versions: + # - v3.4.0 => 3.4.0 (upstream clean) + # - v3.4.0-1 => 3.4.0 (downstream clean) + # - v3.4.0-2-g34e62f => 3.4.0 (upstream dirty) + # - v3.4.0-1-2-g34e62f => 3.4.0 (downstream dirty) + AWK_VERSION=' + BEGIN { FS="-" } + /^v[0-9]/ { + sub(/^v/,"") ; print $1 + }' + + echo $PKG_VERSION | awk "$AWK_VERSION" | tr -cd '[:alnum:].' +} + +get_release() +{ + # tags and output releases: + # - v3.4.0 => 0 (upstream clean) + # - v3.4.0-1 => 1 (downstream clean) + # - v3.4.0-2-g34e62f1 => 2.git34e62f1 (upstream dirty) + # - v3.4.0-1-2-g34e62f1 => 1.2.git34e62f1 (downstream dirty) + AWK_RELEASE=' + BEGIN { FS="-"; OFS="." } + /^v[0-9]/ { + if (NF == 1) print 0 + else if (NF == 2) print $2 + else if (NF == 3) print $2, "git" substr($3, 2) + else if (NF == 4) print $2, $3, "git" substr($4, 2) + }' + + echo $PKG_VERSION | awk "$AWK_RELEASE" | tr -cd '[:alnum:].' +} + +if test "x$1" = "x--full"; then + echo -n "v$(get_version)-$(get_release)" +elif test "x$1" = "x--version"; then + get_version +elif test "x$1" = "x--release"; then + get_release +else + echo "usage: $0 [--full|--version|--release]" + exit 1 +fi From 20cf5455332ac0418c7b1b13dd1cd894171f45ba Mon Sep 17 00:00:00 2001 From: Kaushal M Date: Thu, 22 Sep 2016 14:57:19 +0530 Subject: [PATCH 2/2] Set version when compiling --- Makefile | 7 +++++-- gdctx/global.go | 7 +++++-- main.go | 13 ++++++++++--- version.go | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 version.go diff --git a/Makefile b/Makefile index 6adb08fe3..420e9ccd6 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +VERSION := $(shell bash ./scripts/pkg-version --full) +LDFLAGS := '-X github.com/gluster/glusterd2/gdctx.GlusterdVersion=$(VERSION)' + .PHONY: all build check check-go check-reqs install vendor-update verify all: build @@ -16,12 +19,12 @@ check-reqs: glusterd2: @echo Building GlusterD-2.0 - @GO15VENDOREXPERIMENT=1 go build + @GO15VENDOREXPERIMENT=1 go build -ldflags $(LDFLAGS) @echo install: @echo Building and installing GlusterD-2.0 - @GO15VENDOREXPERIMENT=1 go install + @GO15VENDOREXPERIMENT=1 go install -ldflags $(LDFLAGS) @echo vendor-update: diff --git a/gdctx/global.go b/gdctx/global.go index 66d9b1006..12eca99ed 100644 --- a/gdctx/global.go +++ b/gdctx/global.go @@ -20,8 +20,11 @@ import ( // Various version constants that will be used by GD2 const ( - MaxOpVersion = 40000 - APIVersion = 1 + MaxOpVersion = 40000 + APIVersion = 1 +) + +var ( GlusterdVersion = "4.0-dev" ) diff --git a/main.go b/main.go index fd942ff6a..e5f26925d 100644 --- a/main.go +++ b/main.go @@ -17,13 +17,20 @@ import ( ) func main() { - log.WithField("pid", os.Getpid()).Info("GlusterD starting") - - // Parse flags and set up logging before continuing + // Parse flags and handle version and logging before continuing parseFlags() + + showvers, _ := flag.CommandLine.GetBool("version") + if showvers { + dumpVersionInfo() + return + } + logLevel, _ := flag.CommandLine.GetString("loglevel") initLog(logLevel, os.Stderr) + log.WithField("pid", os.Getpid()).Info("GlusterD starting") + // Read in config confFile, _ := flag.CommandLine.GetString("config") initConfig(confFile) diff --git a/version.go b/version.go new file mode 100644 index 000000000..e682bbcd3 --- /dev/null +++ b/version.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + + "github.com/gluster/glusterd2/gdctx" + + flag "github.com/spf13/pflag" +) + +func init() { + //Register the `--version` flag + flag.Bool("version", false, "Show the version information") +} + +func dumpVersionInfo() { + fmt.Println(gdctx.GlusterdVersion) +}