Class: Chef::Knife::SoftlayerServerDestroy

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

Constant Summary

Constants included from SoftlayerBase

Chef::Knife::SoftlayerBase::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SoftlayerBase

#compute, #connection, included, #locate_config_value, #msg_pair, #network

Instance Attribute Details

#cciObject

Returns the value of attribute cci.



17
18
19
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 17

def cci
  @cci
end

#nodeObject

Returns the value of attribute node.



16
17
18
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 16

def node
  @node
end

Instance Method Details

#destroy_item(klass, name, type_name) ⇒ nil

Parameters:

  • klass (Chef::*)
  • name (String)
  • type_name (String)

Returns:

  • (nil)


104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 104

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

#err_msg(msg = nil) ⇒ Object



114
115
116
117
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 114

def err_msg(msg=nil)
  @msgs ||= []
  @msgs.push(msg).compact
end

#runnil

Run the procedure to destroy a SoftLayer VM and clean up its Chef node and client.

Returns:

  • (nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 36

def run

  $stdout.sync = true

  puts ui.color("Decommissioning SoftLayer VM, this may take a few minutes.", :green)

  @chef = Chef::Search::Query.new

  if config[:chef_node_name]
    @chef.search('node', "name:#{config[:chef_node_name]}") do |node|
      config[:ip_address] = node.ipaddress
      @node = node
    end
  elsif config[:ip_address]
    @chef.search('node', "ipaddress:#{config[:ip_address]}") do |node|
      @node = node
    end
  else
    raise "#{ui.color("FATAL: Please supply the node name or IP address.", :red)}"
  end
  @slid = @node.tags.select { |s| s =~ /^slid=/ }.reduce.gsub('slid=', '').to_i
  @instance = connection.servers.get(@slid)

  @node.nil? and raise "#{ui.color('Chef node not found!', :red)}"
  @instance.nil? and raise "#{ui.color('VM instance with IP: ' + config[:ip_address] +' not found!', :red)}"


  begin
    begin
      destroy_item(Chef::Node, @node.name, "node")
      puts ui.color("Chef node successfully deleted.", :green)
    rescue Exception => e
      err_msg ui.color("ERROR DELETING CHEF NODE", :red)
      err_msg ui.color(e.message, :yellow)
      err_msg ui.color(e.backtrace, :yellow)
    end

    begin
      destroy_item(Chef::ApiClient, @node.name, "client")
      puts ui.color("Chef client successfully deleted.", :green)
    rescue Exception => e
      err_msg ui.color("ERROR DELETING CHEF CLIENT", :red)
      err_msg ui.color(e.message, :yellow)
      err_msg ui.color(e.backtrace, :yellow)
    end

    begin
      @instance.destroy
      puts ui.color("SoftLayer VM successfully deleted. You are no longer being billed for this instance.", :green)
    rescue Exception => e
      err_msg ui.color("ERROR DELETING SOFTLAYER VM. IT'S POSSIBLE YOU ARE STILL BEING BILLED FOR THIS INSTANCE.  PLEASE CONTACT SUPPORT FOR FURTHER ASSISTANCE", :red)
      err_msg ui.color(e.message, :yellow)
      err_msg ui.color(e.backtrace, :yellow)
    end
  ensure
    unless err_msg.empty?
      err_msg.each do |msg|
        puts msg
      end
    end
  end

end