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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vagrant-google/config.rb', line 143

def initialize(zone_specific=false)
  @google_client_email = UNSET_VALUE
  @google_key_location = UNSET_VALUE
  @google_json_key_location = UNSET_VALUE
  @google_project_id   = UNSET_VALUE
  @image               = 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
  @tags                = []
  @can_ip_forward      = UNSET_VALUE
  @external_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
  @service_accounts    = 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

#auto_restartObject

Availability policy whether to have instance restart on failures

Returns:

  • Boolean



115
116
117
# File 'lib/vagrant-google/config.rb', line 115

def auto_restart
  @auto_restart
end

#autodelete_diskObject

whether to autodelete disk on instance delete

Returns:

  • Boolean



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

def autodelete_disk
  @autodelete_disk
end

#can_ip_forwardObject

whether to enable ip forwarding

Returns:

  • Boolean



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

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

#external_ipObject

The external IP Address to use

Returns:

  • String



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

def external_ip
  @external_ip
end

#google_client_emailString

The Service Account Client ID Email address

Returns:

  • (String)


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

def google_client_email
  @google_client_email
end

#google_json_key_locationString

The path to the Service Account json-formatted private key

Returns:

  • (String)


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

def google_json_key_location
  @google_json_key_location
end

#google_key_locationString

The path to the Service Account private key

Returns:

  • (String)


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

def google_key_location
  @google_key_location
end

#google_project_idString

The Google Cloud Project ID (not name or number)

Returns:

  • (String)


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

def google_project_id
  @google_project_id
end

#imageString

The image name of the instance to use.

Returns:

  • (String)


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

def image
  @image
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)


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

def instance_ready_timeout
  @instance_ready_timeout
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

#on_host_maintenanceObject

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

Returns:

  • String



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

def on_host_maintenance
  @on_host_maintenance
end

#preemptibleObject

Availability policy whether to run instance as preemptible

Returns:

  • Boolean



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

def preemptible
  @preemptible
end

#service_accountsArray Also known as: scopes

The list of access controls for service accounts.

Returns:

  • (Array)


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

def service_accounts
  @service_accounts
end

#tagsArray

Tags to apply to the instance

Returns:

  • (Array)


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

def tags
  @tags
end

#zoneString

The zone to launch the instance into. If nil, it will use the default us-central1-f.

Returns:

  • (String)


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

def zone
  @zone
end

Instance Method Details

#finalize!Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
# File 'lib/vagrant-google/config.rb', line 237

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_client_email = ENV['GOOGLE_CLIENT_EMAIL'] if @google_client_email == UNSET_VALUE
  @google_key_location = ENV['GOOGLE_KEY_LOCATION'] if @google_key_location == UNSET_VALUE
  @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

  # Image must be nil, since we can't default that
  @image = "debian-8-jessie-v20160511" if @image == 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

  # Default zone is us-central1-f.
  @zone = "us-central1-f" if @zone == UNSET_VALUE

  # 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

  # 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 service_accounts
  @service_accounts = nil if @service_accounts == 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.



360
361
362
363
364
365
366
367
# File 'lib/vagrant-google/config.rb', line 360

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.




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
# File 'lib/vagrant-google/config.rb', line 211

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..merge!(self.)
    result..merge!(other.)
  end
end

#validate(machine) ⇒ Object



324
325
326
327
328
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
# File 'lib/vagrant-google/config.rb', line 324

def validate(machine)
  errors = _detected_errors

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

  if @zone
    config = get_zone_config(@zone)

    errors << I18n.t("vagrant_google.config.google_project_id_required") if \
      config.google_project_id.nil?
    errors << I18n.t("vagrant_google.config.google_client_email_required") if \
      config.google_client_email.nil?
    errors << I18n.t("vagrant_google.config.google_duplicate_key_location") if \
      !config.google_key_location.nil? and !config.google_json_key_location.nil?
    errors << I18n.t("vagrant_google.config.google_key_location_required") if \
      config.google_key_location.nil? and config.google_json_key_location.nil?
    errors << I18n.t("vagrant_google.config.private_key_missing") unless \
      File.exist?(config.google_key_location.to_s) or \
      File.exist?(config.google_json_key_location.to_s)

    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
  end

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

  { "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 = "debian-7-wheezy-v20150127"
  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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/vagrant-google/config.rb', line 189

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