2023-07-08 19:32:49 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"runtime/debug"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.ntppool.org/common/logger"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VERSION has the current software version (set in the build process)
|
|
|
|
var VERSION string
|
|
|
|
var buildTime string
|
|
|
|
var gitVersion string
|
|
|
|
var gitModified bool
|
|
|
|
|
|
|
|
var info Info
|
|
|
|
|
|
|
|
type Info struct {
|
2023-07-08 21:45:55 +00:00
|
|
|
Version string `json:",omitempty"`
|
|
|
|
GitRev string `json:",omitempty"`
|
|
|
|
GitRevShort string `json:",omitempty"`
|
|
|
|
BuildTime string `json:",omitempty"`
|
2023-07-08 19:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
info.BuildTime = buildTime
|
|
|
|
info.GitRev = gitVersion
|
|
|
|
|
|
|
|
if len(VERSION) == 0 {
|
|
|
|
VERSION = "dev-snapshot"
|
|
|
|
} else {
|
|
|
|
if !strings.HasPrefix(VERSION, "v") {
|
|
|
|
VERSION = "v" + VERSION
|
|
|
|
}
|
|
|
|
if !semver.IsValid(VERSION) {
|
|
|
|
logger.Setup().Warn("invalid version number", "version", VERSION)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bi, ok := debug.ReadBuildInfo(); ok {
|
|
|
|
for _, h := range bi.Settings {
|
|
|
|
// logger.Setup().Info("build info", "k", h.Key, "v", h.Value)
|
|
|
|
switch h.Key {
|
|
|
|
case "vcs.time":
|
2023-07-08 21:45:55 +00:00
|
|
|
if len(buildTime) == 0 {
|
|
|
|
buildTime = h.Value
|
|
|
|
}
|
2023-07-08 19:32:49 +00:00
|
|
|
info.BuildTime = h.Value
|
|
|
|
case "vcs.revision":
|
|
|
|
// https://blog.carlmjohnson.net/post/2023/golang-git-hash-how-to/
|
|
|
|
// todo: use BuildInfo.Main.Version if revision is empty
|
|
|
|
info.GitRev = h.Value
|
2023-07-08 21:45:55 +00:00
|
|
|
// if gitVersion != h.Value {
|
|
|
|
// logger.Setup().Warn("gitVersion and info.GitRev differs", "gitVersion", gitVersion, "gitRev", info.GitRev)
|
|
|
|
// }
|
|
|
|
gitVersion = h.Value
|
2023-07-08 19:32:49 +00:00
|
|
|
case "vcs.modified":
|
|
|
|
if h.Value == "true" {
|
|
|
|
gitModified = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 21:45:55 +00:00
|
|
|
info.GitRevShort = info.GitRev
|
|
|
|
|
|
|
|
if len(info.GitRevShort) > 7 {
|
|
|
|
info.GitRevShort = info.GitRevShort[:7]
|
|
|
|
}
|
|
|
|
|
2023-07-08 19:32:49 +00:00
|
|
|
info.Version = VERSION
|
|
|
|
|
|
|
|
Version()
|
|
|
|
}
|
|
|
|
|
|
|
|
func VersionCmd(name string) *cobra.Command {
|
|
|
|
versionCmd := &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Print version and build information",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
ver := Version()
|
|
|
|
fmt.Printf("%s %s\n", name, ver)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return versionCmd
|
|
|
|
}
|
|
|
|
|
2023-07-08 22:56:57 +00:00
|
|
|
func RegisterMetric(name string, registry prometheus.Registerer) {
|
|
|
|
if len(name) > 0 {
|
2024-02-03 08:21:31 +00:00
|
|
|
name = strings.ReplaceAll(name, "-", "_")
|
2023-07-08 22:56:57 +00:00
|
|
|
name = name + "_build_info"
|
|
|
|
} else {
|
|
|
|
name = "build_info"
|
|
|
|
}
|
2023-07-08 19:32:49 +00:00
|
|
|
buildInfo := prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
2023-07-08 22:56:57 +00:00
|
|
|
Name: name,
|
2023-07-08 19:32:49 +00:00
|
|
|
Help: "Build information",
|
|
|
|
},
|
2023-07-08 21:45:55 +00:00
|
|
|
[]string{
|
|
|
|
"version",
|
|
|
|
"buildtime",
|
|
|
|
"gittime",
|
|
|
|
"git",
|
|
|
|
},
|
2023-07-08 19:32:49 +00:00
|
|
|
)
|
|
|
|
registry.MustRegister(buildInfo)
|
2023-07-08 20:09:37 +00:00
|
|
|
info := VersionInfo()
|
2023-07-08 21:45:55 +00:00
|
|
|
buildInfo.WithLabelValues(
|
|
|
|
fmt.Sprintf("%s/%s",
|
|
|
|
info.Version, info.GitRevShort,
|
|
|
|
),
|
|
|
|
buildTime,
|
|
|
|
info.BuildTime,
|
|
|
|
info.GitRev,
|
|
|
|
).Set(1)
|
2023-07-08 19:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var v string
|
|
|
|
|
|
|
|
func VersionInfo() Info {
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
func Version() string {
|
|
|
|
if len(v) > 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
extra := []string{}
|
|
|
|
if len(buildTime) > 0 {
|
|
|
|
extra = append(extra, buildTime)
|
|
|
|
}
|
|
|
|
extra = append(extra, runtime.Version())
|
|
|
|
|
|
|
|
v := VERSION
|
|
|
|
if len(gitVersion) > 0 {
|
|
|
|
g := gitVersion
|
|
|
|
if len(g) > 7 {
|
|
|
|
g = g[0:7]
|
|
|
|
}
|
|
|
|
v += "/" + g
|
|
|
|
if gitModified {
|
|
|
|
v += "-M"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
v = fmt.Sprintf("%s (%s)", v, strings.Join(extra, ", "))
|
|
|
|
return v
|
|
|
|
}
|