Skip to content

Commit

Permalink
Merge pull request #615 from owncloud/settings-version-command
Browse files Browse the repository at this point in the history
add version command and add build information to metrics
  • Loading branch information
butonic authored Sep 29, 2020
2 parents 369b8d3 + 116ba34 commit 60bc955
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 20 deletions.
5 changes: 5 additions & 0 deletions ocis/pkg/command/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package command

import (
"github.com/micro/cli/v2"
"github.com/owncloud/ocis/ocis/pkg/version"
"github.com/owncloud/ocis/settings/pkg/command"
svcconfig "github.com/owncloud/ocis/settings/pkg/config"
"github.com/owncloud/ocis/settings/pkg/flagset"
Expand All @@ -18,6 +19,9 @@ func SettingsCommand(cfg *config.Config) *cli.Command {
Usage: "Start settings server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Settings),
Subcommands: []*cli.Command{
command.PrintVersion(cfg.Settings),
},
Action: func(ctx *cli.Context) error {
settingsCommand := command.Server(configureSettings(cfg))

Expand All @@ -34,6 +38,7 @@ func configureSettings(cfg *config.Config) *svcconfig.Config {
cfg.Settings.Log.Level = cfg.Log.Level
cfg.Settings.Log.Pretty = cfg.Log.Pretty
cfg.Settings.Log.Color = cfg.Log.Color
cfg.Settings.Service.Version = version.String

if cfg.Tracing.Enabled {
cfg.Settings.Tracing.Enabled = cfg.Tracing.Enabled
Expand Down
6 changes: 6 additions & 0 deletions settings/changelog/unreleased/add-version-command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: add version command

Added a version command to print the versions of all running ocis-settings service instances.
Also added build information to the metrics.

https://github.com/owncloud/product/issues/226
6 changes: 4 additions & 2 deletions settings/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ require (
github.com/micro/go-micro/v2 v2.9.1
github.com/mitchellh/gox v1.0.1
github.com/oklog/run v1.0.0
github.com/olekukonko/tablewriter v0.0.1
github.com/openzipkin/zipkin-go v0.2.2
github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee // indirect
github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee
github.com/prometheus/client_golang v1.7.1
github.com/restic/calens v0.2.0
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.6.1
Expand All @@ -31,6 +33,6 @@ require (
)

replace (
google.golang.org/grpc => google.golang.org/grpc v1.26.0
github.com/owncloud/ocis/ocis-pkg => ../ocis-pkg
google.golang.org/grpc => google.golang.org/grpc v1.26.0
)
2 changes: 2 additions & 0 deletions settings/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -775,6 +776,7 @@ github.com/owncloud/ocis-settings v0.3.2-0.20200828091056-47af10a0e872 h1:VGWM/e
github.com/owncloud/ocis-settings v0.3.2-0.20200828091056-47af10a0e872/go.mod h1:vRge9QDkOsc6j76gPBmZs1Z5uOPrV4DIkZCgZCEFwBA=
github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee h1:P/bd6+zVU+I+X1+VfSg9av0OD2bfw1tUSHQtWj1JX/I=
github.com/owncloud/ocis/ocis-pkg v0.0.0-20200918114005-1a0ddd2190ee/go.mod h1:kukd3chd0ARpGNSLcIutgFywSvWIIdQcpyhPtVTyZbM=
github.com/owncloud/ocis/settings v0.0.0-20200918114005-1a0ddd2190ee/go.mod h1:5w91idmyOd8LgYK3eGuqsFBOfVJnSDeEp7S6dHheW14=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
Expand Down
2 changes: 2 additions & 0 deletions settings/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func Execute() error {
Flags: flagset.RootWithConfig(cfg),

Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
return ParseConfig(c, cfg)
},

Commands: []*cli.Command{
Server(cfg),
Health(cfg),
PrintVersion(cfg),
},
}

Expand Down
10 changes: 8 additions & 2 deletions settings/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"context"
"github.com/owncloud/ocis/settings/pkg/metrics"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -128,16 +129,20 @@ func Server(cfg *config.Config) *cli.Command {
var (
gr = run.Group{}
ctx, cancel = context.WithCancel(context.Background())
mtrcs = metrics.New()
)

defer cancel()

mtrcs.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)

{
server := http.Server(
http.Name("settings"),
http.Name(cfg.Service.Name),
http.Logger(logger),
http.Context(ctx),
http.Config(cfg),
http.Metrics(mtrcs),
http.Flags(flagset.RootWithConfig(cfg)),
http.Flags(flagset.ServerWithConfig(cfg)),
)
Expand All @@ -153,10 +158,11 @@ func Server(cfg *config.Config) *cli.Command {

{
server := grpc.Server(
grpc.Name("settings"),
grpc.Name(cfg.Service.Name),
grpc.Logger(logger),
grpc.Context(ctx),
grpc.Config(cfg),
grpc.Metrics(mtrcs),
)

gr.Add(server.Run, func(_ error) {
Expand Down
45 changes: 45 additions & 0 deletions settings/pkg/command/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package command

import (
"fmt"
"os"

"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/registry/mdns"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/settings/pkg/config"
"github.com/owncloud/ocis/settings/pkg/flagset"
)

// PrintVersion prints the service versions of all running instances.
func PrintVersion(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "Print the versions of the running instances",
Flags: flagset.ListSettingsWithConfig(cfg),
Action: func(c *cli.Context) error {
reg := mdns.NewRegistry()
services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Service.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get settings services from the registry: %v", err))
return err
}

if len(services) == 0 {
fmt.Println("No running settings service found.")
return nil
}

table := tw.NewWriter(os.Stdout)
table.SetHeader([]string{"Version", "Address", "Id"})
table.SetAutoFormatHeaders(false)
for _, s := range services {
for _, n := range s.Nodes {
table.Append([]string{s.Version, n.Address, n.Id})
}
}
table.Render()
return nil
},
}
}
14 changes: 8 additions & 6 deletions settings/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type GRPC struct {
Namespace string
}

// Service provides configuration options for the service
type Service struct {
Name string
Version string
DataPath string
}

// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool
Expand All @@ -42,11 +49,6 @@ type Asset struct {
Path string
}

// Storage defines the available storage configuration.
type Storage struct {
DataPath string
}

// TokenManager is the config for using the reva token manager
type TokenManager struct {
JWTSecret string
Expand All @@ -55,7 +57,7 @@ type TokenManager struct {
// Config combines all available configuration parts.
type Config struct {
File string
Storage Storage
Service Service
Log Log
Debug Debug
HTTP HTTP
Expand Down
29 changes: 28 additions & 1 deletion settings/pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,19 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
&cli.StringFlag{
Name: "name",
Value: "settings",
Usage: "service name",
EnvVars: []string{"SETTINGS_NAME"},
Destination: &cfg.Service.Name,
},
&cli.StringFlag{
Name: "data-path",
Value: "/var/tmp/ocis-settings",
Usage: "Mount path for the storage",
EnvVars: []string{"SETTINGS_DATA_PATH"},
Destination: &cfg.Storage.DataPath,
Destination: &cfg.Service.DataPath,
},
&cli.StringFlag{
Name: "jwt-secret",
Expand All @@ -173,3 +180,23 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
},
}
}

// ListSettingsWithConfig applies list command flags to cfg
func ListSettingsWithConfig(cfg *config.Config) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "grpc-namespace",
Value: "com.owncloud.api",
Usage: "Set the base namespace for the grpc namespace",
EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
&cli.StringFlag{
Name: "name",
Value: "settings",
Usage: "service name",
EnvVars: []string{"SETTINGS_NAME"},
Destination: &cfg.Service.Name,
},
}
}
13 changes: 13 additions & 0 deletions settings/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

var (
// Namespace defines the namespace for the defines metrics.
Namespace = "ocis"
Expand All @@ -11,6 +13,7 @@ var (
// Metrics defines the available metrics of this service.
type Metrics struct {
// Counter *prometheus.CounterVec
BuildInfo *prometheus.GaugeVec
}

// New initializes the available metrics.
Expand All @@ -22,11 +25,21 @@ func New() *Metrics {
// Name: "greet_total",
// Help: "How many greeting requests processed",
// }, []string{}),
BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: Subsystem,
Name: "build_info",
Help: "Build information",
}, []string{"version"}),
}

// prometheus.Register(
// m.Counter,
// )

_ = prometheus.Register(
m.BuildInfo,
)

return m
}
2 changes: 1 addition & 1 deletion settings/pkg/proto/v0/settings.pb.micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func init() {
)

cfg := config.New()
cfg.Storage.DataPath = dataPath
cfg.Service.DataPath = dataPath
handler = svc.NewService(cfg, ocislog.NewLogger(ocislog.Color(true), ocislog.Pretty(true)))
err := proto.RegisterBundleServiceHandler(service.Server(), handler)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions settings/pkg/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/settings/pkg/proto/v0"
svc "github.com/owncloud/ocis/settings/pkg/service/v0"
"github.com/owncloud/ocis/settings/pkg/version"
)

// Server initializes a new go-micro service ready to run
Expand All @@ -14,7 +13,7 @@ func Server(opts ...Option) grpc.Service {
service := grpc.NewService(
grpc.Logger(options.Logger),
grpc.Name(options.Name),
grpc.Version(version.String),
grpc.Version(options.Config.Service.Version),
grpc.Address(options.Config.GRPC.Addr),
grpc.Namespace(options.Config.GRPC.Namespace),
grpc.Context(options.Context),
Expand Down
2 changes: 1 addition & 1 deletion settings/pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Server(opts ...Option) http.Service {
service := http.NewService(
http.Logger(options.Logger),
http.Name(options.Name),
http.Version(version.String),
http.Version(options.Config.Service.Version),
http.Address(options.Config.HTTP.Addr),
http.Namespace(options.Config.HTTP.Namespace),
http.Context(options.Context),
Expand Down
10 changes: 5 additions & 5 deletions settings/pkg/store/filesystem/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ func New(cfg *config.Config) settings.Manager {
),
}

if _, err := os.Stat(cfg.Storage.DataPath); err != nil {
s.Logger.Info().Msgf("creating container on %v", cfg.Storage.DataPath)
err := os.MkdirAll(cfg.Storage.DataPath, 0700)
if _, err := os.Stat(cfg.Service.DataPath); err != nil {
s.Logger.Info().Msgf("creating container on %v", cfg.Service.DataPath)
err := os.MkdirAll(cfg.Service.DataPath, 0700)

if err != nil {
s.Logger.Err(err).Msgf("providing container on %v", cfg.Storage.DataPath)
s.Logger.Err(err).Msgf("providing container on %v", cfg.Service.DataPath)
}
}

s.dataPath = cfg.Storage.DataPath
s.dataPath = cfg.Service.DataPath
return &s
}

Expand Down

0 comments on commit 60bc955

Please sign in to comment.