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
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (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)
|
2018-09-18 05:36:21 -04:00
|
|
|
state.Put("config", &b.config)
|
2013-08-31 15:25:08 -04:00
|
|
|
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,
|
2019-07-03 16:30:29 -04:00
|
|
|
Host: communicator.CommHost(b.config.Comm.SSHHost, "droplet_ip"),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2013-07-15 01:14:10 -04:00
|
|
|
},
|
2013-07-16 02:44:41 -04:00
|
|
|
new(common.StepProvision),
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2013-08-24 07:04:51 -04:00
|
|
|
new(stepShutdown),
|
2013-06-13 11:58:06 -04:00
|
|
|
new(stepPowerOff),
|
2019-07-04 10:25:42 -04:00
|
|
|
&stepSnapshot{
|
|
|
|
snapshotTimeout: b.config.SnapshotTimeout,
|
|
|
|
},
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, 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{
|
2018-11-22 21:33:34 -05:00
|
|
|
SnapshotName: state.Get("snapshot_name").(string),
|
|
|
|
SnapshotId: state.Get("snapshot_image_id").(int),
|
|
|
|
RegionNames: state.Get("regions").([]string),
|
|
|
|
Client: client,
|
2013-06-19 00:54:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|