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.



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_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)


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

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>)


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

def 
  @metadata
end

#nameString

The name of the instance

Returns:

  • (String)


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

def name
  @name
end

#networkString

The name of the network

Returns:

  • (String)


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

def network
  @network
end

#zoneString

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

Returns:

  • (String)


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

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.



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.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