Skip to content

Commit

Permalink
Adds flasher version command and export version metrics
Browse files Browse the repository at this point in the history
❯ ./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
  • Loading branch information
joelrebel committed May 30, 2023
1 parent 2121f02 commit d1874b9
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
28 changes: 22 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -14,23 +20,33 @@ 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
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
Expand Down
23 changes: 23 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
87 changes: 87 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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 ""
}

0 comments on commit d1874b9

Please sign in to comment.