Class: Chef::Knife::TerremarkServerDelete

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/terremark_server_delete.rb

Instance Method Summary collapse

Instance Method Details

#hObject



30
31
32
# File 'lib/chef/knife/terremark_server_delete.rb', line 30

def h
  @highline ||= HighLine.new
end

#runObject



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
81
82
# File 'lib/chef/knife/terremark_server_delete.rb', line 34

def run 
  terremark = Fog::Terremark.new(
    :terremark_username => Chef::Config[:knife][:terremark_username],
    :terremark_password => Chef::Config[:knife][:terremark_password],
    :terremark_service  => Chef::Config[:knife][:terremark_service] || :vcloud
  )

  $stdout.sync = true

  vapp_id = terremark.servers.detect {|server| server.name == @name_args[0]}.id
  confirm("Do you really want to delete server ID #{vapp_id} named #{@name_args[0]}")

  puts "Cleaning up internet services..."
  private_ip = terremark.servers.get(vapp_id).ip_address
  internet_services = terremark.get_internet_services(terremark.default_vdc_id).body['InternetServices']
  public_ip_usage = {}
  internet_services.each do |internet_service|
    public_ip_address = internet_service['PublicIpAddress']['Name']
    public_ip_usage[public_ip_address] ||= []
    public_ip_usage[public_ip_address] << internet_service['Id']
  end
  internet_services.each do |internet_service|
    node_services = terremark.get_node_services(internet_service['Id']).body['NodeServices']
    node_services.delete_if do |node_service|
      if node_service['IpAddress'] == private_ip
        terremark.delete_node_service(node_service['Id'])
      end
    end
    if node_services.empty?
      terremark.delete_internet_service(internet_service['Id'])
      public_ip_usage.each_value {|internet_services| internet_services.delete(internet_service['Id'])}
      if public_ip_usage[internet_service['PublicIpAddress']['Name']].empty?
        terremark.delete_public_ip(internet_service['PublicIpAddress']['Id'])
      end
    end
  end

  power_off_task_id = terremark.power_off(vapp_id).body['href'].split('/').last
  print "Waiting for power off task [#{h.color(power_off_task_id, :bold)}]"
  terremark.tasks.get(power_off_task_id).wait_for { print '.'; ready? }
  print "\n"

  print "Deleting vApp #{h.color(vapp_id, :bold)}"
  delete_vapp_task_id = terremark.delete_vapp(vapp_id).headers['Location'].split('/').last
  terremark.tasks.get(delete_vapp_task_id).wait_for { print '.'; ready? }
  print "\n"
  
  Chef::Log.warn("Deleted server #{@name_args[0]}")
end