2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2016-06-28 22:35:41 -04:00
|
|
|
package profitbricks
|
|
|
|
|
|
|
|
import (
|
2016-08-01 07:09:07 -04:00
|
|
|
"errors"
|
2018-01-22 20:21:10 -05:00
|
|
|
"os"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2016-06-28 22:35:41 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2016-08-01 07:09:07 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2016-07-07 04:28:46 -04:00
|
|
|
|
2016-08-01 07:09:07 -04:00
|
|
|
PBUsername string `mapstructure:"username"`
|
|
|
|
PBPassword string `mapstructure:"password"`
|
|
|
|
PBUrl string `mapstructure:"url"`
|
2016-07-07 04:28:46 -04:00
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
Region string `mapstructure:"location"`
|
|
|
|
Image string `mapstructure:"image"`
|
|
|
|
SSHKey string
|
2019-10-14 10:09:27 -04:00
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
|
|
|
DiskSize int `mapstructure:"disk_size"`
|
|
|
|
DiskType string `mapstructure:"disk_type"`
|
|
|
|
Cores int `mapstructure:"cores"`
|
|
|
|
Ram int `mapstructure:"ram"`
|
|
|
|
Retries int `mapstructure:"retries"`
|
2016-11-15 18:17:30 -05:00
|
|
|
ctx interpolate.Context
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2016-06-28 22:35:41 -04:00
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
err := config.Decode(&c, &config.DecodeOpts{
|
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
|
2018-08-23 10:35:07 -04:00
|
|
|
if c.Comm.SSHPassword == "" && c.Comm.SSHPrivateKeyFile == "" {
|
2016-11-15 18:17:30 -05:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("Either ssh private key path or ssh password must be set."))
|
|
|
|
}
|
2016-06-28 22:35:41 -04:00
|
|
|
|
2016-07-07 04:28:46 -04:00
|
|
|
if c.SnapshotName == "" {
|
|
|
|
def, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to packer-{{ unix timestamp (utc) }}
|
|
|
|
c.SnapshotName = def
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.PBUsername == "" {
|
|
|
|
c.PBUsername = os.Getenv("PROFITBRICKS_USERNAME")
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:28:46 -04:00
|
|
|
if c.PBPassword == "" {
|
|
|
|
c.PBPassword = os.Getenv("PROFITBRICKS_PASSWORD")
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.PBUrl == "" {
|
2017-08-10 09:15:53 -04:00
|
|
|
c.PBUrl = "https://api.profitbricks.com/cloudapi/v4"
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Cores == 0 {
|
|
|
|
c.Cores = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Ram == 0 {
|
|
|
|
c.Ram = 2048
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskSize == 0 {
|
|
|
|
c.DiskSize = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Region == "" {
|
|
|
|
c.Region = "us/las"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskType == "" {
|
|
|
|
c.DiskType = "HDD"
|
|
|
|
}
|
|
|
|
|
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
2016-11-15 18:17:30 -05:00
|
|
|
|
|
|
|
if c.Image == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("ProfitBricks 'image' is required"))
|
|
|
|
}
|
2016-06-28 22:35:41 -04:00
|
|
|
|
|
|
|
if c.PBUsername == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("ProfitBricks username is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.PBPassword == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2016-07-01 08:28:29 -04:00
|
|
|
errs, errors.New("ProfitBricks password is required"))
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, errs
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(c.PBUsername)
|
2016-06-28 22:35:41 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
2016-06-28 22:35:41 -04:00
|
|
|
}
|