Class: VagrantPlugins::AWS::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region_specific = false) ⇒ Config

Returns a new instance of Config.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vagrant-aws/config.rb', line 90

def initialize(region_specific=false)
  @access_key_id      = UNSET_VALUE
  @ami                = UNSET_VALUE
  @availability_zone  = UNSET_VALUE
  @instance_type      = UNSET_VALUE
  @keypair_name       = UNSET_VALUE
  @private_ip_address = UNSET_VALUE
  @region             = UNSET_VALUE
  @endpoint           = UNSET_VALUE
  @version            = UNSET_VALUE
  @secret_access_key  = UNSET_VALUE
  @security_groups    = UNSET_VALUE
  @ssh_port           = UNSET_VALUE
  @ssh_private_key_path = UNSET_VALUE
  @ssh_username       = UNSET_VALUE
  @subnet_id          = UNSET_VALUE
  @tags               = {}

  # Internal state (prefix with __ so they aren't automatically
  # merged)
  @__compiled_region_configs = {}
  @__finalized = false
  @__region_config = {}
  @__region_specific = region_specific
end

Instance Attribute Details

#access_key_idString

The access key ID for accessing AWS.

Returns:

  • (String)


9
10
11
# File 'lib/vagrant-aws/config.rb', line 9

def access_key_id
  @access_key_id
end

#amiString

The ID of the AMI to use.

Returns:

  • (String)


14
15
16
# File 'lib/vagrant-aws/config.rb', line 14

def ami
  @ami
end

#availability_zoneString

The availability zone to launch the instance into. If nil, it will use the default for your account.

Returns:

  • (String)


20
21
22
# File 'lib/vagrant-aws/config.rb', line 20

def availability_zone
  @availability_zone
end

#endpointString

The EC2 endpoint to connect to

Returns:

  • (String)


45
46
47
# File 'lib/vagrant-aws/config.rb', line 45

def endpoint
  @endpoint
end

#instance_typeString

The type of instance to launch, such as “m1.small”

Returns:

  • (String)


25
26
27
# File 'lib/vagrant-aws/config.rb', line 25

def instance_type
  @instance_type
end

#keypair_nameString

The name of the keypair to use.

Returns:

  • (String)


30
31
32
# File 'lib/vagrant-aws/config.rb', line 30

def keypair_name
  @keypair_name
end

#private_ip_addressString

The private IP address to give this machine (VPC).

Returns:

  • (String)


35
36
37
# File 'lib/vagrant-aws/config.rb', line 35

def private_ip_address
  @private_ip_address
end

#regionString

The name of the AWS region in which to create the instance.

Returns:

  • (String)


40
41
42
# File 'lib/vagrant-aws/config.rb', line 40

def region
  @region
end

#secret_access_keyString

The secret access key for accessing AWS.

Returns:

  • (String)


55
56
57
# File 'lib/vagrant-aws/config.rb', line 55

def secret_access_key
  @secret_access_key
end

#security_groupsArray<String>

The security groups to set on the instance. For VPC this must be a list of IDs. For EC2, it can be either.

Returns:

  • (Array<String>)


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

def security_groups
  @security_groups
end

#ssh_portint

The SSH port used by the instance

Returns:

  • (int)


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

def ssh_port
  @ssh_port
end

#ssh_private_key_pathString

The path to the SSH private key to use with this EC2 instance. This overrides the ‘config.ssh.private_key_path` variable.

Returns:

  • (String)


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

def ssh_private_key_path
  @ssh_private_key_path
end

#ssh_usernameString

The SSH username to use with this EC2 instance. This overrides the ‘config.ssh.username` variable.

Returns:

  • (String)


78
79
80
# File 'lib/vagrant-aws/config.rb', line 78

def ssh_username
  @ssh_username
end

#subnet_idString

The subnet ID to launch the machine into (VPC).

Returns:

  • (String)


83
84
85
# File 'lib/vagrant-aws/config.rb', line 83

def subnet_id
  @subnet_id
end

#tagsHash<String, String>

The tags for the machine.

Returns:

  • (Hash<String, String>)


