Use more generic communicator configuration

This commit is contained in:
flx5 2021-09-28 10:39:15 +02:00
parent 35efd42e55
commit ade8d0d819
10 changed files with 112 additions and 120 deletions

View File

@ -0,0 +1,93 @@
package common
import (
"errors"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
// Based upon https://github.com/hashicorp/packer-plugin-qemu/blob/a2121bb95d84288a1df5d7fbce94985a7cdfb793/builder/qemu/comm_config.go
type CommConfig struct {
Comm communicator.Config `mapstructure:",squash"`
// The minimum port to use for the Communicator port on the host machine which is forwarded
// to the SSH or WinRM port on the guest machine. By default this is 2222.
HostPortMin int `mapstructure:"host_port_min" required:"false"`
// The maximum port to use for the Communicator port on the host machine which is forwarded
// to the SSH or WinRM port on the guest machine. Because Packer often runs in parallel,
// Packer will choose a randomly available port in this range to use as the
// host port. By default this is 4444.
HostPortMax int `mapstructure:"host_port_max" required:"false"`
// Defaults to false. When enabled, Packer
// does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port
// on the host to communicate to the virtual machine.
SkipNatMapping bool `mapstructure:"skip_nat_mapping" required:"false"`
// These are deprecated, but we keep them around for backwards compatibility
// TODO: remove later
sshHostPortMin int `mapstructure:"ssh_host_port_min" required:"false"`
// TODO: remove later
sshHostPortMax int `mapstructure:"ssh_host_port_max"`
// TODO: remove later
sshKeyPath string `mapstructure:"ssh_key_path"`
// TODO: remove later
sshSkipNatMapping bool `mapstructure:"ssh_skip_nat_mapping"`
}
func (c *CommConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) {
// Backwards compatibility
if c.sshHostPortMin != 0 {
warnings = append(warnings, "ssh_host_port_min is deprecated and is being replaced by host_port_min. "+
"Please, update your template to use host_port_min. In future versions of Packer, inclusion of ssh_host_port_min will error your builds.")
c.HostPortMin = c.sshHostPortMin
}
// Backwards compatibility
if c.sshHostPortMax != 0 {
warnings = append(warnings, "ssh_host_port_max is deprecated and is being replaced by host_port_max. "+
"Please, update your template to use host_port_max. In future versions of Packer, inclusion of ssh_host_port_max will error your builds.")
c.HostPortMax = c.sshHostPortMax
}
// Backwards compatibility
if c.sshKeyPath != "" {
warnings = append(warnings, "ssh_key_path is deprecated and is being replaced by ssh_private_key_file. "+
"Please, update your template to use ssh_private_key_file. In future versions of Packer, inclusion of ssh_key_path will error your builds.")
c.Comm.SSHPrivateKeyFile = c.sshKeyPath
}
// Backwards compatibility
if c.sshSkipNatMapping {
warnings = append(warnings, "ssh_skip_nat_mapping is deprecated and is being replaced by skip_nat_mapping. "+
"Please, update your template to use skip_nat_mapping. In future versions of Packer, inclusion of ssh_skip_nat_mapping will error your builds.")
c.SkipNatMapping = c.sshSkipNatMapping
}
if c.Comm.SSHHost == "" && c.SkipNatMapping {
c.Comm.SSHHost = "127.0.0.1"
c.Comm.WinRMHost = "127.0.0.1"
}
if c.HostPortMin == 0 {
c.HostPortMin = 2222
}
if c.HostPortMax == 0 {
c.HostPortMax = 4444
}
errs = c.Comm.Prepare(ctx)
if c.HostPortMin > c.HostPortMax {
errs = append(errs,
errors.New("host_port_min must be less than host_port_max"))
}
if c.HostPortMin < 0 {
errs = append(errs, errors.New("host_port_min must be positive"))
}
return
}

View File

@ -3,7 +3,6 @@ package common
import (
"errors"
"fmt"
"os"
"time"
"github.com/hashicorp/packer-plugin-sdk/bootcommand"
@ -30,9 +29,6 @@ type CommonConfig struct {
NetworkNames []string `mapstructure:"network_names"`
ExportNetworkNames []string `mapstructure:"export_network_names"`
HostPortMin uint `mapstructure:"host_port_min"`
HostPortMax uint `mapstructure:"host_port_max"`
ShutdownCommand string `mapstructure:"shutdown_command"`
RawBootWait string `mapstructure:"boot_wait"`
@ -40,15 +36,7 @@ type CommonConfig struct {
ToolsIsoName string `mapstructure:"tools_iso_name"`
// SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
// SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHPassword string `mapstructure:"ssh_password"`
SSHPort uint `mapstructure:"ssh_port"`
SSHUser string `mapstructure:"ssh_username"`
SSHConfig `mapstructure:",squash"`
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
SSHWaitTimeout time.Duration
CommConfig `mapstructure:",squash"`
OutputDir string `mapstructure:"output_directory"`
Format string `mapstructure:"format"`
@ -86,32 +74,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
c.HTTPPortMax = 9000
}
if c.RawSSHWaitTimeout == "" {
c.RawSSHWaitTimeout = "200m"
}
if c.FloppyFiles == nil {
c.FloppyFiles = make([]string, 0)
}
/*
if c.SSHHostPortMin == 0 {
c.SSHHostPortMin = 2222
}
if c.SSHHostPortMax == 0 {
c.SSHHostPortMax = 4444
}
*/
if c.SSHPort == 0 {
c.SSHPort = 22
}
if c.RawSSHWaitTimeout == "" {
c.RawSSHWaitTimeout = "20m"
}
if c.OutputDir == "" {
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
}
@ -159,30 +125,6 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
errs = append(errs, fmt.Errorf("Failed to parse boot_wait: %s", err))
}
if c.SSHKeyPath != "" {
if _, err := os.Stat(c.SSHKeyPath); err != nil {
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
} else if _, err := FileSigner(c.SSHKeyPath); err != nil {
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
}
}
/*
if c.SSHHostPortMin > c.SSHHostPortMax {
errs = append(errs,
errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
}
*/
if c.SSHUser == "" {
errs = append(errs, errors.New("An ssh_username must be specified."))
}
c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout)
if err != nil {
errs = append(errs, fmt.Errorf("Failed to parse ssh_wait_timeout: %s", err))
}
switch c.Format {
case "xva", "xva_compressed", "vdi_raw", "vdi_vhd", "none":
default:

View File

@ -5,14 +5,12 @@ import (
"time"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
CommonConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`
VCPUsMax uint `mapstructure:"vcpus_max"`
VCPUsAtStartup uint `mapstructure:"vcpus_atstartup"`

View File

@ -8,7 +8,7 @@ import (
// FindPort finds and starts listening on a port in the range [portMin, portMax]
// returns the listener and the port number on success, or nil, 0 on failure
func FindPort(portMin uint, portMax uint) (net.Listener, uint) {
func FindPort(portMin int, portMax int) (net.Listener, int) {
log.Printf("Looking for an available port between %d and %d", portMin, portMax)
for port := portMin; port <= portMax; port++ {

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/pem"
"fmt"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"io"
"io/ioutil"
"log"
@ -39,15 +40,14 @@ func CommHost(state multistep.StateBag) (string, error) {
return "127.0.0.1", nil
}
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
func SSHConfigFunc(commConfig communicator.Config) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("commonconfig").(CommonConfig)
auth := []gossh.AuthMethod{
gossh.Password(config.SSHPassword),
gossh.Password(commConfig.SSHPassword),
}
if config.SSHKeyPath != "" {
signer, err := FileSigner(config.SSHKeyPath)
if commConfig.SSHPrivateKeyFile != "" {
signer, err := FileSigner(commConfig.SSHPrivateKeyFile)
if err != nil {
return nil, err
}
@ -56,7 +56,7 @@ func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConf
}
return &gossh.ClientConfig{
User: config.SSHUser,
User: commConfig.SSHUsername,
Auth: auth,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
@ -106,7 +106,7 @@ func ExecuteGuestSSHCmd(state multistep.StateBag, cmd string) (stdout string, er
if err != nil {
return
}
sshConfig, err := SSHConfigFunc(config.SSHConfig)(state)
sshConfig, err := SSHConfigFunc(config.Comm)(state)
if err != nil {
return
}

View File

@ -1,42 +0,0 @@
package common
import (
"errors"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type SSHConfig struct {
Comm communicator.Config `mapstructure:",squash"`
SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHSkipNatMapping bool `mapstructure:"ssh_skip_nat_mapping"`
// These are deprecated, but we keep them around for BC
// TODO(@mitchellh): remove
SSHKeyPath string `mapstructure:"ssh_key_path"`
}
func (c *SSHConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) {
if c.SSHHostPortMin == 0 {
c.SSHHostPortMin = 2222
}
if c.SSHHostPortMax == 0 {
c.SSHHostPortMax = 4444
}
// TODO: backwards compatibility, write fixer instead
if c.SSHKeyPath != "" {
c.Comm.SSHPrivateKeyFile = c.SSHKeyPath
}
errs = c.Comm.Prepare(ctx)
if c.SSHHostPortMin > c.SSHHostPortMax {
errs = append(errs,
errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
}
return
}

View File

@ -12,8 +12,8 @@ type StepForwardPortOverSSH struct {
RemotePort func(state multistep.StateBag) (int, error)
RemoteDest func(state multistep.StateBag) (string, error)
HostPortMin uint
HostPortMax uint
HostPortMin int
HostPortMax int
ResultKey string
}

View File

@ -10,6 +10,7 @@ import (
gossh "golang.org/x/crypto/ssh"
)
// TODO Seems to be unused
type StepStartOnHIMN struct{}
/*
@ -131,7 +132,7 @@ func HimnSSHIP(state multistep.StateBag) (string, error) {
return ip, nil
}
func HimnSSHPort(state multistep.StateBag) (uint, error) {
func HimnSSHPort(state multistep.StateBag) (int, error) {
config := state.Get("commonconfig").(CommonConfig)
return config.SSHPort, nil
return config.Comm.SSHPort, nil
}

View File

@ -45,7 +45,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
errs = packer.MultiErrorAppend(
errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(self.config.GetInterpContext())...)
errs = packer.MultiErrorAppend(errs, self.config.Comm.Prepare(self.config.GetInterpContext())...)
// Set default values
@ -76,7 +76,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
if self.config.CloneTemplate == "" {
self.config.CloneTemplate = "Other install media"
}
if self.config.Firmware == "" {
self.config.Firmware = "bios"
}
@ -275,7 +275,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
ResultKey: "local_ssh_port",
},
&communicator.StepConnect{
Config: &self.config.SSHConfig.Comm,
Config: &self.config.Comm,
Host: xscommon.InstanceSSHIP,
SSHConfig: self.config.Comm.SSHConfigFunc(),
SSHPort: xscommon.InstanceSSHPort,

View File

@ -155,9 +155,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this
},
&communicator.StepConnect{
Config: &self.config.SSHConfig.Comm,
Config: &self.config.Comm,
Host: xscommon.CommHost,
SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.SSHConfig),
SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.Comm),
SSHPort: xscommon.SSHPort,
},
new(commonsteps.StepProvision),