Class: VagrantPlugins::Google::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-google/config.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zone_specific = false) ⇒ Config

Returns a new instance of Config.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/vagrant-google/config.rb', line 211

def initialize(zone_specific=false)
  @google_json_key_location    = UNSET_VALUE
  @google_project_id           = UNSET_VALUE
  @image                       = UNSET_VALUE
  @image_family                = UNSET_VALUE
  @image_project_id            = UNSET_VALUE
  @instance_group              = UNSET_VALUE
  @machine_type                = UNSET_VALUE
  @disk_size                   = UNSET_VALUE
  @disk_name                   = UNSET_VALUE
  @disk_type                   = UNSET_VALUE
  @metadata                    = {}
  @name                        = UNSET_VALUE
  @network                     = UNSET_VALUE
  @network_project_id          = UNSET_VALUE
  @subnetwork                  = UNSET_VALUE
  @tags                        = []
  @labels                      = {}
  @can_ip_forward              = UNSET_VALUE
  @external_ip                 = UNSET_VALUE
  @network_ip                  = UNSET_VALUE
  @use_private_ip              = UNSET_VALUE
  @autodelete_disk             = UNSET_VALUE
  @preemptible                 = UNSET_VALUE
  @auto_restart                = UNSET_VALUE
  @on_host_maintenance         = UNSET_VALUE
  @instance_ready_timeout      = UNSET_VALUE
  @zone                        = UNSET_VALUE
  @scopes                      = UNSET_VALUE
  @service_accounts            = UNSET_VALUE
  @service_account             = UNSET_VALUE
  @additional_disks            = []
  @setup_winrm_password        = UNSET_VALUE
  @accelerators                = []
  @enable_secure_boot          = UNSET_VALUE
  @enable_display              = UNSET_VALUE
  @enable_vtpm                 = UNSET_VALUE
  @enable_integrity_monitoring = UNSET_VALUE

  # Internal state (prefix with __ so they aren't automatically
  # merged)
  @__compiled_zone_configs = {}
  @__finalized = false
  @__zone_config = {}
  @__zone_specific = zone_specific
end

Instance Attribute Details

#acceleratorsArray<Hash>

Accelerators

Returns:

  • (Array<Hash>)


189
190
191
# File 'lib/vagrant-google/config.rb', line 189

def accelerators
  @accelerators
end

#additional_disksArray<Hash>

The configuration for additional disks.

Returns:

  • (Array<Hash>)


179
180
181
# File 'lib/vagrant-google/config.rb', line 179

def additional_disks
  @additional_disks
end

#auto_restartObject

Availability policy whether to have instance restart on failures

Returns:

  • Boolean



140
141
142
# File 'lib/vagrant-google/config.rb', line 140

def auto_restart
  @auto_restart
end

#autodelete_diskObject

whether to autodelete disk on instance delete

Returns:

  • Boolean



128
129
130
# File 'lib/vagrant-google/config.rb', line 128

def autodelete_disk
  @autodelete_disk
end

#can_ip_forwardObject

whether to enable ip forwarding

Returns:

  • Boolean



108
109
110
# File 'lib/vagrant-google/config.rb', line 108

def can_ip_forward
  @can_ip_forward
end

#disk_nameString

The name of the disk to be used, it it exists, it will be reused, otherwise created.

Returns:

  • (String)


63
64
65
# File 'lib/vagrant-google/config.rb', line 63

def disk_name
  @disk_name
end

#disk_sizeInt

The size of disk in GB

Returns:

  • (Int)


58
59
60
# File 'lib/vagrant-google/config.rb', line 58

def disk_size
  @disk_size
end

#disk_typeString

The type of the disk to be used, such as “pd-standard”

Returns:

  • (String)


68
69
70
# File 'lib/vagrant-google/config.rb', line 68

def disk_type
  @disk_type
end

#enable_displayObject

whether the instance has a display enabled

Returns:

  • Boolean



199
200
201
# File 'lib/vagrant-google/config.rb', line 199

def enable_display
  @enable_display
end

#enable_integrity_monitoringObject

whether the instance has integrity monitoring enabled

Returns:

  • Boolean



209
210
211
# File 'lib/vagrant-google/config.rb', line 209

def enable_integrity_monitoring
  @enable_integrity_monitoring
end

#enable_secure_bootObject

whether the instance has Secure Boot enabled

Returns:

  • Boolean



194
195
196
# File 'lib/vagrant-google/config.rb', line 194

def enable_secure_boot
  @enable_secure_boot
end

#enable_vtpmObject

whether the instance has the vTPM enabled

Returns:

  • Boolean



