aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/prod.go
blob: 0e00597221ad39584e495cb9e09872fefebae310 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package cmd

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"

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

func newProdCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "prod",
		Short: "Deploy an application package to production in Vespa Cloud",
		Long: `Deploy an application package to production in Vespa Cloud.

Configure and deploy your application package to production in Vespa Cloud.`,
		Example: `$ vespa prod init
$ vespa prod submit`,
		DisableAutoGenTag: true,
		SilenceUsage:      false,
		Args:              cobra.MinimumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return fmt.Errorf("invalid command: %s", args[0])
		},
	}
}

func newProdInitCmd(cli *CLI) *cobra.Command {
	return &cobra.Command{
		Use:   "init",
		Short: "Modify service.xml and deployment.xml for production deployment",
		Long: `Modify service.xml and deployment.xml for production deployment.

Only basic deployment configuration is available through this command. For
advanced configuration see the relevant Vespa Cloud documentation and make
changes to deployment.xml and services.xml directly.

Reference:
https://cloud.vespa.ai/en/reference/services
https://cloud.vespa.ai/en/reference/deployment`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		RunE: func(cmd *cobra.Command, args []string) error {
			pkg, err := cli.applicationPackageFrom(args, false)
			if err != nil {
				return err
			}
			if pkg.IsZip() {
				return errHint(fmt.Errorf("cannot modify compressed application package %s", pkg.Path),
					"Try running 'mvn clean' and run this command again")
			}

			deploymentXML, err := readDeploymentXML(pkg)
			if err != nil {
				return fmt.Errorf("could not read deployment.xml: %w", err)
			}
			servicesXML, err := readServicesXML(pkg)
			if err != nil {
				return fmt.Errorf("a services.xml declaring your cluster(s) must exist: %w", err)
			}
			target, err := cli.target(targetOptions{noCertificate: true})
			if err != nil {
				return err
			}

			fmt.Fprint(cli.Stdout, "This will modify any existing ", color.YellowString("deployment.xml"), " and ", color.YellowString("services.xml"),
				"!\nBefore modification a backup of the original file will be created.\n\n")
			fmt.Fprint(cli.Stdout, "A default value is suggested (shown inside brackets) based on\nthe files' existing contents. Press enter to use it.\n\n")
			fmt.Fprint(cli.Stdout, "Abort the configuration at any time by pressing Ctrl-C. The\nfiles will remain untouched.\n\n")
			fmt.Fprint(cli.Stdout, "See this guide for sizing a Vespa deployment:\n", color.GreenString("https://docs.vespa.ai/en/performance/sizing-search.html\n\n"))
			r := bufio.NewReader(cli.Stdin)
			deploymentXML, err = updateRegions(cli, r, deploymentXML, target.Deployment().System)
			if err != nil {
				return err
			}
			servicesXML, err = updateNodes(cli, r, servicesXML)
			if err != nil {
				return err
			}

			fmt.Fprintln(cli.Stdout)
			if err := writeWithBackup(cli.Stdout, pkg, "deployment.xml", deploymentXML.String()); err != nil {
				return err
			}
			if err := writeWithBackup(cli.Stdout, pkg, "services.xml", servicesXML.String()); err != nil {
				return err
			}
			return nil
		},
	}
}

func newProdSubmitCmd(cli *CLI) *cobra.Command {
	return &cobra.Command{
		Use:   "submit",
		Short: "Submit your application for production deployment",
		Long: `Submit your application for production deployment.

This commands uploads your application package to Vespa Cloud and deploys it to
the production zones specified in deployment.xml.

Nodes are allocated to your application according to resources specified in
services.xml.

While submitting an application from a local development environment is
supported, it's strongly recommended that production deployments are performed
by a continuous build system.

For more information about production deployments in Vespa Cloud see:
https://cloud.vespa.ai/en/getting-to-production
https://cloud.vespa.ai/en/automated-deployments`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Example: `$ mvn package # when adding custom Java components
$ vespa prod submit`,
		RunE: func(cmd *cobra.Command, args []string) error {
			target, err := cli.target(targetOptions{noCertificate: true})
			if err != nil {
				return err
			}
			if target.Type() != vespa.TargetCloud {
				// TODO: Add support for hosted
				return fmt.Errorf("prod submit does not support %s target", target.Type())
			}
			pkg, err := cli.applicationPackageFrom(args, true)
			if err != nil {
				return err
			}
			if !pkg.HasDeployment() {
				return errHint(fmt.Errorf("no deployment.xml found"), "Try creating one with vespa prod init")
			}
			if err := verifyTests(cli, pkg); err != nil {
				return err
			}
			opts, err := cli.createDeploymentOptions(pkg, target)
			if err != nil {
				return err
			}
			if err := vespa.Submit(opts); err != nil {
				return fmt.Errorf("could not submit application for deployment: %w", err)
			} else {
				cli.printSuccess("Submitted ", color.CyanString(pkg.Path), " for deployment")
				log.Printf("See %s for deployment progress\n", color.CyanString(fmt.Sprintf("%s/tenant/%s/application/%s/prod/deployment",
					opts.Target.Deployment().System.ConsoleURL, opts.Target.Deployment().Application.Tenant, opts.Target.Deployment().Application.Application)))
			}
			return nil
		},
	}
}

