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)


127
128
129
130
131
132
133
134
135
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 127

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



137
138
139
140
# File 'lib/chef/knife/softlayer_server_destroy.rb', line 137

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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)
  connection.servers.each do |server|
    if config[:ip_address]
      if server.public_ip_address == config[:ip_address]
        @instance = server
        break
      end
    elsif config[:chef_node_name]
      if server.name == config[:chef_node_name]
        config[:ip_address] = server.public_ip_address
        @instance = server
        break
      end
    elsif  arg = name_args[0]
      if arg =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ # ipv4
        if server.public_ip_address == arg
          @instance = server
          break
        end
      elsif arg =~ /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/ # ipv6
        if server.public_ip_address == arg
          @instance = server
          break
        end
      else
        if server.name == arg
          config[:ip_address] = server.public_ip_address
          @instance = server
          break
        end
      end
    end              
  end
  @instance.nil? and raise "#{ui.color('VM instance with IP: ' + (config[:ip_address].to_s) +' not found!', :red)}"
  @chef = Chef::Search::Query.new
  @chef.search('node', "name:#{@instance.name}") do |node|
    begin
      @node = node               
    rescue
    end
  end

  begin
    if @node
      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.join("\n"), :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.join("\n"), :yellow)
      end
    else
      "#{ui.color('Chef node: ' + config[:chef_node_name] +' not found! will destroy instance.', :red)}"
    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.join("\n"), :yellow)
    end
  ensure
    unless err_msg.empty?
      err_msg.each do |msg|
        puts msg
      end
    end
  end

end