aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/status.go
blob: a0602494bffbcfa2f10b225f6d61bfca82405f4e (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
// 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 (
	"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
	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`,
		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
			}
			waiter := cli.waiter(time.Duration(waitSecs) * time.Second)
			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, waiter, cli) {
						failingContainers = append(failingContainers, s)
					}
				}
			} else {
				s, err := waiter.Service(t, cluster)
				if err != nil {
					return err
				}
				if !printServiceStatus(s, waiter, cli) {
					failingContainers = append(failingContainers, s)
				}
			}
			return failingServicesErr(failingContainers...)
		},
	}
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	return cmd
}

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
	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
			}
			waiter := cli.waiter(time.Duration(waitSecs) * time.Second)
			s, err := waiter.DeployService(t)
			if err != nil {
				return err
			}
			if !printServiceStatus(s, waiter, cli) {
				return failingServicesErr(s)
			}
			return nil
		},
	}
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	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)
			id, err := waiter.Deployment(t, wantedID)
			if err != nil {
				return 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, waiter *Waiter, cli *CLI) bool {
	desc := s.Description()
	desc = strings.ToUpper(string(desc[0])) + string(desc[1:])
	err := s.Wait(waiter.Timeout)
	var sb strings.Builder
	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())
	}
	fmt.Fprintln(cli.Stdout, sb.String())
	return err == nil
}