github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/digitalocean/builder.go (about)

     1  // The digitalocean package contains a packer.Builder implementation
     2  // that builds DigitalOcean images (snapshots).
     3  
     4  package digitalocean
     5  
     6  import (
     7  	"fmt"
     8  	"log"
     9  	"net/url"
    10  
    11  	"github.com/digitalocean/godo"
    12  	"github.com/mitchellh/multistep"
    13  	"github.com/mitchellh/packer/common"
    14  	"github.com/mitchellh/packer/helper/communicator"
    15  	"github.com/mitchellh/packer/packer"
    16  	"golang.org/x/oauth2"
    17  )
    18  
    19  // The unique id for the builder
    20  const BuilderId = "pearkes.digitalocean"
    21  
    22  type Builder struct {
    23  	config Config
    24  	runner multistep.Runner
    25  }
    26  
    27  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    28  	c, warnings, errs := NewConfig(raws...)
    29  	if errs != nil {
    30  		return warnings, errs
    31  	}
    32  	b.config = *c
    33  
    34  	return nil, nil
    35  }
    36  
    37  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    38  	client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{
    39  		AccessToken: b.config.APIToken,
    40  	}))
    41  	if b.config.APIURL != "" {
    42  		u, err := url.Parse(b.config.APIURL)
    43  		if err != nil {
    44  			return nil, fmt.Errorf("DigitalOcean: Invalid API URL, %s.", err)
    45  		}
    46  		client.BaseURL = u
    47  	}
    48  
    49  	// Set up the state
    50  	state := new(multistep.BasicStateBag)
    51  	state.Put("config", b.config)
    52  	state.Put("client", client)
    53  	state.Put("hook", hook)
    54  	state.Put("ui", ui)
    55  
    56  	// Build the steps
    57  	steps := []multistep.Step{
    58  		&stepCreateSSHKey{
    59  			Debug:        b.config.PackerDebug,
    60  			DebugKeyPath: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName),
    61  		},
    62  		new(stepCreateDroplet),
    63  		new(stepDropletInfo),
    64  		&communicator.StepConnect{
    65  			Config:    &b.config.Comm,
    66  			Host:      commHost,
    67  			SSHConfig: sshConfig,
    68  		},
    69  		new(common.StepProvision),
    70  		new(stepShutdown),
    71  		new(stepPowerOff),
    72  		new(stepSnapshot),
    73  	}
    74  
    75  	// Run the steps
    76  	b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
    77  	b.runner.Run(state)
    78  
    79  	// If there was an error, return that
    80  	if rawErr, ok := state.GetOk("error"); ok {
    81  		return nil, rawErr.(error)
    82  	}
    83  
    84  	if _, ok := state.GetOk("snapshot_name"); !ok {
    85  		log.Println("Failed to find snapshot_name in state. Bug?")
    86  		return nil, nil
    87  	}
    88  
    89  	artifact := &Artifact{
    90  		snapshotName: state.Get("snapshot_name").(string),
    91  		snapshotId:   state.Get("snapshot_image_id").(int),
    92  		regionName:   state.Get("region").(string),
    93  		client:       client,
    94  	}
    95  
    96  	return artifact, nil
    97  }
    98  
    99  func (b *Builder) Cancel() {
   100  	if b.runner != nil {
   101  		log.Println("Cancelling the step runner...")
   102  		b.runner.Cancel()
   103  	}
   104  }