From 30498d8258e515a80fd9a5de409d88c3c008e659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mahni=C4=8D?= Date: Thu, 3 Aug 2023 11:22:39 +0200 Subject: [PATCH] Add a new setting HostSshPort A XenServer host may accept SSH connections on port other than 22. This commit adds a packer option 'remote_ssh_port' to connect to a custom port. --- builder/xenserver/common/common_config.go | 11 ++++++++--- builder/xenserver/common/config.hcl2spec.go | 2 ++ builder/xenserver/common/ssh.go | 2 +- .../xenserver/common/step_set_vm_host_ssh_address.go | 4 ++++ builder/xenserver/iso/builder_test.go | 4 ++++ builder/xenserver/xva/builder_test.go | 4 ++++ docs/builders/iso/xenserver-iso.html.markdown | 2 ++ 7 files changed, 25 insertions(+), 4 deletions(-) diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index dd040d1..c49c544 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -13,9 +13,10 @@ import ( ) type CommonConfig struct { - Username string `mapstructure:"remote_username"` - Password string `mapstructure:"remote_password"` - HostIp string `mapstructure:"remote_host"` + Username string `mapstructure:"remote_username"` + Password string `mapstructure:"remote_password"` + HostIp string `mapstructure:"remote_host"` + HostSshPort uint `mapstructure:"remote_ssh_port"` VMName string `mapstructure:"vm_name"` VMDescription string `mapstructure:"vm_description"` @@ -64,6 +65,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig // Set default values + if c.HostSshPort == 0 { + c.HostSshPort = 22 + } + if c.HostPortMin == 0 { c.HostPortMin = 5900 } diff --git a/builder/xenserver/common/config.hcl2spec.go b/builder/xenserver/common/config.hcl2spec.go index 88a1494..80d8ce8 100644 --- a/builder/xenserver/common/config.hcl2spec.go +++ b/builder/xenserver/common/config.hcl2spec.go @@ -21,6 +21,7 @@ type FlatConfig struct { Username *string `mapstructure:"remote_username" cty:"remote_username" hcl:"remote_username"` Password *string `mapstructure:"remote_password" cty:"remote_password" hcl:"remote_password"` HostIp *string `mapstructure:"remote_host" cty:"remote_host" hcl:"remote_host"` + HostSshPort *uint `mapstructure:"remote_ssh_port" cty:"remote_ssh_port" hcl:"remote_ssh_port"` VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"` VMDescription *string `mapstructure:"vm_description" cty:"vm_description" hcl:"vm_description"` SrName *string `mapstructure:"sr_name" cty:"sr_name" hcl:"sr_name"` @@ -136,6 +137,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "remote_username": &hcldec.AttrSpec{Name: "remote_username", Type: cty.String, Required: false}, "remote_password": &hcldec.AttrSpec{Name: "remote_password", Type: cty.String, Required: false}, "remote_host": &hcldec.AttrSpec{Name: "remote_host", Type: cty.String, Required: false}, + "remote_ssh_port": &hcldec.AttrSpec{Name: "remote_ssh_port", Type: cty.Number, Required: false}, "vm_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false}, "vm_description": &hcldec.AttrSpec{Name: "vm_description", Type: cty.String, Required: false}, "sr_name": &hcldec.AttrSpec{Name: "sr_name", Type: cty.String, Required: false}, diff --git a/builder/xenserver/common/ssh.go b/builder/xenserver/common/ssh.go index 1f3d136..99493a0 100644 --- a/builder/xenserver/common/ssh.go +++ b/builder/xenserver/common/ssh.go @@ -17,7 +17,7 @@ import ( func SSHAddress(state multistep.StateBag) (string, error) { sshIP := state.Get("ssh_address").(string) - sshHostPort := 22 + sshHostPort := state.Get("ssh_port").(uint) return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil } diff --git a/builder/xenserver/common/step_set_vm_host_ssh_address.go b/builder/xenserver/common/step_set_vm_host_ssh_address.go index 33f7f5d..c43ee8e 100644 --- a/builder/xenserver/common/step_set_vm_host_ssh_address.go +++ b/builder/xenserver/common/step_set_vm_host_ssh_address.go @@ -13,6 +13,7 @@ type StepSetVmHostSshAddress struct{} func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { c := state.Get("client").(*Connection) + config := state.Get("config").(Config) ui := state.Get("ui").(packer.Ui) ui.Say("Step: Set SSH address to VM host IP") @@ -37,6 +38,9 @@ func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.St state.Put("ssh_address", address) ui.Say(fmt.Sprintf("Set host SSH address to '%s'.", address)) + state.Put("ssh_port", config.HostSshPort) + ui.Say(fmt.Sprintf("Set host SSH port to %d.", config.HostSshPort)) + return multistep.ActionContinue } diff --git a/builder/xenserver/iso/builder_test.go b/builder/xenserver/iso/builder_test.go index 65c7542..125b51c 100644 --- a/builder/xenserver/iso/builder_test.go +++ b/builder/xenserver/iso/builder_test.go @@ -61,6 +61,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) { if b.config.KeepVM != "never" { t.Errorf("bad keep instance: %s", b.config.KeepVM) } + + if b.config.HostSshPort != 22 { + t.Errorf("bad ssh port: %d", b.config.HostSshPort) + } } func TestBuilderPrepare_DiskSize(t *testing.T) { diff --git a/builder/xenserver/xva/builder_test.go b/builder/xenserver/xva/builder_test.go index 9ebe620..4b80e31 100644 --- a/builder/xenserver/xva/builder_test.go +++ b/builder/xenserver/xva/builder_test.go @@ -55,6 +55,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) { if b.config.KeepVM != "never" { t.Errorf("bad keep instance: %s", b.config.KeepVM) } + + if b.config.HostSshPort != 22 { + t.Errorf("bad ssh port: %d", b.config.HostSshPort) + } } func TestBuilderPrepare_Format(t *testing.T) { diff --git a/docs/builders/iso/xenserver-iso.html.markdown b/docs/builders/iso/xenserver-iso.html.markdown index bd7be5c..8272d78 100644 --- a/docs/builders/iso/xenserver-iso.html.markdown +++ b/docs/builders/iso/xenserver-iso.html.markdown @@ -58,6 +58,8 @@ each category, the available options are alphabetized and described. * `remote_host` (string) - The host of the Xenserver / XCP-ng pool primary. Typically, these will be specified through environment variables as seen in the [examples](../../../examples). +* `remote_ssh_port` (integer) - The port that SSH will be listening on in the Xenserver / XCP-ng pool primary. By default this is 22. + * `remote_username` (string) - The XenServer username used to access the remote machine. * `remote_password` (string) - The XenServer password for access to the remote machine.