Class: VagrantPlugins::OpenStack::Action::DeleteServer

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-openstack-plugin/action/delete_server.rb

Overview

This deletes the running server, if there is one.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ DeleteServer

Returns a new instance of DeleteServer.



13
14
15
16
# File 'lib/vagrant-openstack-plugin/action/delete_server.rb', line 13

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

Instance Method Details

#call(env) ⇒ Object



18
19
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
# File 'lib/vagrant-openstack-plugin/action/delete_server.rb', line 18

def call(env)
  machine = env[:machine]
  id = machine.id || (env[:openstack_compute].servers.all( :name => machine.name ).length == 1 and
                      env[:openstack_compute].servers.all( :name => machine.name ).first.id)

  if id
    env[:ui].info(I18n.t("vagrant_openstack.deleting_server"))

    # TODO: Validate the fact that we get a server back from the API.
    server = env[:openstack_compute].servers.get(id)
    if server
      # get volumes before destroying server
      volumes = server.volume_attachments

      ip = server.floating_ip_address

      retryable(:on => Timeout::Error, :tries => 20) do
        # If we're interrupted don't worry about waiting
        next if env[:interrupted]

        begin
          server.destroy if server
          status = Timeout::timeout(10) {
            while server.reload
              sleep(1)
            end
          }
        rescue RuntimeError => e
          # If we don't have an error about a state transition, then
          # we just move on.
          raise if e.message !~ /should have transitioned/
          raise Errors::ServerNotDestroyed
        rescue Fog::Compute::OpenStack::NotFound
          # If we don't have a server anymore we should be done here just continue on
        end
      end

      if machine.provider_config.floating_ip_pool && machine.provider_config.floating_ip == ":auto"
        address = env[:openstack_compute].list_all_addresses.body["floating_ips"].find{|i| i["ip"] == ip}
        if address
          env[:openstack_compute].release_address(address["id"])
        end
      end

      env[:ui].info(I18n.t("vagrant_openstack.deleting_volumes"))
      volumes.each do |compute_volume|
        volume = env[:openstack_volume].volumes.get(compute_volume["id"])
        if volume
          env[:ui].info("Deleting volume: #{volume.display_name}")
          begin
            volume.destroy
          rescue Excon::Errors::Error => e
            raise Errors::VolumeBadState, :volume => volume.display_name, :state => e.message
          end
        end
      end
    end
  else
    env[:ui].info(I18n.t("vagrant_openstack.not_created"))
  end

  @app.call(env)
end