func writeWithBackup(stdout io.Writer, pkg vespa.ApplicationPackage, filename, contents string) error {
	dst := filepath.Join(pkg.Path, filename)
	if util.PathExists(dst) {
		data, err := os.ReadFile(dst)
		if err != nil {
			return err
		}
		if bytes.Equal(data, []byte(contents)) {
			fmt.Fprintf(stdout, "Not writing %s: File is unchanged\n", color.YellowString(filename))
			return nil
		}
		renamed := false
		for i := 1; i <= 1000; i++ {
			bak := fmt.Sprintf("%s.%d.bak", dst, i)
			if !util.PathExists(bak) {
				fmt.Fprintf(stdout, "Backing up existing %s to %s\n", color.YellowString(filename), color.YellowString(bak))
				if err := os.Rename(dst, bak); err != nil {
					return err
				}
				renamed = true
				break
			}
		}
		if !renamed {
			return fmt.Errorf("could not find an unused backup name for %s", dst)
		}
	}
	fmt.Fprintf(stdout, "Writing %s\n", color.GreenString(dst))
	return os.WriteFile(dst, []byte(contents), 0644)
}

func updateRegions(cli *CLI, stdin *bufio.Reader, deploymentXML xml.Deployment, system vespa.System) (xml.Deployment, error) {
	regions, err := promptRegions(cli, stdin, deploymentXML, system)
	if err != nil {
		return xml.Deployment{}, err
	}
	parts := strings.Split(regions, ",")
	regionElements := xml.Regions(parts...)
	if err := deploymentXML.Replace("prod", "region", regionElements); err != nil {
		return xml.Deployment{}, fmt.Errorf("could not update region elements in deployment.xml: %w", err)
	}
	// TODO: Some sample apps come with production <test> elements, but not necessarily working production tests, we
	//       therefore remove <test> elements here.
	//       This can be improved by supporting <test> elements in xml package and allow specifying testing as part of
	//       region prompt, e.g. region1;test,region2
	if err := deploymentXML.Replace("prod", "test", nil); err != nil {
		return xml.Deployment{}, fmt.Errorf("could not remove test elements in deployment.xml: %w", err)
	}
	return deploymentXML, nil
}

func promptRegions(cli *CLI, stdin *bufio.Reader, deploymentXML xml.Deployment, system vespa.System) (string, error) {
	fmt.Fprintln(cli.Stdout, color.CyanString("> Deployment regions"))
	fmt.Fprintf(cli.Stdout, "Documentation: %s\n", color.GreenString("https://cloud.vespa.ai/en/reference/zones"))
	fmt.Fprintf(cli.Stdout, "Example: %s\n\n", color.YellowString("aws-us-east-1c,aws-us-west-2a"))
	var currentRegions []string
	for _, r := range deploymentXML.Prod.Regions {
		currentRegions = append(currentRegions, r.Name)
	}
	if len(deploymentXML.Instance) > 0 {
		for _, r := range deploymentXML.Instance[0].Prod.Regions {
			currentRegions = append(currentRegions, r.Name)
		}
	}
	validator := func(input string) error {
		regions := strings.Split(input, ",")
		for _, r := range regions {
			if !xml.IsProdRegion(r, system) {
				return fmt.Errorf("invalid region %s", r)
			}
		}
		return nil
	}
	return prompt(cli, stdin, "Which regions do you wish to deploy in?", strings.Join(currentRegions, ","), validator)
}

func updateNodes(cli *CLI, r *bufio.Reader, servicesXML xml.Services) (xml.Services, error) {
	for _, c := range servicesXML.Container {
		nodes, err := promptNodes(cli, r, c.ID, c.Nodes)
		if err != nil {
			return xml.Services{}, err
		}
		if err := servicesXML.Replace("container#"+c.ID, "nodes", nodes); err != nil {
			return xml.Services{}, err
		}
	}
	for _, c := range servicesXML.Content {
		nodes, err := promptNodes(cli, r, c.ID, c.Nodes)
		if err != nil {
			return xml.Services{}, err
		}
		if err := servicesXML.Replace("content#"+c.ID, "nodes", nodes); err != nil {
			return xml.Services{}, err
		}
	}
	return servicesXML, nil
}

func promptNodes(cli *CLI, r *bufio.Reader, clusterID string, defaultValue xml.Nodes) (xml.Nodes, error) {
	count, err := promptNodeCount(cli, r, clusterID, defaultValue.Count)
	if err != nil {
		return xml.Nodes{}, err
	}
	const autoSpec = "auto"
	defaultSpec := autoSpec
	resources := defaultValue.Resources
	if resources != nil {
		defaultSpec = defaultValue.Resources.String()
	}
	spec, err := promptResources(cli, r, clusterID, defaultSpec)
	if err != nil {
		return xml.Nodes{}, err
	}
	if spec == autoSpec {
		resources = nil
	} else {
		r, err := xml.ParseResources(spec)
		if err != nil {
			return xml.Nodes{}, err // Should not happen as resources have already been validated
		}
		resources = &r
	}
	return xml.Nodes{Count: count, Resources: resources}, nil
}

