github.phpd.cn/hashicorp/packer@v1.3.2/builder/scaleway/step_create_image.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/scaleway/scaleway-cli/pkg/api"
    11  )
    12  
    13  type stepImage struct{}
    14  
    15  func (s *stepImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    16  	client := state.Get("client").(*api.ScalewayAPI)
    17  	ui := state.Get("ui").(packer.Ui)
    18  	c := state.Get("config").(*Config)
    19  	snapshotID := state.Get("snapshot_id").(string)
    20  	bootscriptID := ""
    21  
    22  	ui.Say(fmt.Sprintf("Creating image: %v", c.ImageName))
    23  
    24  	image, err := client.GetImage(c.Image)
    25  	if err != nil {
    26  		err := fmt.Errorf("Error getting initial image info: %s", err)
    27  		state.Put("error", err)
    28  		ui.Error(err.Error())
    29  		return multistep.ActionHalt
    30  	}
    31  
    32  	if image.DefaultBootscript != nil {
    33  		bootscriptID = image.DefaultBootscript.Identifier
    34  	}
    35  
    36  	imageID, err := client.PostImage(snapshotID, c.ImageName, bootscriptID, image.Arch)
    37  	if err != nil {
    38  		err := fmt.Errorf("Error creating image: %s", err)
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	log.Printf("Image ID: %s", imageID)
    45  	state.Put("image_id", imageID)
    46  	state.Put("image_name", c.ImageName)
    47  	state.Put("region", c.Region)
    48  
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *stepImage) Cleanup(state multistep.StateBag) {
    53  	// no cleanup
    54  }