Sha256: 8a76f4b1f47acb7dd6db16dfaea90dbd812532bea5177af16853764c053fd764

Contents?: true

Size: 1.63 KB

Versions: 40

Compression:

Stored size: 1.63 KB

Contents

require "log4r"
require "json"

module VagrantPlugins
  module MCS
    module Action
      # This terminates the running instance.
      class TerminateInstance
        def initialize(app, env)
          @app    = app
          @logger = Log4r::Logger.new("vagrant_mcs::action::terminate_instance")
        end

        def call(env)
          server         = env[:mcs_compute].describe_instances(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_mcs.terminating"))
          #server.destroy
          env[:mcs_compute].terminate_instance(env[:machine].id)
          env[:machine].id = nil

          @app.call(env)
        end

        # Release an elastic IP address
        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[:mcs_compute].disassociate_address(nil,h['association_id'])
            env[:mcs_compute].release_address(h['allocation_id'])
          else
            env[:mcs_compute].disassociate_address(h['public_ip'])
            env[:mcs_compute].release_address(h['public_ip'])
          end
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
vagrant-mcs-0.8.39 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.38 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.37 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.36 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.35 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.34 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.33 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.32 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.31 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.30 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.29 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.28 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.27 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.26 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.25 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.24 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.23 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.22 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.21 lib/vagrant-mcs/action/terminate_instance.rb
vagrant-mcs-0.8.20 lib/vagrant-mcs/action/terminate_instance.rb