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
2018-03-07 18:35:01 -05:00
AcceleratorType string ` mapstructure:"accelerator_type" `
AcceleratorCount int64 ` mapstructure:"accelerator_count" `
Address string ` mapstructure:"address" `
DisableDefaultServiceAccount bool ` mapstructure:"disable_default_service_account" `
DiskName string ` mapstructure:"disk_name" `
DiskSizeGb int64 ` mapstructure:"disk_size" `
DiskType string ` mapstructure:"disk_type" `
ImageName string ` mapstructure:"image_name" `
ImageDescription string ` mapstructure:"image_description" `
ImageFamily string ` mapstructure:"image_family" `
ImageLabels map [ string ] string ` mapstructure:"image_labels" `
ImageLicenses [ ] string ` mapstructure:"image_licenses" `
InstanceName string ` mapstructure:"instance_name" `
Labels map [ string ] string ` mapstructure:"labels" `
MachineType string ` mapstructure:"machine_type" `
Metadata map [ string ] string ` mapstructure:"metadata" `
Network string ` mapstructure:"network" `
NetworkProjectId string ` mapstructure:"network_project_id" `
OmitExternalIP bool ` mapstructure:"omit_external_ip" `
OnHostMaintenance string ` mapstructure:"on_host_maintenance" `
Preemptible bool ` mapstructure:"preemptible" `
RawStateTimeout string ` mapstructure:"state_timeout" `
Region string ` mapstructure:"region" `
Scopes [ ] string ` mapstructure:"scopes" `
ServiceAccountEmail string ` mapstructure:"service_account_email" `
SourceImage string ` mapstructure:"source_image" `
SourceImageFamily string ` mapstructure:"source_image_family" `
SourceImageProjectId string ` mapstructure:"source_image_project_id" `
StartupScriptFile string ` mapstructure:"startup_script_file" `
Subnetwork string ` mapstructure:"subnetwork" `
Tags [ ] string ` mapstructure:"tags" `
UseInternalIP bool ` mapstructure:"use_internal_ip" `
Zone string ` mapstructure:"zone" `
2018-02-21 12:22:39 -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
}
2017-10-17 00:48:15 -04:00
func NewConfig ( raws ... interface { } ) ( * Config , [ ] string , error ) {
2013-12-12 19:41:44 -05:00
c := new ( Config )
2017-10-17 00:48:15 -04:00
c . ctx . Funcs = TemplateFuncs
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.
2017-09-13 17:13:47 -04:00
if c . Network == "" && c . Subnetwork == "" {
2013-12-12 19:41:44 -05:00
c . Network = "default"
}
2017-09-13 17:12:03 -04:00
if c . NetworkProjectId == "" {
c . NetworkProjectId = c . ProjectId
}
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" ) )
}
2018-03-13 15:36:14 -04:00
// If DisableDefaultServiceAccount is provided, don't allow a value for ServiceAccountEmail
if c . DisableDefaultServiceAccount && c . ServiceAccountEmail != "" {
errs = packer . MultiErrorAppend ( fmt . Errorf ( "you may not specify a 'service_account_email' when 'disable_default_service_account' is true" ) )
}
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
}