2015-06-10 17:02:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2015-06-13 18:26:13 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-06-13 18:26:13 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2015-06-10 17:02:06 -04:00
|
|
|
|
|
|
|
APIToken string `mapstructure:"api_token"`
|
2016-02-05 07:57:43 -05:00
|
|
|
APIURL string `mapstructure:"api_url"`
|
2015-06-10 17:02:06 -04:00
|
|
|
|
|
|
|
Region string `mapstructure:"region"`
|
|
|
|
Size string `mapstructure:"size"`
|
|
|
|
Image string `mapstructure:"image"`
|
|
|
|
|
2015-06-13 18:26:13 -04:00
|
|
|
PrivateNetworking bool `mapstructure:"private_networking"`
|
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
|
|
|
StateTimeout time.Duration `mapstructure:"state_timeout"`
|
|
|
|
DropletName string `mapstructure:"droplet_name"`
|
|
|
|
UserData string `mapstructure:"user_data"`
|
2016-09-28 09:00:57 -04:00
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
2015-06-10 17:02:06 -04:00
|
|
|
|
2015-06-22 12:22:42 -04:00
|
|
|
ctx interpolate.Context
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
2015-06-12 17:02:09 -04:00
|
|
|
c := new(Config)
|
2015-06-10 17:02:06 -04:00
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
2015-06-12 17:02:09 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-06-10 17:02:06 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if c.APIToken == "" {
|
|
|
|
// Default to environment variable for api_token, if it exists
|
|
|
|
c.APIToken = os.Getenv("DIGITALOCEAN_API_TOKEN")
|
|
|
|
}
|
2016-02-05 07:57:43 -05:00
|
|
|
if c.APIURL == "" {
|
|
|
|
c.APIURL = os.Getenv("DIGITALOCEAN_API_URL")
|
|
|
|
}
|
2015-06-10 17:02:06 -04:00
|
|
|
if c.SnapshotName == "" {
|
2015-06-10 22:53:07 -04:00
|
|
|
def, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
// Default to packer-{{ unix timestamp (utc) }}
|
2015-06-10 22:53:07 -04:00
|
|
|
c.SnapshotName = def
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.DropletName == "" {
|
|
|
|
// Default to packer-[time-ordered-uuid]
|
|
|
|
c.DropletName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:26:13 -04:00
|
|
|
if c.Comm.SSHUsername == "" {
|
2015-06-10 17:02:06 -04:00
|
|
|
// Default to "root". You can override this if your
|
|
|
|
// SourceImage has a different user account then the DO default
|
2015-06-13 18:26:13 -04:00
|
|
|
c.Comm.SSHUsername = "root"
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:26:13 -04:00
|
|
|
if c.StateTimeout == 0 {
|
2015-06-10 17:02:06 -04:00
|
|
|
// Default to 6 minute timeouts waiting for
|
|
|
|
// desired state. i.e waiting for droplet to become active
|
2015-06-13 18:26:13 -04:00
|
|
|
c.StateTimeout = 6 * time.Minute
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
2015-06-22 12:22:42 -04:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
2015-06-13 18:26:13 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
2015-06-10 17:02:06 -04:00
|
|
|
if c.APIToken == "" {
|
|
|
|
// Required configurations that will display errors if not set
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("api_token for auth must be specified"))
|
|
|
|
}
|
|
|
|
|
2015-06-10 21:54:51 -04:00
|
|
|
if c.Region == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("region is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Size == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("size is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Image == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("image is required"))
|
|
|
|
}
|
|
|
|
|
2016-09-28 09:00:57 -04:00
|
|
|
if c.UserData != "" && c.UserDataFile != "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("only one of user_data or user_data_file can be specified"))
|
|
|
|
} else if c.UserDataFile != "" {
|
|
|
|
if _, err := os.Stat(c.UserDataFile); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New(fmt.Sprintf("user_data_file not found: %s", c.UserDataFile)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
common.ScrubConfig(c, c.APIToken)
|
2015-06-12 17:02:09 -04:00
|
|
|
return c, nil, nil
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|