Class: Chef::Knife::ProxmoxServerDestroy

Inherits:
Chef::Knife show all
Includes:
ProxmoxBase
Defined in:
lib/chef/knife/proxmox_server_destroy.rb

Instance Method Summary collapse

Methods included from ProxmoxBase

#action_response, #check_config_parameter, #check_global_parameter, #connection, #destroy_item, included, #locate_config_value, #new_vmid, #server_create, #server_destroy, #server_get_data, #server_name_to_vmid, #server_start, #server_stop, #template_number_to_name, #vmid_to_node, #waitfor

Instance Method Details

#runObject



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
60
61
62
63
64
65
# File 'lib/chef/knife/proxmox_server_destroy.rb', line 29

def run
  # Needed
  connection
  
  #TODO: must detect which parameter has been used: name or vmid
  vm_id = nil
  if (config[:vm_id].nil? and config[:chef_node_name].nil?) then
    ui.error("You must use -I <id> or -H <Hostname>")
    exit 1
  elsif (!config[:chef_node_name].nil?)
      name = config[:chef_node_name]
      vm_id = server_name_to_vmid(name)
      puts "Server to destroy: #{name} [vmid: #{vm_id}]"
      ui.confirm("Continue")
  else
    vm_id = config[:vm_id]
  end
  
  begin
    server_stop(vm_id)
    ui.msg("Preparing the server to delete")
    sleep(5)
    server_destroy(vm_id)
  rescue Exception => e
    ui.warn("Error trying to destroy the server. Does the server exists?")
    exit 1
  end
  
  #TODO: remove server from chef
  if config[:purge]
    thing_to_delete = config[:chef_node_name] || server_get_data(config[:vm_id],"name")
    destroy_item(Chef::Node, thing_to_delete, "node")
    destroy_item(Chef::ApiClient, thing_to_delete, "client")
  else
    ui.warn("Corresponding node and client for the #{vm_id} server were not deleted and remain registered with the Chef Server")
  end
end