2019-01-30 19:33:56 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
"github.com/antihax/optional"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-08-17 17:04:19 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-01-30 19:33:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepSourceOMIInfo extracts critical information from the source OMI
|
|
|
|
// that is used throughout the OMI creation process.
|
|
|
|
//
|
|
|
|
// Produces:
|
2020-09-30 11:37:09 -04:00
|
|
|
// source_image *osc.Image - the source OMI info
|
2019-01-30 19:33:56 -05:00
|
|
|
type StepSourceOMIInfo struct {
|
2019-04-05 18:06:17 -04:00
|
|
|
SourceOmi string
|
|
|
|
OMIVirtType string
|
|
|
|
OmiFilters OmiFilterOptions
|
2019-01-30 19:33:56 -05:00
|
|
|
}
|
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
type imageOscSort []osc.Image
|
2019-01-30 19:33:56 -05:00
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
func (a imageOscSort) Len() int { return len(a) }
|
|
|
|
func (a imageOscSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a imageOscSort) Less(i, j int) bool {
|
|
|
|
itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
|
|
|
|
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
|
|
|
|
return itime.Unix() < jtime.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the most recent OMI out of a slice of images.
|
|
|
|
func mostRecentOscOmi(images []osc.Image) osc.Image {
|
|
|
|
sortedImages := images
|
|
|
|
sort.Sort(imageOscSort(sortedImages))
|
|
|
|
return sortedImages[len(sortedImages)-1]
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:33:56 -05:00
|
|
|
func (s *StepSourceOMIInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-08-17 17:04:19 -04:00
|
|
|
oscconn := state.Get("osc").(*osc.APIClient)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-30 19:33:56 -05:00
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
params := osc.ReadImagesRequest{
|
|
|
|
Filters: osc.FiltersImage{},
|
2019-01-30 19:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.SourceOmi != "" {
|
|
|
|
params.Filters.ImageIds = []string{s.SourceOmi}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have filters to apply
|
|
|
|
if len(s.OmiFilters.Filters) > 0 {
|
2020-08-17 17:04:19 -04:00
|
|
|
params.Filters = buildOSCOMIFilters(s.OmiFilters.Filters)
|
2019-01-30 19:33:56 -05:00
|
|
|
}
|
|
|
|
//TODO:Check if AccountIds correspond to Owners.
|
|
|
|
if len(s.OmiFilters.Owners) > 0 {
|
|
|
|
params.Filters.AccountIds = s.OmiFilters.Owners
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:43:16 -05:00
|
|
|
log.Printf("Using OMI Filters %#v", params)
|
2020-08-17 17:04:19 -04:00
|
|
|
imageResp, _, err := oscconn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
|
|
|
|
ReadImagesRequest: optional.NewInterface(params),
|
|
|
|
})
|
2019-01-30 19:33:56 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying OMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
if len(imageResp.Images) == 0 {
|
2019-02-11 17:43:16 -05:00
|
|
|
err := fmt.Errorf("No OMI was found matching filters: %#v", params)
|
2019-01-30 19:33:56 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
if len(imageResp.Images) > 1 && !s.OmiFilters.MostRecent {
|
|
|
|
err := fmt.Errorf("your query returned more than one result. Please try a more specific search, or set most_recent to true")
|
2019-01-30 19:33:56 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-17 17:04:19 -04:00
|
|
|
var image osc.Image
|
2019-01-30 19:33:56 -05:00
|
|
|
if s.OmiFilters.MostRecent {
|
2020-08-17 17:04:19 -04:00
|
|
|
image = mostRecentOscOmi(imageResp.Images)
|
2019-01-30 19:33:56 -05:00
|
|
|
} else {
|
2020-08-17 17:04:19 -04:00
|
|
|
image = imageResp.Images[0]
|
2019-01-30 19:33:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found Image ID: %s", image.ImageId))
|
|
|
|
|
|
|
|
state.Put("source_image", image)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSourceOMIInfo) Cleanup(multistep.StateBag) {}
|