Commit f55e2d2c authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/openstack: convert to helper/comm

parent 502076c9
...@@ -5,10 +5,11 @@ package openstack ...@@ -5,10 +5,11 @@ package openstack
import ( import (
"fmt" "fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common"
"log" "log"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate" "github.com/mitchellh/packer/template/interpolate"
...@@ -19,6 +20,7 @@ const BuilderId = "mitchellh.openstack" ...@@ -19,6 +20,7 @@ const BuilderId = "mitchellh.openstack"
type Config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
AccessConfig `mapstructure:",squash"` AccessConfig `mapstructure:",squash"`
ImageConfig `mapstructure:",squash"` ImageConfig `mapstructure:",squash"`
RunConfig `mapstructure:",squash"` RunConfig `mapstructure:",squash"`
...@@ -88,10 +90,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -88,10 +90,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
FloatingIpPool: b.config.FloatingIpPool, FloatingIpPool: b.config.FloatingIpPool,
FloatingIp: b.config.FloatingIp, FloatingIp: b.config.FloatingIp,
}, },
&common.StepConnectSSH{ &communicator.StepConnect{
SSHAddress: SSHAddress(computeClient, b.config.SSHInterface, b.config.SSHPort), Config: &b.config.RunConfig.Comm,
SSHConfig: SSHConfig(b.config.SSHUsername), SSHAddress: SSHAddress(
SSHWaitTimeout: b.config.SSHTimeout(), computeClient,
b.config.SSHInterface,
b.config.RunConfig.Comm.SSHPort),
SSHConfig: SSHConfig(b.config.RunConfig.Comm.SSHUsername),
}, },
&common.StepProvision{}, &common.StepProvision{},
&stepCreateImage{}, &stepCreateImage{},
......
...@@ -2,21 +2,19 @@ package openstack ...@@ -2,21 +2,19 @@ package openstack
import ( import (
"errors" "errors"
"fmt"
"time"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/template/interpolate" "github.com/mitchellh/packer/template/interpolate"
) )
// RunConfig contains configuration for running an instance from a source // RunConfig contains configuration for running an instance from a source
// image and details on how to access that launched image. // image and details on how to access that launched image.
type RunConfig struct { type RunConfig struct {
Comm communicator.Config `mapstructure:",squash"`
SSHInterface string `mapstructure:"ssh_interface"`
SourceImage string `mapstructure:"source_image"` SourceImage string `mapstructure:"source_image"`
Flavor string `mapstructure:"flavor"` Flavor string `mapstructure:"flavor"`
RawSSHTimeout string `mapstructure:"ssh_timeout"`
SSHUsername string `mapstructure:"ssh_username"`
SSHPort int `mapstructure:"ssh_port"`
SSHInterface string `mapstructure:"ssh_interface"`
AvailabilityZone string `mapstructure:"availability_zone"` AvailabilityZone string `mapstructure:"availability_zone"`
RackconnectWait bool `mapstructure:"rackconnect_wait"` RackconnectWait bool `mapstructure:"rackconnect_wait"`
FloatingIpPool string `mapstructure:"floating_ip_pool"` FloatingIpPool string `mapstructure:"floating_ip_pool"`
...@@ -27,23 +25,12 @@ type RunConfig struct { ...@@ -27,23 +25,12 @@ type RunConfig struct {
// Not really used, but here for BC // Not really used, but here for BC
OpenstackProvider string `mapstructure:"openstack_provider"` OpenstackProvider string `mapstructure:"openstack_provider"`
UseFloatingIp bool `mapstructure:"use_floating_ip"` UseFloatingIp bool `mapstructure:"use_floating_ip"`
// Unexported fields that are calculated from others
sshTimeout time.Duration
} }
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
// Defaults // Defaults
if c.SSHUsername == "" { if c.Comm.SSHUsername == "" {
c.SSHUsername = "root" c.Comm.SSHUsername = "root"
}
if c.SSHPort == 0 {
c.SSHPort = 22
}
if c.RawSSHTimeout == "" {
c.RawSSHTimeout = "5m"
} }
if c.UseFloatingIp && c.FloatingIpPool == "" { if c.UseFloatingIp && c.FloatingIpPool == "" {
...@@ -51,8 +38,7 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { ...@@ -51,8 +38,7 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
} }
// Validation // Validation
var err error errs := c.Comm.Prepare(ctx)
errs := make([]error, 0)
if c.SourceImage == "" { if c.SourceImage == "" {
errs = append(errs, errors.New("A source_image must be specified")) errs = append(errs, errors.New("A source_image must be specified"))
} }
...@@ -61,18 +47,5 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { ...@@ -61,18 +47,5 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
errs = append(errs, errors.New("A flavor must be specified")) errs = append(errs, errors.New("A flavor must be specified"))
} }
if c.SSHUsername == "" {
errs = append(errs, errors.New("An ssh_username must be specified"))
}
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
}
return errs return errs
} }
func (c *RunConfig) SSHTimeout() time.Duration {
return c.sshTimeout
}
...@@ -3,6 +3,8 @@ package openstack ...@@ -3,6 +3,8 @@ package openstack
import ( import (
"os" "os"
"testing" "testing"
"github.com/mitchellh/packer/helper/communicator"
) )
func init() { func init() {
...@@ -17,7 +19,10 @@ func testRunConfig() *RunConfig { ...@@ -17,7 +19,10 @@ func testRunConfig() *RunConfig {
return &RunConfig{ return &RunConfig{
SourceImage: "abcd", SourceImage: "abcd",
Flavor: "m1.small", Flavor: "m1.small",
SSHUsername: "root",
Comm: communicator.Config{
SSHUsername: "foo",
},
} }
} }
...@@ -47,41 +52,28 @@ func TestRunConfigPrepare_SourceImage(t *testing.T) { ...@@ -47,41 +52,28 @@ func TestRunConfigPrepare_SourceImage(t *testing.T) {
func TestRunConfigPrepare_SSHPort(t *testing.T) { func TestRunConfigPrepare_SSHPort(t *testing.T) {
c := testRunConfig() c := testRunConfig()
c.SSHPort = 0 c.Comm.SSHPort = 0
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.SSHPort != 22 {
t.Fatalf("invalid value: %d", c.SSHPort)
}
c.SSHPort = 44
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if c.SSHPort != 44 { if c.Comm.SSHPort != 22 {
t.Fatalf("invalid value: %d", c.SSHPort) t.Fatalf("invalid value: %d", c.Comm.SSHPort)
} }
}
func TestRunConfigPrepare_SSHTimeout(t *testing.T) { c.Comm.SSHPort = 44
c := testRunConfig()
c.RawSSHTimeout = ""
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
c.RawSSHTimeout = "bad" if c.Comm.SSHPort != 44 {
if err := c.Prepare(nil); len(err) != 1 { t.Fatalf("invalid value: %d", c.Comm.SSHPort)
t.Fatalf("err: %s", err)
} }
} }
func TestRunConfigPrepare_SSHUsername(t *testing.T) { func TestRunConfigPrepare_SSHUsername(t *testing.T) {
c := testRunConfig() c := testRunConfig()
c.SSHUsername = "" c.Comm.SSHUsername = ""
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment