2019-02-05 15:43:46 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-09-30 11:37:09 -04:00
|
|
|
"context"
|
2019-02-05 15:43:46 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-02-05 15:43:46 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-09-30 11:37:09 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-05 15:43:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Artifact is an artifact implementation that contains built OMIs.
|
|
|
|
type Artifact struct {
|
|
|
|
// A map of regions to OMI IDs.
|
2019-02-07 11:33:48 -05:00
|
|
|
Omis map[string]string
|
2019-02-05 15:43:46 -05:00
|
|
|
|
|
|
|
// BuilderId is the unique ID for the builder that created this OMI
|
|
|
|
BuilderIdValue string
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
// StateData should store data such as GeneratedData
|
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
2019-02-05 15:43:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) BuilderId() string {
|
|
|
|
return a.BuilderIdValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Artifact) Files() []string {
|
|
|
|
// We have no files
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Id() string {
|
2019-02-07 11:33:48 -05:00
|
|
|
parts := make([]string, 0, len(a.Omis))
|
|
|
|
for region, amiId := range a.Omis {
|
2019-02-05 15:43:46 -05:00
|
|
|
parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(parts)
|
|
|
|
return strings.Join(parts, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) String() string {
|
2019-02-07 11:33:48 -05:00
|
|
|
amiStrings := make([]string, 0, len(a.Omis))
|
|
|
|
for region, id := range a.Omis {
|
2019-02-05 15:43:46 -05:00
|
|
|
single := fmt.Sprintf("%s: %s", region, id)
|
|
|
|
amiStrings = append(amiStrings, single)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(amiStrings)
|
|
|
|
return fmt.Sprintf("OMIs were created:\n%s\n", strings.Join(amiStrings, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2020-01-30 05:27:58 -05:00
|
|
|
if _, ok := a.StateData[name]; ok {
|
|
|
|
return a.StateData[name]
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:43:46 -05:00
|
|
|
switch name {
|
|
|
|
case "atlas.artifact.metadata":
|
|
|
|
return a.stateAtlasMetadata()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Destroy() error {
|
|
|
|
errors := make([]error, 0)
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
config := a.State("accessConfig").(*AccessConfig)
|
|
|
|
|
2019-02-07 11:33:48 -05:00
|
|
|
for region, imageId := range a.Omis {
|
2019-02-05 15:43:46 -05:00
|
|
|
log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region)
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
regionConn := config.NewOSCClientByRegion(region)
|
2019-02-05 15:43:46 -05:00
|
|
|
|
|
|
|
// Get image metadata
|
2020-09-30 11:37:09 -04:00
|
|
|
imageResp, _, err := regionConn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
|
|
|
|
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
|
|
|
|
Filters: osc.FiltersImage{
|
|
|
|
ImageIds: []string{imageId},
|
|
|
|
},
|
|
|
|
}),
|
2019-02-05 15:43:46 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2020-09-30 11:37:09 -04:00
|
|
|
if len(imageResp.Images) == 0 {
|
2019-02-05 15:43:46 -05:00
|
|
|
err := fmt.Errorf("Error retrieving details for OMI (%s), no images found", imageId)
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister ami
|
2020-09-30 11:37:09 -04:00
|
|
|
input := osc.DeleteImageRequest{
|
2019-02-05 15:43:46 -05:00
|
|
|
ImageId: imageId,
|
|
|
|
}
|
2020-09-30 11:37:09 -04:00
|
|
|
if _, _, err := regionConn.ImageApi.DeleteImage(context.Background(), &osc.DeleteImageOpts{
|
|
|
|
DeleteImageRequest: optional.NewInterface(input),
|
|
|
|
}); err != nil {
|
2019-02-05 15:43:46 -05:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Delete the snapshots associated with an OMI too
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
if len(errors) == 1 {
|
|
|
|
return errors[0]
|
|
|
|
} else {
|
|
|
|
return &packer.MultiError{Errors: errors}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) stateAtlasMetadata() interface{} {
|
|
|
|
metadata := make(map[string]string)
|
2019-02-07 11:33:48 -05:00
|
|
|
for region, imageId := range a.Omis {
|
2019-02-05 15:43:46 -05:00
|
|
|
k := fmt.Sprintf("region.%s", region)
|
|
|
|
metadata[k] = imageId
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|