-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds flasher version command and export version metrics
❯ ./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
Showing
5 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |