Class: Chef::Knife::Ec2ServerDelete

Inherits:
Chef::Knife show all
Includes:
Ec2Base
Defined in:
lib/chef/knife/ec2_server_delete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ec2Base

#ami, #connection_string, #ec2_connection, #fetch_ami, #fetch_ec2_instance, #fetch_network_interfaces, #fetch_password_data, #fetch_region, #fetch_subnet, #find_name_tag, included, #is_image_windows?, #msg_pair, #normalize_server_data, #validate_aws_config!, #vpc_mode?

Methods inherited from Chef::Knife

#aws_cred_file_location, #custom_warnings!, #find_server_platform, #iam_name_from_profile, #ini_parse

Instance Attribute Details

#server ⇒ Object (readonly)

Returns the value of attribute server.



36
37
38
# File 'lib/chef/knife/ec2_server_delete.rb', line 36

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.



62
63
64
65
66
67
68
# File 'lib/chef/knife/ec2_server_delete.rb', line 62

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

#fetch_instance_id(name) ⇒ String

Returns:

  • (String)


120
121
122
123
124
125
126
127
128
# File 'lib/chef/knife/ec2_server_delete.rb', line 120

def fetch_instance_id(name)
  result = query.search(:node, "name:#{name}")
  unless result.first.empty?
    node = result.first.first
    if node.attribute?("ec2")
      node["ec2"]["instance_id"]
    end
  end
end

#fetch_node_name(instance_id) ⇒ String

Returns:

  • (String)


110
111
112
113
114
115
116
117
# File 'lib/chef/knife/ec2_server_delete.rb', line 110

def fetch_node_name(instance_id)
  result = query.search(:node, "ec2_instance_id:#{instance_id}")
  unless result.first.empty?
    result.first.first.name
  else
    instance_id
  end
end

#query ⇒ Chef::Search::Query

Returns:

  • (Chef::Search::Query)


131
132
133
# File 'lib/chef/knife/ec2_server_delete.rb', line 131

def query
  @query ||= Chef::Search::Query.new
end

#run ⇒ Object



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
102
103
104
105
106
107
# File 'lib/chef/knife/ec2_server_delete.rb', line 70

def run
  validate_aws_config!
  validate_instances!

  server_hashes.each do |h|
    instance_id = h["instance_id"]
    msg_pair("Instance ID", instance_id)
    msg_pair("Instance Name", h["name"])
    msg_pair("Flavor", h["instance_type"])
    msg_pair("Image", h["image_id"])
    msg_pair("Region", fetch_region)
    msg_pair("Availability Zone", h["az"])
    msg_pair("Security Groups", h["security_groups"])
    msg_pair("IAM Profile", h["iam_instance_profile"])
    msg_pair("SSH Key", h["key_name"])
    msg_pair("Root Device Type", h["root_device_type"])
    msg_pair("Public DNS Name", h["public_dns_name"])
    msg_pair("Public IP Address", h["public_ip_address"])
    msg_pair("Private DNS Name", h["private_dns_name"])
    msg_pair("Private IP Address", h["private_ip_address"])

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

    delete_instance(instance_id) unless config[:dry_run]

    ui.warn("Deleted server #{instance_id}")

    if config[:purge]
      node_name = config[:chef_node_name] || fetch_node_name(instance_id)
      destroy_item(Chef::Node, node_name, "node")
      destroy_item(Chef::ApiClient, node_name, "client")
    else
      ui.warn("Corresponding node and client for the #{instance_id} server were not deleted and remain registered with the Chef Server")
    end
    puts "\n"
  end
end