aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/status.go
blob: d660e71abf9e377f3e89fca4480c42645ddcb17b (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa status command
// author: bratseth

package cmd

import (
	"errors"
	"fmt"
	"log"
	"strconv"
	"strings"
	"time"

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

func newStatusCmd(cli *CLI) *cobra.Command {
	var (
		waitSecs int
		format   string
	)
	cmd := &cobra.Command{
		Use: "status",
		Aliases: []string{
			"status container",
			"status document", // TODO: Remove on Vespa 9
			"status query",    // TODO: Remove on Vespa 9
		},
		Short: "Show Vespa endpoints and status",
		Long: `Show Vespa endpoints and status.

This command shows the current endpoints, and their status, of a deployed Vespa
application.`,
		Example: `$ vespa status
$ vespa status --cluster mycluster
$ vespa status --cluster mycluster --wait 600
$ vepsa status --format plain --cluster mycluster`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Args:              cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			cluster := cli.config.cluster()
			t, err := cli.target(targetOptions{})
			if err != nil {
				return err
			}
			if err := verifyFormat(format); err != nil {
				return err
			}
			waiter := cli.waiter(time.Duration(waitSecs)*time.Second, cmd)
			var failingContainers []*vespa.Service
			if cluster == "" {
				services, err := waiter.Services(t)
				if err != nil {
					return err
				}
				if len(services) == 0 {
					return errHint(fmt.Errorf("no services exist"), "Deployment may not be ready yet", "Try 'vespa status deployment'")
				}
				for _, s := range services {
					if !printServiceStatus(s, format, waiter, cli) {
						failingContainers = append(failingContainers, s)
					}
				}
			} else {
				s, err := waiter.Service(t, cluster)
				if err != nil {
					return err
				}
				if !printServiceStatus(s, format, waiter, cli) {
					failingContainers = append(failingContainers, s)
				}
			}
			return failingServicesErr(failingContainers...)
		},
	}
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	cmd.PersistentFlags().StringVarP(&format, "format", "", "human", "Output format. Must be 'human' (human-readable) or 'plain' (cluster URL only)")
	return cmd
}

func verifyFormat(format string) error {
	switch format {
	case "human", "plain":
		return nil
	default:
		return fmt.Errorf("invalid format: %s", format)
	}
}

func failingServicesErr(services ...*vespa.Service) error {
	if len(services) == 0 {
		return nil
	}
	var nameOrURL []string
	for _, s := range services {
		if s.Name != "" {
			nameOrURL = append(nameOrURL, s.Name)
		} else {
			nameOrURL = append(nameOrURL, s.BaseURL)
		}
	}
	return fmt.Errorf("services not ready: %s", strings.Join(nameOrURL, ", "))
}

func newStatusDeployCmd(cli *CLI) *cobra.Command {
	var (
		waitSecs int
		format   string
	)
	cmd := &cobra.Command{
		Use:               "deploy",
		Short:             "Show status of the Vespa deploy service",
		Example:           `$ vespa status deploy`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Args:              cobra.ExactArgs(0),
		RunE: func(cmd *cobra.Command, args []string) error {
			t, err := cli.target(targetOptions{})
			if err != nil {
				return err
			}
			if err := verifyFormat(format); err != nil {
				return err
			}
			waiter := cli.waiter(time.Duration(waitSecs)*time.Second, cmd)
			s, err := waiter.DeployService(t)
			if err != nil {
				return err
			}
			if !printServiceStatus(s, format, waiter, cli) {
				return failingServicesErr(s)
			}
			return nil
		},
	}
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	cmd.PersistentFlags().StringVarP(&format, "format", "", "human", "Output format. Must be 'human' (human-readable text) or 'plain' (cluster URL only)")
	return cmd
}

func newStatusDeploymentCmd(cli *CLI) *cobra.Command {
	var waitSecs int
	cmd := &cobra.Command{
		Use:   "deployment",
		Short: "Show status of a Vespa deployment",
		Long: `Show status of a Vespa deployment.

This commands shows whether a Vespa deployment has converged on the latest run
 (Vespa Cloud) or config generation (self-hosted). If an argument is given,
show the convergence status of that particular run or generation.
`,
		Example: `$ vespa status deployment
$ vespa status deployment -t cloud [run-id]
$ vespa status deployment -t local [session-id]
$ vespa status deployment -t local [session-id] --wait 600
`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Args:              cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			wantedID := vespa.LatestDeployment
			if len(args) > 0 {
				n, err := strconv.ParseInt(args[0], 10, 64)
				if err != nil {
					return fmt.Errorf("invalid id: %s: %w", args[0], err)
				}
				wantedID = n
			}
			t, err := cli.target(targetOptions{logLevel: "none"})
			if err != nil {
				return err
			}
			waiter := cli.waiter(time.Duration(waitSecs)*time.Second, cmd)
			id, err := waiter.Deployment(t, wantedID)
			if err != nil {
				var hints []string
				if waiter.Timeout == 0 && !errors.Is(err, vespa.ErrDeployment) {
					hints = []string{"Consider using the --wait flag to wait for completion"}
				}
				return ErrCLI{Status: 1, warn: true, hints: hints, error: err}
			}
			if t.IsCloud() {
				log.Printf("Deployment run %s has completed", color.CyanString(strconv.FormatInt(id, 10)))
				log.Printf("See %s for more details", color.CyanString(t.Deployment().System.ConsoleRunURL(t.Deployment(), id)))
			} else {
				log.Printf("Deployment is %s on config generation %s", color.GreenString("ready"), color.CyanString(strconv.FormatInt(id, 10)))
			}
			return nil
		},
	}
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	return cmd
}

func printServiceStatus(s *vespa.Service, format string, waiter *Waiter, cli *CLI) bool {
	err := s.Wait(waiter.Timeout)
	var sb strings.Builder
	switch format {
	case "human":
		desc := s.Description()
		desc = strings.ToUpper(string(desc[0])) + string(desc[1:])
		sb.WriteString(fmt.Sprintf("%s at %s is ", desc, color.CyanString(s.BaseURL)))
		if err == nil {
			sb.WriteString(color.GreenString("ready"))
		} else {
			sb.WriteString(color.RedString("not ready"))
			sb.WriteString(": ")
			sb.WriteString(err.Error())
		}
	case "plain":
		sb.WriteString(s.BaseURL)
	default:
		panic("invalid format: " + format)
	}
	fmt.Fprintln(cli.Stdout, sb.String())
	return err == nil
}