2013-06-13 16:03:10 +02: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 18:58:50 -07:00
|
|
|
"fmt"
|
2014-08-28 16:49:52 +04:00
|
|
|
"log"
|
2016-02-05 12:57:43 +00:00
|
|
|
"net/url"
|
2014-08-28 16:49:52 +04:00
|
|
|
|
2015-06-10 14:02:06 -07:00
|
|
|
"github.com/digitalocean/godo"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-10 14:02:06 -07:00
|
|
|
"golang.org/x/oauth2"
|
2013-06-13 16:03:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique id for the builder
|
|
|
|
const BuilderId = "pearkes.digitalocean"
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 12:50:43 -07:00
|
|
|
config Config
|
2013-06-13 16:03:10 +02:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:03:59 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
2015-06-10 14:02:06 -07:00
|
|
|
c, warnings, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
2013-06-13 16:03:10 +02:00
|
|
|
}
|
2015-06-10 14:02:06 -07:00
|
|
|
b.config = *c
|
2013-06-13 16:03:10 +02:00
|
|
|
|
2013-11-02 23:03:59 -05:00
|
|
|
return nil, nil
|
2013-06-13 16:03:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2015-06-10 14:02:06 -07:00
|
|
|
client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{
|
|
|
|
AccessToken: b.config.APIToken,
|
|
|
|
}))
|
2016-02-05 12:57:43 +00: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 17:58:06 +02: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 20:55:39 -07:00
|
|
|
validRegions := make(map[string]struct{})
|
2017-05-16 17:05:40 -04:00
|
|
|
for _, val := range regions {
|
2017-05-16 20:55:39 -07:00
|
|
|
validRegions[val.Slug] = struct{}{}
|
2017-05-16 17:05:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-16 20:55:39 -07: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 17:58:06 +02:00
|
|
|
// Set up the state
|
2013-08-31 12:25:08 -07:00
|
|
|
state := new(multistep.BasicStateBag)
|
2018-09-18 11:36:21 +02:00
|
|
|
state.Put("config", &b.config)
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2013-06-13 17:58:06 +02:00
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2015-01-10 23:52:45 +11:00
|
|
|
&stepCreateSSHKey{
|
2015-06-10 18:58:50 -07:00
|
|
|
Debug: b.config.PackerDebug,
|
2015-01-10 23:52:45 +11:00
|
|
|
DebugKeyPath: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
2013-06-13 17:58:06 +02:00
|
|
|
new(stepCreateDroplet),
|
2013-06-13 18:48:19 +02: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,
|
2018-08-22 17:02:23 +02:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2013-07-15 14:14:10 +09:00
|
|
|
},
|
2013-07-16 15:44:41 +09:00
|
|
|
new(common.StepProvision),
|
2018-09-14 11:03:23 -07:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2013-08-24 13:04:51 +02:00
|
|
|
new(stepShutdown),
|
2013-06-13 17:58:06 +02:00
|
|
|
new(stepPowerOff),
|
|
|
|
new(stepSnapshot),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2016-09-14 00:04:18 +00:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2013-06-13 17:58:06 +02:00
|
|
|
b.runner.Run(state)
|
2013-06-13 16:03:10 +02:00
|
|
|
|
2013-06-19 21:00:51 -07:00
|
|
|
// If there was an error, return that
|
2013-08-31 12:25:08 -07:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2013-06-19 21:00:51 -07:00
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:25:08 -07:00
|
|
|
if _, ok := state.GetOk("snapshot_name"); !ok {
|
2013-06-18 21:54:15 -07:00
|
|
|
log.Println("Failed to find snapshot_name in state. Bug?")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2013-08-31 12:25:08 -07:00
|
|
|
snapshotName: state.Get("snapshot_name").(string),
|
2015-06-10 14:02:06 -07:00
|
|
|
snapshotId: state.Get("snapshot_image_id").(int),
|
2017-05-16 10:38:53 -04:00
|
|
|
regionNames: state.Get("regions").([]string),
|
2013-06-18 21:54:15 -07:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-06-13 17:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
2013-06-13 16:03:10 +02:00
|
|
|
}
|