204
205
206
# File 'lib/vagrant-google/config.rb', line 204

def enable_vtpm
  @enable_vtpm
end

#external_ipObject

The external IP Address to use

Returns:

  • String



113
114
115
# File 'lib/vagrant-google/config.rb', line 113

def external_ip
  @external_ip
end

#google_json_key_locationString

The path to the Service Account json-formatted private key

Returns:

  • (String)


23
24
25
# File 'lib/vagrant-google/config.rb', line 23

def google_json_key_location
  @google_json_key_location
end

#google_project_idString

The Google Cloud Project ID (not name or number)

Returns:

  • (String)


28
29
30
# File 'lib/vagrant-google/config.rb', line 28

def google_project_id
  @google_project_id
end

#imageString

The image name of the instance to use.

Returns:

  • (String)


33
34
35
# File 'lib/vagrant-google/config.rb', line 33

def image
  @image
end

#image_familyString

The image family of the instance to use.

Returns:

  • (String)


38
39
40
# File 'lib/vagrant-google/config.rb', line 38

def image_family
  @image_family
end

#image_project_idString

The name of the image_project_id

Returns:

  • (String)


43
44
45
# File 'lib/vagrant-google/config.rb', line 43

def image_project_id
  @image_project_id
end

#instance_groupString

The instance group name to put the instance in.

Returns:

  • (String)


48
49
50
# File 'lib/vagrant-google/config.rb', line 48

def instance_group
  @instance_group
end

#instance_ready_timeoutInt

The timeout value waiting for instance ready

Returns:

  • (Int)


153
154
155
# File 'lib/vagrant-google/config.rb', line 153

def instance_ready_timeout
  @instance_ready_timeout
end

#labelsHash<String, String>

Labels to apply to the instance

Returns:

  • (Hash<String, String>)


103
104
105
# File 'lib/vagrant-google/config.rb', line 103

def labels
  @labels
end

#machine_typeString

The type of machine to launch, such as “n1-standard-1”

Returns:

  • (String)


53
54
55
# File 'lib/vagrant-google/config.rb', line 53

def machine_type
  @machine_type
end

#metadataHash<String, String>

The user metadata string

Returns:

  • (Hash<String, String>)


73
74
75
# File 'lib/vagrant-google/config.rb', line 73

def 
  @metadata
end

#nameString

The name of the instance

Returns:

  • (String)


78
79
80
# File 'lib/vagrant-google/config.rb', line 78

def name
  @name
end

#networkString

The name of the network

Returns:

  • (String)


83
84
85
# File 'lib/vagrant-google/config.rb', line 83

def network
  @network
end

#network_ipObject

The network IP Address to use

Returns:

  • String



118
119
120
# File 'lib/vagrant-google/config.rb', line 118

def network_ip
  @network_ip
end

#network_project_idString

The name of the network_project_id

Returns:

  • (String)


88
89
90
# File 'lib/vagrant-google/config.rb', line 88

def network_project_id
  @network_project_id
end

#on_host_maintenanceObject

Availability policy specify what to do when infrastructure maintenance events occur Options: MIGRATE, TERMINATE The default is MIGRATE.

Returns:

  • String



148
149
150
# File 'lib/vagrant-google/config.rb', line 148

def on_host_maintenance
  @on_host_maintenance
end

#preemptibleObject

Availability policy whether to run instance as preemptible

Returns:

  • Boolean



134
135
136
# File 'lib/vagrant-google/config.rb', line 134

def preemptible
  @preemptible
end

#scopesArray

The list of access scopes for instance.

Returns:

  • (Array)


164
165
166
# File 'lib/vagrant-google/config.rb', line 164

def scopes
  @scopes
end

#service_accountString

IAM service account for instance.

Returns:

  • (String)


174
175
176
# File 'lib/vagrant-google/config.rb', line 174

def 
  @service_account
end

#service_accountsArray

Deprecated: the list of access scopes for instance.

Returns:

  • (Array)


169
170
171
# File 'lib/vagrant-google/config.rb', line 169

def service_accounts
  @service_accounts
end

#setup_winrm_passwordBoolean

(Optional - Override default WinRM setup before for Public Windows images)

Returns:

  • (Boolean)


184
185
186
# File 'lib/vagrant-google/config.rb', line 184

def setup_winrm_password
  @setup_winrm_password
end

#subnetworkString

The name of the subnetwork

Returns:

  • (String)


93
94
95
# File 'lib/vagrant-google/config.rb', line 93

def subnetwork
  @subnetwork
end

#tagsArray

Tags to apply to the instance

Returns:

  • (Array)


98
99
100
# File 'lib/vagrant-google/config.rb', line 98

