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
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-10-30 14:58:56 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/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)
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
regionConfig := &aws.Config{
|
|
|
|
Credentials: a.Conn.Config.Credentials,
|
2015-07-28 20:10:21 -04:00
|
|
|
Region: aws.String(region),
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2016-11-01 18:53:04 -04:00
|
|
|
session, err := session.NewSession(regionConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
regionConn := ec2.New(session)
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2016-10-18 22:46:41 -04:00
|
|
|
// Get image metadata
|
|
|
|
imageResp, err := regionConn.DescribeImages(&ec2.DescribeImagesInput{
|
|
|
|
ImageIds: []*string{&imageId},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
if len(imageResp.Images) == 0 {
|
|
|
|
err := fmt.Errorf("Error retrieving details for AMI (%s), no images found", imageId)
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister ami
|
2015-04-05 17:58:48 -04:00
|
|
|
input := &ec2.DeregisterImageInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
ImageId: &imageId,
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
if _, err := regionConn.DeregisterImage(input); 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 {
|
2015-08-05 22:41:29 -04:00
|
|
|
return &packer.MultiError{Errors: errors}
|
2013-06-18 19:25:35 -04:00
|
|
|
}
|
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
|
|
|
|
}
|