2013-06-13 10:03:10 -04:00
|
|
|
// The digitalocean package contains a packer.Builder implementation
|
|
|
|
// that builds DigitalOcean images (snapshots).
|
|
|
|
|
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-16 11:34:32 -04:00
|
|
|
"context"
|
2015-06-10 21:58:50 -04:00
|
|
|
"fmt"
|
2014-08-28 08:49:52 -04:00
|
|
|
"log"
|
2016-02-05 07:57:43 -05:00
|
|
|
"net/url"
|
2014-08-28 08:49:52 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/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,
|
|
|
|
}))
|
2016-02-05 07:57:43 -05:00
|
|
|
if b.config.APIURL != "" {
|
|
|
|
u, err := url.Parse(b.config.APIURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DigitalOcean: Invalid API URL, %s.", err)
|
|
|
|
}
|
|
|
|
client.BaseURL = u
|
|
|
|
}
|
2013-06-13 11:58:06 -04:00
|
|
|
|
2017-05-16 11:34:32 -04:00
|
|
|
if len(b.config.SnapshotRegions) > 0 {
|
|
|
|
opt := &godo.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 200,
|
|
|
|
}
|
|
|
|
regions, _, err := client.Regions.List(context.TODO(), opt)
|
|
|
|
if err != nil {
|
2017-05-16 17:05:40 -04:00
|
|
|
return nil, fmt.Errorf("DigitalOcean: Unable to get regions, %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:55:39 -04:00
|
|
|
validRegions := make(map[string]struct{})
|
2017-05-16 17:05:40 -04:00
|
|
|
for _, val := range regions {
|
2017-05-16 23:55:39 -04:00
|
|
|
validRegions[val.Slug] = struct{}{}
|
2017-05-16 17:05:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-16 23:55:39 -04:00
|
|
|
for _, region := range append(b.config.SnapshotRegions, b.config.Region) {
|
|
|
|
if _, ok := validRegions[region]; !ok {
|
|
|
|
return nil, fmt.Errorf("DigitalOcean: Invalid region, %s", region)
|
2017-05-16 11:34:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2015-06-13 19:23:33 -04:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
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
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
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),
|
2017-05-16 10:38:53 -04:00
|
|
|
regionNames: state.Get("regions").([]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
|
|
|
}
|