Skip to content

Commit e4d317b

Browse files
committed
Adding cookbook site knife commands
1 parent 8a5f1d5 commit e4d317b

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Author:: Adam Jacob (<[email protected]>)
2+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
3+
# License:: Apache License, Version 2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
require'chef/knife'
19+
20+
classChef
21+
classKnife
22+
classCookbookSiteDownload < Knife
23+
24+
attr_reader:version
25+
26+
banner"Sub-Command: cookbook site download COOKBOOK [VERSION] (options)"
27+
28+
option:file,
29+
:short=>"-f FILE",
30+
:long=>"--file FILE",
31+
:description=>"The filename to write to"
32+
33+
defrun
34+
if@name_args.length == 1
35+
current=rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{name_args[0]}")
36+
cookbook_data=rest.get_rest(current["latest_version"])
37+
else
38+
cookbook_data=rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{name_args[0]}/versions/#{name_args[1].gsub('.','_')}")
39+
end
40+
41+
@version=cookbook_data['version']
42+
43+
Chef::Log.info("Downloading #{@name_args[0]} from the cookbooks site at version #{cookbook_data['version']}")
44+
rest.sign_on_redirect=false
45+
tf=rest.get_rest(cookbook_data["file"],true)
46+
unlessconfig[:file]
47+
config[:file]=File.join(Dir.pwd,"#{@name_args[0]}-#{cookbook_data['version']}.tar.gz")
48+
end
49+
FileUtils.cp(tf.path,config[:file])
50+
Chef::Log.info("Cookbook saved: #{config[:file]}")
51+
end
52+
53+
end
54+
end
55+
end
56+
57+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# Author:: Adam Jacob (<[email protected]>)
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+
21+
classChef
22+
classKnife
23+
classCookbookSiteList < Knife
24+
25+
banner"Sub-Command: cookbook site list (options)"
26+
27+
option:with_uri,
28+
:short=>"-w",
29+
:long=>"--with-uri",
30+
:description=>"Show corresponding URIs"
31+
32+
defrun
33+
json_pretty_print(format_list_for_display(get_cookbook_list))
34+
end
35+
36+
defget_cookbook_list(items=10,start=0,cookbook_collection={})
37+
cookbooks_url="http://cookbooks.opscode.com/api/v1/cookbooks?items=#{items}&start=#{start}"
38+
cr=rest.get_rest(cookbooks_url)
39+
cr["items"].eachdo |cookbook|
40+
cookbook_collection[cookbook["cookbook_name"]]=cookbook
41+
end
42+
new_start=start + cr["items"].length
43+
ifnew_start < cr["total"]
44+
get_cookbook_list(items,new_start,cookbook_collection)
45+
else
46+
cookbook_collection
47+
end
48+
end
49+
end
50+
end
51+
end
52+
53+
54+
55+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Author:: Adam Jacob (<[email protected]>)
2+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
3+
# License:: Apache License, Version 2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
require'chef/knife'
19+
20+
classChef
21+
classKnife
22+
classCookbookSiteSearch < Knife
23+
24+
banner"Sub-Command: cookbook site search QUERY (options)"
25+
26+
defrun
27+
json_pretty_print(search_cookbook(name_args[0]))
28+
end
29+
30+
defsearch_cookbook(query,items=10,start=0,cookbook_collection={})
31+
cookbooks_url="http://cookbooks.opscode.com/api/v1/search?q=#{query}&items=#{items}&start=#{start}"
32+
cr=rest.get_rest(cookbooks_url)
33+
cr["items"].eachdo |cookbook|
34+
cookbook_collection[cookbook["cookbook_name"]]=cookbook
35+
end
36+
new_start=start + cr["items"].length
37+
ifnew_start < cr["total"]
38+
search_cookbook(query,items,new_start,cookbook_collection)
39+
else
40+
cookbook_collection
41+
end
42+
end
43+
end
44+
end
45+
end
46+
47+
48+
49+
50+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Author:: Adam Jacob (<[email protected]>)
2+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
3+
# License:: Apache License, Version 2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
require'chef/knife'
19+
20+
classChef
21+
classKnife
22+
classCookbookSiteShow < Knife
23+
24+
banner"Sub-Command: cookbook site show COOKBOOK [VERSION] (options)"
25+
26+
defrun
27+
case@name_args.length
28+
when1
29+
cookbook_data=rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{@name_args[0]}")
30+
when2
31+
cookbook_data=rest.get_rest("http://cookbooks.opscode.com/api/v1/cookbooks/#{@name_args[0]}/versions/#{name_args[1].gsub('.','_')}")
32+
end
33+
json_pretty_print(format_for_display(cookbook_data))
34+
end
35+
36+
defget_cookbook_list(items=10,start=0,cookbook_collection={})
37+
cookbooks_url="http://cookbooks.opscode.com/api/v1/cookbooks?items=#{items}&start=#{start}"
38+
cr=rest.get_rest(cookbooks_url)
39+
cr["items"].eachdo |cookbook|
40+
cookbook_collection[cookbook["cookbook_name"]]=cookbook
41+
end
42+
new_start=start + cr["items"].length
43+
ifnew_start < cr["total"]
44+
get_cookbook_list(items,new_start,cookbook_collection)
45+
else
46+
cookbook_collection
47+
end
48+
end
49+
end
50+
end
51+
end
52+
53+
54+
55+
56+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# Author:: Adam Jacob (<[email protected]>)
3+
# Copyright:: Copyright (c) 2010 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+
21+
classChef
22+
classKnife
23+
classCookbookSiteVendor < Knife
24+
25+
banner"Sub-Command: cookbook site vendor COOKBOOK [VERSION] (options)"
26+
27+
defrun
28+
vendor_path=File.join(Chef::Config[:cookbook_path].first)
29+
cookbook_path=File.join(vendor_path,name_args[0])
30+
upstream_file=File.join(vendor_path,"#{name_args[0]}.tar.gz")
31+
branch_name="#{name_args[0]}-chef-upstream"
32+
33+
download=Chef::Knife::CookbookSiteDownload.new
34+
download.config[:file]=upstream_file
35+
download.name_args=name_args
36+
download.run
37+
38+
Dir.chdir(vendor_path)do
39+
Chef::Log.info("Checking out the master branch")
40+
system("git checkout master")
41+
branch_output=`git branch --no-color | grep #{branch_name}`
42+
ifbranch_output =~ /#{branch_name}$/m
43+
system("git checkout #{branch_name}")
44+
else
45+
system("git checkout -b #{branch_name}")
46+
end
47+
system("rm -r #{cookbook_path}")
48+
system("tar zxvf #{upstream_file}")
49+
system("rm #{upstream_file}")
50+
system("git add #{name_args[0]}")
51+
system("git commit -a -m 'Import #{name_args[0]} version #{download.version}'")
52+
system("git tag -f #{name_args[0]}-#{download.version}")
53+
system("git checkout master")
54+
system("git merge #{branch_name}")
55+
end
56+
Chef::Log.info("Cookbook #{name_args[0]} version #{download.version} successfully vendored")
57+
end
58+
59+
end
60+
end
61+
end
62+
63+
64+
65+
66+
67+

0 commit comments

Comments
(0)