2013-07-30 19:58:58 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-09-05 19:10:33 -04:00
|
|
|
"time"
|
|
|
|
|
2013-07-30 19:58:58 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepSnapshot creates a snapshot of the created volume.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// snapshot_id string - ID of the created snapshot
|
|
|
|
type StepSnapshot struct {
|
|
|
|
snapshotId string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
volumeId := state.Get("volume_id").(string)
|
2013-07-30 19:58:58 -04:00
|
|
|
|
|
|
|
ui.Say("Creating snapshot...")
|
2014-09-05 19:10:33 -04:00
|
|
|
createSnapResp, err := ec2conn.CreateSnapshot(
|
|
|
|
volumeId,
|
|
|
|
fmt.Sprintf("Packer: %s", time.Now().String()))
|
2013-07-30 19:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the snapshot ID so we can delete it later
|
|
|
|
s.snapshotId = createSnapResp.Id
|
|
|
|
ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId))
|
|
|
|
|
|
|
|
// Wait for the snapshot to be ready
|
|
|
|
stateChange := awscommon.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
StepState: state,
|
|
|
|
Target: "completed",
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
resp, err := ec2conn.Snapshots([]string{s.snapshotId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Snapshots) == 0 {
|
|
|
|
return nil, "", errors.New("No snapshots found.")
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:44:03 -04:00
|
|
|
s := resp.Snapshots[0]
|
|
|
|
return s, s.Status, nil
|
2013-07-30 19:58:58 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = awscommon.WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for snapshot: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("snapshot_id", s.snapshotId)
|
2013-07-30 19:58:58 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSnapshot) Cleanup(state multistep.StateBag) {
|
2013-07-30 19:58:58 -04:00
|
|
|
if s.snapshotId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2013-07-30 19:58:58 -04:00
|
|
|
|
|
|
|
if cancelled || halted {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Say("Removing snapshot since we cancelled or halted...")
|
|
|
|
_, err := ec2conn.DeleteSnapshots([]string{s.snapshotId})
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|