2013-12-12 19:41:44 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
2014-04-26 14:12:43 -04:00
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2015-06-13 18:30:16 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2015-05-27 15:57:48 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
2013-12-12 19:41:44 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-05-27 15:57:48 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-12-12 19:41:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the GCE builder. It stores
|
|
|
|
// both the publicly settable state as well as the privately generated
|
|
|
|
// state of the config object.
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-06-13 18:30:16 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2013-12-12 19:41:44 -05:00
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
AccountFile string `mapstructure:"account_file"`
|
|
|
|
ProjectId string `mapstructure:"project_id"`
|
2014-09-05 12:47:20 -04:00
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
DiskName string `mapstructure:"disk_name"`
|
2014-08-20 13:20:28 -04:00
|
|
|
DiskSizeGb int64 `mapstructure:"disk_size"`
|
|
|
|
ImageName string `mapstructure:"image_name"`
|
|
|
|
ImageDescription string `mapstructure:"image_description"`
|
|
|
|
InstanceName string `mapstructure:"instance_name"`
|
|
|
|
MachineType string `mapstructure:"machine_type"`
|
|
|
|
Metadata map[string]string `mapstructure:"metadata"`
|
|
|
|
Network string `mapstructure:"network"`
|
2015-12-24 10:19:29 -05:00
|
|
|
Address string `mapstructure:"address"`
|
2015-12-04 15:13:35 -05:00
|
|
|
Preemptible bool `mapstructure:"preemptible"`
|
2014-08-20 13:20:28 -04:00
|
|
|
SourceImage string `mapstructure:"source_image"`
|
|
|
|
SourceImageProjectId string `mapstructure:"source_image_project_id"`
|
|
|
|
RawStateTimeout string `mapstructure:"state_timeout"`
|
|
|
|
Tags []string `mapstructure:"tags"`
|
2015-05-29 17:50:11 -04:00
|
|
|
UseInternalIP bool `mapstructure:"use_internal_ip"`
|
2014-08-20 13:20:28 -04:00
|
|
|
Zone string `mapstructure:"zone"`
|
2013-12-12 19:41:44 -05:00
|
|
|
|
2014-09-05 12:47:20 -04:00
|
|
|
account accountFile
|
2013-12-12 19:41:44 -05:00
|
|
|
privateKeyBytes []byte
|
|
|
|
stateTimeout time.Duration
|
2015-06-22 12:22:42 -04:00
|
|
|
ctx interpolate.Context
|
2013-12-12 19:41:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
2015-06-12 17:00:59 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 15:57:48 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-12-12 19:41:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-07 19:12:21 -04:00
|
|
|
var errs *packer.MultiError
|
|
|
|
|
2013-12-12 19:41:44 -05:00
|
|
|
// Set defaults.
|
|
|
|
if c.Network == "" {
|
|
|
|
c.Network = "default"
|
|
|
|
}
|
|
|
|
|
2014-08-07 15:34:08 -04:00
|
|
|
if c.DiskSizeGb == 0 {
|
|
|
|
c.DiskSizeGb = 10
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:41:44 -05:00
|
|
|
if c.ImageDescription == "" {
|
|
|
|
c.ImageDescription = "Created by Packer"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ImageName == "" {
|
2015-07-06 05:32:08 -04:00
|
|
|
img, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
2015-07-07 19:12:21 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Unable to parse image name: %s ", err))
|
2016-01-19 14:12:19 -05:00
|
|
|
} else {
|
2015-07-07 19:12:21 -04:00
|
|
|
c.ImageName = img
|
2015-07-06 05:32:08 -04:00
|
|
|
}
|
2013-12-12 19:41:44 -05:00
|
|
|
}
|
|
|
|
|
2014-04-25 22:14:53 -04:00
|
|
|
if c.InstanceName == "" {
|
|
|
|
c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
|
|
|
}
|
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
if c.DiskName == "" {
|
|
|
|
c.DiskName = c.InstanceName
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:41:44 -05:00
|
|
|
if c.MachineType == "" {
|
|
|
|
c.MachineType = "n1-standard-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawStateTimeout == "" {
|
|
|
|
c.RawStateTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:30:16 -04:00
|
|
|
if c.Comm.SSHUsername == "" {
|
|
|
|
c.Comm.SSHUsername = "root"
|
2013-12-12 19:41:44 -05:00
|
|
|
}
|
|
|
|
|
2015-06-29 12:56:33 -04:00
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
2013-12-12 19:41:44 -05:00
|
|
|
|
|
|
|
// Process required parameters.
|
|
|
|
if c.ProjectId == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a project_id must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SourceImage == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a source_image must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Zone == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("a zone must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
stateTimeout, err := time.ParseDuration(c.RawStateTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing state_timeout: %s", err))
|
|
|
|
}
|
|
|
|
c.stateTimeout = stateTimeout
|
|
|
|
|
2014-09-05 12:47:20 -04:00
|
|
|
if c.AccountFile != "" {
|
2015-10-08 00:36:31 -04:00
|
|
|
if err := processAccountFile(&c.account, c.AccountFile); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2013-12-13 22:32:01 -05:00
|
|
|
}
|
2013-12-12 19:41:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil, nil
|
|
|
|
}
|