knife-bluebox 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,14 @@ Depending on your system configuration, you may need to run this command with
10
10
  root privileges.
11
11
 
12
12
  == Configuration
13
- TODO
13
+ Set the following environmental variables
14
+
15
+ export BLUEBOX_API_KEY="YourAPIKey" # should match /[a-f0-9]+/
16
+ export BLUEBOX_CUSTOMER_ID="YourCustomerNumber" # should match /d+/
14
17
 
15
18
  == Usage
16
- TODO
19
+ knife bluebox images list
20
+ knife bluebox server create [RUN LIST...] (options)
21
+ knife bluebox server delete BLOCK-HOSTNAME
22
+ knife bluebox server list (options)
17
23
 
@@ -72,7 +72,7 @@ class Chef
72
72
  :short => "-d DISTRO",
73
73
  :long => "--distro DISTRO",
74
74
  :description => "Bootstrap a distro using a template",
75
- :default => "ubuntu10.04-gems"
75
+ :default => "chef-full"
76
76
 
77
77
  option :identity_file,
78
78
  :short => "-I IDENTITY_FILE",
@@ -165,7 +165,7 @@ class Chef
165
165
  begin
166
166
  bootstrap = Chef::Knife::Bootstrap.new
167
167
  bootstrap.name_args = [ server.ips[0]['address'] ]
168
- bootstrap.config[:run_list] = @name_args
168
+ bootstrap.config[:run_list] = run_list
169
169
  bootstrap.config[:password] = password unless config[:password].empty?
170
170
  bootstrap.config[:ssh_user] = config[:username]
171
171
  bootstrap.config[:identity_file] = config[:identity_file]
@@ -190,8 +190,14 @@ class Chef
190
190
  puts "\n\n#{h.color("Encountered error starting up BBG block. Auto destroy called. Please try again.", :red)}"
191
191
 
192
192
  end
193
+ end
193
194
 
195
+ def run_list
196
+ candidate = @name_args.first
197
+ return candidate if /[\s,]+/ !~ candidate
198
+ candidate.split(/[\s,]+/)
194
199
  end
200
+
195
201
  end
196
202
  end
197
203
  end
@@ -6,9 +6,9 @@
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
14
14
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,42 +27,102 @@ class Chef
27
27
  require 'highline'
28
28
  require 'readline'
29
29
  require 'chef/json_compat'
30
+ require 'chef/node'
31
+ require 'chef/api_client'
32
+ require 'chef/search/query'
30
33
  end
31
34
 
32
35
  banner "knife bluebox server delete BLOCK-HOSTNAME"
33
36
 
34
- def h
35
- @highline ||= HighLine.new
36
- end
37
-
38
37
  def run
39
38
  bluebox = Fog::Compute::Bluebox.new(
40
39
  :bluebox_customer_id => Chef::Config[:knife][:bluebox_customer_id],
41
40
  :bluebox_api_key => Chef::Config[:knife][:bluebox_api_key]
42
41
  )
43
42
 
43
+ @server_to_remove = @name_args[0]
44
+
44
45
  # Build hash of hostname => id
45
46
  servers = bluebox.servers.inject({}) { |h,f| h[f.hostname] = f.id; h }
46
47
 
47
- unless servers.has_key?(@name_args[0])
48
- ui.error("Can't find a block named #{@name_args[0]}")
48
+ unless servers.has_key?(@server_to_remove)
49
+ ui.error("Can't find a block named #{@server_to_remove}")
49
50
  exit 1
50
51
  end
51
52
 
52
- confirm(h.color("Do you really want to delete block UUID #{servers[@name_args[0]]} with hostname #{@name_args[0]}", :green))
53
-
53
+ # remove the block instance
54
+ ui.confirm("Do you really want to delete block UUID #{servers[@server_to_remove]} with hostname #{@server_to_remove}")
54
55
  begin
55
- response = bluebox.destroy_block(servers[@name_args[0]])
56
+ response = bluebox.destroy_block(servers[@server_to_remove])
56
57
  if response.status == 200
57
- puts "\n\n#{h.color("Successfully destroyed #{@name_args[0]}", :green)}"
58
+ ui.msg(green("Successfully destroyed #{@server_to_remove}"))
58
59
  else
59
- puts "\n\n#{h.color("There was a problem destroying #{@name_args[0]}. Please check Box Panel.", :red)}"
60
+ ui.msg(red("There was a problem destroying #{@server_to_remove}. Please check Box Panel."))
60
61
  exit 1
61
62
  end
62
63
  rescue Excon::Errors::UnprocessableEntity
63
- puts "\n\n#{h.color("There was a problem destroying #{@name_args[0]}. Please check Box Panel.", :red)}"
64
+ ui.msg(red("There was a problem destroying #{@server_to_remove}. Please check Box Panel."))
65
+ end
66
+
67
+ # remove chef client and node
68
+ ui.confirm("Do you wish to remove the #{chef_name} node and client objects from the chef server?")
69
+ if !chef_name.nil?
70
+ remove_node(chef_name)
71
+ remove_client(chef_name)
72
+ else
73
+ ui.msg "Could not locate a chef node associated with #{@server_to_remove}"
74
+ end
75
+ end
76
+
77
+ def chef_name
78
+ short_name = @server_to_remove.split(".").first
79
+
80
+ if node_exists?(@server_to_remove)
81
+ @server_to_remove
82
+ elsif node_exists?(short_name)
83
+ short_name
84
+ else
85
+ nil
64
86
  end
65
87
  end
88
+
89
+ def remove_node(node)
90
+ begin
91
+ object = Chef::Node.load(node)
92
+ object.destroy
93
+ ui.msg(green("#{node} node removed from chef server"))
94
+ rescue
95
+ ui.warn(" Could not remove the #{node} node")
96
+ end
97
+ end
98
+
99
+ def remove_client(client)
100
+ begin
101
+ object = Chef::ApiClient.load(client)
102
+ object.destroy
103
+ ui.msg(green("#{client} client removed from chef server"))
104
+ rescue
105
+ ui.warn("Could not remove the #{client} client")
106
+ end
107
+ end
108
+
109
+ def node_exists?(node)
110
+ search = Chef::Search::Query.new
111
+ !search.search('node', "name:#{node}").first.empty?
112
+ end
113
+
114
+ def highline
115
+ @highline ||= HighLine.new
116
+ end
117
+
118
+ def green(text)
119
+ "#{highline.color(text, :green)}"
120
+ end
121
+
122
+ def red(text)
123
+ "#{highline.color(text, :red)}"
124
+ end
125
+
66
126
  end
67
127
  end
68
128
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeBlueBox
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-bluebox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-29 00:00:00.000000000 Z
13
+ date: 2012-11-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: chef
@@ -84,3 +84,4 @@ signing_key:
84
84
  specification_version: 3
85
85
  summary: Chef knife plugin for Blue Box
86
86
  test_files: []
87
+ has_rdoc: true