diff --git a/accounts/changelog/unreleased/add-version-command.md b/accounts/changelog/unreleased/add-version-command.md new file mode 100644 index 000000000..4ada95382 --- /dev/null +++ b/accounts/changelog/unreleased/add-version-command.md @@ -0,0 +1,5 @@ +Enhancement: add a command to list the versions of running instances + +Added a micro command to list the versions of running accounts services. + +https://github.com/owncloud/product/issues/226 diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 2234e91c3..ad7776bca 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -37,6 +37,7 @@ func Execute() error { Flags: flagset.RootWithConfig(cfg), Before: func(c *cli.Context) error { + cfg.Server.Version = version.String return ParseConfig(c, cfg) }, @@ -47,6 +48,7 @@ func Execute() error { ListAccounts(cfg), InspectAccount(cfg), RemoveAccount(cfg), + PrintVersion(cfg), }, } diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index c5bc96c56..781edfc74 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -43,6 +43,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() + mtrcs.BuildInfo.WithLabelValues(cfg.Server.Version).Set(1) + handler, err := svc.New(svc.Logger(logger), svc.Config(cfg)) if err != nil { logger.Fatal().Err(err).Msg("could not initialize service handler") diff --git a/accounts/pkg/command/version.go b/accounts/pkg/command/version.go new file mode 100644 index 000000000..0705b40a6 --- /dev/null +++ b/accounts/pkg/command/version.go @@ -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/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/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.ListAccountsWithConfig(cfg), + Action: func(c *cli.Context) error { + reg := mdns.NewRegistry() + services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Server.Name) + if err != nil { + fmt.Println(fmt.Errorf("could not get accounts services from the registry: %v", err)) + return err + } + + if len(services) == 0 { + fmt.Println("No running accounts 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 + }, + } +} diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 5c6f88a7d..fea59089e 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -39,6 +39,7 @@ type GRPC struct { // Server configures a server. type Server struct { + Version string Name string AccountsDataPath string } diff --git a/accounts/pkg/metrics/metrics.go b/accounts/pkg/metrics/metrics.go index 071272427..a3350518e 100644 --- a/accounts/pkg/metrics/metrics.go +++ b/accounts/pkg/metrics/metrics.go @@ -1,5 +1,7 @@ package metrics +import "github.com/prometheus/client_golang/prometheus" + var ( // Namespace defines the namespace for the defines metrics. Namespace = "ocis" @@ -11,12 +13,21 @@ var ( // Metrics defines the available metrics of this service. type Metrics struct { // Counter *prometheus.CounterVec + BuildInfo *prometheus.GaugeVec } // New initializes the available metrics. func New() *Metrics { m := &Metrics{ + BuildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: Namespace, + Subsystem: Subsystem, + Name: "build_info", + Help: "Build information", + }, []string{"version"}), } + + _ = prometheus.Register(m.BuildInfo) // TODO: implement metrics return m } diff --git a/accounts/pkg/server/grpc/server.go b/accounts/pkg/server/grpc/server.go index 13f7c2e8d..80fd1c3cc 100644 --- a/accounts/pkg/server/grpc/server.go +++ b/accounts/pkg/server/grpc/server.go @@ -17,6 +17,7 @@ func Server(opts ...Option) grpc.Service { grpc.Namespace(options.Config.GRPC.Namespace), grpc.Logger(options.Logger), grpc.Flags(options.Flags...), + grpc.Version(options.Config.Server.Version), ) if err := proto.RegisterAccountsServiceHandler(service.Server(), handler); err != nil { diff --git a/accounts/pkg/server/http/server.go b/accounts/pkg/server/http/server.go index 1b420390d..b4a3bed7c 100644 --- a/accounts/pkg/server/http/server.go +++ b/accounts/pkg/server/http/server.go @@ -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.Server.Version), http.Address(options.Config.HTTP.Addr), http.Namespace(options.Config.HTTP.Namespace), http.Context(options.Context), diff --git a/ocis/pkg/command/accounts.go b/ocis/pkg/command/accounts.go index fc4e472fd..b46a1f9b8 100644 --- a/ocis/pkg/command/accounts.go +++ b/ocis/pkg/command/accounts.go @@ -9,6 +9,7 @@ import ( "github.com/owncloud/ocis/accounts/pkg/flagset" "github.com/owncloud/ocis/ocis/pkg/config" "github.com/owncloud/ocis/ocis/pkg/register" + "github.com/owncloud/ocis/ocis/pkg/version" ) // AccountsCommand is the entrypoint for the accounts command. @@ -24,6 +25,7 @@ func AccountsCommand(cfg *config.Config) *cli.Command { command.UpdateAccount(cfg.Accounts), command.RemoveAccount(cfg.Accounts), command.InspectAccount(cfg.Accounts), + command.PrintVersion(cfg.Accounts), }, Action: func(c *cli.Context) error { accountsCommand := command.Server(configureAccounts(cfg)) @@ -40,6 +42,7 @@ func configureAccounts(cfg *config.Config) *svcconfig.Config { cfg.Accounts.Log.Level = cfg.Log.Level cfg.Accounts.Log.Pretty = cfg.Log.Pretty cfg.Accounts.Log.Color = cfg.Log.Color + cfg.Accounts.Server.Version = version.String return cfg.Accounts }