Class: VagrantPlugins::Google::Config
- Inherits:
-
Object
- Object
- VagrantPlugins::Google::Config
- Defined in:
- lib/vagrant-google/config.rb
Instance Attribute Summary collapse
-
#google_client_email ⇒ String
The Service Account Client ID Email address.
-
#google_key_location ⇒ String
The path to the Service Account 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.
-
#instance_ready_timeout ⇒ Int
The timeout value waiting for instance ready.
-
#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.
-
#zone ⇒ String
The zone to launch the instance into.
Instance Method Summary collapse
- #finalize! ⇒ Object
-
#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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant-google/config.rb', line 76 def initialize(zone_specific=false) @google_client_email = UNSET_VALUE @google_key_location = UNSET_VALUE @google_project_id = UNSET_VALUE @image = UNSET_VALUE @machine_type = UNSET_VALUE @metadata = {} @name = UNSET_VALUE @network = UNSET_VALUE @instance_ready_timeout = UNSET_VALUE @zone = 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
#google_client_email ⇒ String
The Service Account Client ID Email address
22 23 24 |
# File 'lib/vagrant-google/config.rb', line 22 def google_client_email @google_client_email end |
#google_key_location ⇒ String
The path to the Service Account private key
27 28 29 |
# File 'lib/vagrant-google/config.rb', line 27 def google_key_location @google_key_location end |
#google_project_id ⇒ String
The Google Cloud Project ID (not name or number)
32 33 34 |
# File 'lib/vagrant-google/config.rb', line 32 def google_project_id @google_project_id end |
#image ⇒ String
The image name of the instance to use.
37 38 39 |
# File 'lib/vagrant-google/config.rb', line 37 def image @image end |
#instance_ready_timeout ⇒ Int
The timeout value waiting for instance ready
62 63 64 |
# File 'lib/vagrant-google/config.rb', line 62 def instance_ready_timeout @instance_ready_timeout end |
#machine_type ⇒ String
The type of machine to launch, such as “n1-standard-1”
42 43 44 |
# File 'lib/vagrant-google/config.rb', line 42 def machine_type @machine_type end |
#metadata ⇒ Hash<String, String>
The user metadata string
47 48 49 |
# File 'lib/vagrant-google/config.rb', line 47 def @metadata end |
#name ⇒ String
The name of the instance
52 53 54 |
# File 'lib/vagrant-google/config.rb', line 52 def name @name end |
#network ⇒ String
The name of the network
57 58 59 |
# File 'lib/vagrant-google/config.rb', line 57 def network @network end |
#zone ⇒ String
The zone to launch the instance into. If nil, it will use the default us-central1-a.
74 75 76 |
# File 'lib/vagrant-google/config.rb', line 74 def zone @zone end |
Instance Method Details
#finalize! ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/vagrant-google/config.rb', line 156 def finalize! # 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_project_id = ENV['GOOGLE_PROJECT_ID'] if @google_project_id == UNSET_VALUE # Image must be nil, since we can't default that @image = "debian-7-wheezy-v20140619" if @image == UNSET_VALUE # Default instance type is an n1-standard-1 @machine_type = "n1-standard-1" if @machine_type == UNSET_VALUE # Instance name defaults to a new datetime value (hour granularity) t = Time.now @name = "i-#{t.year}#{t.month.to_s.rjust(2,'0')}#{t.day.to_s.rjust(2,'0')}#{t.hour.to_s.rjust(2,'0')}" if @name == UNSET_VALUE # Network defaults to 'default' @network = "default" if @network == UNSET_VALUE # Default zone is us-central1-a. @zone = "us-central1-a" if @zone == UNSET_VALUE # Default instance_ready_timeout @instance_ready_timeout = 20 if @instance_ready_timeout == UNSET_VALUE # Compile our zone specific configurations only within # NON-zone-SPECIFIC configurations. if !@__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.
231 232 233 234 235 236 237 238 |
# File 'lib/vagrant-google/config.rb', line 231 def get_zone_config(name) if !@__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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/vagrant-google/config.rb', line 131 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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/vagrant-google/config.rb', line 207 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_key_location_required") if \ config.google_key_location.nil? 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-a" do |zone|
zone.image = "debian-7-wheezy-v20140619"
zone.machine_type = "n1-standard-4"
end
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/vagrant-google/config.rb', line 109 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 |