2018-09-06 01:44:31 -04:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-07 19:18:47 -04:00
|
|
|
"fmt"
|
2018-10-18 00:23:22 -04:00
|
|
|
"log"
|
2018-09-06 01:44:31 -04:00
|
|
|
|
2018-10-18 00:23:22 -04:00
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
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"
|
2018-09-06 01:44:31 -04:00
|
|
|
)
|
|
|
|
|
2018-09-12 19:16:08 -04:00
|
|
|
type stepCreateImage struct {
|
|
|
|
}
|
2018-09-06 01:44:31 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-09-12 19:16:08 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2018-10-20 17:11:11 -04:00
|
|
|
imageFile := state.Get("image_file").(string)
|
2018-09-06 01:44:31 -04:00
|
|
|
|
2018-10-18 00:23:22 -04:00
|
|
|
// Image uploaded, let's register it
|
|
|
|
machineImageClient := client.MachineImages()
|
|
|
|
createMI := &compute.CreateMachineImageInput{
|
|
|
|
// Two-part name of the account
|
2018-10-18 00:46:43 -04:00
|
|
|
Account: fmt.Sprintf("/Compute-%s/cloud_storage", config.IdentityDomain),
|
2018-10-26 16:00:45 -04:00
|
|
|
Description: "Packer generated Machine Image.",
|
2018-10-18 00:23:22 -04:00
|
|
|
// The three-part name of the object
|
2018-10-20 17:11:11 -04:00
|
|
|
Name: config.ImageName,
|
2018-10-24 17:49:09 -04:00
|
|
|
// image_file.tar.gz, where image_file is the .tar.gz name of the
|
|
|
|
// machine image file that you have uploaded to Oracle Cloud
|
|
|
|
// Infrastructure Object Storage Classic.
|
2018-10-19 18:51:17 -04:00
|
|
|
File: imageFile,
|
2018-10-18 00:23:22 -04:00
|
|
|
}
|
|
|
|
mi, err := machineImageClient.CreateMachineImage(createMI)
|
|
|
|
if err != nil {
|
2018-10-24 17:49:09 -04:00
|
|
|
err = fmt.Errorf("Error creating machine image: %s", err)
|
2018-10-18 00:23:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-10-24 17:49:09 -04:00
|
|
|
log.Println("Registered machine image.")
|
2018-10-20 01:51:58 -04:00
|
|
|
state.Put("machine_image", mi.Name)
|
2018-10-18 00:23:22 -04:00
|
|
|
|
2018-09-06 01:44:31 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:11:11 -04:00
|
|
|
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-10-20 17:11:11 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-10-20 17:11:11 -04:00
|
|
|
ui.Say("Cleaning up Image...")
|
|
|
|
|
|
|
|
machineImageClient := client.MachineImages()
|
|
|
|
deleteMI := &compute.DeleteMachineImageInput{
|
|
|
|
Name: config.ImageName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := machineImageClient.DeleteMachineImage(deleteMI); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error cleaning up machine image: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Deleted Image: %s", config.ImageName))
|
|
|
|
}
|