github.phpd.cn/hashicorp/packer@v1.3.2/builder/oneandone/step_take_snapshot.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/1and1/oneandone-cloudserver-sdk-go"
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  type stepTakeSnapshot struct{}
    12  
    13  func (s *stepTakeSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    14  	ui := state.Get("ui").(packer.Ui)
    15  	c := state.Get("config").(*Config)
    16  
    17  	ui.Say("Creating Snapshot...")
    18  
    19  	token := oneandone.SetToken(c.Token)
    20  	api := oneandone.New(token, c.Url)
    21  
    22  	serverId := state.Get("server_id").(string)
    23  
    24  	req := oneandone.ImageConfig{
    25  		Name:        c.SnapshotName,
    26  		Description: "Packer image",
    27  		ServerId:    serverId,
    28  		Frequency:   "WEEKLY",
    29  		NumImages:   1,
    30  	}
    31  
    32  	img_id, img, err := api.CreateImage(&req)
    33  
    34  	if err != nil {
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  
    39  	err = api.WaitForState(img, "ENABLED", 10, c.Retries)
    40  
    41  	if err != nil {
    42  		ui.Error(err.Error())
    43  		return multistep.ActionHalt
    44  	}
    45  
    46  	state.Put("snapshot_id", img_id)
    47  	state.Put("snapshot_name", img.Name)
    48  	return multistep.ActionContinue
    49  }
    50  
    51  func (s *stepTakeSnapshot) Cleanup(state multistep.StateBag) {
    52  }