github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/scaleway/builder.go (about)

     1  // The scaleway package contains a packer.Builder implementation
     2  // that builds Scaleway images (snapshots).
     3  
     4  package scaleway
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"log"
    10  
    11  	"github.com/hashicorp/packer/common"
    12  	"github.com/hashicorp/packer/helper/communicator"
    13  	"github.com/hashicorp/packer/helper/multistep"
    14  	"github.com/hashicorp/packer/packer"
    15  	"github.com/scaleway/scaleway-cli/pkg/api"
    16  )
    17  
    18  // The unique id for the builder
    19  const BuilderId = "hashicorp.scaleway"
    20  
    21  type Builder struct {
    22  	config Config
    23  	runner multistep.Runner
    24  }
    25  
    26  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    27  	c, warnings, errs := NewConfig(raws...)
    28  	if errs != nil {
    29  		return warnings, errs
    30  	}
    31  	b.config = *c
    32  
    33  	return nil, nil
    34  }
    35  
    36  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    37  	client, err := api.NewScalewayAPI(b.config.Organization, b.config.Token, b.config.UserAgent, b.config.Region)
    38  
    39  	if err != nil {
    40  		ui.Error(err.Error())
    41  		return nil, err
    42  	}
    43  
    44  	state := new(multistep.BasicStateBag)
    45  	state.Put("config", b.config)
    46  	state.Put("client", client)
    47  	state.Put("hook", hook)
    48  	state.Put("ui", ui)
    49  
    50  	steps := []multistep.Step{
    51  		&stepCreateSSHKey{
    52  			Debug:          b.config.PackerDebug,
    53  			DebugKeyPath:   fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
    54  			PrivateKeyFile: b.config.Comm.SSHPrivateKey,
    55  		},
    56  		new(stepCreateServer),
    57  		new(stepServerInfo),
    58  		&communicator.StepConnect{
    59  			Config:    &b.config.Comm,
    60  			Host:      commHost,
    61  			SSHConfig: sshConfig,
    62  		},
    63  		new(common.StepProvision),
    64  		new(stepShutdown),
    65  		new(stepSnapshot),
    66  		new(stepImage),
    67  	}
    68  
    69  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
    70  	b.runner.Run(state)
    71  
    72  	if rawErr, ok := state.GetOk("error"); ok {
    73  		return nil, rawErr.(error)
    74  	}
    75  
    76  	// If we were interrupted or cancelled, then just exit.
    77  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
    78  		return nil, errors.New("Build was cancelled.")
    79  	}
    80  
    81  	if _, ok := state.GetOk(multistep.StateHalted); ok {
    82  		return nil, errors.New("Build was halted.")
    83  	}
    84  
    85  	if _, ok := state.GetOk("snapshot_name"); !ok {
    86  		return nil, errors.New("Cannot find snapshot_name in state.")
    87  	}
    88  
    89  	artifact := &Artifact{
    90  		imageName:    state.Get("image_name").(string),
    91  		imageID:      state.Get("image_id").(string),
    92  		snapshotName: state.Get("snapshot_name").(string),
    93  		snapshotID:   state.Get("snapshot_id").(string),
    94  		regionName:   state.Get("region").(string),
    95  		client:       client,
    96  	}
    97  
    98  	return artifact, nil
    99  }
   100  
   101  func (b *Builder) Cancel() {
   102  	if b.runner != nil {
   103  		log.Println("Cancelling the step runner...")
   104  		b.runner.Cancel()
   105  	}
   106  }