Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ssh_no_proxy parameter (via @gaserre to address issue #84) #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type CommonConfig struct {

// SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
// SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHNoProxy bool `mapstructure:"ssh_no_proxy"`
SSHKeyPath string `mapstructure:"ssh_key_path"`
SSHPassword string `mapstructure:"ssh_password"`
SSHPort uint `mapstructure:"ssh_port"`
Expand Down
43 changes: 36 additions & 7 deletions builder/xenserver/common/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"bytes"
"errors"
"fmt"
"github.com/mitchellh/multistep"
commonssh "github.com/mitchellh/packer/common/ssh"
Expand All @@ -19,6 +20,12 @@ func SSHAddress(state multistep.StateBag) (string, error) {
return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil
}

func SSHInstanceAddress(state multistep.StateBag) (string, error) {
sshIP := state.Get("instance_ssh_address").(string)
sshPort := 22
return fmt.Sprintf("%s:%d", sshIP, sshPort), nil
}

func SSHLocalAddress(state multistep.StateBag) (string, error) {
sshLocalPort, ok := state.Get("local_ssh_port").(uint)
if !ok {
Expand All @@ -30,11 +37,26 @@ func SSHLocalAddress(state multistep.StateBag) (string, error) {

func SSHPort(state multistep.StateBag) (int, error) {
sshHostPort := state.Get("local_ssh_port").(uint)
if sshHostPort == 0 {
sshHostPort = state.Get("commonconfig").(CommonConfig).SSHPort
}
return int(sshHostPort), nil
}

func CommHost(state multistep.StateBag) (string, error) {
return "127.0.0.1", nil
var sshIP string
config := state.Get("commonconfig").(CommonConfig)
if config.SSHNoProxy {
sshIP = state.Get("instance_ssh_address").(string)
} else {
if state.Get("local_ssh_port").(uint) > 0 {
sshIP = "127.0.0.1"
} else {
return "", errors.New("CommHost: local_ssh_port not set")
}
}
log.Printf("[DEBUG] CommHost: sshIP: %s", sshIP)
return sshIP, nil
}

func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
Expand Down Expand Up @@ -100,16 +122,23 @@ func ExecuteHostSSHCmd(state multistep.StateBag, cmd string) (stdout string, err

func ExecuteGuestSSHCmd(state multistep.StateBag, cmd string) (stdout string, err error) {
config := state.Get("commonconfig").(CommonConfig)
localAddress, err := SSHLocalAddress(state)
if err != nil {
return
var address string
var address_err error
if config.SSHNoProxy {
address, address_err = SSHInstanceAddress(state)
} else {
address, err = SSHLocalAddress(state)
}
sshConfig, err := SSHConfigFunc(config.SSHConfig)(state)
if err != nil {
if address_err != nil {
return "", address_err
}
sshConfig, ssh_err := SSHConfigFunc(config.SSHConfig)(state)
if ssh_err != nil {
return
}

return doExecuteSSHCmd(cmd, localAddress, sshConfig)
log.Printf("[DEBUG] doExecuteSSHCmd: %s, %s", cmd, address)
return doExecuteSSHCmd(cmd, address, sshConfig)
}

func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_dest string, remote_port uint) error {
Expand Down
7 changes: 7 additions & 0 deletions builder/xenserver/common/step_forward_port_over_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

type StepForwardPortOverSSH struct {
// If NoProxy, don't do proxying (forwarding)
NoProxy bool
RemotePort func(state multistep.StateBag) (uint, error)
RemoteDest func(state multistep.StateBag) (string, error)

Expand All @@ -20,6 +22,11 @@ func (self *StepForwardPortOverSSH) Run(state multistep.StateBag) multistep.Step

config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
if self.NoProxy {
ui.Say(fmt.Sprintf("Not using SSH port forwarding"))
state.Put(self.ResultKey, uint(0))
return multistep.ActionContinue
}

// Find a free local port:

Expand Down
2 changes: 2 additions & 0 deletions builder/xenserver/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
new(xscommon.StepSetVmHostSshAddress),
new(xscommon.StepGetVNCPort),
&xscommon.StepForwardPortOverSSH{
NoProxy: false,
RemotePort: xscommon.InstanceVNCPort,
RemoteDest: xscommon.InstanceVNCIP,
HostPortMin: self.config.HostPortMin,
Expand All @@ -292,6 +293,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
Timeout: self.config.InstallTimeout, // @todo change this
},
&xscommon.StepForwardPortOverSSH{
NoProxy: self.config.SSHNoProxy,
RemotePort: xscommon.InstanceSSHPort,
RemoteDest: xscommon.InstanceSSHIP,
HostPortMin: self.config.HostPortMin,
Expand Down
2 changes: 2 additions & 0 deletions builder/xenserver/xva/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
new(xscommon.StepSetVmHostSshAddress),
new(xscommon.StepGetVNCPort),
&xscommon.StepForwardPortOverSSH{
NoProxy: false,
RemotePort: xscommon.InstanceVNCPort,
RemoteDest: xscommon.InstanceVNCIP,
HostPortMin: self.config.HostPortMin,
Expand All @@ -161,6 +162,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this
},
&xscommon.StepForwardPortOverSSH{
NoProxy: self.config.SSHNoProxy,
RemotePort: xscommon.InstanceSSHPort,
RemoteDest: xscommon.InstanceSSHIP,
HostPortMin: self.config.HostPortMin,
Expand Down