class Chef::Knife::VagrantServerDelete

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/vagrant_server_delete.rb, line 29
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/vagrant_server_delete.rb, line 39
def run

  @name_args.each do |name|
    instance = vagrant_instance_list.detect { |i| i[:name] == name }
    unless instance
      ui.error("No instance named #{name}")
      next            
    end

    msg_pair("Instance Name", instance[:name])
    msg_pair("Box", instance[:box])
    msg_pair("Vagrant File", instance[:vagrant_file])
    msg_pair("IP Address", instance[:ip_address])

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

    vagrant_exec(instance[:name], 'destroy -f')
    instance_dir = File.join(locate_config_value(:vagrant_dir), instance[:name])
    FileUtils.rm_rf(instance_dir)

    ui.warn("Deleted instance #{instance[:name]}")

    if config[:purge]
      destroy_item(Chef::Node, instance[:name], "node")
      destroy_item(Chef::ApiClient, instance[:name], "client")
    else
      ui.warn("Corresponding node and client for the #{instance[:name]} instance were not deleted and remain registered with the Chef Server")
    end
  end
end