Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optionally destroy node and client with server.

  • Loading branch information...
commit 3216adc70737f40cec96c82e9c4282d8a781e60d 1 parent 7673a6f
Christopher Maier christophermaier authored
Showing with 30 additions and 1 deletion.
  1. +1 −1  README.rdoc
  2. +29 −0 lib/chef/knife/ec2_server_delete.rb
2  README.rdoc
View
@@ -53,7 +53,7 @@ Provisions a new server in the Amazon EC2 and then perform a Chef bootstrap (usi
== knife ec2 server delete
-Deletes an existing server in the currently configured AWS account. <b>PLEASE NOTE</b> - this does not delete the associated node and client objects from the Chef server.
+Deletes an existing server in the currently configured AWS account. <b>PLEASE NOTE</b> - By default, this does not delete the associated node and client objects from the Chef server. To do so, add the <tt>--purge</tt> flag.
== knife ec2 server list
29 lib/chef/knife/ec2_server_delete.rb
View
@@ -19,6 +19,10 @@
require 'chef/knife/ec2_base'
+# These two are needed for the '--purge' deletion case
+require 'chef/node'
+require 'chef/api_client'
+
class Chef
class Knife
class Ec2ServerDelete < Knife
@@ -27,6 +31,24 @@ class Ec2ServerDelete < Knife
banner "knife ec2 server delete SERVER [SERVER] (options)"
+ option :purge,
+ :short => "-P",
+ :long => "--purge",
+ :boolean => true,
+ :default => false,
+ :description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the EC2 node itself."
+
+ # 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.
+ def destroy_item(klass, name, type_name)
+ object = klass.load(name)
+ object.destroy
+ ui.warn("Deleted #{type_name} #{name}")
+ end
+
def run
validate!
@@ -56,6 +78,13 @@ def run
ui.warn("Deleted server #{server.id}")
+ if config[:purge]
+ destroy_item(Chef::Node, instance_id, "node")
+ destroy_item(Chef::ApiClient, instance_id, "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 in the '#{locate_config_value(:region)}' region.")
end
Please sign in to comment.
Something went wrong with that request. Please try again.