2013-06-13 10:03:10 -04:00
|
|
|
// The digitalocean package contains a packer.Builder implementation
|
|
|
|
// that builds DigitalOcean images (snapshots).
|
|
|
|
|
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2015-06-10 21:58:50 -04:00
|
|
|
"fmt"
|
2014-08-28 08:49:52 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2013-06-13 10:03:10 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-08-01 15:11:54 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-13 18:26:13 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2013-06-13 10:03:10 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-06-10 17:02:06 -04:00
|
|
|
"golang.org/x/oauth2"
|
2013-06-13 10:03:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique id for the builder
|
|
|
|
const BuilderId = "pearkes.digitalocean"
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 15:50:43 -04:00
|
|
|
config Config
|
2013-06-13 10:03:10 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2013-11-03 00:03:59 -04:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
2015-06-10 17:02:06 -04:00
|
|
|
c, warnings, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
2015-06-10 17:02:06 -04:00
|
|
|
b.config = *c
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-11-03 00:03:59 -04:00
|
|
|
return nil, nil
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{
|
|
|
|
AccessToken: b.config.APIToken,
|
|
|
|
}))
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
// Set up the state
|
2013-08-31 15:25:08 -04:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2015-01-10 07:52:45 -05:00
|
|
|
&stepCreateSSHKey{
|
2015-06-10 21:58:50 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
2015-01-10 07:52:45 -05:00
|
|
|
DebugKeyPath: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
2013-06-13 11:58:06 -04:00
|
|
|
new(stepCreateDroplet),
|
2013-06-13 12:48:19 -04:00
|
|
|
new(stepDropletInfo),
|
2015-06-13 18:26:13 -04:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
SSHAddress: sshAddress,
|
|
|
|
SSHConfig: sshConfig,
|
2013-07-15 01:14:10 -04:00
|
|
|
},
|
2013-07-16 02:44:41 -04:00
|
|
|
new(common.StepProvision),
|
2013-08-24 07:04:51 -04:00
|
|
|
new(stepShutdown),
|
2013-06-13 11:58:06 -04:00
|
|
|
new(stepPowerOff),
|
|
|
|
new(stepSnapshot),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2013-06-15 14:09:26 -04:00
|
|
|
if b.config.PackerDebug {
|
|
|
|
b.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
}
|
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
b.runner.Run(state)
|
2013-06-13 10:03:10 -04:00
|
|
|
|
2013-06-20 00:00:51 -04:00
|
|
|
// If there was an error, return that
|
2013-08-31 15:25:08 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2013-06-20 00:00:51 -04:00
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
if _, ok := state.GetOk("snapshot_name"); !ok {
|
2013-06-19 00:54:15 -04:00
|
|
|
log.Println("Failed to find snapshot_name in state. Bug?")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2013-08-31 15:25:08 -04:00
|
|
|
snapshotName: state.Get("snapshot_name").(string),
|
2015-06-10 17:02:06 -04:00
|
|
|
snapshotId: state.Get("snapshot_image_id").(int),
|
|
|
|
regionName: state.Get("region").(string),
|
2013-06-19 00:54:15 -04:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
2013-06-13 10:03:10 -04:00
|
|
|
}
|