From 4f3ebb245a2ab5615aa4dedc277a87b4d8dc959f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Thu, 21 Mar 2024 10:13:22 +0100 Subject: [PATCH] feat: add health and version commands --- services/collaboration/pkg/command/health.go | 54 +++++++++++++++++++ services/collaboration/pkg/command/root.go | 4 +- services/collaboration/pkg/command/server.go | 10 +--- services/collaboration/pkg/command/version.go | 50 +++++++++++++++++ services/collaboration/pkg/logging/logging.go | 17 ++++++ 5 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 services/collaboration/pkg/command/health.go create mode 100644 services/collaboration/pkg/command/version.go create mode 100644 services/collaboration/pkg/logging/logging.go diff --git a/services/collaboration/pkg/command/health.go b/services/collaboration/pkg/command/health.go new file mode 100644 index 0000000000..a0fc40bfc7 --- /dev/null +++ b/services/collaboration/pkg/command/health.go @@ -0,0 +1,54 @@ +package command + +import ( + "fmt" + "net/http" + + "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/config/parser" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/logging" + "github.com/urfave/cli/v2" +) + +// Health is the entrypoint for the health command. +func Health(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "health", + Usage: "check health status", + Category: "info", + Before: func(c *cli.Context) error { + return configlog.ReturnError(parser.ParseConfig(cfg)) + }, + Action: func(c *cli.Context) error { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + + resp, err := http.Get( + fmt.Sprintf( + "http://%s/healthz", + cfg.Debug.Addr, + ), + ) + + if err != nil { + logger.Fatal(). + Err(err). + Msg("Failed to request health check") + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + logger.Fatal(). + Int("code", resp.StatusCode). + Msg("Health seems to be in bad state") + } + + logger.Debug(). + Int("code", resp.StatusCode). + Msg("Health got a good state") + + return nil + }, + } +} diff --git a/services/collaboration/pkg/command/root.go b/services/collaboration/pkg/command/root.go index 1e73df302f..31fe6db63f 100644 --- a/services/collaboration/pkg/command/root.go +++ b/services/collaboration/pkg/command/root.go @@ -12,8 +12,8 @@ import ( func GetCommands(cfg *config.Config) cli.Commands { return []*cli.Command{ Server(cfg), - //Health(cfg), - //Version(cfg), + Health(cfg), + Version(cfg), } } diff --git a/services/collaboration/pkg/command/server.go b/services/collaboration/pkg/command/server.go index 0ccc2bfd09..c18b641486 100644 --- a/services/collaboration/pkg/command/server.go +++ b/services/collaboration/pkg/command/server.go @@ -7,12 +7,12 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" - "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" "github.com/owncloud/ocis/v2/services/collaboration/pkg/config/parser" "github.com/owncloud/ocis/v2/services/collaboration/pkg/connector" "github.com/owncloud/ocis/v2/services/collaboration/pkg/helpers" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/logging" "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/debug" "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/grpc" "github.com/owncloud/ocis/v2/services/collaboration/pkg/server/http" @@ -29,13 +29,7 @@ func Server(cfg *config.Config) *cli.Command { return configlog.ReturnFatal(parser.ParseConfig(cfg)) }, Action: func(c *cli.Context) error { - logger := log.NewLogger( - log.Name(cfg.Service.Name), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) + logger := logging.Configure(cfg.Service.Name, cfg.Log) traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name) if err != nil { return err diff --git a/services/collaboration/pkg/command/version.go b/services/collaboration/pkg/command/version.go new file mode 100644 index 0000000000..57aa0f6ece --- /dev/null +++ b/services/collaboration/pkg/command/version.go @@ -0,0 +1,50 @@ +package command + +import ( + "fmt" + "os" + + "github.com/owncloud/ocis/v2/ocis-pkg/registry" + "github.com/owncloud/ocis/v2/ocis-pkg/version" + + tw "github.com/olekukonko/tablewriter" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" + "github.com/urfave/cli/v2" +) + +// Version prints the service versions of all running instances. +func Version(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "print the version of this binary and the running service instances", + Category: "info", + Action: func(c *cli.Context) error { + fmt.Println("Version: " + version.GetString()) + fmt.Printf("Compiled: %s\n", version.Compiled()) + fmt.Println("") + + reg := registry.GetRegistry() + services, err := reg.GetService(cfg.HTTP.Namespace + "." + cfg.Service.Name) + if err != nil { + fmt.Println(fmt.Errorf("could not get %s services from the registry: %v", cfg.Service.Name, err)) + return err + } + + if len(services) == 0 { + fmt.Println("No running " + cfg.Service.Name + " 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/services/collaboration/pkg/logging/logging.go b/services/collaboration/pkg/logging/logging.go new file mode 100644 index 0000000000..4bbd15e279 --- /dev/null +++ b/services/collaboration/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" +) + +// Configure initializes a service-specific logger instance. +func Configure(name string, cfg *config.Log) log.Logger { + return log.NewLogger( + log.Name(name), + log.Level(cfg.Level), + log.Pretty(cfg.Pretty), + log.Color(cfg.Color), + log.File(cfg.File), + ) +}