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

71 lines
1.8 KiB
Go
Raw Normal View History

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