2021-04-16 05:52:03 -04:00
|
|
|
//go:generate packer-sdc struct-markdown
|
|
|
|
//go:generate packer-sdc mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-08-15 09:27:00 -04:00
|
|
|
"regexp"
|
2019-10-31 10:49:34 -04:00
|
|
|
"time"
|
2015-06-10 17:02:06 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/uuid"
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-06-13 18:26:13 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The client TOKEN to use to access your account. It
|
2019-06-06 10:29:25 -04:00
|
|
|
// can also be specified via environment variable DIGITALOCEAN_API_TOKEN, if
|
|
|
|
// set.
|
2019-05-28 11:50:58 -04:00
|
|
|
APIToken string `mapstructure:"api_token" required:"true"`
|
|
|
|
// Non standard api endpoint URL. Set this if you are
|
2019-06-06 10:29:25 -04:00
|
|
|
// using a DigitalOcean API compatible service. It can also be specified via
|
|
|
|
// environment variable DIGITALOCEAN_API_URL.
|
|
|
|
APIURL string `mapstructure:"api_url" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name (or slug) of the region to launch the droplet
|
2019-06-06 10:29:25 -04:00
|
|
|
// in. Consequently, this is the region where the snapshot will be available.
|
|
|
|
// See
|
|
|
|
// https://developers.digitalocean.com/documentation/v2/#list-all-regions
|
|
|
|
// for the accepted region names/slugs.
|
2019-05-28 11:50:58 -04:00
|
|
|
Region string `mapstructure:"region" required:"true"`
|
|
|
|
// The name (or slug) of the droplet size to use. See
|
2019-06-06 10:29:25 -04:00
|
|
|
// https://developers.digitalocean.com/documentation/v2/#list-all-sizes
|
|
|
|
// for the accepted size names/slugs.
|
|
|
|
Size string `mapstructure:"size" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name (or slug) of the base image to use. This is the
|
2019-06-06 10:29:25 -04:00
|
|
|
// image that will be used to launch a new droplet and provision it. See
|
|
|
|
// https://developers.digitalocean.com/documentation/v2/#list-all-images
|
|
|
|
// for details on how to get a list of the accepted image names/slugs.
|
|
|
|
Image string `mapstructure:"image" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true to enable private networking
|
2019-06-06 10:29:25 -04:00
|
|
|
// for the droplet being created. This defaults to false, or not enabled.
|
|
|
|
PrivateNetworking bool `mapstructure:"private_networking" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true to enable monitoring for the droplet
|
2019-06-06 10:29:25 -04:00
|
|
|
// being created. This defaults to false, or not enabled.
|
|
|
|
Monitoring bool `mapstructure:"monitoring" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true to enable ipv6 for the droplet being
|
2019-06-06 10:29:25 -04:00
|
|
|
// created. This defaults to false, or not enabled.
|
|
|
|
IPv6 bool `mapstructure:"ipv6" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the resulting snapshot that will
|
2020-03-23 20:02:12 -04:00
|
|
|
// appear in your account. Defaults to `packer-{{timestamp}}` (see
|
2019-06-06 10:29:25 -04:00
|
|
|
// configuration templates for more info).
|
|
|
|
SnapshotName string `mapstructure:"snapshot_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The regions of the resulting
|
2019-06-06 10:29:25 -04:00
|
|
|
// snapshot that will appear in your account.
|
|
|
|
SnapshotRegions []string `mapstructure:"snapshot_regions" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The time to wait, as a duration string, for a
|
2019-06-06 10:29:25 -04:00
|
|
|
// droplet to enter a desired state (such as "active") before timing out. The
|
|
|
|
// default state timeout is "6m".
|
2019-10-31 10:49:34 -04:00
|
|
|
StateTimeout time.Duration `mapstructure:"state_timeout" required:"false"`
|
2019-08-21 06:28:34 -04:00
|
|
|
// How long to wait for an image to be published to the shared image
|
|
|
|
// gallery before timing out. If your Packer build is failing on the
|
|
|
|
// Publishing to Shared Image Gallery step with the error `Original Error:
|
|
|
|
// context deadline exceeded`, but the image is present when you check your
|
|
|
|
// Azure dashboard, then you probably need to increase this timeout from
|
|
|
|
// its default of "60m" (valid time units include `s` for seconds, `m` for
|
|
|
|
// minutes, and `h` for hours.)
|
2019-10-31 10:49:34 -04:00
|
|
|
SnapshotTimeout time.Duration `mapstructure:"snapshot_timeout" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name assigned to the droplet. DigitalOcean
|
2019-06-06 10:29:25 -04:00
|
|
|
// sets the hostname of the machine to this value.
|
|
|
|
DropletName string `mapstructure:"droplet_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// User data to launch with the Droplet. Packer will
|
2019-06-06 10:29:25 -04:00
|
|
|
// not automatically wait for a user script to finish before shutting down the
|
|
|
|
// instance this must be handled in a provisioner.
|
|
|
|
UserData string `mapstructure:"user_data" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Path to a file that will be used for the user
|
2019-06-06 10:29:25 -04:00
|
|
|
// data when launching the Droplet.
|
|
|
|
UserDataFile string `mapstructure:"user_data_file" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Tags to apply to the droplet when it is created
|
2019-06-06 10:29:25 -04:00
|
|
|
Tags []string `mapstructure:"tags" required:"false"`
|
2020-10-13 11:26:00 -04:00
|
|
|
// UUID of the VPC which the droplet will be created in. Before using this,
|
|
|
|
// private_networking should be enabled.
|
|
|
|
VPCUUID string `mapstructure:"vpc_uuid" required:"false"`
|
|
|
|
// Wheter the communicators should use private IP or not (public IP in that case).
|
|
|
|
// If the droplet is or going to be accessible only from the local network because
|
|
|
|
// it is at behind a firewall, then communicators should use the private IP
|
|
|
|
// instead of the public IP. Before using this, private_networking should be enabled.
|
|
|
|
ConnectWithPrivateIP bool `mapstructure:"connect_with_private_ip" required:"false"`
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
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 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:49:34 -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
|
2019-10-31 10:49:34 -04:00
|
|
|
c.StateTimeout = 6 * time.Minute
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:49:34 -04:00
|
|
|
if c.SnapshotTimeout == 0 {
|
2019-07-04 10:25:42 -04:00
|
|
|
// Default to 60 minutes timeout, waiting for snapshot action to finish
|
2019-10-31 10:49:34 -04:00
|
|
|
c.SnapshotTimeout = 60 * time.Minute
|
2019-07-04 10:25:42 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2019-10-31 06:31:17 -04:00
|
|
|
|
2015-06-22 12:22:42 -04:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, es...)
|
2015-06-13 18:26:13 -04:00
|
|
|
}
|
2015-06-10 17:02:06 -04:00
|
|
|
if c.APIToken == "" {
|
|
|
|
// Required configurations that will display errors if not set
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2015-06-10 17:02:06 -04:00
|
|
|
errs, errors.New("api_token for auth must be specified"))
|
|
|
|
}
|
|
|
|
|
2015-06-10 21:54:51 -04:00
|
|
|
if c.Region == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2015-06-10 21:54:51 -04:00
|
|
|
errs, errors.New("region is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Size == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2015-06-10 21:54:51 -04:00
|
|
|
errs, errors.New("size is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Image == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2015-06-10 21:54:51 -04:00
|
|
|
errs, errors.New("image is required"))
|
|
|
|
}
|
|
|
|
|
2016-09-28 09:00:57 -04:00
|
|
|
if c.UserData != "" && c.UserDataFile != "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2016-09-28 09:00:57 -04:00
|
|
|
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 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2016-09-28 09:00:57 -04:00
|
|
|
errs, errors.New(fmt.Sprintf("user_data_file not found: %s", c.UserDataFile)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 09:55:06 -04:00
|
|
|
if c.Tags == nil {
|
|
|
|
c.Tags = make([]string, 0)
|
2018-07-30 09:52:40 -04:00
|
|
|
}
|
2018-08-15 09:27:00 -04:00
|
|
|
tagRe := regexp.MustCompile("^[[:alnum:]:_-]{1,255}$")
|
|
|
|
|
|
|
|
for _, t := range c.Tags {
|
|
|
|
if !tagRe.MatchString(t) {
|
2020-11-20 13:53:16 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("invalid tag: %s", t))
|
2018-08-15 09:27:00 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 09:52:40 -04:00
|
|
|
|
2020-10-13 11:26:00 -04:00
|
|
|
// Check if the PrivateNetworking is enabled by user before use VPC UUID
|
|
|
|
if c.VPCUUID != "" {
|
|
|
|
if c.PrivateNetworking != true {
|
2020-11-30 17:34:20 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, errors.New("private networking should be enabled to use vpc_uuid"))
|
2020-10-13 11:26:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the PrivateNetworking is enabled by user before use ConnectWithPrivateIP
|
|
|
|
if c.ConnectWithPrivateIP == true {
|
|
|
|
if c.PrivateNetworking != true {
|
2020-11-30 17:34:20 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, errors.New("private networking should be enabled to use connect_with_private_ip"))
|
2020-10-13 11:26:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, errs
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 17:03:11 -05:00
|
|
|
packersdk.LogSecretFilter.Set(c.APIToken)
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
2015-06-10 17:02:06 -04:00
|
|
|
}
|