2013-07-20 23:08:41 -04:00
|
|
|
package common
|
2013-05-22 01:28:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-18 19:24:35 -04:00
|
|
|
"log"
|
2014-06-30 10:07:36 -04:00
|
|
|
"sort"
|
2013-05-22 01:28:41 -04:00
|
|
|
"strings"
|
2014-12-02 21:22:26 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-22 01:28:41 -04:00
|
|
|
)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-09-04 21:11:05 -04:00
|
|
|
sort.Strings(parts)
|
2013-06-26 20:40:21 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-09-04 21:11:05 -04:00
|
|
|
sort.Strings(amiStrings)
|
2013-05-22 01:28:41 -04:00
|
|
|
return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n"))
|
|
|
|
}
|
2013-06-18 19:01:14 -04:00
|
|
|
|
2014-01-19 13:32:44 -05:00
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2014-12-02 21:22:26 -05:00
|
|
|
switch name {
|
|
|
|
case "atlas.artifact.metadata":
|
|
|
|
return a.stateAtlasMetadata()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-19 13:32:44 -05: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
|
|
|
}
|
2014-12-02 21:22:26 -05:00
|
|
|
|
|
|
|
func (a *Artifact) stateAtlasMetadata() interface{} {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for region, imageId := range a.Amis {
|
|
|
|
k := fmt.Sprintf("region.%s", region)
|
|
|
|
metadata[k] = imageId
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|