2018-01-29 19:50:53 -05:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepListImages struct{}
|
|
|
|
|
|
|
|
func (s *stepListImages) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
// get variables from state
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
client := state.Get("client").(*compute.ComputeClient)
|
|
|
|
ui.Say("Adding image to image list...")
|
|
|
|
|
|
|
|
imageListClient := client.ImageList()
|
|
|
|
getInput := compute.GetImageListInput{
|
|
|
|
Name: config.DestImageList,
|
|
|
|
}
|
|
|
|
imList, err := imageListClient.GetImageList(&getInput)
|
|
|
|
if err != nil {
|
|
|
|
// If the list didn't exist, create it.
|
2018-01-31 18:02:19 -05:00
|
|
|
ui.Say(fmt.Sprintf(err.Error()))
|
2018-01-31 13:47:19 -05:00
|
|
|
ui.Say(fmt.Sprintf("Destination image list %s does not exist; Creating it...",
|
|
|
|
config.DestImageList))
|
2018-01-29 19:50:53 -05:00
|
|
|
|
|
|
|
ilInput := compute.CreateImageListInput{
|
|
|
|
Name: config.DestImageList,
|
|
|
|
Description: "Packer-built image list",
|
|
|
|
}
|
|
|
|
|
|
|
|
imList, err = imageListClient.CreateImageList(&ilInput)
|
|
|
|
if err != nil {
|
2018-01-31 13:47:19 -05:00
|
|
|
err = fmt.Errorf("Problem creating image list: %s", err)
|
2018-01-29 19:50:53 -05:00
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Message(fmt.Sprintf("Image list %s created!", imList.URI))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create and image list entry for the image into that list.
|
|
|
|
snap := state.Get("snapshot").(*compute.Snapshot)
|
2018-01-31 13:47:19 -05:00
|
|
|
version := len(imList.Entries) + 1
|
2018-01-29 19:50:53 -05:00
|
|
|
entriesClient := client.ImageListEntries()
|
|
|
|
entriesInput := compute.CreateImageListEntryInput{
|
2018-01-31 18:22:09 -05:00
|
|
|
Name: config.DestImageList,
|
|
|
|
MachineImages: []string{fmt.Sprintf("/Compute-%s/%s/%s",
|
|
|
|
config.IdentityDomain, config.Username, snap.MachineImage)},
|
|
|
|
Version: version,
|
2018-01-29 19:50:53 -05:00
|
|
|
}
|
|
|
|
entryInfo, err := entriesClient.CreateImageListEntry(&entriesInput)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem creating an image list entry: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-01-31 13:47:19 -05:00
|
|
|
state.Put("image_list_entry", entryInfo)
|
2018-01-29 19:50:53 -05:00
|
|
|
ui.Message(fmt.Sprintf("created image list entry %s", entryInfo.Name))
|
2018-01-31 13:47:19 -05:00
|
|
|
|
|
|
|
machineImagesClient := client.MachineImages()
|
|
|
|
getImagesInput := compute.GetMachineImageInput{
|
|
|
|
Name: config.ImageName,
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:37:55 -05:00
|
|
|
// Update image list default to use latest version
|
|
|
|
updateInput := compute.UpdateImageListInput{
|
|
|
|
Default: version,
|
|
|
|
Description: config.DestImageListDescription,
|
|
|
|
Name: config.DestImageList,
|
|
|
|
}
|
|
|
|
_, err = imageListClient.UpdateImageList(&updateInput)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem updating default image list version: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:47:19 -05:00
|
|
|
// Grab info about the machine image to return with the artifact
|
|
|
|
imInfo, err := machineImagesClient.GetMachineImage(&getImagesInput)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem getting machine image info: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("machine_image_file", imInfo.File)
|
|
|
|
state.Put("machine_image_name", imInfo.Name)
|
|
|
|
state.Put("image_list_version", version)
|
|
|
|
|
2018-01-29 19:50:53 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepListImages) Cleanup(state multistep.StateBag) {
|
|
|
|
// Nothing to do
|
|
|
|
return
|
|
|
|
}
|