Class: VagrantPlugins::ProfitBricks::Action::DeleteServer

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-profitbricks/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.



10
11
12
13
# File 'lib/vagrant-profitbricks/action/delete_server.rb', line 10

def initialize(app, _env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_profitbricks::action::delete_server')
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
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
# File 'lib/vagrant-profitbricks/action/delete_server.rb', line 15

def call(env)
  compute = env[:profitbricks_compute]
  server_id = env[:machine].id
  datacenter_id = env[:machine].provider_config.datacenter_id
  config           = env[:machine].provider_config
  server = begin
             compute.servers.get(datacenter_id, server_id)
           rescue
             nil
           end
  if server.nil?
    @logger.warn "Unable to find server #{server_id}..."
    env[:ui].warn(I18n.t('vagrant_profitbricks.errors.server_not_found'))
    env[:machine].id = nil
  else
    @logger.info "Deleting server #{server_id}..."
    env[:ui].info(I18n.t('vagrant_profitbricks.deleting_server'))

    volumes = compute.list_attached_volumes(datacenter_id, server_id)
    nics = compute.get_all_nic(datacenter_id, server_id)
    if server.delete
      30.times do |_t|
        begin
          s = compute.servers.get(datacenter_id, server_id)
        rescue Excon::Error::NotFound
        end
        break unless s
        sleep 10
      end
    end

    nics.body['items'].each do |nic|
      if nic['properties']['lan'] != config.lan_id
        compute.delete_lan(datacenter_id, nic['properties']['lan'])
      end
    end

    volumes.body['items'].each do |volume|
      compute.delete_volume(datacenter_id, volume['id'])
    end

    env[:machine].id = nil
    @app.call(env)
  end
end