Skip to content

Commit bc21b3c

Browse files
authored
Merge pull request #92 from mmahnic/remote_ssh_port
Add remote_ssh_port packer option
2 parents e460d5b + e62c1a1 commit bc21b3c

File tree

8 files changed

+31
-9
lines changed

8 files changed

+31
-9
lines changed

builder/xenserver/common/common_config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
)
1414

1515
type CommonConfig struct {
16-
Username string `mapstructure:"remote_username"`
17-
Password string `mapstructure:"remote_password"`
18-
HostIp string `mapstructure:"remote_host"`
16+
Username string `mapstructure:"remote_username"`
17+
Password string `mapstructure:"remote_password"`
18+
HostIp string `mapstructure:"remote_host"`
19+
HostSshPort uint `mapstructure:"remote_ssh_port"`
1920

2021
VMName string `mapstructure:"vm_name"`
2122
VMDescription string `mapstructure:"vm_description"`
@@ -64,6 +65,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
6465

6566
// Set default values
6667

68+
if c.HostSshPort == 0 {
69+
c.HostSshPort = 22
70+
}
71+
6772
if c.HostPortMin == 0 {
6873
c.HostPortMin = 5900
6974
}

builder/xenserver/common/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/xenserver/common/ssh.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
func SSHAddress(state multistep.StateBag) (string, error) {
1919
sshIP := state.Get("ssh_address").(string)
20-
sshHostPort := 22
20+
sshHostPort := state.Get("ssh_port").(uint)
2121
return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil
2222
}
2323

@@ -114,10 +114,10 @@ func ExecuteGuestSSHCmd(state multistep.StateBag, cmd string) (stdout string, er
114114
return doExecuteSSHCmd(cmd, localAddress, sshConfig)
115115
}
116116

117-
func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_dest string, remote_port uint) error {
117+
func forward(local_conn net.Conn, config *gossh.ClientConfig, server string, server_ssh_port int, remote_dest string, remote_port uint) error {
118118
defer local_conn.Close()
119119

120-
ssh_client_conn, err := gossh.Dial("tcp", server+":22", config)
120+
ssh_client_conn, err := gossh.Dial("tcp", fmt.Sprintf("%s:%d", server, server_ssh_port), config)
121121
if err != nil {
122122
log.Printf("local ssh.Dial error: %s", err)
123123
return err
@@ -157,7 +157,7 @@ func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_des
157157
return nil
158158
}
159159

160-
func ssh_port_forward(local_listener net.Listener, remote_port int, remote_dest, host, username, password string) error {
160+
func ssh_port_forward(local_listener net.Listener, remote_port int, remote_dest, host string, host_ssh_port int, username, password string) error {
161161

162162
config := &gossh.ClientConfig{
163163
User: username,
@@ -176,7 +176,7 @@ func ssh_port_forward(local_listener net.Listener, remote_port int, remote_dest,
176176
}
177177

178178
// Forward to a remote port
179-
go forward(local_connection, config, host, remote_dest, uint(remote_port))
179+
go forward(local_connection, config, host, host_ssh_port, remote_dest, uint(remote_port))
180180
}
181181

182182
return nil

builder/xenserver/common/step_forward_port_over_ssh.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ func (self *StepForwardPortOverSSH) Run(ctx context.Context, state multistep.Sta
3535
ui.Say(fmt.Sprintf("Creating a local port forward over SSH on local port %d", sshHostPort))
3636

3737
hostAddress, _ := state.Get("ssh_address").(string)
38+
hostSshPort, _ := state.Get("ssh_port").(int)
3839
remotePort, _ := self.RemotePort(state)
3940
remoteDest, _ := self.RemoteDest(state)
4041

41-
go ssh_port_forward(l, remotePort, remoteDest, hostAddress, config.Username, config.Password)
42+
go ssh_port_forward(l, remotePort, remoteDest, hostAddress, hostSshPort, config.Username, config.Password)
4243
ui.Say(fmt.Sprintf("Port forward setup. %d ---> %s:%d on %s", sshHostPort, remoteDest, remotePort, hostAddress))
4344

4445
// Provide the local port to future steps.

builder/xenserver/common/step_set_vm_host_ssh_address.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type StepSetVmHostSshAddress struct{}
1313
func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
1414

1515
c := state.Get("client").(*Connection)
16+
config := state.Get("config").(Config)
1617
ui := state.Get("ui").(packer.Ui)
1718

1819
ui.Say("Step: Set SSH address to VM host IP")
@@ -37,6 +38,9 @@ func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.St
3738
state.Put("ssh_address", address)
3839
ui.Say(fmt.Sprintf("Set host SSH address to '%s'.", address))
3940

41+
state.Put("ssh_port", config.HostSshPort)
42+
ui.Say(fmt.Sprintf("Set host SSH port to %d.", config.HostSshPort))
43+
4044
return multistep.ActionContinue
4145
}
4246

builder/xenserver/iso/builder_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
6161
if b.config.KeepVM != "never" {
6262
t.Errorf("bad keep instance: %s", b.config.KeepVM)
6363
}
64+
65+
if b.config.HostSshPort != 22 {
66+
t.Errorf("bad ssh port: %d", b.config.HostSshPort)
67+
}
6468
}
6569

6670
func TestBuilderPrepare_DiskSize(t *testing.T) {

builder/xenserver/xva/builder_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
5555
if b.config.KeepVM != "never" {
5656
t.Errorf("bad keep instance: %s", b.config.KeepVM)
5757
}
58+
59+
if b.config.HostSshPort != 22 {
60+
t.Errorf("bad ssh port: %d", b.config.HostSshPort)
61+
}
5862
}
5963

6064
func TestBuilderPrepare_Format(t *testing.T) {

docs/builders/iso/xenserver-iso.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ each category, the available options are alphabetized and described.
5858
* `remote_host` (string) - The host of the Xenserver / XCP-ng pool primary. Typically, these will be specified through
5959
environment variables as seen in the [examples](../../../examples).
6060

61+
* `remote_ssh_port` (integer) - The port that SSH will be listening on in the Xenserver / XCP-ng pool primary. By default this is 22.
62+
6163
* `remote_username` (string) - The XenServer username used to access the remote machine.
6264

6365
* `remote_password` (string) - The XenServer password for access to the remote machine.

0 commit comments

Comments
 (0)