Class: VagrantPlugins::AWS::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-aws/action/run_instance.rb

Overview

This runs the configured instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



15
16
17
18
# File 'lib/vagrant-aws/action/run_instance.rb', line 15

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_aws::action::run_instance")
end

Instance Method Details

#allows_ssh_port?(env, test_sec_groups, is_vpc) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/vagrant-aws/action/run_instance.rb', line 179

def allows_ssh_port?(env, test_sec_groups, is_vpc)
  port = 22 # TODO get ssh_info port
  test_sec_groups = [ "default" ] if test_sec_groups.empty? # AWS default security group
  # filter groups by name or group_id (vpc)
  groups = test_sec_groups.map do |tsg|
    env[:aws_compute].security_groups.all.select { |sg| tsg == (is_vpc ? sg.group_id : sg.name) }
  end.flatten
  # filter TCP rules
  rules = groups.map { |sg| sg.ip_permissions.select { |r| r["ipProtocol"] == "tcp" } }.flatten
  # test if any range includes port
  !rules.select { |r| (r["fromPort"]..r["toPort"]).include?(port) }.empty?
end

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vagrant-aws/action/run_instance.rb', line 20

def call(env)
  # Initialize metrics if they haven't been
  env[:metrics] ||= {}

  # Get the region we're going to booting up in
  region = env[:machine].provider_config.region

  # Get the configs
  region_config         = env[:machine].provider_config.get_region_config(region)
  ami                   = region_config.ami
  availability_zone     = region_config.availability_zone
  instance_type         = region_config.instance_type
  keypair               = region_config.keypair_name
  private_ip_address    = region_config.private_ip_address
  security_groups       = region_config.security_groups
  subnet_id             = region_config.subnet_id
  tags                  = region_config.tags
  user_data             = region_config.user_data
  block_device_mapping  = region_config.block_device_mapping
  elastic_ip            = region_config.elastic_ip
  terminate_on_shutdown = region_config.terminate_on_shutdown
  iam_instance_profile_arn  = region_config.iam_instance_profile_arn
  iam_instance_profile_name = region_config.iam_instance_profile_name

  # If there is no keypair then warn the user
  if !keypair
    env[:ui].warn(I18n.t("vagrant_aws.launch_no_keypair"))
  end

  # If there is a subnet ID then warn the user
  if subnet_id && !elastic_ip
    env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
  end

  # Launch!
  env[:ui].info(I18n.t("vagrant_aws.launching_instance"))
  env[:ui].info(" -- Type: #{instance_type}")
  env[:ui].info(" -- AMI: #{ami}")
  env[:ui].info(" -- Region: #{region}")
  env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
  env[:ui].info(" -- Keypair: #{keypair}") if keypair
  env[:ui].info(" -- Subnet ID: #{subnet_id}") if subnet_id
  env[:ui].info(" -- IAM Instance Profile ARN: #{iam_instance_profile_arn}") if iam_instance_profile_arn
  env[:ui].info(" -- IAM Instance Profile Name: #{iam_instance_profile_name}") if iam_instance_profile_name
  env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
  env[:ui].info(" -- Elastic IP: #{elastic_ip}") if elastic_ip
  env[:ui].info(" -- User Data: yes") if user_data
  env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?
  env[:ui].info(" -- User Data: #{user_data}") if user_data
  env[:ui].info(" -- Block Device Mapping: #{block_device_mapping}") if block_device_mapping
  env[:ui].info(" -- Terminate On Shutdown: #{terminate_on_shutdown}")

  options = {
    :availability_zone         => availability_zone,
    :flavor_id                 => instance_type,
    :image_id                  => ami,
    :key_name                  => keypair,
    :private_ip_address        => private_ip_address,
    :subnet_id                 => subnet_id,
    :iam_instance_profile_arn  => iam_instance_profile_arn,
    :iam_instance_profile_name => iam_instance_profile_name,
    :tags                      => tags,
    :user_data                 => user_data,
    :block_device_mapping      => block_device_mapping,
    :instance_initiated_shutdown_behavior => terminate_on_shutdown == true ? "terminate" : nil
  }
  if !security_groups.empty?
    security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
    options[security_group_key] = security_groups
  end

  begin
    env[:ui].warn(I18n.t("vagrant_aws.warn_ssh_access")) unless allows_ssh_port?(env, security_groups, subnet_id)

    server = env[:aws_compute].servers.create(options)
  rescue Fog::Compute::AWS::NotFound => e
    # Invalid subnet doesn't have its own error so we catch and
    # check the error message here.
    if e.message =~ /subnet ID/
      raise Errors::FogError,
        :message => "Subnet ID not found: #{subnet_id}"
    end

    raise
  rescue Fog::Compute::AWS::Error => e
    raise Errors::FogError, :message => e.message
  rescue Excon::Errors::HTTPStatusError => e
    raise Errors::InternalFogError,
      :error => e.message,
      :response => e.response.body
  end

  # Immediately save the ID since it is created at this point.
  env[:machine].id = server.id

  # Wait for the instance to be ready first
  env[:metrics]["instance_ready_time"] = Util::Timer.time do
    tries = region_config.instance_ready_timeout / 2

    env[:ui].info(I18n.t("vagrant_aws.waiting_for_ready"))
    begin
      retryable(:on => Fog::Errors::TimeoutError, :tries => tries) do
        # If we're interrupted don't worry about waiting
        next if env[:interrupted]

        # Wait for the server to be ready
        server.wait_for(2) { ready? }
      end
    rescue Fog::Errors::TimeoutError
      # Delete the instance
      terminate(env)

      # Notify the user
      raise Errors::InstanceReadyTimeout,
        timeout: region_config.instance_ready_timeout
    end
  end

  @logger.info("Time to instance ready: #{env[:metrics]["instance_ready_time"]}")

  # Allocate and associate an elastic IP if requested
  if elastic_ip
    domain = subnet_id ? 'vpc' : 'standard'
    do_elastic_ip(env, domain, server)
  end

  if !env[:interrupted]
    env[:metrics]["instance_ssh_time"] = Util::Timer.time do
      # Wait for SSH to be ready.
      env[:ui].info(I18n.t("vagrant_aws.waiting_for_ssh"))
      while true
        # If we're interrupted then just back out
        break if env[:interrupted]
        break if env[:machine].communicate.ready?
        sleep 2
      end
    end

    @logger.info("Time for SSH ready: #{env[:metrics]["instance_ssh_time"]}")

    # Ready and booted!
    env[:ui].info(I18n.t("vagrant_aws.ready"))
  end

  # Terminate the instance if we were interrupted
  terminate(env) if env[:interrupted]

  @app.call(env)
