github.com/jerryclinesmith/packer@v0.3.7/builder/amazon/common/artifact.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/goamz/aws"
     6  	"github.com/mitchellh/goamz/ec2"
     7  	"github.com/mitchellh/packer/packer"
     8  	"log"
     9  	"strings"
    10  )
    11  
    12  // Artifact is an artifact implementation that contains built AMIs.
    13  type Artifact struct {
    14  	// A map of regions to AMI IDs.
    15  	Amis map[string]string
    16  
    17  	// BuilderId is the unique ID for the builder that created this AMI
    18  	BuilderIdValue string
    19  
    20  	// EC2 connection for performing API stuff.
    21  	Conn *ec2.EC2
    22  }
    23  
    24  func (a *Artifact) BuilderId() string {
    25  	return a.BuilderIdValue
    26  }
    27  
    28  func (*Artifact) Files() []string {
    29  	// We have no files
    30  	return nil
    31  }
    32  
    33  func (a *Artifact) Id() string {
    34  	parts := make([]string, 0, len(a.Amis))
    35  	for region, amiId := range a.Amis {
    36  		parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
    37  	}
    38  
    39  	return strings.Join(parts, ",")
    40  }
    41  
    42  func (a *Artifact) String() string {
    43  	amiStrings := make([]string, 0, len(a.Amis))
    44  	for region, id := range a.Amis {
    45  		single := fmt.Sprintf("%s: %s", region, id)
    46  		amiStrings = append(amiStrings, single)
    47  	}
    48  
    49  	return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n"))
    50  }
    51  
    52  func (a *Artifact) Destroy() error {
    53  	errors := make([]error, 0)
    54  
    55  	for region, imageId := range a.Amis {
    56  		log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region)
    57  		regionconn := ec2.New(a.Conn.Auth, aws.Regions[region])
    58  		if _, err := regionconn.DeregisterImage(imageId); err != nil {
    59  			errors = append(errors, err)
    60  		}
    61  
    62  		// TODO(mitchellh): Delete the snapshots associated with an AMI too
    63  	}
    64  
    65  	if len(errors) > 0 {
    66  		if len(errors) == 1 {
    67  			return errors[0]
    68  		} else {
    69  			return &packer.MultiError{errors}
    70  		}
    71  	}
    72  
    73  	return nil
    74  }