/yokeadm/commands/clusterList.go

https://gitlab.com/oytunistrator/yoke · Go · 78 lines · 65 code · 5 blank · 8 comment · 0 complexity · ca26e34626763c00fa3fe7df1af3c233 MD5 · raw file

  1. // Copyright (c) 2015 Pagoda Box Inc
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public License, v.
  4. // 2.0. If a copy of the MPL was not distributed with this file, You can obtain one
  5. // at http://mozilla.org/MPL/2.0/.
  6. //
  7. package commands
  8. import (
  9. "fmt"
  10. "net/rpc"
  11. "os"
  12. "regexp"
  13. "time"
  14. "github.com/spf13/cobra"
  15. )
  16. // Status represents the Status of a node in the cluser
  17. type Status struct {
  18. CRole string // the nodes 'role' in the cluster (primary, secondary, monitor)
  19. DataDir string // directory of the postgres database
  20. DBRole string // the 'role' of the running pgsql instance inside the node (master, slave)
  21. Ip string // advertise_ip
  22. PGPort int //
  23. State string // the current state of the node
  24. UpdatedAt time.Time // the last time the node state was updated
  25. }
  26. //
  27. var clusterListCmd = &cobra.Command{
  28. Use: "list",
  29. Short: "Returns status information for all nodes in the cluster",
  30. Long: ``,
  31. Run: clusterList,
  32. }
  33. // clusterList displays select information about all of the nodes in a cluster
  34. func clusterList(ccmd *cobra.Command, args []string) {
  35. // create an RPC client that will connect to the designated node
  36. client, err := rpc.Dial("tcp", fmt.Sprintf("%s:%s", fHost, fPort))
  37. if err != nil {
  38. fmt.Println("[cli.ClusterList.run] Failed to dial!", err)
  39. os.Exit(1)
  40. }
  41. defer client.Close()
  42. // issue a request to the designated node for the status of the cluster
  43. var members = &[]Status{}
  44. if err := client.Call("Status.RPCCluster", "", members); err != nil {
  45. fmt.Println("[cli.ClusterList.run] Failed to call!", err)
  46. os.Exit(1)
  47. }
  48. //
  49. fmt.Println(`
  50. Cluster Role | Cluster IP | State | Status | Postgres Role | Postgres Port | Last Updated
  51. ---------------------------------------------------------------------------------------------------------------------------`)
  52. for _, member := range *members {
  53. state := "--"
  54. status := "running"
  55. //
  56. if subMatch := regexp.MustCompile(`^\((.*)\)(.*)$`).FindStringSubmatch(member.State); subMatch != nil {
  57. state = subMatch[1]
  58. status = subMatch[2]
  59. }
  60. //
  61. fmt.Printf("%-12s | %-15s | %-13s | %-12s | %-15s | %-15d | %-25s\n", member.CRole, member.Ip, state, status, member.DBRole, member.PGPort, member.UpdatedAt.Format("01.02.06 (15:04:05) MST"))
  62. }
  63. fmt.Println("")
  64. }