end

#do_elastic_ip(env, domain, server) ⇒ Object



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
# File 'lib/vagrant-aws/action/run_instance.rb', line 192

def do_elastic_ip(env, domain, server)
  begin
    allocation = env[:aws_compute].allocate_address(domain)
  rescue
    @logger.debug("Could not allocate Elastic IP.")
    terminate(env)
    raise Errors::FogError,
                    :message => "Could not allocate Elastic IP."
  end
  @logger.debug("Public IP #{allocation.body['publicIp']}")

  # Associate the address and save the metadata to a hash
  if domain == 'vpc'
    # VPC requires an allocation ID to assign an IP
    association = env[:aws_compute].associate_address(server.id, nil, nil, allocation.body['allocationId'])
    h = { :allocation_id => allocation.body['allocationId'], :association_id => association.body['associationId'], :public_ip => allocation.body['publicIp'] }
  else
    # Standard EC2 instances only need the allocated IP address
    association = env[:aws_compute].associate_address(server.id, allocation.body['publicIp'])
    h = { :public_ip => allocation.body['publicIp'] }
  end

  unless association.body['return']
    @logger.debug("Could not associate Elastic IP.")
    terminate(env)
    raise Errors::FogError,
                    :message => "Could not allocate Elastic IP."
  end

  # Save this IP to the data dir so it can be released when the instance is destroyed
  ip_file = env[:machine].data_dir.join('elastic_ip')
  ip_file.open('w+') do |f|
    f.write(h.to_json)
  end
end

#recover(env) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/vagrant-aws/action/run_instance.rb', line 170

def recover(env)
  return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)

  if env[:machine].provider.state.id != :not_created
    # Undo the import
    terminate(env)
  end
end

#terminate(env) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/vagrant-aws/action/run_instance.rb', line 228

def terminate(env)
  destroy_env = env.dup
  destroy_env.delete(:interrupted)
  destroy_env[:config_validate] = false
  destroy_env[:force_confirm_destroy] = true
  env[:action_runner].run(Action.action_destroy, destroy_env)
end