Skip to content

Commit

Permalink
feat(all): add version to the daemons
Browse files Browse the repository at this point in the history
This PR adds the version to the daemons. It infers the correct version from the git's latest tag.
  • Loading branch information
ijsong committed Oct 6, 2023
1 parent 7a4117e commit ab345de
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ linters-settings:
- "fmt.Print"
- "fmt.Printf"
- "fmt.Println"
- "strings.Builder.WriteString"
- "strings.Builder.WriteByte"


issues:
Expand Down
16 changes: 9 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ SHELL := /bin/bash

GO := go
GCFLAGS := -gcflags=all='-N -l'
LDFLAGS := -X 'github.com/kakao/varlog/internal/buildinfo.version=$(shell git describe --abbrev=0 --tags)'
LDFLAGS := -ldflags "$(LDFLAGS)"
GOPATH := $(shell $(GO) env GOPATH)
PKGS := $(shell $(GO) list ./... | \
egrep -v "github.com/kakao/varlog/vendor" | \
Expand Down Expand Up @@ -39,19 +41,19 @@ BENCHMARK := $(BIN_DIR)/benchmark
.PHONY: build vmr varlogadm varlogsn varlogctl varlogcli mrtool benchmark
build: vmr varlogadm varlogsn varlogctl varlogcli mrtool benchmark
vmr:
$(GO) build $(GCFLAGS) -o $(VMR) $(CURDIR)/cmd/varlogmr
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VMR) $(CURDIR)/cmd/varlogmr
varlogadm:
$(GO) build $(GCFLAGS) -o $(VARLOGADM) $(CURDIR)/cmd/varlogadm
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGADM) $(CURDIR)/cmd/varlogadm
varlogsn:
$(GO) build $(GCFLAGS) -o $(VARLOGSN) $(CURDIR)/cmd/varlogsn
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGSN) $(CURDIR)/cmd/varlogsn
varlogctl:
$(GO) build $(GCFLAGS) -o $(VARLOGCTL) $(CURDIR)/cmd/varlogctl
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGCTL) $(CURDIR)/cmd/varlogctl
varlogcli:
$(GO) build $(GCFLAGS) -o $(VARLOGCLI) $(CURDIR)/cmd/varlogcli
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGCLI) $(CURDIR)/cmd/varlogcli
mrtool:
$(GO) build $(GCFLAGS) -o $(MRTOOL) $(CURDIR)/cmd/mrtool
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(MRTOOL) $(CURDIR)/cmd/mrtool
benchmark:
$(GO) build $(GCFLAGS) -o $(BENCHMARK) $(CURDIR)/cmd/benchmark
$(GO) build $(GCFLAGS) $(LDFLAGS) -o $(BENCHMARK) $(CURDIR)/cmd/benchmark


# testing
Expand Down
8 changes: 7 additions & 1 deletion cmd/varlogadm/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"

"github.com/urfave/cli/v2"
"go.uber.org/zap"
Expand All @@ -10,17 +11,22 @@ import (
"github.com/kakao/varlog/internal/admin/mrmanager"
"github.com/kakao/varlog/internal/admin/snmanager"
"github.com/kakao/varlog/internal/admin/snwatcher"
"github.com/kakao/varlog/internal/buildinfo"
"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/pkg/util/log"
"github.com/kakao/varlog/pkg/util/telemetry"
)