def tags
  @tags
end

#use_private_ipObject

Use private ip address

Returns:

  • Boolean



123
124
125
# File 'lib/vagrant-google/config.rb', line 123

def use_private_ip
  @use_private_ip
end

#zoneString

The zone to launch the instance into. If nil and the “default” network is set use the default us-central1-f.

Returns:

  • (String)


159
160
161
# File 'lib/vagrant-google/config.rb', line 159

def zone
  @zone
end

Instance Method Details

#finalize!Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/vagrant-google/config.rb', line 329

def finalize! # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  # Try to get access keys from standard Google environment variables; they
  # will default to nil if the environment variables are not present.
  @google_json_key_location = ENV['GOOGLE_JSON_KEY_LOCATION'] if @google_json_key_location == UNSET_VALUE
  @google_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE

  # Default image is nil
  @image = nil if @image == UNSET_VALUE

  # Default image family is nil
  @image_family = nil if @image_family == UNSET_VALUE

  # Default image project is nil
  @image_project_id = nil if @image_project_id == UNSET_VALUE

  # Default instance group name is nil
  @instance_group = nil if @instance_group == UNSET_VALUE

  # Default instance type is an n1-standard-1
  @machine_type = "n1-standard-1" if @machine_type == UNSET_VALUE

  # Default disk size is 10 GB
  @disk_size = 10 if @disk_size == UNSET_VALUE

  # Default disk name is nil
  @disk_name = nil if @disk_name == UNSET_VALUE

  # Default disk type is pd-standard
  @disk_type = "pd-standard" if @disk_type == UNSET_VALUE

  # Instance name defaults to a new datetime value + random seed
  # e.g. i-2015081013-15637fdb
  if @name == UNSET_VALUE
    t = Time.now
    @name = "i-#{t.strftime("%Y%m%d%H")}-" + SecureRandom.hex(4)
  end

  # Network defaults to 'default'
  @network = "default" if @network == UNSET_VALUE

  # Network project id defaults to project_id
  @network_project_id = @google_project_id if @network_project_id == UNSET_VALUE

  # Subnetwork defaults to nil
  @subnetwork = nil if @subnetwork == UNSET_VALUE

  # Default zone is us-central1-f if using the default network
  if @zone == UNSET_VALUE
    @zone = nil
    if @network == "default"
      @zone = "us-central1-f"
    end
  end

  # autodelete_disk defaults to true
  @autodelete_disk = true if @autodelete_disk == UNSET_VALUE

  # can_ip_forward defaults to nil
  @can_ip_forward = nil if @can_ip_forward == UNSET_VALUE

  # external_ip defaults to nil
  @external_ip = nil if @external_ip == UNSET_VALUE

  # network_ip defaults to nil
  @network_ip = nil if @network_ip == UNSET_VALUE

  # use_private_ip defaults to false
  @use_private_ip = false if @use_private_ip == UNSET_VALUE

  # preemptible defaults to false
  @preemptible = false if @preemptible == UNSET_VALUE

  # auto_restart defaults to true
  @auto_restart = true if @auto_restart == UNSET_VALUE

  # on_host_maintenance defaults to MIGRATE
  @on_host_maintenance = "MIGRATE" if @on_host_maintenance == UNSET_VALUE

  # Default instance_ready_timeout
  @instance_ready_timeout = 20 if @instance_ready_timeout == UNSET_VALUE

  # Default access scopes
  @scopes = nil if @scopes == UNSET_VALUE

  # Default access scopes
  @service_accounts = nil if @service_accounts == UNSET_VALUE

  # Default IAM service account
  @service_account = nil if @service_account == UNSET_VALUE

  # Default Setup WinRM Password
  @setup_winrm_password = nil if @setup_winrm_password == UNSET_VALUE

  # Config option service_accounts is deprecated
  if @service_accounts
    @scopes = @service_accounts
  end

  # enable_secure_boot defaults to nil
  @enable_secure_boot = false if @enable_secure_boot == UNSET_VALUE

  # enable_display defaults to nil
  @enable_display = false if @enable_display == UNSET_VALUE

  # enable_vtpm defaults to nil
  @enable_vtpm = false if @enable_vtpm == UNSET_VALUE

  # enable_integrity_monitoring defaults to nil
  @enable_integrity_monitoring = false if @enable_integrity_monitoring == UNSET_VALUE

  # Compile our zone specific configurations only within
  # NON-zone-SPECIFIC configurations.
  unless @__zone_specific
    @__zone_config.each do |zone, blocks|
      config = self.class.new(true).merge(self)

      # Execute the configuration for each block
      blocks.each { |b| b.call(config) }

      # The zone name of the configuration always equals the
      # zone config name:
      config.zone = zone

      # Finalize the configuration
      config.finalize!

      # Store it for retrieval
      @__compiled_zone_configs[zone] = config
    end
  end

  # Mark that we finalized
  @__finalized = true
