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

package vespa

import (
	"archive/zip"
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"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 {
	Application ApplicationID
	Zone        ZoneID
}

type DeploymentOpts struct {
	ApplicationPackage ApplicationPackage
	Target             Target
	Deployment         Deployment
	APIKey             []byte
}

type ApplicationPackage struct {
	Path string
}

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 DeploymentOpts) String() string {
	return fmt.Sprintf("%s to %s", d.Deployment, d.Target.Type())
}

func (d *DeploymentOpts) IsCloud() bool { return d.Target.Type() == cloudTargetType }

func (d *DeploymentOpts) 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 (ap *ApplicationPackage) HasCertificate() bool {
	if ap.IsZip() {
		r, err := zip.OpenReader(ap.Path)
		if err != nil {
			return false
		}
		defer r.Close()
		for _, f := range r.File {
			if f.Name == "security/clients.pem" {
				return true
			}
		}
		return false
	}
	return util.PathExists(filepath.Join(ap.Path, "security", "clients.pem"))
}

func (ap *ApplicationPackage) IsZip() bool { return isZip(ap.Path) }

func (ap *ApplicationPackage) zipReader() (io.ReadCloser, error) {
	zipFile := ap.Path
	if !ap.IsZip() {
		tempZip, error := ioutil.TempFile("", "application.zip")
		if error != nil {
			return nil, fmt.Errorf("Could not create a temporary zip file for the application package: %w", error)
		}
		if err := zipDir(ap.Path, tempZip.Name()); err != nil {
			return nil, err
		}
		defer os.Remove(tempZip.Name())
		zipFile = tempZip.Name()
	}
	r, err := os.Open(zipFile)
	if err != nil {
		return nil, fmt.Errorf("Could not open application package at %s: %w", ap.Path, err)
	}
	return r, nil
}

// FindApplicationPackage finds the path to an application package from the zip file or directory zipOrDir.
func FindApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) {
	if isZip(zipOrDir) {
		return ApplicationPackage{Path: zipOrDir}, nil
	}
	if util.PathExists(filepath.Join(zipOrDir, "pom.xml")) {
		zip := filepath.Join(zipOrDir, "target", "application.zip")
		if util.PathExists(zip) {
			return ApplicationPackage{Path: zip}, nil
		}
		if requirePackaging {
			return ApplicationPackage{}, errors.New("pom.xml exists but no target/application.zip. Run mvn package first")
		}
	}
	if util.PathExists(filepath.Join(zipOrDir, "src", "main", "application")) {
		return ApplicationPackage{Path: filepath.Join(zipOrDir, "src", "main", "application")}, nil
	}
	if util.PathExists(filepath.Join(zipOrDir, "services.xml")) {
		return ApplicationPackage{Path: zipOrDir}, nil
	}
	return ApplicationPackage{}, errors.New("Could not find an application package source in '" + zipOrDir + "'")
}

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 DeploymentOpts) (int64, error) {
	if deployment.IsCloud() {
		return 0, 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 0, err
	}
	sessionID, err := uploadApplicationPackage(sessionURL, deployment)
	if err != nil {
		return 0, err
	}
	prepareURL, err := deployment.url(fmt.Sprintf("/application/v2/tenant/default/session/%d/prepared", sessionID))
	if err != nil {
		return 0, err
	}
	req, err := http.NewRequest("PUT", prepareURL.String(), nil)
	if err != nil {
		return 0, err
	}
	serviceDescription := "Deploy service"
	response, err := util.HttpDo(req, time.Second*30, serviceDescription)
	if err != nil {
		return 0, err
	}
	defer response.Body.Close()
	if err := checkResponse(req, response, serviceDescription); err != nil {
		return 0, err
	}
	return sessionID, nil
}

// Activate deployment with sessionID from a past prepare
func Activate(sessionID int64, deployment DeploymentOpts) 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 := util.HttpDo(req, time.Second*30, serviceDescription)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	return checkResponse(req, response, serviceDescription)
}

