From 89af447c8cfd34dcabaf760bf35ac54239bf1731 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Jun 2015 18:47:59 -0400 Subject: [PATCH] builder/qemu: convert to helper/comm --- builder/qemu/builder.go | 69 ++++++++++++++---------------------- builder/qemu/builder_test.go | 8 ++--- builder/qemu/ssh.go | 10 +++--- 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index cb0ac6c88..2a1a2fe1c 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -12,7 +12,7 @@ import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/common" - commonssh "github.com/mitchellh/packer/common/ssh" + "github.com/mitchellh/packer/helper/communicator" "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template/interpolate" @@ -78,6 +78,7 @@ type Builder struct { type Config struct { common.PackerConfig `mapstructure:",squash"` + Comm communicator.Config `mapstructure:",squash"` Accelerator string `mapstructure:"accelerator"` BootCommand []string `mapstructure:"boot_command"` @@ -103,25 +104,24 @@ type Config struct { ShutdownCommand string `mapstructure:"shutdown_command"` 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"` - SSHKeyPath string `mapstructure:"ssh_key_path"` VNCPortMin uint `mapstructure:"vnc_port_min"` VNCPortMax uint `mapstructure:"vnc_port_max"` VMName string `mapstructure:"vm_name"` + // These are deprecated, but we keep them around for BC + // TODO(@mitchellh): remove + SSHKeyPath string `mapstructure:"ssh_key_path"` + SSHWaitTimeout time.Duration `mapstructure:"ssh_wait_timeout"` + // TODO(mitchellh): deprecate RunOnce bool `mapstructure:"run_once"` RawBootWait string `mapstructure:"boot_wait"` RawSingleISOUrl string `mapstructure:"iso_url"` RawShutdownTimeout string `mapstructure:"shutdown_timeout"` - RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"` bootWait time.Duration `` shutdownTimeout time.Duration `` - sshWaitTimeout time.Duration `` ctx interpolate.Context } @@ -139,9 +139,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, err } - var errs *packer.MultiError - warnings := make([]string, 0) - if b.config.DiskSize == 0 { b.config.DiskSize = 40000 } @@ -190,10 +187,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.SSHHostPortMax = 4444 } - if b.config.SSHPort == 0 { - b.config.SSHPort = 22 - } - if b.config.VNCPortMin == 0 { b.config.VNCPortMin = 5900 } @@ -222,6 +215,21 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.DiskInterface = "virtio" } + // TODO: backwards compatibility, write fixer instead + if b.config.SSHKeyPath != "" { + b.config.Comm.SSHPrivateKey = b.config.SSHKeyPath + } + if b.config.SSHWaitTimeout != 0 { + b.config.Comm.SSHTimeout = b.config.SSHWaitTimeout + } + + var errs *packer.MultiError + warnings := make([]string, 0) + + if es := b.config.Comm.Prepare(&b.config.ctx); len(es) > 0 { + errs = packer.MultiErrorAppend(errs, es...) + } + if !(b.config.Format == "qcow2" || b.config.Format == "raw") { errs = packer.MultiErrorAppend( errs, errors.New("invalid format, only 'qcow2' or 'raw' are allowed")) @@ -314,42 +322,17 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.RawShutdownTimeout = "5m" } - if b.config.RawSSHWaitTimeout == "" { - b.config.RawSSHWaitTimeout = "20m" - } - b.config.shutdownTimeout, err = time.ParseDuration(b.config.RawShutdownTimeout) if err != nil { errs = packer.MultiErrorAppend( errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err)) } - if b.config.SSHKeyPath != "" { - if _, err := os.Stat(b.config.SSHKeyPath); err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) - } else if _, err := commonssh.FileSigner(b.config.SSHKeyPath); err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) - } - } - if b.config.SSHHostPortMin > b.config.SSHHostPortMax { errs = packer.MultiErrorAppend( errs, errors.New("ssh_host_port_min must be less than ssh_host_port_max")) } - if b.config.SSHUser == "" { - errs = packer.MultiErrorAppend( - errs, errors.New("An ssh_username must be specified.")) - } - - b.config.sshWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) - } - if b.config.VNCPortMin > b.config.VNCPortMax { errs = packer.MultiErrorAppend( errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max")) @@ -409,10 +392,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe steprun, &stepBootWait{}, &stepTypeBootCommand{}, - &common.StepConnectSSH{ - SSHAddress: sshAddress, - SSHConfig: sshConfig, - SSHWaitTimeout: b.config.sshWaitTimeout, + &communicator.StepConnect{ + Config: &b.config.Comm, + SSHAddress: sshAddress, + SSHConfig: sshConfig, }, new(common.StepProvision), new(stepShutdown), diff --git a/builder/qemu/builder_test.go b/builder/qemu/builder_test.go index e3415d514..84d1d40c3 100644 --- a/builder/qemu/builder_test.go +++ b/builder/qemu/builder_test.go @@ -79,8 +79,8 @@ func TestBuilderPrepare_Defaults(t *testing.T) { t.Errorf("bad max ssh host port: %d", b.config.SSHHostPortMax) } - if b.config.SSHPort != 22 { - t.Errorf("bad ssh port: %d", b.config.SSHPort) + if b.config.Comm.SSHPort != 22 { + t.Errorf("bad ssh port: %d", b.config.Comm.SSHPort) } if b.config.VMName != "packer-foo" { @@ -595,10 +595,6 @@ func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) { t.Fatalf("err: %s", err) } - if b.config.RawSSHWaitTimeout != "20m" { - t.Fatalf("bad value: %s", b.config.RawSSHWaitTimeout) - } - // Test with a bad value config["ssh_wait_timeout"] = "this is not good" b = Builder{} diff --git a/builder/qemu/ssh.go b/builder/qemu/ssh.go index 9724d7483..17dc36de9 100644 --- a/builder/qemu/ssh.go +++ b/builder/qemu/ssh.go @@ -18,13 +18,13 @@ func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) { config := state.Get("config").(*Config) auth := []gossh.AuthMethod{ - gossh.Password(config.SSHPassword), + gossh.Password(config.Comm.SSHPassword), gossh.KeyboardInteractive( - ssh.PasswordKeyboardInteractive(config.SSHPassword)), + ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)), } - if config.SSHKeyPath != "" { - signer, err := commonssh.FileSigner(config.SSHKeyPath) + if config.Comm.SSHPrivateKey != "" { + signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey) if err != nil { return nil, err } @@ -33,7 +33,7 @@ func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) { } return &gossh.ClientConfig{ - User: config.SSHUser, + User: config.Comm.SSHUsername, Auth: auth, }, nil }