2013-07-20 23:08:41 -04:00
|
|
|
package common
|
2013-05-22 01:28:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-04 20:48:00 -04:00
|
|
|
"github.com/mitchellh/goamz/aws"
|
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-07-20 23:08:41 -04:00
|
|
|
// Artifact is an artifact implementation that contains built AMIs.
|
|
|
|
type Artifact struct {
|
2013-05-22 01:28:41 -04:00
|
|
|
// A map of regions to AMI IDs.
|
2013-07-20 23:08:41 -04:00
|
|
|
Amis map[string]string
|
|
|
|
|
|
|
|
// BuilderId is the unique ID for the builder that created this AMI
|
|
|
|
BuilderIdValue string
|
2013-06-18 19:24:35 -04:00
|
|
|
|
|
|
|
// EC2 connection for performing API stuff.
|
2013-07-20 23:08:41 -04:00
|
|
|
Conn *ec2.EC2
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
2013-07-20 23:08:41 -04:00
|
|
|
func (a *Artifact) BuilderId() string {
|
|
|
|
return a.BuilderIdValue
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
2013-07-20 23:08:41 -04:00
|
|
|
func (*Artifact) Files() []string {
|
2013-05-22 01:28:41 -04:00
|
|
|
// We have no files
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-20 23:08:41 -04:00
|
|
|
func (a *Artifact) Id() string {
|
|
|
|
parts := make([]string, 0, len(a.Amis))
|
|
|
|
for region, amiId := range a.Amis {
|
2013-06-26 20:40:21 -04:00
|
|
|
parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(parts, ",")
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
|
|
|
|
2013-07-20 23:08:41 -04:00
|
|
|
func (a *Artifact) String() string {
|
|
|
|
amiStrings := make([]string, 0, len(a.Amis))
|
|
|
|
for region, id := range a.Amis {
|
2013-05-22 01:28:41 -04:00
|
|
|
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
|
|
|
|
2013-07-20 23:08:41 -04:00
|
|
|
func (a *Artifact) Destroy() error {
|
2013-06-18 19:24:35 -04:00
|
|
|
errors := make([]error, 0)
|
|
|
|
|
2013-09-04 20:48:00 -04:00
|
|
|
for region, imageId := range a.Amis {
|
|
|
|
log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region)
|
|
|
|
regionconn := ec2.New(a.Conn.Auth, aws.Regions[region])
|
|
|
|
if _, err := regionconn.DeregisterImage(imageId); err != nil {
|
2013-06-18 19:24:35 -04:00
|
|
|
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
|
|
|
}
|