Class: VagrantPlugins::Cosmic::Config

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

Constant Summary collapse

INSTANCE_VAR_DEFAULT_NIL =
%w(affinity_group_id
affinity_group_name
display_name
domain_id
group
host
keypair
name
network_id
network_name
path
pf_ip_address
pf_ip_address_id
pf_private_port
pf_public_port
pf_public_rdp_port
pf_trusted_networks
port
private_ip_address
project_id
service_offering_id
service_offering_name
ssh_key
ssh_network_id
ssh_network_name
ssh_user
template_id
template_name
user_data
vm_password
vm_user
zone_id
zone_name).freeze
INSTANCE_VAR_DEFAULT_EMPTY_ARRAY =
%w(static_nat port_forwarding_rules firewall_rules).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_specific = false) ⇒ Config

Returns a new instance of Config.



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

def initialize(domain_specific = false)
  # Initialize groups in bulk, re-use these groups to set defaults in bulk
  INSTANCE_VAR_DEFAULT_NIL.each do |instance_variable|
    instance_variable_set("@#{instance_variable}", UNSET_VALUE)
  end
  # Initialize groups in bulk, re-use these groups to set defaults in bulk
  INSTANCE_VAR_DEFAULT_EMPTY_ARRAY.each do |instance_variable|
    instance_variable_set("@#{instance_variable}", UNSET_VALUE)
  end

  @scheme                     = UNSET_VALUE
  @api_key                    = UNSET_VALUE
  @secret_key                 = UNSET_VALUE
  @instance_ready_timeout     = UNSET_VALUE
  @network_type               = UNSET_VALUE
  @pf_private_rdp_port        = UNSET_VALUE
  @pf_public_port_randomrange = UNSET_VALUE
  @pf_open_firewall           = UNSET_VALUE
  @expunge_on_destroy         = UNSET_VALUE

  # Internal state (prefix with __ so they aren't automatically
  # merged)
  @__compiled_domain_configs = {}
  @__finalized               = false
  @__domain_config           = {}
  @__domain_specific         = domain_specific
end

Instance Attribute Details

#affinity_group_idString

Affinity group ID(s) the instance should be applied to

Returns:

  • (String)


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

def affinity_group_id
  @affinity_group_id
end

#affinity_group_nameString

Affinity group name(s) the instance should be applied to

Returns:

  • (String)


89
90
91
# File 'lib/vagrant-cosmic/config.rb', line 89

def affinity_group_name
  @affinity_group_name
end

#api_keyString

The API key for accessing Cosmic.

Returns:

  • (String)


66
67
68
# File 'lib/vagrant-cosmic/config.rb', line 66

def api_key
  @api_key
end

#disk_offering_idString

Disk offering uuid to use for the instance

Returns:

  • (String)


94
95
96
# File 'lib/vagrant-cosmic/config.rb', line 94

def disk_offering_id
  @disk_offering_id
end

#disk_offering_nameString

Disk offering name to use for the instance

Returns:

  • (String)


99
100
101
# File 'lib/vagrant-cosmic/config.rb', line 99

def disk_offering_name
  @disk_offering_name
end

#display_nameString

display name for the instance

Returns:

  • (String)


104
105
106
# File 'lib/vagrant-cosmic/config.rb', line 104

def display_name
  @display_name
end

#domain_idString

Domain id to launch the instance into.

Returns:

  • (String)


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

def domain_id
  @domain_id
end

#expunge_on_destroyBoolean

flag to enable/disable expunge vm on destroy

Returns:

  • (Boolean)


114
115
116
# File 'lib/vagrant-cosmic/config.rb', line 114

def expunge_on_destroy
  @expunge_on_destroy
end

#firewall_rulesArray

comma separated list of firewall rules (hash with rule parameters)

Returns:

  • (Array)


234
235
236
# File 'lib/vagrant-cosmic/config.rb', line 234

def firewall_rules
  @firewall_rules
end

#groupString

group for the instance

Returns:

  • (String)


119
120
121
# File 'lib/vagrant-cosmic/config.rb', line 119

def group
  @group
end

#hostString

Cosmic API host.

Returns:

  • (String)


46
47
48
# File 'lib/vagrant-cosmic/config.rb', line 46

def host
  @host
end

#instance_ready_timeoutFixnum

