Class: VagrantPlugins::AWS::Action::TerminateInstance

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

Overview

This terminates the running instance.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ TerminateInstance

Returns a new instance of TerminateInstance.



9
10
11
12
# File 'lib/vagrant-aws/action/terminate_instance.rb', line 9

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

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-aws/action/terminate_instance.rb', line 14

def call(env)
  server         = env[:aws_compute].servers.get(env[:machine].id)
  region         = env[:machine].provider_config.region
  region_config  = env[:machine].provider_config.get_region_config(region)

  elastic_ip     = region_config.elastic_ip

  # Release the elastic IP
  ip_file = env[:machine].data_dir.join('elastic_ip')
  if ip_file.file?
    release_address(env,ip_file.read)
    ip_file.delete
  end

  # Destroy the server and remove the tracking ID
  env[:ui].info(I18n.t("vagrant_aws.terminating"))
  server.destroy
  env[:machine].id = nil

  @app.call(env)
end

#release_address(env, eip) ⇒ Object

Release an elastic IP address



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-aws/action/terminate_instance.rb', line 37

def release_address(env,eip)
  h = JSON.parse(eip)
  # Use association_id and allocation_id for VPC, use public IP for EC2
  if h['association_id']
    env[:aws_compute].disassociate_address(nil,h['association_id'])
    env[:aws_compute].release_address(h['allocation_id'])
  else
    env[:aws_compute].disassociate_address(h['public_ip'])
    env[:aws_compute].release_address(h['public_ip'])
  end
end