summaryrefslogtreecommitdiffstats
path: root/client/go/vespa/deploy.go
blob: 9993a773e4f0a0fd540f0a6766c06bf884e590ae (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa deploy API
// Author: bratseth

package vespa

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/vespa-engine/vespa/client/go/util"
)

var DefaultApplication = ApplicationID{Tenant: "default", Application: "application", Instance: "default"}

type ApplicationID struct {
	Tenant      string
	Application string
	Instance    string
}

type ZoneID struct {
	Environment string
	Region      string
}

type Deployment struct {
	System      System
	Application ApplicationID
	Zone        ZoneID
}

type DeploymentOptions struct {
	Target             Target
	ApplicationPackage ApplicationPackage
	Timeout            time.Duration
	HTTPClient         util.HTTPClient
}

type LogLinePrepareResponse struct {
	Time    int64
	Level   string
	Message string
}

type PrepareResult struct {
	// Session or Run ID
	ID       int64
	LogLines []LogLinePrepareResponse
}

func (a ApplicationID) String() string {
	return fmt.Sprintf("%s.%s.%s", a.Tenant, a.Application, a.Instance)
}

func (a ApplicationID) SerializedForm() string {
	return fmt.Sprintf("%s:%s:%s", a.Tenant, a.Application, a.Instance)
}

func (d Deployment) String() string {
	return fmt.Sprintf("deployment of %s in %s", d.Application, d.Zone)
}

func (d DeploymentOptions) String() string {
	return fmt.Sprintf("%s to %s", d.Target.Deployment(), d.Target.Type())
}

// IsCloud returns whether this is a deployment to Vespa Cloud or hosted Vespa
func (d *DeploymentOptions) IsCloud() bool {
	return d.Target.Type() == TargetCloud || d.Target.Type() == TargetHosted
}

func (d *DeploymentOptions) url(path string) (*url.URL, error) {
	service, err := d.Target.Service(DeployService, 0, 0, "")
	if err != nil {
		return nil, err
	}
	return url.Parse(service.BaseURL + path)
}

func ApplicationFromString(s string) (ApplicationID, error) {
	parts := strings.Split(s, ".")
	if len(parts) != 3 {
		return ApplicationID{}, fmt.Errorf("invalid application: %q", s)
	}
	return ApplicationID{Tenant: parts[0], Application: parts[1], Instance: parts[2]}, nil
}

func ZoneFromString(s string) (ZoneID, error) {
	parts := strings.Split(s, ".")
	if len(parts) != 2 {
		return ZoneID{}, fmt.Errorf("invalid zone: %q", s)
	}
	return ZoneID{Environment: parts[0], Region: parts[1]}, nil
}

// Prepare deployment and return the session ID
func Prepare(deployment DeploymentOptions) (PrepareResult, error) {
	if deployment.IsCloud() {
		return PrepareResult{}, fmt.Errorf("prepare is not supported with %s target", deployment.Target.Type())
	}
	sessionURL, err := deployment.url("/application/v2/tenant/default/session")
	if err != nil {
		return PrepareResult{}, err
	}
	result, err := uploadApplicationPackage(sessionURL, deployment)
	if err != nil {
		return PrepareResult{}, err
	}
	prepareURL, err := deployment.url(fmt.Sprintf("/application/v2/tenant/default/session/%d/prepared", result.ID))
	if err != nil {
		return PrepareResult{}, err
	}
	req, err := http.NewRequest("PUT", prepareURL.String(), nil)
	if err != nil {
		return PrepareResult{}, err
	}
	serviceDescription := "Deploy service"
	response, err := deployment.HTTPClient.Do(req, time.Second*30)
	if err != nil {
		return PrepareResult{}, err
	}
	defer response.Body.Close()
	if err := checkResponse(req, response, serviceDescription); err != nil {
		return PrepareResult{}, err
	}
	return result, nil
}

// Activate deployment with sessionID from a past prepare
func Activate(sessionID int64, deployment DeploymentOptions) error {
	if deployment.IsCloud() {
		return fmt.Errorf("activate is not supported with %s target", deployment.Target.Type())
	}
	u, err := deployment.url(fmt.Sprintf("/application/v2/tenant/default/session/%d/active", sessionID))
	if err != nil {
		return err
	}
	req, err := http.NewRequest("PUT", u.String(), nil)
	if err != nil {
		return err
	}
	serviceDescription := "Deploy service"
	response, err := deployment.HTTPClient.Do(req, time.Second*30)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	return checkResponse(req, response, serviceDescription)
}

func Deploy(opts DeploymentOptions) (PrepareResult, error) {
	path := "/application/v2/tenant/default/prepareandactivate"
	if opts.IsCloud() {
		if err := checkDeploymentOpts(opts); err != nil {
			return PrepareResult{}, err
		}
		if opts.Target.Deployment().Zone.Environment == "" || opts.Target.Deployment().Zone.Region == "" {
			return PrepareResult{}, fmt.Errorf("%s: missing zone", opts)
		}
		path = fmt.Sprintf("/application/v4/tenant/%s/application/%s/instance/%s/deploy/%s-%s",
			opts.Target.Deployment().Application.Tenant,
			opts.Target.Deployment().Application.Application,
			opts.Target.Deployment().Application.Instance,
			opts.Target.Deployment().Zone.Environment,
			opts.Target.Deployment().Zone.Region)
	}
	u, err := opts.url(path)
	if err != nil {
		return PrepareResult{}, err
	}
	return uploadApplicationPackage(u, opts)
}

