-
Notifications
You must be signed in to change notification settings - Fork 346
/
vagrant.go
144 lines (127 loc) · 4.5 KB
/
vagrant.go
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
// Copyright 2019 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package providers
import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"github.com/vmware-tanzu/antrea/test/e2e/providers/exec"
)
func vagrantPath() (string, error) {
// TODO: find a better way to get this path?
_, fileName, _, ok := runtime.Caller(1)
if !ok {
return "", fmt.Errorf("cannot locate caller information")
}
return path.Join(path.Dir(path.Dir(fileName)), "infra", "vagrant"), nil
}
func importConfig() (*ssh_config.Config, error) {
vagrantPath, err := vagrantPath()
if err != nil {
return nil, fmt.Errorf("cannot compute path to infra/vagrant")
}
configPath := path.Join(vagrantPath, "ssh-config")
f, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("cannot open ssh-config file: %v", err)
}
config, err := ssh_config.Decode(f)
if err != nil {
return nil, fmt.Errorf("cannot decode ssh-config file: %v", err)
}
return config, nil
}
func convertConfig(inConfig *ssh_config.Config, name string) (string, *ssh.ClientConfig, error) {
if inConfig == nil {
return "", nil, fmt.Errorf("input config is nil")
}
getFromKeyStrict := func(key string) (string, error) {
v, err := inConfig.Get(name, key)
if err != nil {
return "", fmt.Errorf("error when retrieving '%s' for '%s' in SSH config: %v", key, name, err)
}
if v == "" {
return "", fmt.Errorf("unable to find '%s' for '%s' in SSH config", key, name)
}
return v, nil
}
keyList := []string{"HostName", "Port", "User", "IdentityFile"}
values := make(map[string]string)
for _, key := range keyList {
if value, err := getFromKeyStrict(key); err != nil {
return "", nil, err
} else {
values[key] = value
}
}
identityFile := values["IdentityFile"]
// Read the private key identified by identityFile.
key, err := ioutil.ReadFile(identityFile)
if err != nil {
return "", nil, fmt.Errorf("unable to read private key from file '%s': %v", identityFile, err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return "", nil, fmt.Errorf("unable to parse private key from file '%s': %v", identityFile, err)
}
// #nosec G106: we are using ssh.InsecureIgnoreHostKey, but this is test code
config := &ssh.ClientConfig{
User: values["User"],
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
host := fmt.Sprintf("%s:%s", values["HostName"], values["Port"])
return host, config, nil
}
func GetSSHConfig(name string) (string, *ssh.ClientConfig, error) {
// We convert the Vagrant ssh config file (generated by "vagrant ssh-config"), which is an
// OpenSSH config file, to an instance of ssh.ClientConfig.
sshConfig, err := importConfig()
if err != nil {
return "", nil, err
}
return convertConfig(sshConfig, name)
}
type VagrantProvider struct{}
func (provider *VagrantProvider) RunCommandOnNode(nodeName string, cmd string) (
code int, stdout string, stderr string, err error,
) {
host, config, err := GetSSHConfig(nodeName)
if err != nil {
return 0, "", "", fmt.Errorf("error when retrieving SSH config for node '%s': %v", nodeName, err)
}
return exec.RunSSHCommand(host, config, cmd)
}
func (provider *VagrantProvider) GetKubeconfigPath() (string, error) {
vagrantPath, err := vagrantPath()
if err != nil {
return "", fmt.Errorf("cannot compute path to infra/vagrant")
}
kubeconfigPath := path.Join(vagrantPath, "playbook", "kube", "config")
if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
return "", fmt.Errorf("Kubeconfig file not found at expected location '%s'", kubeconfigPath)
}
return kubeconfigPath, nil
}
// NewVagrantProvider returns an implementation of ProviderInterface which is suitable for a
// Kubernetes test cluster created with the provided Vagrantfile.
// configPath is unused for the vagrant provider
func NewVagrantProvider(configPath string) (ProviderInterface, error) {
return &VagrantProvider{}, nil
}