2013-07-15 02:02:18 -04:00
|
|
|
package ebs
|
2013-05-22 01:28:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-18 19:24:35 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
2013-05-22 01:28:41 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-05-22 17:36:33 -04:00
|
|
|
type artifact struct {
|
2013-05-22 01:28:41 -04:00
|
|
|
// A map of regions to AMI IDs.
|
|
|
|
amis map[string]string
|
2013-06-18 19:24:35 -04:00
|
|
|
|
|
|
|
// EC2 connection for performing API stuff.
|
|
|
|
conn *ec2.EC2
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifact) BuilderId() string {
|
|
|
|
return BuilderId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifact) Files() []string {
|
|
|
|
// We have no files
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-26 20:40:21 -04:00
|
|
|
func (a *artifact) Id() string {
|
|
|
|
parts := make([]string, 0, len(a.amis))
|
|
|
|
for region, amiId := range a.amis {
|
|
|
|
parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(parts, ",")
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
2013-06-18 19:01:14 -04:00
|
|
|
|
|
|
|
func (a *artifact) Destroy() error {
|
2013-06-18 19:24:35 -04:00
|
|
|
errors := make([]error, 0)
|
|
|
|
|
|
|
|
for _, imageId := range a.amis {
|
2013-06-28 12:33:42 -04:00
|
|
|
log.Printf("Deregistering image ID: %s", imageId)
|
2013-06-18 19:24:35 -04:00
|
|
|
if _, err := a.conn.DeregisterImage(imageId); err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mitchellh): Delete the snapshots associated with an AMI too
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
2013-06-18 19:25:35 -04:00
|
|
|
if len(errors) == 1 {
|
|
|
|
return errors[0]
|
|
|
|
} else {
|
|
|
|
return &packer.MultiError{errors}
|
|
|
|
}
|
2013-06-18 19:24:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-06-18 19:01:14 -04:00
|
|
|
}
|