func newAdminApp() *cli.App {
buildInfo := buildinfo.ReadVersionInfo()
cli.VersionPrinter = func(*cli.Context) {
fmt.Println(buildInfo.String())
}
return &cli.App{
Name: "varlogadm",
Usage: "run varlog admin server",
Version: "0.0.1",
Version: buildInfo.Version,
Commands: []*cli.Command{
newStartCommand(),
},
Expand Down
11 changes: 9 additions & 2 deletions cmd/varlogcli/varlogcli.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"fmt"
"log"
"os"

"github.com/pkg/errors"
"github.com/urfave/cli/v2"

"github.com/kakao/varlog/internal/buildinfo"
"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/varlogcli"
"github.com/kakao/varlog/pkg/types"
Expand All @@ -26,9 +28,14 @@ func run() int {
}

func newApp() *cli.App {
buildInfo := buildinfo.ReadVersionInfo()
cli.VersionPrinter = func(*cli.Context) {
fmt.Println(buildInfo.String())
}
app := &cli.App{
Name: "varlogcli",
Usage: "Varlog client",
Name: "varlogcli",
Usage: "Varlog client",
Version: buildInfo.Version,
Commands: []*cli.Command{
newAppend(),
newSubscribe(),
Expand Down
10 changes: 8 additions & 2 deletions cmd/varlogctl/cli.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"fmt"
"time"

"github.com/urfave/cli/v2"

"github.com/kakao/varlog/internal/buildinfo"
)

const (
appName = "varlogctl"
version = "0.0.1"

defaultTimeout = time.Second * 5
)
Expand All @@ -23,10 +25,14 @@ func commonFlags(flags ...cli.Flag) []cli.Flag {
}

func newVarlogControllerApp() *cli.App {
buildInfo := buildinfo.ReadVersionInfo()
cli.VersionPrinter = func(*cli.Context) {
fmt.Println(buildInfo.String())
}
app := &cli.App{
Name: appName,
Usage: "controller application for varlog",
Version: version,
Version: buildInfo.Version,
Commands: []*cli.Command{
newStorageNodeCommand(),
newTopicCommand(),
Expand Down
7 changes: 6 additions & 1 deletion cmd/varlogmr/metadata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
_ "go.uber.org/automaxprocs"
"go.uber.org/zap"

"github.com/kakao/varlog/internal/buildinfo"
"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/metarepos"
"github.com/kakao/varlog/pkg/types"
Expand Down Expand Up @@ -111,10 +112,14 @@ func start(c *cli.Context) error {
}

func initCLI() *cli.App {
buildInfo := buildinfo.ReadVersionInfo()
cli.VersionPrinter = func(*cli.Context) {
fmt.Println(buildInfo.String())
}
return &cli.App{
Name: "metadata_repository",
Usage: "run metadata repository",
Version: "v0.0.1",
Version: buildInfo.Version,
Commands: []*cli.Command{{
Name: "start",
Aliases: []string{"s"},
Expand Down
10 changes: 8 additions & 2 deletions cmd/varlogsn/cli.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"fmt"

"github.com/urfave/cli/v2"

"github.com/kakao/varlog/internal/buildinfo"
"github.com/kakao/varlog/internal/flags"
"github.com/kakao/varlog/internal/storagenode"
"github.com/kakao/varlog/internal/storagenode/logstream"
Expand All @@ -12,14 +15,17 @@ import (

const (
appName = "varlogsn"
version = "0.0.1"
)

func newStorageNodeApp() *cli.App {
buildInfo := buildinfo.ReadVersionInfo()
cli.VersionPrinter = func(*cli.Context) {
fmt.Println(buildInfo.String())
}
return &cli.App{
Name: appName,
Usage: "storage node",
Version: version,
Version: buildInfo.Version,
Commands: []*cli.Command{
newStartCommand(),
},
Expand Down
46 changes: 46 additions & 0 deletions internal/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package buildinfo

import (
"runtime/debug"
"strings"
)

const (
goOS = "GOOS"
goArch = "GOARCH"
vcsRevision = "vcs.revision"
vcsTime = "vcs.time"
)

var version = "devel"

type Info struct {
*debug.BuildInfo
settingsMap map[string]string

Version string
}

func ReadVersionInfo() Info {
info := Info{
Version: version,
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
info.BuildInfo = buildInfo
info.settingsMap = make(map[string]string, len(buildInfo.Settings))
for _, kv := range buildInfo.Settings {
info.settingsMap[kv.Key] = kv.Value
}
}
return info
}

func (info Info) String() string {
var sb strings.Builder
sb.WriteString("Version: " + info.Version + "\n")
sb.WriteString("Go Version: " + info.GoVersion + "\n")
sb.WriteString("Git Commit: " + info.settingsMap[vcsRevision] + "\n")
sb.WriteString("Built: " + info.settingsMap[vcsTime] + "\n")
sb.WriteString("OS/Arch: " + info.settingsMap[goOS] + "/" + info.settingsMap[goArch])
return sb.String()
}

0 comments on commit ab345de

Please sign in to comment.