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
|
|
|
|
2017-12-19 14:04:17 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"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
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
// StateData should store data such as GeneratedData
|
2020-01-16 06:04:03 -05:00
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
|
|
|
|
2013-06-18 19:24:35 -04:00
|
|
|
// EC2 connection for performing API stuff.
|
2017-12-19 14:04:17 -05:00
|
|
|
Session *session.Session
|
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)
|
2017-08-24 14:57:12 -04:00
|
|
|
return fmt.Sprintf("AMIs were created:\n%s\n", strings.Join(amiStrings, "\n"))
|
2013-05-22 01:28:41 -04:00
|
|
|
}
|
2013-06-18 19:01:14 -04:00
|
|
|
|
2014-01-19 13:32:44 -05:00
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2020-01-16 06:04:03 -05:00
|
|
|
if _, ok := a.StateData[name]; ok {
|
|
|
|
return a.StateData[name]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-12-19 14:04:17 -05:00
|
|
|
regionConn := ec2.New(a.Session, &aws.Config{
|
2019-03-04 19:22:52 -05:00
|
|
|
Region: aws.String(region),
|
2017-12-19 14:04:17 -05:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:17:10 -04:00
|
|
|
err = DestroyAMIs([]*string{&imageId}, regionConn)
|
|
|
|
if err != nil {
|
2013-06-18 19:24:35 -04:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|