Skip to content

Commit a967b04

Browse files
committed
Final set of knife commands
1 parent 24d3708 commit a967b04

7 files changed

Lines changed: 228 additions & 13 deletions

File tree

chef/lib/chef/knife.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def self.find_command(args=ARGV, merge_opts={})
6060

6161
non_dash_args = Array.new
6262
args.each do |arg|
63-
non_dash_args << arg if arg =~ /^([[:alpha:]]|_)+$/
63+
non_dash_args << arg if arg =~ /^([[:alnum:]]|_)+$/
6464
end
6565

6666
to_try = non_dash_args.length
@@ -140,6 +140,8 @@ def format_for_display(item)
140140
elsif config[:run_list]
141141
data = data.run_list.run_list
142142
{ "run_list" => data }
143+
elsif config[:id_only]
144+
data.respond_to?(:name) ? data.name : data["id"]
143145
else
144146
data
145147
end
@@ -225,12 +227,18 @@ def edit_object(klass, name)
225227
json_pretty_print(format_for_display(object)) if config[:print_after]
226228
end
227229

228-
def create_object(object)
230+
def create_object(object, pretty_name=nil, &block)
229231
output = edit_data(object)
230232

231-
output.save
233+
if Kernel.block_given?
234+
output = block.call(output)
235+
else
236+
output.save
237+
end
238+
239+
pretty_name ||= output
232240

233-
Chef::Log.info("Created (or updated) #{output}")
241+
Chef::Log.info("Created (or updated) #{pretty_name}")
234242

235243
json_pretty_print(output) if config[:print_after]
236244
end

chef/lib/chef/knife/data_bag_create.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ class Chef
2323
class Knife
2424
class DataBagCreate < Knife
2525

26-
banner "Sub-Command: data bag create BAG (options)"
26+
banner "Sub-Command: data bag create BAG [ITEM] (options)"
2727

2828
def run
29-
rest.post_rest("data", { "name" => @name_args[0] })
30-
Chef::Log.info("Created data_bag[#{@name_args[0]}]")
29+
if @name_args.length == 2
30+
create_object({ "id" => @name_args[1] }, "data_bag_item[#{@name_args[1]}]") do |output|
31+
rest.put_rest("data/#{@name_args[0]}/#{@name_args[1]}", output)
32+
end
33+
else
34+
rest.post_rest("data", { "name" => @name_args[0] })
35+
Chef::Log.info("Created data_bag[#{@name_args[0]}]")
36+
end
3137
end
3238
end
3339
end

chef/lib/chef/knife/data_bag_delete.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ class Chef
2323
class Knife
2424
class DataBagDelete < Knife
2525

26-
banner "Sub-Command: data bag delete BAG (options)"
26+
banner "Sub-Command: data bag delete BAG [ITEM] (options)"
2727

2828
def run
29-
delete_object(Chef::DataBag, @name_args[0], "data_bag") do
30-
rest.delete_rest("data/#{@name_args[0]}")
29+
if @name_args.length == 2
30+
delete_object(Chef::DataBagItem, @name_args[1], "data_bag_item") do
31+
rest.delete_rest("data/#{@name_args[0]}/#{@name_args[1]}")
32+
end
33+
else
34+
delete_object(Chef::DataBag, @name_args[0], "data_bag") do
35+
rest.delete_rest("data/#{@name_args[0]}")
36+
end
3137
end
3238
end
3339
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/knife'
20+
require 'chef/data_bag_item'
21+
22+
class Chef
23+
class Knife
24+
class DataBagEdit < Knife
25+
26+
banner "Sub-Command: data bag edit BAG ITEM (options)"
27+
28+
def run
29+
if @name_args.length != 2
30+
Chef::Log.fatal("You must supply the data bag and an item to edit!")
31+
exit 42
32+
else
33+
object = Chef::DataBagItem.load(@name_args[0], @name_args[1])
34+
35+
output = edit_data(object)
36+
37+
rest.put_rest("data/#{@name_args[0]}/#{@name_args[1]}", output)
38+
39+
Chef::Log.info("Saved data_bag_item[#{@name_args[1]}]")
40+
41+
json_pretty_print(format_for_display(object)) if config[:print_after]
42+
end
43+
end
44+
end
45+
end
46+
end
47+
48+
49+

