Class: Chef::Knife::OcaServerDelete

Inherits:
Chef::Knife show all
Includes:
OcaBase
Defined in:
lib/chef/knife/oca_server_delete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OcaBase

#connection, #dns_reverse_lookup, included, #locate_config_value, #msg_pair, #validate!

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



37
38
39
# File 'lib/chef/knife/oca_server_delete.rb', line 37

def server
  @server
end

Instance Method Details

#destroy_item(klass, name, type_name) ⇒ Object

Extracted from Chef::Knife.delete_object, because it has a confirmation step built in… By specifying the ‘–purge’ flag (and also explicitly confirming the server destruction!) the user is already making their intent known. It is not necessary to make them confirm two more times.



56
57
58
59
60
61
62
63
64
# File 'lib/chef/knife/oca_server_delete.rb', line 56

def destroy_item(klass, name, type_name)
  begin
    object = klass.load(name)
    object.destroy
    ui.warn("Deleted #{type_name} #{name}")
  rescue Net::HTTPServerException
    ui.warn("Could not find a #{type_name} named #{name} to delete!")
  end
end

#runObject



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/knife/oca_server_delete.rb', line 66

def run

  validate!

  @name_args.each do |instance_id|

    begin
      @server = connection.virtual_machines.get(instance_id.to_s)

      fqdn = dns_reverse_lookup(@server.template['NIC']['IP'].to_s)

      msg_pair("ID", @server.id.to_s)
      msg_pair("Public IP", @server.template['NIC']['IP'].to_s)
      msg_pair("Public DNS Name", fqdn)
      msg_pair("Template", connection.templates.get(@server.template['TEMPLATE_ID']).name.to_s)

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

      @server.finalize

      ui.warn("Deleted server #{@server.id}")

      if config[:purge]
        thing_to_delete = config[:chef_node_name] || fqdn
        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 #{instance_id} server were not deleted and remain registered with the Chef Server")
      end

    rescue NoMethodError
      ui.error("Could not locate server '#{instance_id}'.")
    end
  end
end