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"
|
2013-12-12 19:41:44 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
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"`
|
|
|
|
SourceImage string `mapstructure:"source_image"`
|
|
|
|
SourceImageProjectId string `mapstructure:"source_image_project_id"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPort uint `mapstructure:"ssh_port"`
|
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
|
|
RawStateTimeout string `mapstructure:"state_timeout"`
|
|
|
|
Tags []string `mapstructure:"tags"`
|
|
|
|
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
|
|
|
|
sshTimeout time.Duration
|
|
|
|
stateTimeout time.Duration
|
|
|
|
tpl *packer.ConfigTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
|
|
|
md, err := common.DecodeConfig(c, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
c.tpl.UserVars = c.PackerUserVars
|
|
|
|
|
|
|
|
// Prepare the errors
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
|
|
|
|
|
|
|
// 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 == "" {
|
|
|
|
c.ImageName = "packer-{{timestamp}}"
|
|
|
|
}
|
|
|
|
|
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.RawSSHTimeout == "" {
|
|
|
|
c.RawSSHTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawStateTimeout == "" {
|
|
|
|
c.RawStateTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHUsername == "" {
|
|
|
|
c.SSHUsername = "root"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Templates
|
|
|
|
templates := map[string]*string{
|
2014-11-24 11:36:14 -05:00
|
|
|
"account_file": &c.AccountFile,
|
2014-09-05 12:47:20 -04:00
|
|
|
|
2014-11-24 11:36:14 -05:00
|
|
|
"disk_name": &c.DiskName,
|
2014-08-20 13:20:28 -04:00
|
|
|
"image_name": &c.ImageName,
|
|
|
|
"image_description": &c.ImageDescription,
|
|
|
|
"instance_name": &c.InstanceName,
|
|
|
|
"machine_type": &c.MachineType,
|
|
|
|
"network": &c.Network,
|
|
|
|
"project_id": &c.ProjectId,
|
|
|
|
"source_image": &c.SourceImage,
|
|
|
|
"source_image_project_id": &c.SourceImageProjectId,
|
|
|
|
"ssh_username": &c.SSHUsername,
|
|
|
|
"ssh_timeout": &c.RawSSHTimeout,
|
|
|
|
"state_timeout": &c.RawStateTimeout,
|
|
|
|
"zone": &c.Zone,
|
2013-12-12 19:41:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = c.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process timeout settings.
|
|
|
|
sshTimeout, err := time.ParseDuration(c.RawSSHTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
|
|
}
|
|
|
|
c.sshTimeout = sshTimeout
|
|
|
|
|
|
|
|
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 != "" {
|
|
|
|
if err := loadJSON(&c.account, c.AccountFile); err != nil {
|
2013-12-13 22:32:01 -05:00
|
|
|
errs = packer.MultiErrorAppend(
|
2014-09-05 12:47:20 -04:00
|
|
|
errs, fmt.Errorf("Failed parsing account file: %s", 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
|
|
|
|
}
|