func Deploy(opts DeploymentOpts) (int64, error) {
	path := "/application/v2/tenant/default/prepareandactivate"
	if opts.IsCloud() {
		if !opts.ApplicationPackage.HasCertificate() {
			return 0, fmt.Errorf("%s: missing certificate in package", opts)
		}
		if opts.APIKey == nil {
			return 0, fmt.Errorf("%s: missing api key", opts.String())
		}
		if opts.Deployment.Zone.Environment == "" || opts.Deployment.Zone.Region == "" {
			return 0, fmt.Errorf("%s: missing zone", opts)
		}
		path = fmt.Sprintf("/application/v4/tenant/%s/application/%s/instance/%s/deploy/%s-%s",
			opts.Deployment.Application.Tenant,
			opts.Deployment.Application.Application,
			opts.Deployment.Application.Instance,
			opts.Deployment.Zone.Environment,
			opts.Deployment.Zone.Region)
	}
	u, err := opts.url(path)
	if err != nil {
		return 0, err
	}
	return uploadApplicationPackage(u, opts)
}

func uploadApplicationPackage(url *url.URL, opts DeploymentOpts) (int64, error) {
	zipReader, err := opts.ApplicationPackage.zipReader()
	if err != nil {
		return 0, err
	}
	header := http.Header{}
	header.Add("Content-Type", "application/zip")
	request := &http.Request{
		URL:    url,
		Method: "POST",
		Header: header,
		Body:   ioutil.NopCloser(zipReader),
	}
	if opts.APIKey != nil {
		signer := NewRequestSigner(opts.Deployment.Application.SerializedForm(), opts.APIKey)
		if err := signer.SignRequest(request); err != nil {
			return 0, err
		}
	}
	serviceDescription := "Deploy service"
	response, err := util.HttpDo(request, time.Minute*10, serviceDescription)
	if err != nil {
		return 0, err
	}
	defer response.Body.Close()

	var jsonResponse struct {
		SessionID string `json:"session-id"` // Config server
		RunID     int64  `json:"run"`        // Controller
	}
	jsonResponse.SessionID = "0" // Set a default session ID for responses that don't contain int (e.g. cloud deployment)
	if err := checkResponse(request, response, serviceDescription); err != nil {
		return 0, err
	}
	jsonDec := json.NewDecoder(response.Body)
	jsonDec.Decode(&jsonResponse) // Ignore error in case this is a non-JSON response
	if jsonResponse.RunID > 0 {
		return jsonResponse.RunID, nil
	}
	return strconv.ParseInt(jsonResponse.SessionID, 10, 64)
}

func checkResponse(req *http.Request, response *http.Response, serviceDescription string) error {
	if response.StatusCode/100 == 4 {
		return fmt.Errorf("Invalid application package (%s)\n\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
}

func isZip(filename string) bool { return filepath.Ext(filename) == ".zip" }

func zipDir(dir string, destination string) error {
	if filepath.IsAbs(dir) {
		message := "Path must be relative, but '" + dir + "'"
		return errors.New(message)
	}
	if !util.PathExists(dir) {
		message := "'" + dir + "' should be an application package zip or dir, but does not exist"
		return errors.New(message)
	}
	if !util.IsDirectory(dir) {
		message := "'" + dir + "' should be an application package dir, but is a (non-zip) file"
		return errors.New(message)
	}

	file, err := os.Create(destination)
	if err != nil {
		message := "Could not create a temporary zip file for the application package: " + err.Error()
		return errors.New(message)
	}
	defer file.Close()

	w := zip.NewWriter(file)
	defer w.Close()

	walker := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()

		zippath := strings.TrimPrefix(path, dir)
		zipfile, err := w.Create(zippath)
		if err != nil {
			return err
		}

		_, err = io.Copy(zipfile, file)
		if err != nil {
			return err
		}
		return nil
	}
	return filepath.Walk(dir, walker)
}

// 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 := util.ReaderToBytes(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()
	}
}