The timeout to wait for an instance to become ready.

Returns:

  • (Fixnum)


124
125
126
# File 'lib/vagrant-cosmic/config.rb', line 124

def instance_ready_timeout
  @instance_ready_timeout
end

#keypairString

The name of the keypair to use.

Returns:

  • (String)


129
130
131
# File 'lib/vagrant-cosmic/config.rb', line 129

def keypair
  @keypair
end

#nameString

Hostname for the machine instance This will be passed through to the api.

Returns:

  • (String)


79
80
81
# File 'lib/vagrant-cosmic/config.rb', line 79

def name
  @name
end

#network_idString, Array

Network uuid(s) that the instance should use

Returns:

  • (String, Array)


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

def network_id
  @network_id
end

#network_nameString, Array

Network name(s) that the instance should use

Returns:

  • (String, Array)


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

def network_name
  @network_name
end

#network_typeString

Network Type

Returns:

  • (String)


144
145
146
# File 'lib/vagrant-cosmic/config.rb', line 144

def network_type
  @network_type
end

#pathString

Cosmic API path.

Returns:

  • (String)


51
52
53
# File 'lib/vagrant-cosmic/config.rb', line 51

def path
  @path
end

#pf_ip_addressString

IP address to use for port forwarding rule

Returns:

  • (String)


251
252
253
# File 'lib/vagrant-cosmic/config.rb', line 251

def pf_ip_address
  @pf_ip_address
end

#pf_ip_address_idString

IP address id to use for port forwarding rule

Returns:

  • (String)


246
247
248
# File 'lib/vagrant-cosmic/config.rb', line 246

def pf_ip_address_id
  @pf_ip_address_id
end

#pf_open_firewallBoolean

flag to enable/disable automatic open firewall rule

Returns:

  • (Boolean)


239
240
241
# File 'lib/vagrant-cosmic/config.rb', line 239

def pf_open_firewall
  @pf_open_firewall
end

#pf_private_portString

private port to use for port forwarding rule

Returns:

  • (String)


276
277
278
# File 'lib/vagrant-cosmic/config.rb', line 276

def pf_private_port
  @pf_private_port
end

#pf_private_rdp_portString

private port to use for port forwarding rule

Returns:

  • (String)


266
267
268
# File 'lib/vagrant-cosmic/config.rb', line 266

def pf_private_rdp_port
  @pf_private_rdp_port
end

#pf_public_portString

public port to use for port forwarding rule

Returns:

  • (String)


256
257
258
# File 'lib/vagrant-cosmic/config.rb', line 256

def pf_public_port
  @pf_public_port
end

#pf_public_port_randomrangeRange

public port to use for port forwarding rule

Returns:

  • (Range)


271
272
273
# File 'lib/vagrant-cosmic/config.rb', line 271

def pf_public_port_randomrange
  @pf_public_port_randomrange
end

#pf_public_rdp_portString

public port to use for port forwarding rule

Returns:

  • (String)


261
262
263
# File 'lib/vagrant-cosmic/config.rb', line 261

def pf_public_rdp_port
  @pf_public_rdp_port
end

#pf_trusted_networksString

CIDR List string of trusted networks

Returns:

  • (String)


281
282
283
# File 'lib/vagrant-cosmic/config.rb', line 281

def pf_trusted_networks
  @pf_trusted_networks
end

#portString

Cosmic API port.

Returns:

  • (String)


56
57
58
# File 'lib/vagrant-cosmic/config.rb', line 56

def port
  @port
end

#port_forwarding_rulesArray

comma separated list of port forwarding rules (hash with rule parameters)

Returns:

  • (Array)


287
288
289
# File 'lib/vagrant-cosmic/config.rb', line 287

def port_forwarding_rules
  @port_forwarding_rules
end

#private_ip_addressString

Private ip for the instance

Returns:

  • (String)


149
150
151
# File 'lib/vagrant-cosmic/config.rb', line 149

def private_ip_address
  @private_ip_address
end

#project_idString

Project uuid that the instance should belong to

Returns:

  • (String)


154
155
156
# File 'lib/vagrant-cosmic/config.rb', line 154

def project_id
  @project_id
end

#schemeString

Cosmic API scheme

Returns:

  • (String)


