/
step_forward_port_over_ssh.go
47 lines (32 loc) · 1.33 KB
/
step_forward_port_over_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
package common
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type StepForwardPortOverSSH struct {
RemotePort func(state multistep.StateBag) (uint, error)
RemoteDest func(state multistep.StateBag) (string, error)
HostPortMin uint
HostPortMax uint
ResultKey string
}
func (self *StepForwardPortOverSSH) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
// Find a free local port:
l, sshHostPort := FindPort(self.HostPortMin, self.HostPortMax)
if l == nil || sshHostPort == 0 {
ui.Error("Error: unable to find free host port. Try providing a larger range [host_port_min, host_port_max]")
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Creating a local port forward over SSH on local port %d", sshHostPort))
remotePort, _ := self.RemotePort(state)
remoteDest, _ := self.RemoteDest(state)
go ssh_port_forward(l, remotePort, remoteDest, config.HostIp, config.Username, config.Password)
ui.Say(fmt.Sprintf("Port forward setup. %d ---> %s:%d on %s", sshHostPort, remoteDest, remotePort, config.HostIp))
// Provide the local port to future steps.
state.Put(self.ResultKey, sshHostPort)
return multistep.ActionContinue
}
func (self *StepForwardPortOverSSH) Cleanup(state multistep.StateBag) {}