class Chef::Knife::SceServerDelete

Attributes

server[R]

Public Instance Methods

destroy_item(klass, name, type_name) click to toggle source

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.

# File lib/chef/knife/sce_server_delete.rb, line 51
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
run() click to toggle source
# File lib/chef/knife/sce_server_delete.rb, line 61
def run

  validate!

  @name_args.each do |instance_id|

    begin
      
      @server = connection.servers.get(instance_id)
      
      if @server.nil?
        connection.servers.all.each do |s|
          if s.name.to_s == instance_id
            @server = s
          end
        end
      end
      
      msg_pair("Instance ID", @server.id.to_s)
      msg_pair("Name", @server.name.to_s)
      msg_pair("Flavor", @server.instance_type.to_s)
      msg_pair("Image", @server.image_id.to_s)
      msg_pair("Region", connection.locations.get(@server.location_id.to_i).name.to_s)
      msg_pair("SSH Key", @server.key_name.to_s)
      msg_pair("Public DNS Name", @server.primary_ip["hostname"].to_s)
      msg_pair("Public IP Address", @server.primary_ip["ip"].to_s)
      msg_pair("Expires at", @server.expires_at.to_s)

      puts "\n"
      confirm("Do you really want to delete this server")
      
      begin
        @server.destroy
      rescue Excon::Errors::PreconditionFailed => e
        if e.data[:body].index("Active or Failed").nil?
          ui.error e.data[:body].to_s
          exit 1
        end
      end

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

      if config[:purge]
        thing_to_delete = config[:chef_node_name] || instance_id
        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}'.  Please verify it was provisioned.")
    end
  end
end