Browse Source

add logs and server info retrieval functionalities

Anish Mukherjee 2 years ago
parent
commit
9466124cf2
7 changed files with 184 additions and 0 deletions
  1. 22 0
      cli/cmd/logs.go
  2. 2 0
      cli/cmd/root.go
  3. 20 0
      cli/cmd/server/config.go
  4. 20 0
      cli/cmd/server/health.go
  5. 20 0
      cli/cmd/server/info.go
  6. 37 0
      cli/cmd/server/root.go
  7. 63 0
      cli/functions/server.go

+ 22 - 0
cli/cmd/logs.go

@@ -0,0 +1,22 @@
+package cmd
+
+import (
+	"fmt"
+
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/spf13/cobra"
+)
+
+var getLogsCmd = &cobra.Command{
+	Use:   "logs",
+	Args:  cobra.NoArgs,
+	Short: "Retrieve server logs",
+	Long:  `Retrieve server logs`,
+	Run: func(cmd *cobra.Command, args []string) {
+		fmt.Println(functions.GetLogs())
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(getLogsCmd)
+}

+ 2 - 0
cli/cmd/root.go

@@ -9,6 +9,7 @@ import (
 	"github.com/gravitl/netmaker/cli/cmd/keys"
 	"github.com/gravitl/netmaker/cli/cmd/network"
 	"github.com/gravitl/netmaker/cli/cmd/node"
+	"github.com/gravitl/netmaker/cli/cmd/server"
 	"github.com/spf13/cobra"
 )
 
@@ -53,4 +54,5 @@ func init() {
 	rootCmd.AddCommand(acl.GetRoot())
 	rootCmd.AddCommand(node.GetRoot())
 	rootCmd.AddCommand(dns.GetRoot())
+	rootCmd.AddCommand(server.GetRoot())
 }

+ 20 - 0
cli/cmd/server/config.go

@@ -0,0 +1,20 @@
+package server
+
+import (
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/spf13/cobra"
+)
+
+var serverConfigCmd = &cobra.Command{
+	Use:   "config",
+	Args:  cobra.NoArgs,
+	Short: "Retrieve server config",
+	Long:  `Retrieve server config`,
+	Run: func(cmd *cobra.Command, args []string) {
+		functions.PrettyPrint(functions.GetServerConfig())
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(serverConfigCmd)
+}

+ 20 - 0
cli/cmd/server/health.go

@@ -0,0 +1,20 @@
+package server
+
+import (
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/spf13/cobra"
+)
+
+var serverHealthCmd = &cobra.Command{
+	Use:   "health",
+	Args:  cobra.NoArgs,
+	Short: "View server health",
+	Long:  `View server health`,
+	Run: func(cmd *cobra.Command, args []string) {
+		functions.PrettyPrint(functions.GetServerHealth())
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(serverHealthCmd)
+}

+ 20 - 0
cli/cmd/server/info.go

@@ -0,0 +1,20 @@
+package server
+
+import (
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/spf13/cobra"
+)
+
+var serverInfoCmd = &cobra.Command{
+	Use:   "info",
+	Args:  cobra.NoArgs,
+	Short: "Retrieve server information",
+	Long:  `Retrieve server information`,
+	Run: func(cmd *cobra.Command, args []string) {
+		functions.PrettyPrint(functions.GetServerInfo())
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(serverInfoCmd)
+}

+ 37 - 0
cli/cmd/server/root.go

@@ -0,0 +1,37 @@
+package server
+
+import (
+	"os"
+
+	"github.com/spf13/cobra"
+)
+
+// rootCmd represents the base command when called without any subcommands
+var rootCmd = &cobra.Command{
+	Use:   "server",
+	Short: "Get netmaker server information",
+	Long:  `Get netmaker server information`,
+	// Run: func(cmd *cobra.Command, args []string) { },
+}
+
+func GetRoot() *cobra.Command {
+	return rootCmd
+}
+
+// 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() {
+	err := rootCmd.Execute()
+	if err != nil {
+		os.Exit(1)
+	}
+}
+
+func init() {
+	// Here you will define your flags and configuration settings.
+	// Cobra supports persistent flags, which, if defined here,
+	// will be global for your application.
+	// Cobra also supports local flags, which will only run
+	// when this action is called directly.
+	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}

+ 63 - 0
cli/functions/server.go

@@ -0,0 +1,63 @@
+package functions
+
+import (
+	"io"
+	"log"
+	"net/http"
+
+	"github.com/gravitl/netmaker/cli/config"
+	cfg "github.com/gravitl/netmaker/config"
+	"github.com/gravitl/netmaker/models"
+)
+
+func GetLogs() string {
+	ctx := config.GetCurrentContext()
+	req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/logs", nil)
+	if err != nil {
+		log.Fatal(err)
+	}
+	if ctx.MasterKey != "" {
+		req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
+	} else {
+		req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx))
+	}
+	res, err := http.DefaultClient.Do(req)
+	if err != nil {
+		log.Fatal(err)
+	}
+	bodyBytes, err := io.ReadAll(res.Body)
+	if err != nil {
+		log.Fatal(err)
+	}
+	return string(bodyBytes)
+}
+
+func GetServerInfo() *models.ServerConfig {
+	return request[models.ServerConfig](http.MethodGet, "/api/server/getserverinfo", nil)
+}
+
+func GetServerConfig() *cfg.ServerConfig {
+	return request[cfg.ServerConfig](http.MethodGet, "/api/server/getconfig", nil)
+}
+
+func GetServerHealth() string {
+	ctx := config.GetCurrentContext()
+	req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/server/health", nil)
+	if err != nil {
+		log.Fatal(err)
+	}
+	if ctx.MasterKey != "" {
+		req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
+	} else {
+		req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx))
+	}
+	res, err := http.DefaultClient.Do(req)
+	if err != nil {
+		log.Fatal(err)
+	}
+	bodyBytes, err := io.ReadAll(res.Body)
+	if err != nil {
+		log.Fatal(err)
+	}
+	return string(bodyBytes)
+}