builder/amazon/chroot: use set AMI name

This commit is contained in:
Mitchell Hashimoto 2013-07-30 22:33:41 -07:00
parent 743682d352
commit 818a10e797
3 changed files with 61 additions and 1 deletions

View File

@ -6,6 +6,7 @@ package chroot
import ( import (
"errors" "errors"
"fmt"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
awscommon "github.com/mitchellh/packer/builder/amazon/common" awscommon "github.com/mitchellh/packer/builder/amazon/common"
@ -13,6 +14,7 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"runtime" "runtime"
"text/template"
) )
// The unique ID for this builder // The unique ID for this builder
@ -24,6 +26,7 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
awscommon.AccessConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"`
AMIName string `mapstructure:"ami_name"`
ChrootMounts [][]string `mapstructure:"chroot_mounts"` ChrootMounts [][]string `mapstructure:"chroot_mounts"`
CopyFiles []string `mapstructure:"copy_files"` CopyFiles []string `mapstructure:"copy_files"`
DevicePath string `mapstructure:"device_path"` DevicePath string `mapstructure:"device_path"`
@ -83,6 +86,17 @@ func (b *Builder) Prepare(raws ...interface{}) error {
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)
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))
}
}
for _, mounts := range b.config.ChrootMounts { for _, mounts := range b.config.ChrootMounts {
if len(mounts) != 3 { if len(mounts) != 3 {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(

View File

@ -7,6 +7,7 @@ import (
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"ami_name": "foo",
"source_ami": "foo", "source_ami": "foo",
} }
} }
@ -19,6 +20,34 @@ func TestBuilder_ImplementsBuilder(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_ChrootMounts(t *testing.T) { func TestBuilderPrepare_ChrootMounts(t *testing.T) {
b := &Builder{} b := &Builder{}
config := testConfig() config := testConfig()

View File

@ -1,23 +1,40 @@
package chroot package chroot
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
awscommon "github.com/mitchellh/packer/builder/amazon/common" awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"strconv"
"text/template"
"time"
) )
type amiNameData struct {
CreateTime string
}
// StepRegisterAMI creates the AMI. // StepRegisterAMI creates the AMI.
type StepRegisterAMI struct{} type StepRegisterAMI struct{}
func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction { func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*Config)
ec2conn := state["ec2"].(*ec2.EC2) ec2conn := state["ec2"].(*ec2.EC2)
image := state["source_image"].(*ec2.Image) image := state["source_image"].(*ec2.Image)
snapshotId := state["snapshot_id"].(string) snapshotId := state["snapshot_id"].(string)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
amiName := "foo" // 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...") ui.Say("Registering the AMI...")
blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices)) blockDevices := make([]ec2.BlockDeviceMapping, len(image.BlockDevices))