summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/deploy.go
blob: 7d180775e24e4e46d71dbb557faa1fdf1c39c03d (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa deploy command
// Author: bratseth

package cmd

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/vespa"
)

const (
	zoneFlag = "zone"
)

var (
	zoneArg string
)

func init() {
	rootCmd.AddCommand(deployCmd)
	rootCmd.AddCommand(prepareCmd)
	rootCmd.AddCommand(activateCmd)
	deployCmd.PersistentFlags().StringVarP(&zoneArg, zoneFlag, "z", "dev.aws-us-east-1c", "The zone to use for deployment")
}

var deployCmd = &cobra.Command{
	Use:   "deploy <application-directory>",
	Short: "Deploy (prepare and activate) an application package",
	Long: `Deploy (prepare and activate) an application package.

When this returns successfully the application package has been validated
and activated on config servers. The process of applying it on individual nodes
has started but may not have completed.`,
	Example: "$ vespa deploy .",
	Args:    cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		pkg, err := vespa.ApplicationPackageFrom(applicationSource(args))
		if err != nil {
			printErr(nil, err.Error())
			return
		}
		target := getTarget()
		opts := vespa.DeploymentOpts{ApplicationPackage: pkg, Target: target}
		if opts.IsCloud() {
			deployment := deploymentFromArgs()
			if !opts.ApplicationPackage.HasCertificate() {
				printErrHint(fmt.Errorf("Missing certificate in application package"), "Applications in Vespa Cloud require a certificate", "Try 'vespa cert'")
			}
			opts.APIKey = readAPIKey(deployment.Application.Tenant)
			opts.Deployment = deployment
		}
		if err := vespa.Deploy(opts); err == nil {
			printSuccess("Deployed ", color.Cyan(pkg.Path))
			if opts.IsCloud() {
				log.Printf("\nUse %s for deployment status, or see", color.Cyan("vespa status"))
				log.Print(color.Cyan(fmt.Sprintf("https://console.vespa.oath.cloud/tenant/%s/application/%s/dev/instance/%s", opts.Deployment.Application.Tenant, opts.Deployment.Application.Application, opts.Deployment.Application.Instance)))
			}
		} else {
			printErr(nil, err.Error())
		}
	},
}

var prepareCmd = &cobra.Command{
	Use:   "prepare <application-directory>",
	Short: "Prepare an application package for activation",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		pkg, err := vespa.ApplicationPackageFrom(applicationSource(args))
		if err != nil {
			printErr(err, "Could not find application package")
			return
		}
		configDir := configDir("default")
		if configDir == "" {
			return
		}
		target := getTarget()
		sessionID, err := vespa.Prepare(vespa.DeploymentOpts{
			ApplicationPackage: pkg,
			Target:             target,
		})
		if err == nil {
			writeSessionID(configDir, sessionID)
			printSuccess("Prepared ", color.Cyan(pkg.Path), " with session ", sessionID)
		} else {
			printErr(nil, err.Error())
		}
	},
}

var activateCmd = &cobra.Command{
	Use:   "activate",
	Short: "Activate (deploy) a previously prepared application package",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		pkg, err := vespa.ApplicationPackageFrom(applicationSource(args))
		if err != nil {
			printErr(err, "Could not find application package")
			return
		}
		configDir := configDir("default")
		sessionID := readSessionID(configDir)
		target := getTarget()
		err = vespa.Activate(sessionID, vespa.DeploymentOpts{
			ApplicationPackage: pkg,
			Target:             target,
		})
		if err == nil {
			printSuccess("Activated ", color.Cyan(pkg.Path), " with session ", sessionID)
		} else {
			printErr(nil, err.Error())
		}
	},
}

func writeSessionID(appConfigDir string, sessionID int64) {
	if err := os.MkdirAll(appConfigDir, 0755); err != nil {
		printErr(err, "Could not create directory for session ID")
	}
	if err := os.WriteFile(sessionIDFile(appConfigDir), []byte(fmt.Sprintf("%d\n", sessionID)), 0600); err != nil {
		printErr(err, "Could not write session ID")
	}
}

func readSessionID(appConfigDir string) int64 {
	b, err := os.ReadFile(sessionIDFile(appConfigDir))
	if err != nil {
		printErr(err, "Could not read session ID")
	}
	id, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
	if err != nil {
		printErr(err, "Invalid session ID")
	}
	return id
}

func sessionIDFile(appConfigDir string) string { return filepath.Join(appConfigDir, "session_id") }