end

#get_zone_config(name) ⇒ Object

This gets the configuration for a specific zone. It shouldn’t be called by the general public and is only used internally.



512
513
514
515
516
517
518
519
# File 'lib/vagrant-google/config.rb', line 512

def get_zone_config(name)
  unless @__finalized
    raise "Configuration must be finalized before calling this method."
  end

  # Return the compiled zone config
  @__compiled_zone_configs[name] || self
end

#merge(other) ⇒ Object


Internal methods.




293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/vagrant-google/config.rb', line 293

def merge(other)
  super.tap do |result|
    # Copy over the zone specific flag. "True" is retained if either
    # has it.
    new_zone_specific = other.instance_variable_get(:@__zone_specific)
    result.instance_variable_set(
      :@__zone_specific, new_zone_specific || @__zone_specific
    )

    # Go through all the zone configs and prepend ours onto
    # theirs.
    new_zone_config = other.instance_variable_get(:@__zone_config)
    @__zone_config.each do |key, value|
      new_zone_config[key] ||= []
      new_zone_config[key] = value + new_zone_config[key]
    end

    # Set it
    result.instance_variable_set(:@__zone_config, new_zone_config)

    # Merge in the metadata
    result. = self..merge(other.)

    # Merge in the labels
    result.labels = self.labels.merge(other.labels)

    # Merge in the tags
    result.tags |= self.tags
    result.tags |= other.tags

    # Merge in the additional disks
    result.additional_disks |= self.additional_disks
    result.additional_disks |= other.additional_disks
  end
end

#validate(machine) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/vagrant-google/config.rb', line 464

def validate(machine)
  errors = _detected_errors

  errors << I18n.t("vagrant_google.config.zone_required") if @zone.nil?

  if @zone
    config = get_zone_config(@zone)

    # TODO: Check why provider-level settings are validated in the zone config
    errors << I18n.t("vagrant_google.config.google_project_id_required") if \
      config.google_project_id.nil?

    if config.google_json_key_location
      errors << I18n.t("vagrant_google.config.private_key_missing") unless \
        File.exist?(File.expand_path(config.google_json_key_location.to_s)) or
        File.exist?(File.expand_path(config.google_json_key_location.to_s, machine.env.root_path))
    end

    if config.preemptible
      errors << I18n.t("vagrant_google.config.auto_restart_invalid_on_preemptible") if \
       config.auto_restart
      errors << I18n.t("vagrant_google.config.on_host_maintenance_invalid_on_preemptible") unless \
       config.on_host_maintenance == "TERMINATE"
    end

    if config.image_family
      errors << I18n.t("vagrant_google.config.image_and_image_family_set") if \
       config.image
    end

    errors << I18n.t("vagrant_google.config.image_required") if config.image.nil? && config.image_family.nil?
    errors << I18n.t("vagrant_google.config.name_required") if @name.nil?

    if !config.accelerators.empty?
      errors << I18n.t("vagrant_google.config.on_host_maintenance_invalid_with_accelerators") unless \
        config.on_host_maintenance == "TERMINATE"
    end
  end

  if @service_accounts
    machine.env.ui.warn(I18n.t("vagrant_google.config.service_accounts_deprecaated"))
  end

  { "Google Provider" => errors }
end

#zone_config(zone, attributes = nil) {|config| ... } ⇒ Object

Allows zone-specific overrides of any of the settings on this configuration object. This allows the user to override things like image and machine type name for zones. Example:

google.zone_config "us-central1-f" do |zone|
  zone.image = "ubuntu-1604-xenial-v20180306"
  zone.machine_type = "n1-standard-4"
end

Parameters:

  • zone (String)

    The zone name to configure.

  • attributes (Hash) (defaults to: nil)

    Direct attributes to set on the configuration as a shortcut instead of specifying a full block.

Yields:

  • (config)

    Yields a new Google configuration.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/vagrant-google/config.rb', line 271

def zone_config(zone, attributes=nil, &block)
  # Append the block to the list of zone configs for that zone.
  # We'll evaluate these upon finalization.
  @__zone_config[zone] ||= []

  # Append a block that sets attributes if we got one
  if attributes
    attr_block = lambda do |config|
      config.set_options(attributes)
    end

    @__zone_config[zone] << attr_block
  end

  # Append a block if we got one
  @__zone_config[zone] << block if block_given?
end