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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej
// utilities to get and manipulate node states in a storage cluster
package clusterstate
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
"github.com/vespa-engine/vespa/client/go/internal/curl"
"github.com/vespa-engine/vespa/client/go/internal/vespa"
)
func curlCommand(url string, args []string) (*curl.Command, error) {
tls, err := vespa.LoadTlsConfig()
if err != nil {
return nil, err
}
if tls != nil && strings.HasPrefix(url, "http:") {
url = "https:" + url[5:]
}
cmd, err := curl.RawArgs(url, args...)
if err != nil {
return nil, err
}
if tls != nil {
if tls.DisableHostnameValidation {
cmd, err = curl.RawArgs(url, append(args, "--insecure")...)
if err != nil {
return nil, err
}
}
cmd.PrivateKey = tls.Files.PrivateKey
cmd.Certificate = tls.Files.Certificates
cmd.CaCertificate = tls.Files.CaCertificates
}
return cmd, err
}
func curlGet(url string, output io.Writer) error {
cmd, err := curlCommand(url, commonCurlArgs())
if err != nil {
return err
}
trace.Trace("running curl:", cmd.String())
err = cmd.Run(output, os.Stderr)
return err
}
func curlPost(url string, input []byte) (string, error) {
cmd, err := curlCommand(url, commonCurlArgs())
cmd.Method = "POST"
cmd.Header("Content-Type", "application/json")
cmd.WithBodyInput(bytes.NewReader(input))
var out bytes.Buffer
trace.Debug("POST input: " + string(input))
trace.Trace("running curl:", cmd.String())
err = cmd.Run(&out, os.Stderr)
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
if ee.ProcessState.ExitCode() == 7 {
return "", fmt.Errorf("HTTP request to %s failed, could not connect", url)
}
}
return "", fmt.Errorf("HTTP request failed with curl %s", err.Error())
}
return out.String(), err
}
func commonCurlArgs() []string {
return []string{
"-A", "vespa-cluster-state",
"--silent",
"--show-error",
"--connect-timeout", "30",
"--max-time", "1200",
"--write-out", "\n%{http_code}",
}
}
|