Skip to content

Commit

Permalink
add version reporting to binaries
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <marques@packet.com>
  • Loading branch information
displague committed Aug 15, 2020
1 parent 74ef27e commit 599def1
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 35 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ server := cmd/tink-server
cli := cmd/tink-cli
worker := cmd/tink-worker
binaries := ${server} ${cli} ${worker}

git_version?=$(shell git log -1 --format="%h")
version?=$(git_version)
release_tag?=$(shell git tag --points-at HEAD)
ifneq (,$(release_tag))
version:=$(release_tag)-$(version)
endif
LDFLAGS?=-ldflags "-X main.version=$(version)"

all: ${binaries}

.PHONY: server ${binaries} cli worker test
Expand All @@ -10,7 +19,7 @@ cli: ${cli}
worker : ${worker}

${server} ${cli} ${worker}:
CGO_ENABLED=0 GOOS=$$GOOS go build -o $@ ./$@
CGO_ENABLED=0 GOOS=$$GOOS go build ${LDFLAGS} -o $@ ./$@

run: ${binaries}
docker-compose up -d --build db
Expand Down
5 changes: 3 additions & 2 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ func GetConnection() (*grpc.ClientConn, error) {
}

// Setup : create a connection to server
func Setup() {
func Setup() error {
conn, err := GetConnection()
if err != nil {
log.Fatal(err)
return err
}
TemplateClient = template.NewTemplateClient(conn)
WorkflowClient = workflow.NewWorkflowSvcClient(conn)
HardwareClient = hardware.NewHardwareServiceClient(conn)
return nil
}

// TinkHardwareClient creates a new hardware client
Expand Down
26 changes: 9 additions & 17 deletions cmd/tink-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -13,34 +12,27 @@ var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "tink",
Short: "tinkerbell CLI",
Use: "tink",
Short: "tinkerbell CLI",
PersistentPreRunE: setupClient,
}

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "facility", "f", "", "used to build grpc and http urls")
}

func setupClient(_ *cobra.Command, _ []string) error {
return client.Setup()
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
if !isHelpCommand() {
client.Setup()
}

func Execute(version string) error {
rootCmd.Version = version
return rootCmd.Execute()
}

func isHelpCommand() bool {
for _, arg := range os.Args {
if arg == "-h" || arg == "--help" {
return true
}
}
return false
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.AutomaticEnv() // read in environment variables that match
Expand Down
5 changes: 4 additions & 1 deletion cmd/tink-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"github.com/tinkerbell/tink/cmd/tink-cli/cmd"
)

// version is set at build time
var version = "devel"

func main() {
if err := cmd.Execute(); err != nil {
if err := cmd.Execute(version); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/tink-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ import (
httpServer "github.com/tinkerbell/tink/http-server"
)

var logger log.Logger
var (
// version is set at build time
version = "devel"

logger log.Logger
)

func main() {
log, cleanup, err := log.Init("github.com/tinkerbell/tink")

if err != nil {
panic(err)
}
logger = log
defer cleanup()

log.Info("starting version " + version)

ctx, closer := context.WithCancel(context.Background())
errCh := make(chan error, 2)
facility := os.Getenv("FACILITY")
Expand Down
12 changes: 10 additions & 2 deletions cmd/tink-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ var (
retries int
)

var logger = logrus.New()
var (
// version is set at build time
version = "devel"

logger = logrus.New()
)

func main() {
logger.Debug("Starting version " + version)
setupRetry()
client.Setup()
if setupErr := client.Setup(); setupErr != nil {
logger.Fatalln(setupErr)
}
conn, err := tryClientConnection()
if err != nil {
logger.Fatalln(err)
Expand Down
15 changes: 4 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/go-openapi/strfmt v0.19.3 // indirect
github.com/gogo/protobuf v1.2.0 // indirect
github.com/golang/protobuf v1.4.2
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v0.0.0-20160910222444-6b7015e65d36
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/kr/pretty v0.2.0 // indirect
Expand All @@ -36,17 +34,12 @@ require (
github.com/rollbar/rollbar-go v1.0.2 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.4.1
github.com/spf13/afero v1.1.1 // indirect
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/spf13/viper v1.0.2
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
go.mongodb.org/mongo-driver v1.1.2 // indirect
go.uber.org/atomic v1.2.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.7.1 // indirect
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d // indirect
golang.org/x/text v0.3.2 // indirect
Expand Down
Loading

0 comments on commit 599def1

Please sign in to comment.