Class: Chef::Knife::GandiServerDelete

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/gandi_server_delete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



39
40
41
# File 'lib/chef/knife/gandi_server_delete.rb', line 39

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



39
40
41
# File 'lib/chef/knife/gandi_server_delete.rb', line 39

def connection
  @connection
end

Instance Method Details

#runObject



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

def run
  # Unsual to extend here but enables 'plugin_helper' to be require'd lazily
  extend KnifeGandi::PluginHelper
  $stdout.sync = true
  
  # Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
  suppress_warnings do
    XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
    XMLRPC::Config.const_set(:ENABLE_NIL_CREATE, true)
  end
  
  @connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
  @api_key    = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key] || raise("Please provide an API key")
  
  server = connection.call('vm.info', api_key, @name_args[0].to_i)
  
  puts "\n"
  puts "#{ui.color("Server ID", :cyan)}: #{server['id']}"
  puts "#{ui.color("Name", :cyan)}: #{ui.color(server['hostname'], :bold)}"
  puts "#{ui.color("Description", :cyan)}: #{server['description']}" if server['description']
  puts "#{ui.color("Memory", :cyan)}: #{server['memory']}"
  puts "#{ui.color("Cores", :cyan)}: #{server['cores']}"
  puts "#{ui.color("Datacenter ID", :cyan)}: #{server['datacenter_id']}"
  puts "#{ui.color("Image", :cyan)}: #{server['disks'].find{ |disk| disk['is_boot_disk'] }['label']}"
  puts "#{ui.color("State", :cyan)}: #{server['state']}"
  
  raise "This server is being created, wait for this to end and try again" if server['state'] == 'being_created'

  puts "\n"
  puts "==============================================="
  confirm("Do you really want to delete this server?")
  
  # Stop the server, the API requires a server to be halted before it can be deleted
  unless (server['state'] == 'halted')
    puts "\n"
    print ui.color("Stopping server.", :magenta)
    halt_operation = connection.call('vm.stop', api_key, server['id'])
    until_done(halt_operation) { print '.' }
  end
  
  vm_delete_operation = connection.call('vm.delete', api_key, server['id'])
  
  puts "\n\n"
  print ui.color("Deleting server.", :magenta)
  
  until_done(vm_delete_operation) { print '.' }
  
  puts "\n\n"
  ui.warn("Deleted server #{ui.color(server['id'], :bold)} named #{ui.color(server['hostname'], :bold)}")
end