61
62
63
# File 'lib/vagrant-cosmic/config.rb', line 61

def scheme
  @scheme
end

#secret_keyString

The secret key for accessing Cosmic.

Returns:

  • (String)


71
72
73
# File 'lib/vagrant-cosmic/config.rb', line 71

def secret_key
  @secret_key
end

#service_offering_idString

Service offering uuid to use for the instance

Returns:

  • (String)


159
160
161
# File 'lib/vagrant-cosmic/config.rb', line 159

def service_offering_id
  @service_offering_id
end

#service_offering_nameString

Service offering name to use for the instance

Returns:

  • (String)


164
165
166
# File 'lib/vagrant-cosmic/config.rb', line 164

def service_offering_name
  @service_offering_name
end

#ssh_keyString

The key to be used when loging in to the vm via ssh

Returns:

  • (String)


169
170
171
# File 'lib/vagrant-cosmic/config.rb', line 169

def ssh_key
  @ssh_key
end

#ssh_network_idString

The network_id to be used when loging in to the vm via ssh

Returns:

  • (String)


174
175
176
# File 'lib/vagrant-cosmic/config.rb', line 174

def ssh_network_id
  @ssh_network_id
end

#ssh_network_nameString

The network_name to be used when loging in to the vm via ssh

Returns:

  • (String)


179
180
181
# File 'lib/vagrant-cosmic/config.rb', line 179

def ssh_network_name
  @ssh_network_name
end

#ssh_userString

The username to be used when loging in to the vm via ssh

Returns:

  • (String)


184
185
186
# File 'lib/vagrant-cosmic/config.rb', line 184

def ssh_user
  @ssh_user
end

#static_natString

Paramters for Static NAT

Returns:

  • (String)


189
190
191
# File 'lib/vagrant-cosmic/config.rb', line 189

def static_nat
  @static_nat
end

#template_idString

Template uuid to use for the instance

Returns:

  • (String)


194
195
196
# File 'lib/vagrant-cosmic/config.rb', line 194

def template_id
  @template_id
end

#template_nameString

Template name to use for the instance

Returns:

  • (String)


199
200
201
# File 'lib/vagrant-cosmic/config.rb', line 199

def template_name
  @template_name
end

#user_dataString

The user data string

Returns:

  • (String)


204
205
206
# File 'lib/vagrant-cosmic/config.rb', line 204

def user_data
  @user_data
end

#vm_passwordString

The username to be used when loging in to the vm

Returns:

  • (String)


209
210
211
# File 'lib/vagrant-cosmic/config.rb', line 209

def vm_password
  @vm_password
end

#vm_userString

The username to be used when loging in to the vm

Returns:

  • (String)


214
215
216
# File 'lib/vagrant-cosmic/config.rb', line 214

def vm_user
  @vm_user
end

#zone_idString

Zone uuid to launch the instance into. If nil, it will launch in default project.

Returns:

  • (String)


220
221
222
# File 'lib/vagrant-cosmic/config.rb', line 220

def zone_id
  @zone_id
end

#zone_nameString

Zone name to launch the instance into. If nil, it will launch in default project.

Returns:

  • (String)


226
227
228
# File 'lib/vagrant-cosmic/config.rb', line 226

def zone_name
  @zone_name
end

Instance Method Details

#domain_config(domain, attributes = nil) {|config| ... } ⇒ Object

Allows domain-specific overrides of any of the settings on this configuration object. This allows the user to override things like template and keypair name for domains. Example:

cosmic.domain_config "abcd-ef01-2345-6789" do |domain|
  domain.template_id = "1234-5678-90ab-cdef"
  domain.keypair_name = "company-east"
end

Parameters:

  • domain (String)

    The Domain 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 domain configuration.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/vagrant-cosmic/config.rb', line 330

def domain_config(domain, attributes=nil, &block)
  # Append the block to the list of domain configs for that domain.
  # We'll evaluate these upon finalization.
  @__domain_config[domain] ||= []

  # Append a block that sets attributes if we got one
  if attributes
    attr_block = lambda do |config|
      config.set_options(attributes)
    end

    @__domain_config[domain] << attr_block
  end

  # Append a block if we got one
  @__domain_config[domain] << block if block_given?
end

#finalize!Object



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

