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.
This commit is contained in:
Marko Mahnič 2023-08-03 11:22:39 +02:00 committed by Dom Del Nano
parent e460d5bd58
commit 30498d8258
7 changed files with 25 additions and 4 deletions

View File

@ -13,9 +13,10 @@ import (
) )
type CommonConfig struct { type CommonConfig struct {
Username string `mapstructure:"remote_username"` Username string `mapstructure:"remote_username"`
Password string `mapstructure:"remote_password"` Password string `mapstructure:"remote_password"`
HostIp string `mapstructure:"remote_host"` HostIp string `mapstructure:"remote_host"`
HostSshPort uint `mapstructure:"remote_ssh_port"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
VMDescription string `mapstructure:"vm_description"` VMDescription string `mapstructure:"vm_description"`
@ -64,6 +65,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
// Set default values // Set default values
if c.HostSshPort == 0 {
c.HostSshPort = 22
}
if c.HostPortMin == 0 { if c.HostPortMin == 0 {
c.HostPortMin = 5900 c.HostPortMin = 5900
} }

View File

@ -21,6 +21,7 @@ type FlatConfig struct {
Username *string `mapstructure:"remote_username" cty:"remote_username" hcl:"remote_username"` Username *string `mapstructure:"remote_username" cty:"remote_username" hcl:"remote_username"`
Password *string `mapstructure:"remote_password" cty:"remote_password" hcl:"remote_password"` Password *string `mapstructure:"remote_password" cty:"remote_password" hcl:"remote_password"`
HostIp *string `mapstructure:"remote_host" cty:"remote_host" hcl:"remote_host"` 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"` VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"`
VMDescription *string `mapstructure:"vm_description" cty:"vm_description" hcl:"vm_description"` VMDescription *string `mapstructure:"vm_description" cty:"vm_description" hcl:"vm_description"`
SrName *string `mapstructure:"sr_name" cty:"sr_name" hcl:"sr_name"` 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_username": &hcldec.AttrSpec{Name: "remote_username", Type: cty.String, Required: false},
"remote_password": &hcldec.AttrSpec{Name: "remote_password", 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_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_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false},
"vm_description": &hcldec.AttrSpec{Name: "vm_description", 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}, "sr_name": &hcldec.AttrSpec{Name: "sr_name", Type: cty.String, Required: false},

View File

@ -17,7 +17,7 @@ import (
func SSHAddress(state multistep.StateBag) (string, error) { func SSHAddress(state multistep.StateBag) (string, error) {
sshIP := state.Get("ssh_address").(string) sshIP := state.Get("ssh_address").(string)
sshHostPort := 22 sshHostPort := state.Get("ssh_port").(uint)
return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil return fmt.Sprintf("%s:%d", sshIP, sshHostPort), nil
} }

View File

@ -13,6 +13,7 @@ type StepSetVmHostSshAddress struct{}
func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*Connection) c := state.Get("client").(*Connection)
config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Set SSH address to VM host IP") 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) state.Put("ssh_address", address)
ui.Say(fmt.Sprintf("Set host SSH address to '%s'.", 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 return multistep.ActionContinue
} }

View File

@ -61,6 +61,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
if b.config.KeepVM != "never" { if b.config.KeepVM != "never" {
t.Errorf("bad keep instance: %s", b.config.KeepVM) 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) { func TestBuilderPrepare_DiskSize(t *testing.T) {

View File

@ -55,6 +55,10 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
if b.config.KeepVM != "never" { if b.config.KeepVM != "never" {
t.Errorf("bad keep instance: %s", b.config.KeepVM) 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) { func TestBuilderPrepare_Format(t *testing.T) {

View File

@ -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 * `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). 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_username` (string) - The XenServer username used to access the remote machine.
* `remote_password` (string) - The XenServer password for access to the remote machine. * `remote_password` (string) - The XenServer password for access to the remote machine.