Class: Chef::Knife::BlueboxServerDelete

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/bluebox_server_delete.rb

Instance Method Summary collapse

Instance Method Details

#chef_nameObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/bluebox_server_delete.rb', line 77

def chef_name
  short_name = @server_to_remove.split(".").first

  if node_exists?(@server_to_remove)
    @server_to_remove
  elsif node_exists?(short_name)
    short_name
  else
    nil
  end
end

#green(text) ⇒ Object



118
119
120
# File 'lib/chef/knife/bluebox_server_delete.rb', line 118

def green(text)
  "#{highline.color(text, :green)}"
end

#highlineObject



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

def highline
  @highline ||= HighLine.new
end

#node_exists?(node) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/chef/knife/bluebox_server_delete.rb', line 109

def node_exists?(node)
  search = Chef::Search::Query.new
  !search.search('node', "name:#{node}").first.empty?
end

#red(text) ⇒ Object



122
123
124
# File 'lib/chef/knife/bluebox_server_delete.rb', line 122

def red(text)
  "#{highline.color(text, :red)}"
end

#remove_client(client) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/chef/knife/bluebox_server_delete.rb', line 99

def remove_client(client)
  begin
    object = Chef::ApiClient.load(client)
    object.destroy
    ui.msg(green("#{client} client removed from chef server"))
  rescue
    ui.warn("Could not remove the #{client} client")
  end
end

#remove_node(node) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/chef/knife/bluebox_server_delete.rb', line 89

def remove_node(node)
  begin
    object = Chef::Node.load(node)
    object.destroy
    ui.msg(green("#{node} node removed from chef server"))
  rescue
    ui.warn(" Could not remove the #{node} node")
  end
end

#runObject



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

def run
  bluebox = Fog::Compute::Bluebox.new(
    :bluebox_customer_id => Chef::Config[:knife][:bluebox_customer_id],
    :bluebox_api_key => Chef::Config[:knife][:bluebox_api_key]
  )

  @server_to_remove = @name_args[0]

  # Build hash of hostname => id
  servers = bluebox.servers.inject({}) { |h,f| h[f.hostname] = f.id; h }

  unless servers.has_key?(@server_to_remove)
    ui.error("Can't find a block named #{@server_to_remove}")
    exit 1
  end

  # remove the block instance
  ui.confirm("Do you really want to delete block UUID #{servers[@server_to_remove]} with hostname #{@server_to_remove}")
  begin
    response = bluebox.destroy_block(servers[@server_to_remove])
    if response.status == 200
      ui.msg(green("Successfully destroyed #{@server_to_remove}"))
    else
      ui.msg(red("There was a problem destroying #{@server_to_remove}. Please check Box Panel."))
      exit 1
    end
  rescue Excon::Errors::UnprocessableEntity
    ui.msg(red("There was a problem destroying #{@server_to_remove}. Please check Box Panel."))
  end

  # remove chef client and node
  ui.confirm("Do you wish to remove the #{chef_name} node and client objects from the chef server?")
  if !chef_name.nil?
    remove_node(chef_name)
    remove_client(chef_name)
  else
    ui.msg "Could not locate a chef node associated with #{@server_to_remove}"
  end
end