diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index 935a78305..4248b9e51 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -15,6 +15,7 @@ import ( "github.com/mitchellh/packer/builder/common" "github.com/mitchellh/packer/packer" "log" + "text/template" "time" ) @@ -98,6 +99,15 @@ func (b *Builder) Prepare(raws ...interface{}) error { errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) } + if b.config.AMIName == "" { + errs = append(errs, errors.New("ami_name must be specified")) + } else { + _, err = template.New("ami").Parse(b.config.AMIName) + if err != nil { + errs = append(errs, fmt.Errorf("Failed parsing ami_name: %s", err)) + } + } + if len(errs) > 0 { return &packer.MultiError{errs} } diff --git a/builder/amazonebs/builder_test.go b/builder/amazonebs/builder_test.go index eb0d72482..b54f8f13a 100644 --- a/builder/amazonebs/builder_test.go +++ b/builder/amazonebs/builder_test.go @@ -13,6 +13,7 @@ func testConfig() map[string]interface{} { "instance_type": "foo", "region": "us-east-1", "ssh_username": "root", + "ami_name": "foo", } } @@ -60,6 +61,34 @@ func TestBuilderPrepare_AccessKey(t *testing.T) { } } +func TestBuilderPrepare_AMIName(t *testing.T) { + var b Builder + config := testConfig() + + // Test good + config["ami_name"] = "foo" + err := b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test bad + config["ami_name"] = "foo {{" + b = Builder{} + err = b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + // Test bad + delete(config, "ami_name") + b = Builder{} + err = b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } +} + func TestBuilderPrepare_InstanceType(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/amazonebs/step_create_ami.go b/builder/amazonebs/step_create_ami.go index ae470cb61..f088e8fc6 100644 --- a/builder/amazonebs/step_create_ami.go +++ b/builder/amazonebs/step_create_ami.go @@ -29,11 +29,7 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction { strconv.FormatInt(time.Now().UTC().Unix(), 10), } - t, err := template.New("ami").Parse(config.AMIName) - if err != nil { - ui.Error(err.Error()) - return multistep.ActionHalt - } + t := template.Must(template.New("ami").Parse(config.AMIName)) t.Execute(amiNameBuf, tData) amiName := amiNameBuf.String()