chef/lib/chef/knife/data_bag_show.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ class Chef
2323
class Knife
2424
class DataBagShow < Knife
2525

26-
banner "Sub-Command: data bag show BAG (options)"
26+
banner "Sub-Command: data bag show BAG [ITEM] (options)"
2727

28-
def run
29-
json_pretty_print(format_list_for_display(Chef::DataBag.load(@name_args[0])))
28+
def run
29+
display = case @name_args.length
30+
when 2
31+
format_for_display(Chef::DataBagItem.load(@name_args[0], @name_args[1]))
32+
else
33+
format_list_for_display(Chef::DataBag.load(@name_args[0]))
34+
end
35+
json_pretty_print(display)
3036
end
3137
end
3238
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/knife'
20+
require 'json'
21+
22+
class Chef
23+
class Knife
24+
class Ec2InstanceData < Knife
25+
26+
banner "Sub-Command: ec2 instance data [RUN LIST...] (options)"
27+
28+
option :edit,
29+
:short => "-e",
30+
:long => "--edit",
31+
:description => "Edit the instance data"
32+
33+
def run
34+
data = {
35+
"chef_server" => Chef::Config[:chef_server_url],
36+
"validation_client_name" => Chef::Config[:validation_client_name],
37+
"validation_key" => IO.read(Chef::Config[:validation_key]),
38+
"attributes" => { "run_list" => @name_args }
39+
}
40+
data = edit_data(data) if config[:edit]
41+
json_pretty_print(data)
42+
end
43+
end
44+
end
45+
end
46+

chef/lib/chef/knife/search.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/knife'
20+
require 'chef/data_bag_item'
21+
22+
class Chef
23+
class Knife
24+
class Search < Knife
25+
26+
banner "Sub-Command: search INDEX QUERY (options)"
27+
28+
option :sort,
29+
:short => "-o SORT",
30+
:long => "--sort SORT",
31+
:description => "The order to sort the results in",
32+
:default => nil
33+
34+
option :start,
35+
:short => "-b ROW",
36+
:long => "--start ROW",
37+
:description => "The row to start returning results at",
38+
:default => nil,
39+
:proc => lambda { |i| i.to_i }
40+
41+
option :rows,
42+
:short => "-R INT",
43+
:long => "--rows INT",
44+
:description => "The number of rows to return",
45+
:default => nil,
46+
:proc => lambda { |i| i.to_i }
47+
48+
option :attribute,
49+
:short => "-a [ATTR]",
50+
:long => "--attribute [ATTR]",
51+
:description => "Show only one attribute"
52+
53+
option :run_list,
54+
:short => "-r",
55+
:long => "--run-list",
56+
:description => "Show only the run list"
57+
58+
option :id_only,
59+
:short => "-i",
60+
:long => "--id-only",
61+
:description => "Show only the ID of matching objects"
62+
63+
def run
64+
q = Chef::Search::Query.new
65+
display = { :total => 0, :start => config[:start] ? config[:start] : 0, :rows => [ ] }
66+
67+
q.search(@name_args[0], @name_args[1], config[:sort], config[:start], config[:rows]) do |item|
68+
formatted_item = format_for_display(item)
69+
if formatted_item.respond_to?(:has_key?) && !formatted_item.has_key?('id')
70+
formatted_item['id'] = item.has_key?('id') ? item['id'] : item.name
71+
end
72+
display[:rows] << formatted_item
73+
display[:total] += 1
74+
end
75+
76+
if config[:id_only]
77+
if config[:attribute]
78+
display[:rows].each do |row|
79+
puts row[config[:attribute]] if row.has_key?(config[:attribute]) && !row[config[:attribute]].nil?
80+
end
81+
else
82+
puts display[:rows].join("\n")
83+
end
84+
else
85+
json_pretty_print(display)
86+
end
87+
end
88+
end
89+
end
90+
end
91+
92+
93+
94+

0 commit comments

Comments
 (0)