func promptNodeCount(cli *CLI, stdin *bufio.Reader, clusterID string, nodeCount string) (string, error) {
	fmt.Fprintln(cli.Stdout, color.CyanString("\n> Node count: "+clusterID+" cluster"))
	fmt.Fprintf(cli.Stdout, "Documentation: %s\n", color.GreenString("https://cloud.vespa.ai/en/reference/services"))
	fmt.Fprintf(cli.Stdout, "Example: %s\nExample: %s\n\n", color.YellowString("4"), color.YellowString("[2,8]"))
	validator := func(input string) error {
		_, _, err := xml.ParseNodeCount(input)
		return err
	}
	return prompt(cli, stdin, fmt.Sprintf("How many nodes should the %s cluster have?", color.CyanString(clusterID)), nodeCount, validator)
}

func promptResources(cli *CLI, stdin *bufio.Reader, clusterID string, resources string) (string, error) {
	fmt.Fprintln(cli.Stdout, color.CyanString("\n> Node resources: "+clusterID+" cluster"))
	fmt.Fprintf(cli.Stdout, "Documentation: %s\n", color.GreenString("https://cloud.vespa.ai/en/reference/services"))
	fmt.Fprintf(cli.Stdout, "Example: %s\nExample: %s\n\n", color.YellowString("auto"), color.YellowString("vcpu=4,memory=8Gb,disk=100Gb"))
	validator := func(input string) error {
		if input == "auto" {
			return nil
		}
		_, err := xml.ParseResources(input)
		return err
	}
	return prompt(cli, stdin, fmt.Sprintf("Which resources should each node in the %s cluster have?", color.CyanString(clusterID)), resources, validator)
}

func readDeploymentXML(pkg vespa.ApplicationPackage) (xml.Deployment, error) {
	f, err := os.Open(filepath.Join(pkg.Path, "deployment.xml"))
	if errors.Is(err, os.ErrNotExist) {
		// Return a default value if there is no current deployment.xml
		return xml.DefaultDeployment, nil
	} else if err != nil {
		return xml.Deployment{}, err
	}
	defer f.Close()
	return xml.ReadDeployment(f)
}

func readServicesXML(pkg vespa.ApplicationPackage) (xml.Services, error) {
	f, err := os.Open(filepath.Join(pkg.Path, "services.xml"))
	if err != nil {
		return xml.Services{}, err
	}
	defer f.Close()
	return xml.ReadServices(f)
}

func prompt(cli *CLI, stdin *bufio.Reader, question, defaultAnswer string, validator func(input string) error) (string, error) {
	var input string
	for input == "" {
		fmt.Fprint(cli.Stdout, question)
		if defaultAnswer != "" {
			fmt.Fprint(cli.Stdout, " [", color.YellowString(defaultAnswer), "]")
		}
		fmt.Fprint(cli.Stdout, " ")

		var err error
		input, err = stdin.ReadString('\n')
		if err != nil {
			return "", err
		}
		input = strings.TrimSpace(input)
		if input == "" {
			input = defaultAnswer
		}

		if err := validator(input); err != nil {
			cli.printErr(err)
			fmt.Fprintln(cli.Stderr)
			input = ""
		}
	}
	return input, nil
}

func verifyTests(cli *CLI, app vespa.ApplicationPackage) error {
	if !app.HasTests() {
		return nil
	}
	// TODO: system-test, staging-setup and staging-test should be required if the application
	//       does not have any Java tests.
	suites := map[string]bool{
		"system-test":     false,
		"staging-setup":   false,
		"staging-test":    false,
		"production-test": false,
	}
	testPath := app.TestPath
	if app.IsZip() {
		path, err := app.Unzip(true)
		if err != nil {
			return err
		}
		defer os.RemoveAll(path)
		testPath = path
	}
	for suite, required := range suites {
		if err := verifyTest(cli, testPath, suite, required); err != nil {
			return err
		}
	}
	return nil
}

func verifyTest(cli *CLI, testsParent string, suite string, required bool) error {
	testDirectory := filepath.Join(testsParent, "tests", suite)
	_, err := os.Stat(testDirectory)
	if err != nil {
		if required {
			if errors.Is(err, os.ErrNotExist) {
				return errHint(fmt.Errorf("no %s tests found: %w", suite, err),
					fmt.Sprintf("No such directory: %s", testDirectory),
					"See https://cloud.vespa.ai/en/reference/testing")
			}
			return errHint(err, "See https://cloud.vespa.ai/en/reference/testing")
		}
		return nil
	}
	_, _, err = runTests(cli, testDirectory, true)
	return err
}