Class: Chef::Knife::VagrantServerDelete

Inherits:
Chef::Knife show all
Includes:
VagrantBase
Defined in:
lib/chef/knife/vagrant_server_delete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VagrantBase

#colored_vagrant_state, included, #locate_config_value, #msg_pair, #vagrant_exec, #vagrant_instance_list, #vagrant_instance_state, #write_insecure_key

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



15
16
17
# File 'lib/chef/knife/vagrant_server_delete.rb', line 15

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.



29
30
31
32
33
34
35
36
37
# 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

#runObject



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
66
67
68
69
# 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