Class: Chef::Knife::TerremarkServerDelete

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

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#hObject



28
29
30
# File 'lib/chef/knife/terremark_server_delete.rb', line 28

def h
  @highline ||= HighLine.new
end

#runObject



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

def run 
  require 'fog'
  require 'highline'

  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