Class: VagrantPlugins::Google::Config
- Inherits:
-
Object
- Object
- VagrantPlugins::Google::Config
- Defined in:
- lib/vagrant-google/config.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#accelerators ⇒ Array<Hash>
Accelerators.
-
#additional_disks ⇒ Array<Hash>
The configuration for additional disks.
-
#auto_restart ⇒ Object
Availability policy whether to have instance restart on failures.
-
#autodelete_disk ⇒ Object
whether to autodelete disk on instance delete.
-
#can_ip_forward ⇒ Object
whether to enable ip forwarding.
-
#disk_name ⇒ String
The name of the disk to be used, it it exists, it will be reused, otherwise created.
-
#disk_size ⇒ Int
The size of disk in GB.
-
#disk_type ⇒ String
The type of the disk to be used, such as “pd-standard”.
-
#enable_display ⇒ Object
whether the instance has a display enabled.
-
#enable_integrity_monitoring ⇒ Object
whether the instance has integrity monitoring enabled.
-
#enable_secure_boot ⇒ Object
whether the instance has Secure Boot enabled.
-
#enable_vtpm ⇒ Object
whether the instance has the vTPM enabled.
-
#external_ip ⇒ Object
The external IP Address to use.
-
#google_json_key_location ⇒ String
The path to the Service Account json-formatted private key.
-
#google_project_id ⇒ String
The Google Cloud Project ID (not name or number).
-
#image ⇒ String
The image name of the instance to use.
-
#image_family ⇒ String
The image family of the instance to use.
-
#image_project_id ⇒ String
The name of the image_project_id.
-
#instance_group ⇒ String
The instance group name to put the instance in.
-
#instance_ready_timeout ⇒ Int
The timeout value waiting for instance ready.
-
#labels ⇒ Hash<String, String>
Labels to apply to the instance.
-
#machine_type ⇒ String
The type of machine to launch, such as “n1-standard-1”.
-
#metadata ⇒ Hash<String, String>
The user metadata string.
-
#name ⇒ String
The name of the instance.
-
#network ⇒ String
The name of the network.
-
#network_ip ⇒ Object
The network IP Address to use.
-
#network_project_id ⇒ String
The name of the network_project_id.
-
#on_host_maintenance ⇒ Object
Availability policy specify what to do when infrastructure maintenance events occur Options: MIGRATE, TERMINATE The default is MIGRATE.
-
#preemptible ⇒ Object
Availability policy whether to run instance as preemptible.
-
#scopes ⇒ Array
The list of access scopes for instance.
-
#service_account ⇒ String
IAM service account for instance.
-
#service_accounts ⇒ Array
Deprecated: the list of access scopes for instance.
-
#setup_winrm_password ⇒ Boolean
(Optional - Override default WinRM setup before for Public Windows images).
-
#subnetwork ⇒ String
The name of the subnetwork.
-
#tags ⇒ Array
Tags to apply to the instance.
-
#use_private_ip ⇒ Object
Use private ip address.
-
#zone ⇒ String
The zone to launch the instance into.
Instance Method Summary collapse
-
#finalize! ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#get_zone_config(name) ⇒ Object
This gets the configuration for a specific zone.
-
#initialize(zone_specific = false) ⇒ Config
constructor
A new instance of Config.
-
#merge(other) ⇒ Object
——————————————————————- Internal methods.
- #validate(machine) ⇒ Object
-
#zone_config(zone, attributes = nil) {|config| ... } ⇒ Object
Allows zone-specific overrides of any of the settings on this configuration object.
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
#accelerators ⇒ Array<Hash>
Accelerators
189 190 191 |
# File 'lib/vagrant-google/config.rb', line 189 def accelerators @accelerators end |
#additional_disks ⇒ Array<Hash>
The configuration for additional disks.
179 180 181 |
# File 'lib/vagrant-google/config.rb', line 179 def additional_disks @additional_disks end |
#auto_restart ⇒ Object
Availability policy whether to have instance restart on failures
140 141 142 |
# File 'lib/vagrant-google/config.rb', line 140 def auto_restart @auto_restart end |
#autodelete_disk ⇒ Object
whether to autodelete disk on instance delete
128 129 130 |
# File 'lib/vagrant-google/config.rb', line 128 def autodelete_disk @autodelete_disk end |
#can_ip_forward ⇒ Object
whether to enable ip forwarding
108 109 110 |
# File 'lib/vagrant-google/config.rb', line 108 def can_ip_forward @can_ip_forward end |
#disk_name ⇒ String
The name of the disk to be used, it it exists, it will be reused, otherwise created.
63 64 65 |
# File 'lib/vagrant-google/config.rb', line 63 def disk_name @disk_name end |
#disk_size ⇒ Int
The size of disk in GB
58 59 60 |
# File 'lib/vagrant-google/config.rb', line 58 def disk_size @disk_size end |
#disk_type ⇒ String
The type of the disk to be used, such as “pd-standard”
68 69 70 |
# File 'lib/vagrant-google/config.rb', line 68 def disk_type @disk_type end |
#enable_display ⇒ Object
whether the instance has a display enabled
199 200 201 |
# File 'lib/vagrant-google/config.rb', line 199 def enable_display @enable_display end |
#enable_integrity_monitoring ⇒ Object
whether the instance has integrity monitoring enabled
209 210 211 |
# File 'lib/vagrant-google/config.rb', line 209 def enable_integrity_monitoring @enable_integrity_monitoring end |
#enable_secure_boot ⇒ Object
whether the instance has Secure Boot enabled
194 195 196 |
# File 'lib/vagrant-google/config.rb', line 194 def enable_secure_boot @enable_secure_boot end |
#enable_vtpm ⇒ Object
whether the instance has the vTPM enabled
204 205 206 |
# File 'lib/vagrant-google/config.rb', line 204 def enable_vtpm @enable_vtpm end |
#external_ip ⇒ Object
The external IP Address to use
113 114 115 |
# File 'lib/vagrant-google/config.rb', line 113 def external_ip @external_ip end |
#google_json_key_location ⇒ String
The path to the Service Account json-formatted private key
23 24 25 |
# File 'lib/vagrant-google/config.rb', line 23 def google_json_key_location @google_json_key_location end |
#google_project_id ⇒ String
The Google Cloud Project ID (not name or number)
28 29 30 |
# File 'lib/vagrant-google/config.rb', line 28 def google_project_id @google_project_id end |
#image ⇒ String
The image name of the instance to use.
33 34 35 |
# File 'lib/vagrant-google/config.rb', line 33 def image @image end |
#image_family ⇒ String
The image family of the instance to use.
38 39 40 |
# File 'lib/vagrant-google/config.rb', line 38 def image_family @image_family end |
#image_project_id ⇒ String
The name of the image_project_id
43 44 45 |
# File 'lib/vagrant-google/config.rb', line 43 def image_project_id @image_project_id end |
#instance_group ⇒ String
The instance group name to put the instance in.
48 49 50 |
# File 'lib/vagrant-google/config.rb', line 48 def instance_group @instance_group end |
#instance_ready_timeout ⇒ Int
The timeout value waiting for instance ready
153 154 155 |
# File 'lib/vagrant-google/config.rb', line 153 def instance_ready_timeout @instance_ready_timeout end |
#labels ⇒ Hash<String, String>
Labels to apply to the instance
103 104 105 |
# File 'lib/vagrant-google/config.rb', line 103 def labels @labels end |
#machine_type ⇒ String
The type of machine to launch, such as “n1-standard-1”
53 54 55 |
# File 'lib/vagrant-google/config.rb', line 53 def machine_type @machine_type end |
#metadata ⇒ Hash<String, String>
The user metadata string
73 74 75 |
# File 'lib/vagrant-google/config.rb', line 73 def @metadata end |
#name ⇒ String
The name of the instance
78 79 80 |
# File 'lib/vagrant-google/config.rb', line 78 def name @name end |
#network ⇒ String
The name of the network
83 84 85 |
# File 'lib/vagrant-google/config.rb', line 83 def network @network end |
#network_ip ⇒ Object
The network IP Address to use
118 119 120 |
# File 'lib/vagrant-google/config.rb', line 118 def network_ip @network_ip end |
#network_project_id ⇒ String
The name of the network_project_id
88 89 90 |
# File 'lib/vagrant-google/config.rb', line 88 def network_project_id @network_project_id end |
#on_host_maintenance ⇒ Object
Availability policy specify what to do when infrastructure maintenance events occur Options: MIGRATE, TERMINATE The default is MIGRATE.
148 149 150 |
# File 'lib/vagrant-google/config.rb', line 148 def on_host_maintenance @on_host_maintenance end |
#preemptible ⇒ Object
Availability policy whether to run instance as preemptible
134 135 136 |
# File 'lib/vagrant-google/config.rb', line 134 def preemptible @preemptible end |
#scopes ⇒ Array
The list of access scopes for instance.
164 165 166 |
# File 'lib/vagrant-google/config.rb', line 164 def scopes @scopes end |
#service_account ⇒ String
IAM service account for instance.
174 175 176 |
# File 'lib/vagrant-google/config.rb', line 174 def service_account @service_account end |
#service_accounts ⇒ Array
Deprecated: the list of access scopes for instance.
169 170 171 |
# File 'lib/vagrant-google/config.rb', line 169 def service_accounts @service_accounts end |
#setup_winrm_password ⇒ Boolean
(Optional - Override default WinRM setup before for Public Windows images)
184 185 186 |
# File 'lib/vagrant-google/config.rb', line 184 def setup_winrm_password @setup_winrm_password end |
#subnetwork ⇒ String
The name of the subnetwork
93 94 95 |
# File 'lib/vagrant-google/config.rb', line 93 def subnetwork @subnetwork end |
#tags ⇒ Array
Tags to apply to the instance
98 99 100 |
# File 'lib/vagrant-google/config.rb', line 98 def @tags end |
#use_private_ip ⇒ Object
Use private ip address
123 124 125 |
# File 'lib/vagrant-google/config.rb', line 123 def use_private_ip @use_private_ip end |
#zone ⇒ String
The zone to launch the instance into. If nil and the “default” network is set use the default us-central1-f.
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. |= self. result. |= other. # 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.(config.google_json_key_location.to_s)) or File.exist?(File.(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
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.(attributes) end @__zone_config[zone] << attr_block end # Append a block if we got one @__zone_config[zone] << block if block_given? end |