From ec526d97aa244c5bac9b6432124209642c03d6f0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 30 Jul 2013 09:55:17 -0700 Subject: [PATCH] builder/amazon/chroot: more settings, validation --- builder/amazon/chroot/builder.go | 16 +++++++++++++++- builder/amazon/chroot/builder_test.go | 21 ++++++++++++++++++++- builder/amazon/chroot/step_attach_volume.go | 3 ++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 1711e58d1..ec344d938 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -24,7 +24,10 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"` - SourceAmi string `mapstructure:"source_ami"` + SourceAmi string `mapstructure:"source_ami"` + AttachedDevicePath string `mapstructure:"attached_device_path"` + DevicePath string `mapstructure:"device_path"` + MountPath string `mapstructure:"mount_path"` } type Builder struct { @@ -39,11 +42,22 @@ func (b *Builder) Prepare(raws ...interface{}) error { } // Defaults + if b.config.DevicePath == "" { + b.config.DevicePath = "/dev/sdh" + } + + if b.config.MountPath == "" { + b.config.MountPath = "/var/packer-amazon-chroot/volumes/{{.Device}}" + } // Accumulate any errors errs := common.CheckUnusedConfig(md) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...) + if b.config.SourceAmi == "" { + errs = packer.MultiErrorAppend(errs, errors.New("source_ami is required.")) + } + if errs != nil && len(errs.Errors) > 0 { return errs } diff --git a/builder/amazon/chroot/builder_test.go b/builder/amazon/chroot/builder_test.go index 32a110545..2736dbbd8 100644 --- a/builder/amazon/chroot/builder_test.go +++ b/builder/amazon/chroot/builder_test.go @@ -6,7 +6,9 @@ import ( ) func testConfig() map[string]interface{} { - return map[string]interface{}{} + return map[string]interface{}{ + "source_ami": "foo", + } } func TestBuilder_ImplementsBuilder(t *testing.T) { @@ -16,3 +18,20 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { t.Fatalf("Builder should be a builder") } } + +func TestBuilderPrepare_SourceAmi(t *testing.T) { + b := &Builder{} + config := testConfig() + + config["source_ami"] = "" + err := b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + config["source_ami"] = "foo" + err = b.Prepare(config) + if err != nil { + t.Errorf("err: %s", err) + } +} diff --git a/builder/amazon/chroot/step_attach_volume.go b/builder/amazon/chroot/step_attach_volume.go index b380420b7..cf5f89e3e 100644 --- a/builder/amazon/chroot/step_attach_volume.go +++ b/builder/amazon/chroot/step_attach_volume.go @@ -20,12 +20,13 @@ type StepAttachVolume struct { } func (s *StepAttachVolume) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*Config) ec2conn := state["ec2"].(*ec2.EC2) instance := state["instance"].(*ec2.Instance) ui := state["ui"].(packer.Ui) volumeId := state["volume_id"].(string) - device := "/dev/sdh" + device := config.DevicePath ui.Say("Attaching the root volume...") _, err := ec2conn.AttachVolume(volumeId, instance.InstanceId, device)