88
89
90
# File 'lib/vagrant-aws/config.rb', line 88

def tags
  @tags
end

#versionString

The version of the AWS api to use

Returns:

  • (String)


50
51
52
# File 'lib/vagrant-aws/config.rb', line 50

def version
  @version
end

Instance Method Details

#finalize!Object



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/vagrant-aws/config.rb', line 176

def finalize!
  # The access keys default to nil
  @access_key_id     = nil if @access_key_id     == UNSET_VALUE
  @secret_access_key = nil if @secret_access_key == UNSET_VALUE

  # AMI must be nil, since we can't default that
  @ami = nil if @ami == UNSET_VALUE

  # Default instance type is an m1.small
  @instance_type = "m1.small" if @instance_type == UNSET_VALUE

  # Keypair defaults to nil
  @keypair_name = nil if @keypair_name == UNSET_VALUE

  # Default the private IP to nil since VPC is not default
  @private_ip_address = nil if @private_ip_address == UNSET_VALUE

  # Default region is us-east-1. This is sensible because AWS
  # generally defaults to this as well.
  @region = "us-east-1" if @region == UNSET_VALUE
  @availability_zone = nil if @availability_zone == UNSET_VALUE
  @endpoint = nil if @endpoint == UNSET_VALUE
  @version = nil if @version == UNSET_VALUE

  # The security groups are empty by default.
  @security_groups = [] if @security_groups == UNSET_VALUE

  # The SSH values by default are nil, and the top-level config
  # `config.ssh` values are used.
  # The SSH port should be 22 if left unset.
  @ssh_port = 22 if @ssh_port == UNSET_VALUE
  @ssh_private_key_path = nil if @ssh_private_key_path == UNSET_VALUE
  @ssh_username = nil if @ssh_username == UNSET_VALUE

  # Subnet is nil by default otherwise we'd launch into VPC.
  @subnet_id = nil if @subnet_id == UNSET_VALUE

  # Compile our region specific configurations only within
  # NON-REGION-SPECIFIC configurations.
  if !@__region_specific
    @__region_config.each do |region, blocks|
      config = self.class.new(true).merge(self)

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

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

      # Finalize the configuration
      config.finalize!

      # Store it for retrieval
      @__compiled_region_configs[region] = config
    end
  end

  # Mark that we finalized
  @__finalized = true
end

#get_region_config(name) ⇒ Object

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



265
266
267
268
269
270
271
272
# File 'lib/vagrant-aws/config.rb', line 265

def get_region_config(name)
  if !@__finalized
    raise "Configuration must be finalized before calling this method."
  end

  # Return the compiled region config
  @__compiled_region_configs[name] || self
end

#merge(other) ⇒ Object


Internal methods.




151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vagrant-aws/config.rb', line 151

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

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

    # Set it
    result.instance_variable_set(:@__region_config, new_region_config)

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

#region_config(region, attributes = nil) {|config| ... } ⇒ Object

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

aws.region_config "us-east-1" do |region|
  region.ami = "ami-12345678"
  region.keypair_name = "company-east"
end

Parameters:

  • region (String)

    The region 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 AWS configuration.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/vagrant-aws/config.rb', line 129

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

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

    @__region_config[region] << attr_block
  end

  # Append a block if we got one
  @__region_config[region] << block if block_given?
end

#validate(machine) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/vagrant-aws/config.rb', line 238

def validate(machine)
  errors = []

  errors << I18n.t("vagrant_aws.config.region_required") if @region.nil?

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

    errors << I18n.t("vagrant_aws.config.access_key_id_required") if \
      config.access_key_id.nil?
    errors << I18n.t("vagrant_aws.config.secret_access_key_required") if \
      config.secret_access_key.nil?
    errors << I18n.t("vagrant_aws.config.ami_required") if config.ami.nil?

    if config.ssh_private_key_path && \
      !File.file?(File.expand_path(config.ssh_private_key_path, machine.env.root_path))
      errors << I18n.t("vagrant_aws.config.private_key_missing")
    end
  end

  { "AWS Provider" => errors }
end