Sha256: e9795a9cadc0230a143893f96fd1ce453bc08d97054d7b2b1e8b6391c7a9a4d3

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'log4r'

require 'vagrant-openstack-illuin-provider/action/abstract_action'

module VagrantPlugins
  module Openstack
    module Action
      # This deletes the running server, if there is one.
      class DeleteServer < AbstractAction
        def initialize(app, _env)
          @app    = app
          @logger = Log4r::Logger.new('vagrant_openstack::action::delete_server')
        end

        def execute(env)
          if env[:machine].id
            @logger.info "Deleting server #{env[:machine].id}..."
            env[:ui].info(I18n.t('vagrant_openstack.deleting_server'))
            env[:openstack_client].nova.delete_server(env, env[:machine].id)
            env[:openstack_client].nova.delete_keypair_if_vagrant(env, env[:machine].id)

            waiting_for_instance_to_be_deleted(env, env[:machine].id)

          end

          @app.call(env)
        end

        private

        def waiting_for_instance_to_be_deleted(env, instance_id, retry_interval = 3)
          @logger.info "Waiting for the instance with id #{instance_id} to be deleted..."
          env[:ui].info(I18n.t('vagrant_openstack.waiting_deleted'))
          config = env[:machine].provider_config
          Timeout.timeout(config.server_delete_timeout, Errors::Timeout) do
            delete_ok = false
            until delete_ok
              begin
                @logger.debug('Waiting for instance to be DELETED')
                server_status = env[:openstack_client].nova.get_server_details(env, instance_id)['status']
                fail Errors::ServerStatusError, server: instance_id if server_status == 'ERROR'
                break if server_status == 'DELETED'
                sleep retry_interval
              rescue Errors::InstanceNotFound
                delete_ok = true
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-openstack-illuin-provider-0.12.0 lib/vagrant-openstack-illuin-provider/action/delete_server.rb