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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa-deploy command
// Author: arnej
package deploy
import (
"fmt"
"os"
"strconv"
)
// main entry point for vespa-deploy prepare
func looksLikeNumber(s string) bool {
var i, j int
n, err := fmt.Sscanf(s+" 123", "%d %d", &i, &j)
return n == 2 && err == nil
}
func RunPrepare(opts *Options, args []string) (err error) {
var response string
if len(args) == 0 {
// prepare last upload
sessId := getSessionIdFromFile(opts.Tenant)
response, err = doPrepare(opts, sessId)
} else if isFileOrDir(args[0]) {
err := RunUpload(opts, args)
if err != nil {
return err
}
return RunPrepare(opts, []string{})
} else if looksLikeNumber(args[0]) {
response, err = doPrepare(opts, args[0])
} else {
err = fmt.Errorf("Command failed. No directory or zip file found: '%s'", args[0])
}
if err != nil {
return err
}
var result PrepareResult
code, err := decodeResponse(response, &result)
if err != nil {
return err
}
for _, entry := range result.Log {
fmt.Println(entry.Level+":", entry.Message)
}
if code != 200 {
return fmt.Errorf("Request failed. HTTP status code: %d\n%s", code, result.Message)
}
fmt.Println(result.Message)
return err
}
func isFileOrDir(name string) bool {
f, err := os.Open(name)
if err != nil {
return false
}
st, err := f.Stat()
if err != nil {
return false
}
return st.Mode().IsRegular() || st.Mode().IsDir()
}
func doPrepare(opts *Options, sessionId string) (output string, err error) {
src := makeConfigsourceUrl(opts)
url := src + pathPrefix(opts) + "/" + sessionId + "/prepared"
url = addUrlPropertyFromFlag(url, opts.Force, "ignoreValidationErrors")
url = addUrlPropertyFromFlag(url, opts.DryRun, "dryRun")
url = addUrlPropertyFromFlag(url, opts.Verbose, "verbose")
url = addUrlPropertyFromFlag(url, opts.Hosted, "hostedVespa")
url = addUrlPropertyFromOption(url, opts.Application, "applicationName")
url = addUrlPropertyFromOption(url, opts.Instance, "instance")
url = addUrlPropertyFromOption(url, strconv.Itoa(opts.Timeout), "timeout")
url = addUrlPropertyFromOption(url, opts.Rotations, "rotations")
url = addUrlPropertyFromOption(url, opts.VespaVersion, "vespaVersion")
fmt.Printf("Preparing session %s using %s\n", sessionId, urlWithoutQuery(url))
output, err = curlPutNothing(url)
return
}
|