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
= 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 = .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 = .servers.get(vapp_id).ip_address
internet_services = .get_internet_services(.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 = .get_node_services(internet_service['Id']).body['NodeServices']
node_services.delete_if do |node_service|
if node_service['IpAddress'] == private_ip
.delete_node_service(node_service['Id'])
end
end
if node_services.empty?
.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?
.delete_public_ip(internet_service['PublicIpAddress']['Id'])
end
end
end
power_off_task_id = .power_off(vapp_id).body['href'].split('/').last
print "Waiting for power off task [#{h.color(power_off_task_id, :bold)}]"
.tasks.get(power_off_task_id).wait_for { print '.'; ready? }
print "\n"
print "Deleting vApp #{h.color(vapp_id, :bold)}"
delete_vapp_task_id = .delete_vapp(vapp_id).['Location'].split('/').last
.tasks.get(delete_vapp_task_id).wait_for { print '.'; ready? }
print "\n"
Chef::Log.warn("Deleted server #{@name_args[0]}")
end
|