From 28e72c7f7bc6e4c088b2cb46c4f98496bba7dc72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Aug 2013 14:54:37 -0700 Subject: [PATCH] builder/amazon/chroot: switch to new template stuff --- builder/amazon/chroot/builder.go | 56 ++++++++++++++++++---- builder/amazon/chroot/step_mount_device.go | 14 +++--- builder/amazon/chroot/step_register_ami.go | 20 +------- builder/amazon/ebs/builder.go | 3 +- 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 689d198c7..30f7bc7d6 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -14,7 +14,6 @@ import ( "github.com/mitchellh/packer/packer" "log" "runtime" - "text/template" ) // The unique ID for this builder @@ -34,6 +33,8 @@ type Config struct { MountPath string `mapstructure:"mount_path"` SourceAmi string `mapstructure:"source_ami"` UnmountCommand string `mapstructure:"unmount_command"` + + tpl *common.Template } type Builder struct { @@ -47,6 +48,11 @@ func (b *Builder) Prepare(raws ...interface{}) error { return err } + b.config.tpl, err = common.NewTemplate() + if err != nil { + return err + } + // Defaults if b.config.ChrootMounts == nil { b.config.ChrootMounts = make([][]string, 0) @@ -84,31 +90,63 @@ func (b *Builder) Prepare(raws ...interface{}) error { // Accumulate any errors errs := common.CheckUnusedConfig(md) - errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...) + errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(b.config.tpl)...) if b.config.AMIName == "" { errs = packer.MultiErrorAppend( errs, errors.New("ami_name must be specified")) - } else { - _, err = template.New("ami").Parse(b.config.AMIName) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Failed parsing ami_name: %s", err)) - } + } else if b.config.AMIName, err = b.config.tpl.Process(b.config.AMIName, nil); err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing ami_name: %s", err)) } - for _, mounts := range b.config.ChrootMounts { + for i, mounts := range b.config.ChrootMounts { if len(mounts) != 3 { errs = packer.MultiErrorAppend( errs, errors.New("Each chroot_mounts entry should be three elements.")) break } + + for j, entry := range mounts { + b.config.ChrootMounts[i][j], err = b.config.tpl.Process(entry, nil) + if err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("Error processing chroot_mounts[%d][%d]: %s", + i, j, err)) + } + } + } + + for i, file := range b.config.CopyFiles { + var err error + b.config.CopyFiles[i], err = b.config.tpl.Process(file, nil) + if err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("Error processing copy_files[%d]: %s", + i, err)) + } } if b.config.SourceAmi == "" { errs = packer.MultiErrorAppend(errs, errors.New("source_ami is required.")) } + templates := map[string]*string{ + "device_path": &b.config.DevicePath, + "mount_command": &b.config.MountCommand, + "source_ami": &b.config.SourceAmi, + "unmount_command": &b.config.UnmountCommand, + } + + for n, ptr := range templates { + var err error + *ptr, err = b.config.tpl.Process(*ptr, nil) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing %s: %s", n, err)) + } + } + if errs != nil && len(errs.Errors) > 0 { return errs } diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go index 8d6815427..1b87163aa 100644 --- a/builder/amazon/chroot/step_mount_device.go +++ b/builder/amazon/chroot/step_mount_device.go @@ -9,7 +9,6 @@ import ( "os" "os/exec" "path/filepath" - "text/template" ) type mountPathData struct { @@ -30,14 +29,17 @@ func (s *StepMountDevice) Run(state map[string]interface{}) multistep.StepAction ui := state["ui"].(packer.Ui) device := state["device"].(string) - mountPathRaw := new(bytes.Buffer) - t := template.Must(template.New("mountPath").Parse(config.MountPath)) - t.Execute(mountPathRaw, &mountPathData{ + mountPath, err := config.tpl.Process(config.MountPath, &mountPathData{ Device: filepath.Base(device), }) - var err error - mountPath := mountPathRaw.String() + if err != nil { + err := fmt.Errorf("Error preparing mount directory: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + mountPath, err = filepath.Abs(mountPath) if err != nil { err := fmt.Errorf("Error preparing mount directory: %s", err) diff --git a/builder/amazon/chroot/step_register_ami.go b/builder/amazon/chroot/step_register_ami.go index 1c08bda61..c93b14ff1 100644 --- a/builder/amazon/chroot/step_register_ami.go +++ b/builder/amazon/chroot/step_register_ami.go @@ -1,21 +1,13 @@ package chroot import ( - "bytes" "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" awscommon "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" - "strconv" - "text/template" - "time" ) -type amiNameData struct { - CreateTime string -} - // StepRegisterAMI creates the AMI. type StepRegisterAMI struct{} @@ -26,16 +18,6 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction snapshotId := state["snapshot_id"].(string) ui := state["ui"].(packer.Ui) - // Parse the name of the AMI - amiNameBuf := new(bytes.Buffer) - tData := amiNameData{ - strconv.FormatInt(time.Now().UTC().Unix(), 10), - } - - t := template.Must(template.New("ami").Parse(config.AMIName)) - t.Execute(amiNameBuf, tData) - amiName := amiNameBuf.String() - ui.Say("Registering the AMI...") blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices)) for i, device := range image.BlockDevices { @@ -48,7 +30,7 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction } registerOpts := &ec2.RegisterImage{ - Name: amiName, + Name: config.AMIName, Architecture: image.Architecture, KernelId: image.KernelId, RamdiskId: image.RamdiskId, diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 3d08050ca..ac0707a99 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -59,7 +59,8 @@ func (b *Builder) Prepare(raws ...interface{}) error { errs = packer.MultiErrorAppend( errs, errors.New("ami_name must be specified")) } else if b.config.AMIName, err = b.config.tpl.Process(b.config.AMIName, nil); err != nil { - errs = packer.MultiErrorAppend(errs, err) + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing ami_name: %s", err)) } newTags := make(map[string]string)