func copyToPart(dst *multipart.Writer, src io.Reader, fieldname, filename string) error {
	var part io.Writer
	var err error
	if filename == "" {
		part, err = dst.CreateFormField(fieldname)
	} else {
		part, err = dst.CreateFormFile(fieldname, filename)
	}
	if err != nil {
		return err
	}
	if _, err := io.Copy(part, src); err != nil {
		return err
	}
	return nil
}

func Submit(opts DeploymentOptions) error {
	if !opts.IsCloud() {
		return fmt.Errorf("%s: submit is unsupported by %s target", opts, opts.Target.Type())
	}
	if err := checkDeploymentOpts(opts); err != nil {
		return err
	}
	path := fmt.Sprintf("/application/v4/tenant/%s/application/%s/submit", opts.Target.Deployment().Application.Tenant, opts.Target.Deployment().Application.Application)
	u, err := opts.url(path)
	if err != nil {
		return err
	}
	var body bytes.Buffer
	writer := multipart.NewWriter(&body)
	if err := copyToPart(writer, strings.NewReader("{}"), "submitOptions", ""); err != nil {
		return err
	}
	applicationZip, err := opts.ApplicationPackage.zipReader(false)
	if err != nil {
		return err
	}
	if err := copyToPart(writer, applicationZip, "applicationZip", "application.zip"); err != nil {
		return err
	}
	testApplicationZip, err := opts.ApplicationPackage.zipReader(true)
	if err != nil {
		return err
	}
	if err := copyToPart(writer, testApplicationZip, "applicationTestZip", "application-test.zip"); err != nil {
		return err
	}
	if err := writer.Close(); err != nil {
		return err
	}
	request := &http.Request{
		URL:    u,
		Method: "POST",
		Body:   io.NopCloser(&body),
		Header: make(http.Header),
	}
	request.Header.Set("Content-Type", writer.FormDataContentType())
	serviceDescription := "Submit service"
	sigKeyId := opts.Target.Deployment().Application.SerializedForm()
	if err := opts.Target.SignRequest(request, sigKeyId); err != nil {
		return fmt.Errorf("failed to sign api request: %w", err)
	}
	response, err := opts.HTTPClient.Do(request, time.Minute*10)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	return checkResponse(request, response, serviceDescription)
}

func checkDeploymentOpts(opts DeploymentOptions) error {
	if opts.Target.Type() == TargetCloud && !opts.ApplicationPackage.HasCertificate() {
		return fmt.Errorf("%s: missing certificate in package", opts)
	}
	return nil
}

func uploadApplicationPackage(url *url.URL, opts DeploymentOptions) (PrepareResult, error) {
	zipReader, err := opts.ApplicationPackage.zipReader(false)
	if err != nil {
		return PrepareResult{}, err
	}
	header := http.Header{}
	header.Add("Content-Type", "application/zip")
	request := &http.Request{
		URL:    url,
		Method: "POST",
		Header: header,
		Body:   io.NopCloser(zipReader),
	}
	service, err := opts.Target.Service(DeployService, opts.Timeout, 0, "")
	if err != nil {
		return PrepareResult{}, err
	}

	keyID := opts.Target.Deployment().Application.SerializedForm()
	if err := opts.Target.SignRequest(request, keyID); err != nil {
		return PrepareResult{}, err
	}
	response, err := service.Do(request, time.Minute*10)
	if err != nil {
		return PrepareResult{}, err
	}
	defer response.Body.Close()

	var jsonResponse struct {
		SessionID string `json:"session-id"` // Config server
		RunID     int64  `json:"run"`        // Controller

		Log []LogLinePrepareResponse `json:"log"`
	}
	jsonResponse.SessionID = "0" // Set a default session ID for responses that don't contain int (e.g. cloud deployment)
	if err := checkResponse(request, response, service.Description()); err != nil {
		return PrepareResult{}, err
	}
	jsonDec := json.NewDecoder(response.Body)
	jsonDec.Decode(&jsonResponse) // Ignore error in case this is a non-JSON response
	id := jsonResponse.RunID
	if id == 0 {
		id, err = strconv.ParseInt(jsonResponse.SessionID, 10, 64)
		if err != nil {
			return PrepareResult{}, err
		}
	}
	return PrepareResult{
		ID:       id,
		LogLines: jsonResponse.Log,
	}, err
}

func checkResponse(req *http.Request, response *http.Response, serviceDescription string) error {
	if response.StatusCode/100 == 4 {
		return fmt.Errorf("invalid application package (%s)\n%s", response.Status, extractError(response.Body))
	} else if response.StatusCode != 200 {
		return fmt.Errorf("error from %s at %s (%s):\n%s", strings.ToLower(serviceDescription), req.URL.Host, response.Status, util.ReaderToJSON(response.Body))
	}
	return nil
}

// Returns the error message in the given JSON, or the entire content if it could not be extracted
func extractError(reader io.Reader) string {
	responseData, _ := io.ReadAll(reader)
	var response map[string]interface{}
	json.Unmarshal(responseData, &response)
	if response["error-code"] == "INVALID_APPLICATION_PACKAGE" {
		return strings.ReplaceAll(response["message"].(string), ": ", ":\n")
	} else {
		var prettyJSON bytes.Buffer
		parseError := json.Indent(&prettyJSON, responseData, "", "    ")
		if parseError != nil { // Not JSON: Print plainly
			return string(responseData)
		}
		return prettyJSON.String()
	}
}