2013-12-12 19:41:44 -05:00
package googlecompute
import (
"errors"
"fmt"
2016-05-06 08:39:55 -04:00
"regexp"
2013-12-12 19:41:44 -05:00
"time"
2017-04-04 16:39:01 -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"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2013-12-12 19:41:44 -05:00
)
2016-05-13 18:37:25 -04:00
var reImageFamily = regexp . MustCompile ( ` ^[a-z]([-a-z0-9] { 0,61}[a-z0-9])?$ ` )
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
2017-07-17 10:09:34 -04:00
AcceleratorType string ` mapstructure:"accelerator_type" `
AcceleratorCount int64 ` mapstructure:"accelerator_count" `
2016-05-24 20:13:36 -04:00
Address string ` mapstructure:"address" `
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" `
2015-10-13 20:18:26 -04:00
DiskType string ` mapstructure:"disk_type" `
2014-08-20 13:20:28 -04:00
ImageName string ` mapstructure:"image_name" `
ImageDescription string ` mapstructure:"image_description" `
2016-05-06 07:43:39 -04:00
ImageFamily string ` mapstructure:"image_family" `
2014-08-20 13:20:28 -04:00
InstanceName string ` mapstructure:"instance_name" `
MachineType string ` mapstructure:"machine_type" `
Metadata map [ string ] string ` mapstructure:"metadata" `
Network string ` mapstructure:"network" `
2016-12-02 16:22:36 -05:00
NetworkProjectId string ` mapstructure:"network_project_id" `
2016-08-02 16:43:04 -04:00
OmitExternalIP bool ` mapstructure:"omit_external_ip" `
2017-02-10 13:58:57 -05:00
OnHostMaintenance string ` mapstructure:"on_host_maintenance" `
2015-12-04 15:13:35 -05:00
Preemptible bool ` mapstructure:"preemptible" `
2016-05-24 20:13:36 -04:00
RawStateTimeout string ` mapstructure:"state_timeout" `
Region string ` mapstructure:"region" `
2016-10-22 01:45:33 -04:00
Scopes [ ] string ` mapstructure:"scopes" `
2014-08-20 13:20:28 -04:00
SourceImage string ` mapstructure:"source_image" `
2016-11-13 10:53:45 -05:00
SourceImageFamily string ` mapstructure:"source_image_family" `
2014-08-20 13:20:28 -04:00
SourceImageProjectId string ` mapstructure:"source_image_project_id" `
2016-05-24 20:13:36 -04:00
StartupScriptFile string ` mapstructure:"startup_script_file" `
Subnetwork string ` mapstructure:"subnetwork" `
2014-08-20 13:20:28 -04:00
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
2016-09-23 08:21:43 -04:00
Account AccountFile
stateTimeout time . Duration
imageAlreadyExists bool
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
}
2015-10-13 20:18:26 -04:00
if c . DiskType == "" {
c . DiskType = "pd-standard"
}
2013-12-12 19:41:44 -05:00
if c . ImageDescription == "" {
c . ImageDescription = "Created by Packer"
}
2017-03-08 13:44:07 -05:00
if c . OnHostMaintenance == "MIGRATE" && c . Preemptible {
errs = packer . MultiErrorAppend ( errs ,
errors . New ( "on_host_maintenance must be TERMINATE when using preemptible instances." ) )
}
// Setting OnHostMaintenance Correct Defaults
// "MIGRATE" : Possible and default if Preemptible is false
// "TERMINATE": Required if Preemptible is true
if c . Preemptible {
2017-02-10 14:57:15 -05:00
c . OnHostMaintenance = "TERMINATE"
2017-03-08 13:44:07 -05:00
} else {
if c . OnHostMaintenance == "" {
c . OnHostMaintenance = "MIGRATE"
}
2017-02-10 14:57:15 -05:00
}
// Make sure user sets a valid value for on_host_maintenance option
if ! ( c . OnHostMaintenance == "MIGRATE" || c . OnHostMaintenance == "TERMINATE" ) {
errs = packer . MultiErrorAppend ( errs ,
errors . New ( "on_host_maintenance must be one of MIGRATE or TERMINATE." ) )
}
2013-12-12 19:41:44 -05:00
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
}
2016-05-06 08:39:55 -04:00
if len ( c . ImageFamily ) > 63 {
errs = packer . MultiErrorAppend ( errs ,
errors . New ( "Invalid image family: Must not be longer than 63 characters" ) )
}
if c . ImageFamily != "" {
2016-05-13 18:37:25 -04:00
if ! reImageFamily . MatchString ( c . ImageFamily ) {
2016-05-06 08:39:55 -04:00
errs = packer . MultiErrorAppend ( errs ,
errors . New ( "Invalid image family: The first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash" ) )
}
}
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-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" ) )
}
2016-10-22 01:45:33 -04:00
if c . Scopes == nil {
c . Scopes = [ ] string {
"https://www.googleapis.com/auth/userinfo.email" ,
"https://www.googleapis.com/auth/compute" ,
"https://www.googleapis.com/auth/devstorage.full_control" ,
}
}
2016-11-13 10:53:45 -05:00
if c . SourceImage == "" && c . SourceImageFamily == "" {
2013-12-12 19:41:44 -05:00
errs = packer . MultiErrorAppend (
2016-11-13 10:53:45 -05:00
errs , errors . New ( "a source_image or source_image_family must be specified" ) )
2013-12-12 19:41:44 -05:00
}
if c . Zone == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "a zone must be specified" ) )
}
2016-02-10 23:31:46 -05:00
if c . Region == "" && len ( c . Zone ) > 2 {
// get region from Zone
region := c . Zone [ : len ( c . Zone ) - 2 ]
c . Region = region
}
2013-12-12 19:41:44 -05:00
2016-07-07 17:50:46 -04:00
err = c . CalcTimeout ( )
2013-12-12 19:41:44 -05:00
if err != nil {
2016-07-07 17:50:46 -04:00
errs = packer . MultiErrorAppend ( errs , err )
2013-12-12 19:41:44 -05:00
}
2014-09-05 12:47:20 -04:00
if c . AccountFile != "" {
2016-07-07 17:50:46 -04:00
if err := ProcessAccountFile ( & c . Account , c . AccountFile ) ; err != nil {
2015-10-08 00:36:31 -04:00
errs = packer . MultiErrorAppend ( errs , err )
2013-12-13 22:32:01 -05:00
}
2013-12-12 19:41:44 -05:00
}
2016-08-02 16:43:04 -04:00
if c . OmitExternalIP && c . Address != "" {
errs = packer . MultiErrorAppend ( fmt . Errorf ( "you can not specify an external address when 'omit_external_ip' is true" ) )
}
if c . OmitExternalIP && ! c . UseInternalIP {
errs = packer . MultiErrorAppend ( fmt . Errorf ( "'use_internal_ip' must be true if 'omit_external_ip' is true" ) )
}
2017-07-19 06:28:49 -04:00
if c . AcceleratorCount > 0 && len ( c . AcceleratorType ) == 0 {
errs = packer . MultiErrorAppend ( fmt . Errorf ( "'accelerator_type' must be set when 'accelerator_count' is more than 0" ) )
}
if c . AcceleratorCount > 0 && c . OnHostMaintenance != "TERMINATE" {
errs = packer . MultiErrorAppend ( fmt . Errorf ( "'on_host_maintenance' must be set to 'TERMINATE' when 'accelerator_count' is more than 0" ) )
}
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
}
2016-07-07 17:50:46 -04:00
func ( c * Config ) CalcTimeout ( ) error {
stateTimeout , err := time . ParseDuration ( c . RawStateTimeout )
if err != nil {
return fmt . Errorf ( "Failed parsing state_timeout: %s" , err )
}
c . stateTimeout = stateTimeout
return nil
2016-09-23 08:21:43 -04:00
}