aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/clusterstate/get_cluster_state.go
blob: d5d2b894fe41770e8ca4d221f4b76895c724203b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// code for the "vespa-get-cluster-state" command
// Author: arnej

// utilities to get and manipulate node states in a storage cluster
package clusterstate

import (
	"fmt"
	"os"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/client/go/internal/admin/envvars"
	"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
	"github.com/vespa-engine/vespa/client/go/internal/build"
)

func NewGetClusterStateCmd() *cobra.Command {
	var (
		curOptions Options
	)
	cmd := &cobra.Command{
		Use:               "vespa-get-cluster-state [-h] [-v] [-f] [-c cluster]",
		Short:             "Get the cluster state of a given cluster.",
		Long:              `Usage: get-cluster-state [Options]`,
		Version:           build.Version,
		Args:              cobra.MaximumNArgs(0),
		CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
		Run: func(cmd *cobra.Command, args []string) {
			curOptions.NodeIndex = AllNodes
			runGetClusterState(&curOptions)
		},
	}
	addCommonOptions(cmd, &curOptions)
	return cmd
}

func runGetClusterState(opts *Options) {
	if opts.Silent {
		trace.Silent()
	}
	if opts.NoColors || os.Getenv(envvars.TERM) == "" {
		color.NoColor = true
	}
	trace.Debug("run getClusterState with: ", opts)
	m := detectModel(opts)
	trace.Debug("model:", m)
	sss := m.findSelectedServices(opts)
	clusters := make(map[string]*ClusterState)
	for _, s := range sss {
		trace.Debug("found service: ", s)
		if clusters[s.cluster] == nil {
			state, _ := m.getClusterState(s.cluster)
			trace.Debug("cluster ", s.cluster, state)
			clusters[s.cluster] = state
		}
	}
	for k, v := range clusters {
		globalState := v.State.Generated.State
		if globalState == "up" {
			fmt.Printf("Cluster %s:\n", k)
		} else {
			fmt.Printf("Cluster %s is %s. Too few nodes available.\n", k, color.HiRedString("%s", globalState))
		}
		for serviceType, serviceList := range v.Service {
			for dn, dv := range serviceList.Node {
				nodeState := dv.State.Generated.State
				if nodeState == "up" {
					fmt.Printf("%s/%s/%s: %v\n", k, serviceType, dn, nodeState)
				} else {
					fmt.Printf("%s/%s/%s: %v\n", k, serviceType, dn, color.HiRedString(nodeState))
				}
			}
		}
	}
}