builder/amazonebs: Artifact returns AMIs

This commit is contained in:
Mitchell Hashimoto 2013-05-21 22:28:41 -07:00
parent e9618b0d07
commit cf6d2218ea
4 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,35 @@
package amazonebs
import (
"fmt"
"strings"
)
type artifact struct{
// A map of regions to AMI IDs.
amis map[string]string
}
func (*artifact) BuilderId() string {
return BuilderId
}
func (*artifact) Files() []string {
// We have no files
return nil
}
func (*artifact) Id() string {
// TODO(mitchellh): Id
return "TODO"
}
func (a *artifact) String() string {
amiStrings := make([]string, 0, len(a.amis))
for region, id := range a.amis {
single := fmt.Sprintf("%s: %s", region, id)
amiStrings = append(amiStrings, single)
}
return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n"))
}

View File

@ -0,0 +1,31 @@
package amazonebs
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"testing"
)
func TestArtifact_Impl(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var actual packer.Artifact
assert.Implementor(&artifact{}, &actual, "should be an Artifact")
}
func TestArtifactString(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
expected := `AMIs were created:
east: foo
west: bar`
amis := make(map[string]string)
amis["east"] = "foo"
amis["west"] = "bar"
a := &artifact{amis}
result := a.String()
assert.Equal(result, expected, "should match output")
}

View File

@ -13,6 +13,9 @@ import (
"log" "log"
) )
// The unique ID for this builder
const BuilderId = "mitchellh.amazonebs"
type config struct { type config struct {
// Access information // Access information
AccessKey string `mapstructure:"access_key"` AccessKey string `mapstructure:"access_key"`
@ -45,7 +48,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return return
} }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
auth := aws.Auth{b.config.AccessKey, b.config.SecretKey} auth := aws.Auth{b.config.AccessKey, b.config.SecretKey}
region := aws.Regions[b.config.Region] region := aws.Regions[b.config.Region]
ec2conn := ec2.New(auth, region) ec2conn := ec2.New(auth, region)
@ -67,4 +70,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
// Run! // Run!
RunSteps(state, steps) RunSteps(state, steps)
// Build the artifact and return it
return &artifact{state["amis"].(map[string]string)}
} }

View File

@ -26,7 +26,11 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) StepAction {
return StepHalt return StepHalt
} }
// Set the AMI ID in the state
ui.Say("AMI: %s", createResp.ImageId) ui.Say("AMI: %s", createResp.ImageId)
amis := make(map[string]string)
amis[config.Region] = createResp.ImageId
state["amis"] = amis
// Wait for the image to become ready // Wait for the image to become ready
ui.Say("Waiting for AMI to become ready...") ui.Say("Waiting for AMI to become ready...")