aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/clusterstate/options.go
blob: 900afef38bda5dafa122e526bfb580b7b5810dd0 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej

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

import (
	"strconv"
	"strings"

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

const (
	OnlyLocalNode int = -2
	AllNodes      int = -1
)

type Options struct {
	Verbose              int
	Silent               bool
	ShowHidden           showHiddenFlag
	Force                bool
	NoColors             bool
	SafeMode             bool
	NoWait               bool
	Cluster              string
	ConfigServerHost     string
	ConfigServerPort     int
	ConfigRequestTimeout int
	NodeType             string
	NodeIndex            int
	WantedState          string
}

func (v *Options) String() string {
	var buf strings.Builder
	buf.WriteString("command-line options [")
	if v.Verbose > 0 {
		buf.WriteString(" verbosity=")
		buf.WriteString(strconv.Itoa(v.Verbose))
	}
	if v.Silent {
		buf.WriteString(" silent")
	}
	if v.ShowHidden.showHidden {
		buf.WriteString(" show-hidden")
	}
	if v.Force {
		buf.WriteString(color.HiYellowString(" force=true"))
	}
	if v.NoColors {
		buf.WriteString(" no-colors")
	}
	if v.SafeMode {
		buf.WriteString(" safe-mode")
	}
	if v.NoWait {
		buf.WriteString(color.HiYellowString(" no-wait=true"))
	}
	if v.Cluster != "" {
		buf.WriteString(" cluster=")
		buf.WriteString(v.Cluster)
	}
	if v.ConfigServerHost != "" {
		buf.WriteString(" config-server=")
		buf.WriteString(v.ConfigServerHost)
	}
	if v.ConfigServerPort != 0 {
		buf.WriteString(" config-server-port=")
		buf.WriteString(strconv.Itoa(v.ConfigServerPort))
	}
	if v.ConfigRequestTimeout != 90 {
		buf.WriteString(" config-request-timeout=")
		buf.WriteString(strconv.Itoa(v.ConfigRequestTimeout))
	}
	if v.NodeType != "" {
		buf.WriteString(" node-type=")
		buf.WriteString(v.NodeType)
	}
	if v.NodeIndex >= 0 {
		buf.WriteString(" node-index=")
		buf.WriteString(strconv.Itoa(int(v.NodeIndex)))
	}
	if v.WantedState != "" {
		buf.WriteString(" WantedState=")
		buf.WriteString(v.WantedState)
	}
	buf.WriteString(" ]")
	return buf.String()
}

type serviceSpec struct {
	cluster     string
	serviceType string
	index       int
	host        string
}

func (o *Options) wantService(s serviceSpec) bool {
	if o.Cluster != "" && o.Cluster != s.cluster {
		return false
	}
	if o.NodeType == "" {
		if s.serviceType != "storage" && s.serviceType != "distributor" {
			return false
		}
	} else if o.NodeType != s.serviceType {
		return false
	}
	switch o.NodeIndex {
	case OnlyLocalNode:
		myName, _ := vespa.FindOurHostname()
		return s.host == "localhost" || s.host == myName
	case AllNodes:
		return true
	case s.index:
		return true
	default:
		return false
	}
}

func addCommonOptions(cmd *cobra.Command, curOptions *Options) {
	cmd.Flags().BoolVar(&curOptions.NoColors, "nocolors", false, "Do not use ansi colors in print.")
	cmd.Flags().BoolVarP(&curOptions.Silent, "silent", "s", false, "Create less verbose output.")
	cmd.Flags().CountVarP(&curOptions.Verbose, "verbose", "v", "Create more verbose output.")
	cmd.Flags().IntVar(&curOptions.ConfigRequestTimeout, "config-request-timeout", 90, "Timeout of config request")
	cmd.Flags().IntVar(&curOptions.ConfigServerPort, "config-server-port", 0, "Port to connect to config server on")
	cmd.Flags().StringVar(&curOptions.ConfigServerHost, "config-server", "", "Host name of config server to query")
	cmd.Flags().StringVarP(&curOptions.Cluster, "cluster", "c", "",
		"Cluster name. If unspecified, and vespa is installed on current node, information will be attempted auto-extracted")
	cmd.Flags().MarkHidden("config-request-timeout")
	cmd.Flags().MarkHidden("config-server-port")
	cmd.Flags().MarkHidden("nocolors")
	curOptions.ShowHidden.cmd = cmd
	flag := cmd.Flags().VarPF(&curOptions.ShowHidden, "show-hidden", "", "Also show hidden undocumented debug options.")
	flag.NoOptDefVal = "true"
	cobra.OnInitialize(func() {
		if curOptions.Silent {
			trace.Silent()
		} else {
			trace.AdjustVerbosity(curOptions.Verbose)
		}
	})
}