This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_ssh.go
132 lines (118 loc) · 3.02 KB
/
instance_ssh.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
package acceptance
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type InstanceSSH interface {
RunCommand(cmd string) (stdout, stderr string, exitCode int, err error)
RunCommandWithSudo(cmd string) (stdout, stderr string, exitCode int, err error)
}
type instanceSSH struct {
vmUsername string
vmIP string
vmPort string
privateKeyPath string
instanceUsername string
instanceIP string
instancePassword string
runner boshsys.CmdRunner
fileSystem boshsys.FileSystem
}
func NewInstanceSSH(
vmUsername string,
vmIP string,
vmPort string,
privateKeyPath string,
instanceUsername string,
instanceIP string,
instancePassword string,
fileSystem boshsys.FileSystem,
logger boshlog.Logger,
) InstanceSSH {
return &instanceSSH{
vmUsername: vmUsername,
vmIP: vmIP,
vmPort: vmPort,
privateKeyPath: privateKeyPath,
instanceUsername: instanceUsername,
instanceIP: instanceIP,
instancePassword: instancePassword,
runner: boshsys.NewExecCmdRunner(logger),
fileSystem: fileSystem,
}
}
func (s *instanceSSH) setupSSH() (boshsys.File, error) {
sshConfigFile, err := s.fileSystem.TempFile("ssh-config")
if err != nil {
return nil, bosherr.WrapError(err, "Creating temp ssh-config file")
}
success := false
defer func() {
if !success {
s.fileSystem.RemoveAll(sshConfigFile.Name())
}
}()
sshConfigTemplate := `
Host vagrant-vm
HostName %s
User %s
Port %s
StrictHostKeyChecking no
IdentityFile %s
Host warden-vm
Hostname %s
User %s
StrictHostKeyChecking no
ProxyCommand ssh -q -F %s vagrant-vm netcat -w 120 %%h %%p
`
sshConfig := fmt.Sprintf(
sshConfigTemplate,
s.vmIP,
s.vmUsername,
s.vmPort,
s.privateKeyPath,
s.instanceIP,
s.instanceUsername,
sshConfigFile.Name(),
)
err = s.fileSystem.WriteFileString(sshConfigFile.Name(), sshConfig)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Writing to temp ssh-config file: '%s'", sshConfigFile.Name())
}
success = true
return sshConfigFile, nil
}
func (s *instanceSSH) RunCommand(cmd string) (stdout, stderr string, exitCode int, err error) {
sshConfigFile, err := s.setupSSH()
if err != nil {
return "", "", -1, bosherr.WrapError(err, "Setting up SSH")
}
defer s.fileSystem.RemoveAll(sshConfigFile.Name())
return s.runner.RunCommand(
"sshpass",
"-p"+s.instancePassword,
"ssh",
"warden-vm",
"-F",
sshConfigFile.Name(),
cmd,
)
}
func (s *instanceSSH) RunCommandWithSudo(cmd string) (stdout, stderr string, exitCode int, err error) {
sshConfigFile, err := s.setupSSH()
if err != nil {
return "", "", -1, bosherr.WrapError(err, "Setting up SSH")
}
defer s.fileSystem.RemoveAll(sshConfigFile.Name())
return s.runner.RunCommand(
"sshpass",
"-p"+s.instancePassword,
"ssh",
"warden-vm",
"-F",
sshConfigFile.Name(),
fmt.Sprintf("echo %s | sudo -p '' -S %s", s.instancePassword, cmd),
)
}