2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-06 10:37:06 -04:00
|
|
|
"os"
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-04-04 22:24:21 -04:00
|
|
|
"github.com/hashicorp/packer/helper/useragent"
|
2017-04-06 05:19:17 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Token string `mapstructure:"api_token"`
|
2017-04-11 06:19:28 -04:00
|
|
|
Organization string `mapstructure:"api_access_key"`
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
Region string `mapstructure:"region"`
|
|
|
|
Image string `mapstructure:"image"`
|
|
|
|
CommercialType string `mapstructure:"commercial_type"`
|
|
|
|
|
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
2017-04-11 06:19:28 -04:00
|
|
|
ImageName string `mapstructure:"image_name"`
|
2017-04-06 05:19:17 -04:00
|
|
|
ServerName string `mapstructure:"server_name"`
|
2018-06-29 13:44:56 -04:00
|
|
|
Bootscript string `mapstructure:"bootscript"`
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
UserAgent string
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-04 22:24:21 -04:00
|
|
|
c.UserAgent = useragent.String()
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2017-04-06 10:37:06 -04:00
|
|
|
if c.Organization == "" {
|
2017-04-11 06:19:28 -04:00
|
|
|
c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY")
|
2017-04-06 10:37:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token == "" {
|
|
|
|
c.Token = os.Getenv("SCALEWAY_API_TOKEN")
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
if c.SnapshotName == "" {
|
2017-04-11 06:19:28 -04:00
|
|
|
def, err := interpolate.Render("snapshot-packer-{{timestamp}}", nil)
|
2017-04-06 05:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SnapshotName = def
|
|
|
|
}
|
|
|
|
|
2017-04-11 06:19:28 -04:00
|
|
|
if c.ImageName == "" {
|
|
|
|
def, err := interpolate.Render("image-packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ImageName = def
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
if c.ServerName == "" {
|
|
|
|
// Default to packer-[time-ordered-uuid]
|
|
|
|
c.ServerName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
|
|
|
if c.Organization == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("Scaleway Organization ID must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Token == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("Scaleway Token must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Region == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("region is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CommercialType == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("commercial type is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Image == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("image is required"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
common.ScrubConfig(c, c.Token)
|
|
|
|
return c, nil, nil
|
|
|
|
}
|