github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/artifact.go (about)

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