Class: Chef::Knife::NcServerDelete

Inherits:
Chef::Knife show all
Includes:
NcBase
Defined in:
lib/chef/knife/nc_server_delete.rb

Instance Method Summary collapse

Methods included from NcBase

#connection, included, #locate_config_value, #msg_pair, #validate!

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.



55
56
57
58
59
60
61
62
63
# File 'lib/chef/knife/nc_server_delete.rb', line 55

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



65
66
67
68
69
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/knife/nc_server_delete.rb', line 65

def run

  validate!

  @name_args.each do |instance_id|

    begin
      server = connection.describe_instances(:instance_id => instance_id).reservationSet.item.first.instancesSet.item.first

      msg_pair("Instance ID", server.instanceId)
      msg_pair("Instance Type", server.instanceType)
      msg_pair("Image ID", server.imageId)
      #msg_pair("Security Groups", server.groups.join(", "))
      msg_pair("SSH Key", server.keyName)
      msg_pair("Root Device Type", server.root_device_type)
      msg_pair("Public IP Address", server.ipAddress)
      msg_pair("Private IP Address", server.privateIpAddress)

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

      if server.instanceState.name != 'stopped'
        print "\n#{ui.color("Waiting for server to shutdown", :magenta)}"
        connection.stop_instances(:instance_id => instance_id, :force => true)
        while server.instanceState.name != 'stopped'
          print "."
          server = connection.describe_instances(:instance_id => instance_id).reservationSet.item.first.instancesSet.item.first
          sleep 5
        end
        puts("done\n")
      end

      attribute = connection.describe_instance_attribute(:instance_id => instance_id, :attribute => 'disableApiTermination')
      if attribute.disableApiTermination.value != 'false'
        if config[:force] == false
          ui.error("Server's 'disableApiTermination' attribute is true. Use --force option to delete server.")
          exit 1
        else
          print "\n#{ui.color("Enabling API termination for server", :magenta)}"
          connection.modify_instance_attribute(:instance_id => instance_id, :attribute => 'disableApiTermination', :value => 'false')
          while attribute.disableApiTermination.value != 'false'
            print "."
            attribute = connection.describe_instance_attribute(:instance_id => instance_id, :attribute => 'disableApiTermination')
            sleep 5
          end
          puts("done\n")
        end
      end

      connection.terminate_instances(:instance_id => instance_id)

      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 in the '#{locate_config_value(:region)}' region.")
    end
  end
end