Class: Chef::Knife::BrightboxServerDelete

Inherits:
Chef::Knife
  • Object
show all
Includes:
BrightboxBase
Defined in:
lib/chef/knife/brightbox_server_delete.rb

Instance Method Summary collapse

Methods included from BrightboxBase

#connection, included, #locate_config_value

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.



46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/brightbox_server_delete.rb', line 46

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

#msg(label, value) ⇒ Object



101
102
103
104
105
# File 'lib/chef/knife/brightbox_server_delete.rb', line 101

def msg(label, value)
  if value && !value.empty?
    puts "#{ui.color(label, :cyan)}: #{value}"
  end
end

#runObject



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

def run
  @name_args.each do |instance_id|
    server = connection.servers.get(instance_id)

    if server.nil?
      ui.error("Server instance #{instance_id} not found. Aborting.")
      exit 1
    end
    msg("Instance ID", server.id.to_s)
    msg("Name", server.name)
    msg("Flavor", server.flavor.name) if server.flavor
    msg("Image", server.image.name) if server.image
    msg("Public IP Address", server.public_ip_address)

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

    server.destroy

    ui.warn("Deleted server #{server.id} named #{server.name}")

    if config[:purge]
      thing_to_delete = config[:chef_node_name] || server.name
      destroy_item(Chef::Node, thing_to_delete, "node")
      destroy_item(Chef::ApiClient, thing_to_delete, "client")

      server.cloud_ips.each do |cid|
        cid = connection.cloud_ips.get(cid["id"])
        print "#{ui.color("WARNING:", :yellow)} Deleted cloud ip (#{cid.id})"
        print(".") until wait_for_release { cid.destroy; puts("done!") }
      end
    else
      ui.warn("Corresponding node and client for the #{instance_id} server were not deleted and remain registered with the Chef Server")
    end
  end
end

#wait_for_releaseObject



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

def wait_for_release
  yield
  true
rescue Excon::Errors::Forbidden
  sleep 1
  false
end