Class: KnifeCloudstack::CsServerDelete

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

Instance Method Summary collapse

Instance Method Details

#connectionObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/knife/cs_server_delete.rb', line 134

def connection
  unless @connection
    @connection = CloudstackClient::Connection.new(
        locate_config_value(:cloudstack_url),
        locate_config_value(:cloudstack_api_key),
        locate_config_value(:cloudstack_secret_key)
    )
  end
  @connection
end

#delete_client(name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/knife/cs_server_delete.rb', line 112

def delete_client(name)
  begin
    client = Chef::ApiClient.load(name)
  rescue Net::HTTPServerException
    return
  end

  client.destroy
  ui.msg "Deleted client #{name}"
end

#delete_node(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/knife/cs_server_delete.rb', line 123

def delete_node(name)
  begin
    node = Chef::Node.load(name)
  rescue Net::HTTPServerException
    return
  end

  node.destroy
  ui.msg "Deleted node #{name}"
end

#disassociate_virtual_ip_address(server) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/cs_server_delete.rb', line 91

def disassociate_virtual_ip_address(server)
  nic = server['nic'].first || {}
  return unless nic['type'] == 'Virtual'

  # get the ssh rule for this server
  ssh_rule = connection.get_ssh_port_forwarding_rule(server)
  return unless ssh_rule

  # get all rules for the same ip address
  rules = connection.list_port_forwarding_rules(ssh_rule['ipaddressid'])
  return unless rules

  # ensure ip address has rules only for this server
  rules.each { |r|
    return if r['virtualmachineid'] != server['id']
  }

  # dissassociate the ip address if all tests passed
  connection.disassociate_ip_address(ssh_rule['ipaddressid'])
end

#locate_config_value(key) ⇒ Object



151
152
153
154
# File 'lib/chef/knife/cs_server_delete.rb', line 151

def locate_config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end

#msg(label, value) ⇒ Object



145
146
147
148
149
# File 'lib/chef/knife/cs_server_delete.rb', line 145

def msg(label, value)
  if value && !value.empty?
    puts "#{ui.color(label, :cyan)}: #{value}"
  end
end

#runObject



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
84
85
86
87
88
89
# File 'lib/chef/knife/cs_server_delete.rb', line 49

def run

  @name_args.each do |hostname|
    server = connection.get_server(hostname)

    if !server then
      ui.error("Server '#{hostname}' not found")
      next
    end

    if server['state'] == 'Destroyed' then
      ui.warn("Server '#{hostname}' already destroyed")
      next
    end

    puts "\n"
    msg("Name", server['name'])
    msg("Public IP", connection.get_server_public_ip(server) || '?')
    msg("Service", server['serviceofferingname'])
    msg("Template", server['templatename'])
    msg("Domain", server['domain'])
    msg("Zone", server['zonename'])
    msg("State", server['state'])

    puts "\n"
    ui.confirm("Do you really want to delete this server")

    print "#{ui.color("Waiting for deletion", :magenta)}"
    disassociate_virtual_ip_address server
    connection.delete_server hostname
    puts "\n"
    ui.msg("Deleted server #{hostname}")

    # delete chef client and node
    node_name = connection.get_server_fqdn server
    ui.confirm("Do you want to delete the chef node and client '#{node_name}")
    delete_node node_name
    delete_client node_name
  end

end