Class: VagrantPlugins::Google::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zone_specific = false) ⇒ Config

Returns a new instance of Config.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vagrant-google/config.rb', line 86

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
  @disk_size           = UNSET_VALUE
  @metadata            = {}
  @name                = UNSET_VALUE
  @network             = UNSET_VALUE
  @tags                = []
  @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

#disk_sizeInt

The size of disk in GB

Returns:

  • (Int)


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

def disk_size
  @disk_size
end

#google_client_emailString

The Service Account Client ID Email address

Returns:

  • (String)


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

def google_client_email
  @google_client_email
end

#google_key_locationString

The path to the Service Account private key

Returns:

  • (String)


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

def google_key_location
  @google_key_location
end

#google_project_idString

The Google Cloud Project ID (not name or number)

Returns:

  • (String)


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

def google_project_id
  @google_project_id
end

#imageString

The image name of the instance to use.

Returns:

  • (String)


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

def image
  @image
end

#instance_ready_timeoutInt

The timeout value waiting for instance ready

Returns:

  • (Int)


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

def instance_ready_timeout
  @instance_ready_timeout
end

#machine_typeString

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

Returns:

  • (String)


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

def machine_type
  @machine_type
end

#metadataHash<String, String>

The user metadata string

Returns:

  • (Hash<String, String>)


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

def 
  @metadata
end

#nameString

The name of the instance

Returns:

  • (String)


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

def name
  @name
end

#networkString

The name of the network

Returns:

  • (String)


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

def network
  @network
end

#tagsArray

Tags to apply to the instance

Returns:

  • (Array)


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

def tags
  @tags
end

#zoneString

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

Returns:

  • (String)


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

def zone
  @zone
end

Instance Method Details

#finalize!Object



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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/vagrant-google/config.rb', line 168

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-v20140926" if @image == 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

  # 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-f.
  @zone = "us-central1-f" 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.



246
247
248
249
250
251
252
253
# File 'lib/vagrant-google/config.rb', line 246

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.




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

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/vagrant-google/config.rb', line 222

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-f" do |zone|
  zone.image = "debian-7-wheezy-v20140926"
  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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vagrant-google/config.rb', line 121

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