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

73 lines
1.9 KiB
Go
Raw Normal View History

2018-01-16 19:55:39 -05:00
package classic
import (
2018-01-25 17:43:55 -05:00
"context"
2018-01-16 19:55:39 -05:00
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
2018-01-25 17:45:09 -05:00
"github.com/hashicorp/packer/helper/multistep"
2018-01-16 19:55:39 -05:00
"github.com/hashicorp/packer/packer"
)
type stepSnapshot struct {
cleanupSnap bool
}
2018-01-16 19:55:39 -05:00
func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2018-01-16 19:55:39 -05:00
// get variables from state
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating Snapshot...")
2018-01-17 19:25:38 -05:00
config := state.Get("config").(*Config)
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
2018-01-16 19:55:39 -05:00
instanceID := state.Get("instance_id").(string)
// get instances client
2018-01-17 16:07:41 -05:00
snapshotClient := client.Snapshots()
2018-01-16 19:55:39 -05:00
// Instances Input
2018-01-17 16:07:41 -05:00
snapshotInput := &compute.CreateSnapshotInput{
2018-01-19 19:00:12 -05:00
Instance: fmt.Sprintf("%s/%s", config.ImageName, instanceID),
2018-01-16 19:55:39 -05:00
MachineImage: config.ImageName,
Timeout: config.SnapshotTimeout,
2018-01-16 19:55:39 -05:00
}
2018-01-17 16:07:41 -05:00
snap, err := snapshotClient.CreateSnapshot(snapshotInput)
2018-01-16 19:55:39 -05:00
if err != nil {
err = fmt.Errorf("Problem creating snapshot: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
2018-01-23 21:13:51 -05:00
state.Put("snapshot", snap)
2018-10-20 01:51:58 -04:00
state.Put("machine_image", snap.MachineImage)
ui.Message(fmt.Sprintf("Created snapshot: %s.", snap.Name))
2018-01-17 16:07:41 -05:00
return multistep.ActionContinue
}
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
// Delete the snapshot
var snap *compute.Snapshot
if snapshot, ok := state.GetOk("snapshot"); ok {
snap = snapshot.(*compute.Snapshot)
} else {
return
}
ui := state.Get("ui").(packer.Ui)
2018-01-31 14:35:34 -05:00
ui.Say("Deleting Snapshot...")
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
snapClient := client.Snapshots()
snapInput := compute.DeleteSnapshotInput{
Snapshot: snap.Name,
MachineImage: snap.MachineImage,
}
2018-01-31 13:47:19 -05:00
err := snapClient.DeleteSnapshotResourceOnly(&snapInput)
if err != nil {
err = fmt.Errorf("Problem deleting snapshot: %s", err)
ui.Error(err.Error())
state.Put("error", err)
}
return
2018-01-16 19:55:39 -05:00
}