Class: Chef::Knife::SceServerDelete

Inherits:
Chef::Knife show all
Includes:
SceBase
Defined in:
lib/chef/knife/sce_server_delete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SceBase

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

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



32
33
34
# File 'lib/chef/knife/sce_server_delete.rb', line 32

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.



51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/sce_server_delete.rb', line 51

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



61
62
63
64
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
# File 'lib/chef/knife/sce_server_delete.rb', line 61

def run

  validate!

  @name_args.each do |instance_id|

    begin
      
      @server = connection.servers.get(instance_id)
      
      if @server.nil?
        connection.servers.all.each do |s|
          if s.name.to_s == instance_id
            @server = s
          end
        end
      end
      
      msg_pair("Instance ID", @server.id.to_s)
      msg_pair("Name", @server.name.to_s)
      msg_pair("Flavor", @server.instance_type.to_s)
      msg_pair("Image", @server.image_id.to_s)
      msg_pair("Region", connection.locations.get(@server.location_id.to_i).name.to_s)
      msg_pair("SSH Key", @server.key_name.to_s)
      msg_pair("Public DNS Name", @server.primary_ip["hostname"].to_s)
      msg_pair("Public IP Address", @server.primary_ip["ip"].to_s)
      msg_pair("Expires at", @server.expires_at.to_s)

      puts "\n"
      confirm("Do you really want to delete this server")
      
      begin
        @server.destroy
      rescue Excon::Errors::PreconditionFailed => e
        if e.data[:body].index("Active or Failed").nil?
          ui.error e.data[:body].to_s
          exit 1
        end
      end

      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.")
    end
  end
end