packer-cn/builder/oracle/classic/step_create_image.go

73 lines
2.1 KiB
Go
Raw Normal View History

2018-09-06 01:44:31 -04:00
package classic
import (
"context"
"fmt"
"log"
2018-09-06 01:44:31 -04:00
"github.com/hashicorp/go-oracle-terraform/compute"
2018-09-06 01:44:31 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
2018-09-12 19:16:08 -04:00
type stepCreateImage struct {
}
2018-09-06 01:44:31 -04:00
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2018-09-06 01:44:31 -04:00
ui := state.Get("ui").(packer.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
// Image uploaded, let's register it
machineImageClient := client.MachineImages()
createMI := &compute.CreateMachineImageInput{
// Two-part name of the account
Account: fmt.Sprintf("/Compute-%s/cloud_storage", config.IdentityDomain),
Description: "Packer generated Machine Image.",
// 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,
}
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)
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-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)
ui := state.Get("ui").(packer.Ui)
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))
}