def finalize!
  INSTANCE_VAR_DEFAULT_NIL.each do |instance_variable|
    # ... must be nil, since we can't default that
    instance_variable_set("@#{instance_variable}", nil) if
        instance_variable_get("@#{instance_variable}") == UNSET_VALUE
  end

  INSTANCE_VAR_DEFAULT_EMPTY_ARRAY.each do |instance_variable|
    # ... must be empty array
    instance_variable_set("@#{instance_variable}", []) if
        instance_variable_get("@#{instance_variable}") == UNSET_VALUE
  end

  # We default the scheme to whatever the user has specifid in the .fog file
  # *OR* whatever is default for the provider in the fog library
  @scheme                 = nil if @scheme == UNSET_VALUE

  # Try to get access keys from environment variables, they will
  # default to nil if the environment variables are not present
  @api_key                = ENV['COSMIC_API_KEY'] if @api_key == UNSET_VALUE
  @secret_key             = ENV['COSMIC_SECRET_KEY'] if @secret_key == UNSET_VALUE

  # Set the default timeout for waiting for an instance to be ready
  @instance_ready_timeout = 120 if @instance_ready_timeout == UNSET_VALUE

  # NetworkType is 'Advanced' by default
  @network_type           = "Advanced" if @network_type == UNSET_VALUE

  # Private rdp port defaults to 3389
  @pf_private_rdp_port     = 3389 if @pf_private_rdp_port == UNSET_VALUE

  # Public port random-range, default to rfc6335 'Dynamic Ports'; "(never assigned)"
  @pf_public_port_randomrange = {:start=>49152, :end=>65535} if @pf_public_port_randomrange == UNSET_VALUE

  # Open firewall is true by default (for backwards compatibility)
  @pf_open_firewall       = true if @pf_open_firewall == UNSET_VALUE

  # expunge on destroy is nil by default
  @expunge_on_destroy     = false if @expunge_on_destroy == UNSET_VALUE

  # Compile our domain specific configurations only within
  # NON-DOMAIN-SPECIFIC configurations.
  unless @__domain_specific
    @__domain_config.each do |domain, blocks|
      config = self.class.new(true).merge(self)

      # Execute the configuration for each block
      blocks.each { |b| b.call(config) }

      # The domain name of the configuration always equals the domain config name:
      config.domain = domain

      # Finalize the configuration
      config.finalize!

      # Store it for retrieval
      @__compiled_domain_configs[domain] = config
    end
  end

  # Mark that we finalized
  @__finalized = true
end

#get_domain_config(name) ⇒ Object

This gets the configuration for a specific domain. It shouldn’t be called by the general public and is only used internally.



461
462
463
464
465
466
# File 'lib/vagrant-cosmic/config.rb', line 461

def get_domain_config(name)
  raise 'Configuration must be finalized before calling this method.' unless @__finalized

  # Return the compiled domain config
  @__compiled_domain_configs[name] || self
end

#merge(other) ⇒ Object


Internal methods.




352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/vagrant-cosmic/config.rb', line 352

def merge(other)
  super.tap do |result|
    # Copy over the domain specific flag. "True" is retained if either
    # has it.
    new_domain_specific = other.instance_variable_get(:@__domain_specific)
    result.instance_variable_set(
        :@__domain_specific, new_domain_specific || @__domain_specific)

    # Go through all the domain configs and prepend ours onto
    # theirs.
    new_domain_config = other.instance_variable_get(:@__domain_config)
    @__domain_config.each do |key, value|
      new_domain_config[key] ||= []
      new_domain_config[key] = value + new_domain_config[key]
    end

    # Set it
    result.instance_variable_set(:@__domain_config, new_domain_config)

    # Merge in the tags
    result.tags.merge!(self.tags)
    result.tags.merge!(other.tags)
  end
end

#validate(machine) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/vagrant-cosmic/config.rb', line 441

def validate(machine)
  errors = []

  if @domain
    # Get the configuration for the domain we're using and validate only that domain.
    config = get_domain_config(@domain)

    unless config.use_fog_profile
      errors << I18n.t("vagrant_cosmic.config.api_key_required") if \
         config.access_key_id.nil?
      errors << I18n.t("vagrant_cosmic.config.secret_key_required") if \
         config.secret_access_key.nil?
    end
  end

  {"Cosmic Provider" => errors}
end