From d1874b9111e4523faf3b2fc3767dbac9b856ddaf Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Tue, 30 May 2023 16:46:59 +0200 Subject: [PATCH] Adds flasher version command and export version metrics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ❯ ./flasher version commit: 2121f02 branch: main git summary: v0.0.1-dirty buildDate: 1685457915 version: Go version: go1.20.3 bmclib version: v2.0.1-0.20230426154556-8907c8d39762 serverservice version: v0.15.3-0.20230519171430-6d4a327f68f3 --- Makefile | 28 +++++++++--- cmd/version.go | 23 ++++++++++ go.mod | 9 +++- go.sum | 4 +- internal/version/version.go | 87 +++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 cmd/version.go create mode 100644 internal/version/version.go diff --git a/Makefile b/Makefile index 4b0a6658..4e24b2e4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,10 @@ export DOCKER_BUILDKIT=1 +LDFLAG_LOCATION := github.com/metal-toolbox/flasher/internal/version +GIT_COMMIT := $(shell git rev-parse --short HEAD) +GIT_BRANCH := $(shell git symbolic-ref -q --short HEAD) +GIT_SUMMARY := $(shell git describe --tags --dirty --always) +VERSION := $(shell git describe --tags 2> /dev/null) +BUILD_DATE := $(shell date +%s) GIT_COMMIT_FULL := $(shell git rev-parse HEAD) GO_VERSION := $(shell expr `go version |cut -d ' ' -f3 |cut -d. -f2` \>= 16) DOCKER_IMAGE := "ghcr.io/metal-toolbox/flasher" @@ -14,15 +20,18 @@ lint: test: CGO_ENABLED=0 go test -timeout 1m -v -covermode=atomic ./... -build: - go build -o flasher - -## build osx bin +## build-osx build-osx: ifeq ($(GO_VERSION), 0) $(error build requies go version 1.17.n or higher) endif - GOOS=darwin GOARCH=amd64 go build -o flasher + go build -o flasher \ + -ldflags \ + "-X $(LDFLAG_LOCATION).GitCommit=$(GIT_COMMIT) \ + -X $(LDFLAG_LOCATION).GitBranch=$(GIT_BRANCH) \ + -X $(LDFLAG_LOCATION).GitSummary=$(GIT_SUMMARY) \ + -X $(LDFLAG_LOCATION).Version=$(VERSION) \ + -X $(LDFLAG_LOCATION).BuildDate=$(BUILD_DATE)" ## Build linux bin @@ -30,7 +39,14 @@ build-linux: ifeq ($(GO_VERSION), 0) $(error build requies go version 1.16.n or higher) endif - GOOS=linux GOARCH=amd64 go build -o flasher + GOOS=linux GOARCH=amd64 go build -o flasher \ + -ldflags \ + "-X $(LDFLAG_LOCATION).GitCommit=$(GIT_COMMIT) \ + -X $(LDFLAG_LOCATION).GitBranch=$(GIT_BRANCH) \ + -X $(LDFLAG_LOCATION).GitSummary=$(GIT_SUMMARY) \ + -X $(LDFLAG_LOCATION).Version=$(VERSION) \ + -X $(LDFLAG_LOCATION).BuildDate=$(BUILD_DATE)" + ## build docker image and tag as ghcr.io/metal-toolbox/flasher:latest build-image: build-linux diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..d9ef8538 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + + "github.com/metal-toolbox/flasher/internal/version" + "github.com/spf13/cobra" +) + +var cmdVersion = &cobra.Command{ + Use: "version", + Short: "Print Alloy version along with dependency - ironlib, bmclib version information.", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf( + "commit: %s\nbranch: %s\ngit summary: %s\nbuildDate: %s\nversion: %s\nGo version: %s\nbmclib version: %s\nserverservice version: %s", + version.GitCommit, version.GitBranch, version.GitSummary, version.BuildDate, version.AppVersion, version.GoVersion, version.BmclibVersion, version.ServerserviceVersion) + + }, +} + +func init() { + rootCmd.AddCommand(cmdVersion) +} diff --git a/go.mod b/go.mod index 1c48a2dd..325691fc 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,9 @@ require ( github.com/jpillora/backoff v1.0.0 github.com/metal-toolbox/conditionorc v0.0.0-20230525152515-9f519f51f7e8 github.com/mitchellh/mapstructure v1.5.0 + github.com/nats-io/nats.go v1.25.0 github.com/pkg/errors v0.9.1 + github.com/prometheus/client_golang v1.15.1 github.com/sirupsen/logrus v1.9.2 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 @@ -34,7 +36,9 @@ require ( require ( github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230 // indirect github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 // indirect + github.com/beorn7/perks v1.0.1 // indirect github.com/bytedance/sonic v1.8.9 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/cockroachdb/cockroach-go/v2 v2.3.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -77,16 +81,19 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/nats-io/jwt/v2 v2.4.0 // indirect - github.com/nats-io/nats.go v1.25.0 // indirect github.com/nats-io/nkeys v0.4.4 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/pelletier/go-toml/v2 v2.0.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect + github.com/prometheus/client_model v0.3.0 // indirect + github.com/prometheus/common v0.42.0 // indirect + github.com/prometheus/procfs v0.9.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect diff --git a/go.sum b/go.sum index 1fc896bc..15fb4120 100644 --- a/go.sum +++ b/go.sum @@ -615,7 +615,6 @@ github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -1717,6 +1716,7 @@ github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrb github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ= github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI= +github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk= github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -1740,6 +1740,7 @@ github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJ github.com/prometheus/common v0.38.0/go.mod h1:MBXfmBQZrK5XpbCkjofnXs96LD2QQ7fEq4C0xjC/yec= github.com/prometheus/common v0.39.0/go.mod h1:6XBZ7lYdLCbkAVhwRsWTZn+IN5AB9F/NXd5w0BbEX0Y= github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= +github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= github.com/prometheus/common/assets v0.1.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI= github.com/prometheus/common/assets v0.2.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI= github.com/prometheus/common/sigv4 v0.1.0/go.mod h1:2Jkxxk9yYvCkE5G1sQT7GuEXm57JrvHu9k5YwTjsNtI= @@ -1759,6 +1760,7 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= +github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/prometheus/prometheus v0.35.0/go.mod h1:7HaLx5kEPKJ0GDgbODG0fZgXbQ8K/XjZNJXQmbmgQlY= github.com/prometheus/prometheus v0.42.0/go.mod h1:Pfqb/MLnnR2KK+0vchiaH39jXxvLMBk+3lnIGP4N7Vk= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..14524a6e --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,87 @@ +package version + +import ( + "runtime" + rdebug "runtime/debug" + "strings" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + GitCommit string + GitBranch string + GitSummary string + BuildDate string + AppVersion string + BmclibVersion = bmclibVersion() + ServerserviceVersion = serverserviceVersion() + GoVersion = runtime.Version() +) + +type Version struct { + GitCommit string `json:"git_commit"` + GitBranch string `json:"git_branch"` + GitSummary string `json:"git_summary"` + BuildDate string `json:"build_date"` + AppVersion string `json:"app_version"` + GoVersion string `json:"go_version"` + BmclibVersion string `json:"bmclib_version"` + ServerserviceVersion string `json:"serverservice_version"` +} + +func Current() Version { + return Version{ + GitBranch: GitBranch, + GitCommit: GitCommit, + GitSummary: GitSummary, + BuildDate: BuildDate, + AppVersion: AppVersion, + GoVersion: GoVersion, + BmclibVersion: BmclibVersion, + ServerserviceVersion: ServerserviceVersion, + } +} + +func ExportBuildInfoMetric() { + buildInfo := promauto.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "flasher_build_info", + Help: "A metric with a constant '1' value, labeled by branch, commit, summary, builddate, version, Go version from which Flasher was built.", + }, + []string{"branch", "commit", "summary", "builddate", "version", "goversion", "serverserviceVersion"}, + ) + + buildInfo.WithLabelValues(GitBranch, GitCommit, GitSummary, BuildDate, AppVersion, GoVersion, ServerserviceVersion).Set(1) +} + +func bmclibVersion() string { + buildInfo, ok := rdebug.ReadBuildInfo() + if !ok { + return "" + } + + for _, d := range buildInfo.Deps { + if strings.Contains(d.Path, "bmclib") { + return d.Version + } + } + + return "" +} + +func serverserviceVersion() string { + buildInfo, ok := rdebug.ReadBuildInfo() + if !ok { + return "" + } + + for _, d := range buildInfo.Deps { + if strings.Contains(d.Path, "serverservice") { + return d.Version + } + } + + return "" +}