knife-chop 0.2.6
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.
- checksums.yaml +15 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +154 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +54 -0
- data/TODO.rdoc +46 -0
- data/VERSION +1 -0
- data/bin/chop +48 -0
- data/knife-chop.gemspec +118 -0
- data/lib/chef/knife/chop/chef_data_bag_item.rb +43 -0
- data/lib/chef/knife/chop/chef_environment.rb +47 -0
- data/lib/chef/knife/chop/chef_knife.rb +85 -0
- data/lib/chef/knife/chop/chef_part.rb +191 -0
- data/lib/chef/knife/chop/chef_role.rb +48 -0
- data/lib/chef/knife/chop/cookbook_upload.rb +143 -0
- data/lib/chef/knife/chop/data_bag_from_file.rb +87 -0
- data/lib/chef/knife/chop/environment_from_file.rb +79 -0
- data/lib/chef/knife/chop/errors.rb +5 -0
- data/lib/chef/knife/chop/logging.rb +245 -0
- data/lib/chef/knife/chop/role_from_file.rb +45 -0
- data/lib/chef/knife/chop/translate.rb +23 -0
- data/lib/chef/knife/chop/translate/eden.rb +23 -0
- data/lib/chef/knife/chop/translate/rbeautify.rb +24 -0
- data/lib/chef/knife/chop/ui.rb +110 -0
- data/lib/chef/knife/chop/version.rb +9 -0
- data/lib/chef/knife/chop_base.rb +821 -0
- data/lib/chef/knife/chop_translate.rb +161 -0
- data/lib/chef/knife/chop_upload.rb +199 -0
- data/lib/ruby-beautify/Gemfile +4 -0
- data/lib/ruby-beautify/LICENSE +22 -0
- data/lib/ruby-beautify/README.md +39 -0
- data/lib/ruby-beautify/RELEASE.md +13 -0
- data/lib/ruby-beautify/Rakefile +2 -0
- data/lib/ruby-beautify/bin/rbeautify +28 -0
- data/lib/ruby-beautify/lib/beautifier.rb +168 -0
- data/lib/ruby-beautify/lib/ruby-beautify.rb +26 -0
- data/lib/ruby-beautify/lib/ruby-beautify/block_end.rb +23 -0
- data/lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb +153 -0
- data/lib/ruby-beautify/lib/ruby-beautify/block_start.rb +119 -0
- data/lib/ruby-beautify/lib/ruby-beautify/config/ruby.rb +131 -0
- data/lib/ruby-beautify/lib/ruby-beautify/language.rb +37 -0
- data/lib/ruby-beautify/lib/ruby-beautify/line.rb +53 -0
- data/lib/ruby-beautify/lib/ruby-beautify/version.rb +3 -0
- data/lib/ruby-beautify/ruby-beautify.gemspec +17 -0
- data/lib/ruby-beautify/spec/fixtures/ruby.yml +408 -0
- data/lib/ruby-beautify/spec/rbeautify/block_matcher_spec.rb +89 -0
- data/lib/ruby-beautify/spec/rbeautify/block_start_spec.rb +51 -0
- data/lib/ruby-beautify/spec/rbeautify/config/ruby_spec.rb +183 -0
- data/lib/ruby-beautify/spec/rbeautify/line_spec.rb +73 -0
- data/lib/ruby-beautify/spec/rbeautify_spec.rb +1 -0
- data/lib/ruby-beautify/spec/spec_helper.rb +124 -0
- data/spec/knife-chop_spec.rb +7 -0
- data/spec/spec_helper.rb +12 -0
- metadata +233 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Jacob (<adam@opscode.com>)
|
3
|
+
# Author:: Nuo Yan (<nuo@opscode.com>)
|
4
|
+
# Author:: Christopher Brown (<cb@opscode.com>)
|
5
|
+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
|
6
|
+
# License:: Apache License, Version 2.0
|
7
|
+
#
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
# you may not use this file except in compliance with the License.
|
10
|
+
# You may obtain a copy of the License at
|
11
|
+
#
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
#
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
# See the License for the specific language governing permissions and
|
18
|
+
# limitations under the License.
|
19
|
+
#
|
20
|
+
require 'json'
|
21
|
+
require 'chef/knife/chop/translate'
|
22
|
+
|
23
|
+
require 'chef/knife/chop/errors'
|
24
|
+
include ChopErrors
|
25
|
+
require 'chef/knife/chop/chef_part'
|
26
|
+
|
27
|
+
class ::Chef::DataBagItem
|
28
|
+
|
29
|
+
# --------------------------------------------------------------------------------------------------------------------
|
30
|
+
def generate_json(part)
|
31
|
+
self.to_json
|
32
|
+
end
|
33
|
+
|
34
|
+
# --------------------------------------------------------------------------------------------------------------------
|
35
|
+
def generate_rb(part)
|
36
|
+
part.logger.todo "generate_rb", true
|
37
|
+
src = []
|
38
|
+
src << "name '#{@name}'"
|
39
|
+
src << "description '#{@description}'"
|
40
|
+
src.join("\n")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Stephen Delano (<stephen@opscode.com>)
|
3
|
+
# Author:: Seth Falcon (<seth@opscode.com>)
|
4
|
+
# Author:: John Keiser (<jkeiser@ospcode.com>)
|
5
|
+
# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
|
6
|
+
# Copyright:: Copyright 2010-2011 Opscode, Inc.
|
7
|
+
# License:: Apache License, Version 2.0
|
8
|
+
#
|
9
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
+
# you may not use this file except in compliance with the License.
|
11
|
+
# You may obtain a copy of the License at
|
12
|
+
#
|
13
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
|
+
#
|
15
|
+
# Unless required by applicable law or agreed to in writing, software
|
16
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
+
# See the License for the specific language governing permissions and
|
19
|
+
# limitations under the License.
|
20
|
+
#
|
21
|
+
require 'json'
|
22
|
+
require 'chef/knife/chop/translate'
|
23
|
+
|
24
|
+
require 'chef/knife/chop/errors'
|
25
|
+
include ChopErrors
|
26
|
+
require 'chef/knife/chop/chef_part'
|
27
|
+
|
28
|
+
class ::Chef::Environment
|
29
|
+
attr_reader :logger
|
30
|
+
|
31
|
+
# --------------------------------------------------------------------------------------------------------------------
|
32
|
+
def generate_json(part)
|
33
|
+
self.to_json
|
34
|
+
end
|
35
|
+
|
36
|
+
# --------------------------------------------------------------------------------------------------------------------
|
37
|
+
def generate_rb(part)
|
38
|
+
src = []
|
39
|
+
src << "name '#{@name}'"
|
40
|
+
src << "description '#{@description}'"
|
41
|
+
src << part.hash_to_rb('cookbook_versions')
|
42
|
+
src << part.hash_to_rb('default_attributes')
|
43
|
+
src << part.hash_to_rb('override_attributes')
|
44
|
+
src.join("\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Christo De Lange (<opscode@dldinternet.com>)
|
3
|
+
# Copyright:: Copyright (c) 2013 DLDInternet, 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
|
+
# Author:: Stephen Delano (<stephen@opscode.com>)
|
20
|
+
# Copyright:: Copyright (c) 2010 Opscode, Inc.
|
21
|
+
# License:: Apache License, Version 2.0
|
22
|
+
#
|
23
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
24
|
+
# you may not use this file except in compliance with the License.
|
25
|
+
# You may obtain a copy of the License at
|
26
|
+
#
|
27
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
28
|
+
#
|
29
|
+
# Unless required by applicable law or agreed to in writing, software
|
30
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
31
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
32
|
+
# See the License for the specific language governing permissions and
|
33
|
+
# limitations under the License.
|
34
|
+
#
|
35
|
+
require 'chef/knife/chop/chef_part'
|
36
|
+
|
37
|
+
class ::Chef::Knife
|
38
|
+
attr_reader :rsrctype
|
39
|
+
attr_reader :location
|
40
|
+
|
41
|
+
# --------------------------------------------------------------------------------------------------------------------
|
42
|
+
def translate()
|
43
|
+
if config[:all]
|
44
|
+
translate_all()
|
45
|
+
else
|
46
|
+
if @name_args[0].nil?
|
47
|
+
show_usage
|
48
|
+
ui.fatal("You must specify a file to translate")
|
49
|
+
exit 1
|
50
|
+
end
|
51
|
+
|
52
|
+
@name_args.each do |arg|
|
53
|
+
translate_one(arg)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# --------------------------------------------------------------------------------------------------------------------
|
59
|
+
private
|
60
|
+
# --------------------------------------------------------------------------------------------------------------------
|
61
|
+
|
62
|
+
# --------------------------------------------------------------------------------------------------------------------
|
63
|
+
def translate_all()
|
64
|
+
set = self.send("find_all_#{@location}")
|
65
|
+
if set.empty?
|
66
|
+
ui.fatal("Unable to find any #{@rsrctype} files in '#{subc.send("#{@rsrctype}_path")}'")
|
67
|
+
exit(1)
|
68
|
+
end
|
69
|
+
set.each do |ent|
|
70
|
+
translate_one(ent)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# --------------------------------------------------------------------------------------------------------------------
|
75
|
+
def translate_one(ent)
|
76
|
+
location = loader.find_file(@location, ent)
|
77
|
+
resource = loader.load_from(@location, ent)
|
78
|
+
part = ::Chef::Part.new(resource,location)
|
79
|
+
part.translate(@config)
|
80
|
+
resource = part.resource
|
81
|
+
output(format_for_display(resource)) if config[:print_after]
|
82
|
+
ui.info("Translated #{@rsrctype.capitalize} #{resource.name}")
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Stephen Delano (<stephen@opscode.com>)
|
3
|
+
# Author:: Seth Falcon (<seth@opscode.com>)
|
4
|
+
# Author:: John Keiser (<jkeiser@ospcode.com>)
|
5
|
+
# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
|
6
|
+
# Copyright:: Copyright 2010-2011 Opscode, Inc.
|
7
|
+
# License:: Apache License, Version 2.0
|
8
|
+
#
|
9
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
+
# you may not use this file except in compliance with the License.
|
11
|
+
# You may obtain a copy of the License at
|
12
|
+
#
|
13
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
|
+
#
|
15
|
+
# Unless required by applicable law or agreed to in writing, software
|
16
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
+
# See the License for the specific language governing permissions and
|
19
|
+
# limitations under the License.
|
20
|
+
#
|
21
|
+
require 'json'
|
22
|
+
require 'chef/knife/chop/translate'
|
23
|
+
|
24
|
+
require 'chef/knife/chop/errors'
|
25
|
+
include ChopErrors
|
26
|
+
|
27
|
+
require 'chef/run_list'
|
28
|
+
# ======================================================================================================================
|
29
|
+
class ::Chef::RunList
|
30
|
+
def to_rb
|
31
|
+
items = @run_list_items.map{|i|
|
32
|
+
i.to_rb
|
33
|
+
}
|
34
|
+
"[ #{items.join(",\n")} ]"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'chef/run_list/run_list_item'
|
39
|
+
# ======================================================================================================================
|
40
|
+
class ::Chef::RunList::RunListItem
|
41
|
+
def to_rb
|
42
|
+
"'#{@type}[#{@name}#{@version ? "@#{@version}" :""}]'"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# ======================================================================================================================
|
47
|
+
class ::Object
|
48
|
+
def to_rb
|
49
|
+
to_s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# ======================================================================================================================
|
54
|
+
class ::Hash
|
55
|
+
def to_rb
|
56
|
+
self.inspect
|
57
|
+
end
|
58
|
+
def inspect
|
59
|
+
if size == 0
|
60
|
+
'{}'
|
61
|
+
else
|
62
|
+
a = keys.inject([]) do |a, key|
|
63
|
+
k = "#{key}:"
|
64
|
+
unless key.match(%r(^[\w]+$))
|
65
|
+
if key.match(%r([']))
|
66
|
+
q = '"'
|
67
|
+
else
|
68
|
+
q = "'"
|
69
|
+
end
|
70
|
+
k = "#{q}#{key}#{q} =>"
|
71
|
+
end
|
72
|
+
v = fetch(key)
|
73
|
+
if v.is_a?(String)
|
74
|
+
if v.match(%r('))
|
75
|
+
a << "#{k} \"#{v}\""
|
76
|
+
else
|
77
|
+
a << "#{k} '#{v}'"
|
78
|
+
end
|
79
|
+
else
|
80
|
+
a << "#{k} #{v.to_rb}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
"{\n #{a.join(",\n")},\n }"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class ::Chef
|
89
|
+
class Part
|
90
|
+
attr_accessor :resource
|
91
|
+
attr_accessor :location
|
92
|
+
attr_reader :logger
|
93
|
+
attr_reader :from
|
94
|
+
attr_reader :to
|
95
|
+
|
96
|
+
def initialize(resource,path)
|
97
|
+
self.resource = resource
|
98
|
+
self.location = path
|
99
|
+
end
|
100
|
+
|
101
|
+
def translate(config)
|
102
|
+
@config = config
|
103
|
+
@logger = @config[:logger]
|
104
|
+
@from,@to = @config[:translate]
|
105
|
+
@logger.debug "#{@resource.class.name} To #{@to}"
|
106
|
+
unless self.resource.respond_to?("generate_#{@to}")
|
107
|
+
raise ChopInternalError.new("Unable to support translation '#{@from}' --> '#{@to}' "+
|
108
|
+
"because #{self.resource.class.name} CANNOT 'generate_#{@to}'")
|
109
|
+
end
|
110
|
+
|
111
|
+
str = self.send("translate_to_#{@to}")
|
112
|
+
end
|
113
|
+
|
114
|
+
# --------------------------------------------------------------------------------------------------------------------
|
115
|
+
def translate_to_json()
|
116
|
+
obj = JSON.parse(self.resource.generate_json(self))
|
117
|
+
json = JSON.pretty_generate(obj)
|
118
|
+
save_source(json,"json")
|
119
|
+
end
|
120
|
+
|
121
|
+
# --------------------------------------------------------------------------------------------------------------------
|
122
|
+
def hash_to_rb(hash)
|
123
|
+
line = ["# #{hash}()"]
|
124
|
+
if self.resource.send(hash).size > 0
|
125
|
+
line = ["#{hash}( "]
|
126
|
+
self.resource.send(hash).each{|k,v|
|
127
|
+
s = v.to_rb
|
128
|
+
#s = s.gsub(%r/"'/, %(``')).gsub(%r/'"/, %('``)).gsub(%r/"/, "'").gsub(%r/``'/, %("')).gsub(%r/'``/, %('"))
|
129
|
+
unless k.match(%r([\.\-]))
|
130
|
+
line << "#{k}: #{s},"
|
131
|
+
else
|
132
|
+
line << "'#{k}' => #{s},"
|
133
|
+
end
|
134
|
+
}
|
135
|
+
#line[-1].gsub!(%r(,\s*$), "")
|
136
|
+
line << ")"
|
137
|
+
end
|
138
|
+
line.join("\n")
|
139
|
+
end
|
140
|
+
|
141
|
+
## --------------------------------------------------------------------------------------------------------------------
|
142
|
+
#def cookbook_versions_to_rb(cookbook_versions)
|
143
|
+
# line = ["# cookbook_versions()"]
|
144
|
+
# if cookbook_versions.size > 0
|
145
|
+
# line = ["cookbook_versions( "]
|
146
|
+
# cookbook_versions.map{|k,v|
|
147
|
+
# line << "#{k}: #{v.to_s.gsub(%r/"/, "'")},"
|
148
|
+
# }
|
149
|
+
# line[-1].gsub!(%r(,\s*$), "")
|
150
|
+
# line << ")"
|
151
|
+
# end
|
152
|
+
# line.join("\n")
|
153
|
+
#end
|
154
|
+
|
155
|
+
# --------------------------------------------------------------------------------------------------------------------
|
156
|
+
def run_lists_to_rb(run_lists,name='run_lists')
|
157
|
+
line = ["# #{name}()"]
|
158
|
+
if run_lists.size > 0
|
159
|
+
line = ["#{name}( "]
|
160
|
+
run_lists.map{|k,v|
|
161
|
+
line << "'#{k}' => #{v.to_rb},"
|
162
|
+
}
|
163
|
+
line[-1] = line[-1].gsub(%r(\n), "<nl>").gsub(%r(<nl>$), "").gsub(%r(,\s*$), "").gsub(%r(<nl>), "\n")
|
164
|
+
line << ")"
|
165
|
+
end
|
166
|
+
line.join("\n")
|
167
|
+
end
|
168
|
+
|
169
|
+
# --------------------------------------------------------------------------------------------------------------------
|
170
|
+
def translate_to_rb()
|
171
|
+
#rb = Eden::Formatter.format_source(to_rb)
|
172
|
+
rb = ::RBeautify.beautify_string :ruby, @resource.generate_rb(self)
|
173
|
+
#rb = @resource.generate_rb(self)
|
174
|
+
#sexp = Ripper::SexpBuilder.new(rb).parse
|
175
|
+
#puts Sorcerer.source(sexp, multiline: true, indent: true)
|
176
|
+
save_source(rb, "rb")
|
177
|
+
end
|
178
|
+
|
179
|
+
# --------------------------------------------------------------------------------------------------------------------
|
180
|
+
def save_source(source,ext)
|
181
|
+
@logger.info "Saving '#{ext}'"
|
182
|
+
location = self.location.gsub(%r(\.#{@from}$), ".#{@to}")
|
183
|
+
@logger.debug "Location: #{location}"
|
184
|
+
#@logger.debug source
|
185
|
+
File.open(location, 'w') do |f|
|
186
|
+
f.write source
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Stephen Delano (<stephen@opscode.com>)
|
3
|
+
# Author:: Seth Falcon (<seth@opscode.com>)
|
4
|
+
# Author:: John Keiser (<jkeiser@ospcode.com>)
|
5
|
+
# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
|
6
|
+
# Copyright:: Copyright 2010-2011 Opscode, Inc.
|
7
|
+
# License:: Apache License, Version 2.0
|
8
|
+
#
|
9
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
+
# you may not use this file except in compliance with the License.
|
11
|
+
# You may obtain a copy of the License at
|
12
|
+
#
|
13
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
|
+
#
|
15
|
+
# Unless required by applicable law or agreed to in writing, software
|
16
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
+
# See the License for the specific language governing permissions and
|
19
|
+
# limitations under the License.
|
20
|
+
#
|
21
|
+
require 'json'
|
22
|
+
require 'chef/knife/chop/translate'
|
23
|
+
|
24
|
+
require 'chef/knife/chop/errors'
|
25
|
+
include ChopErrors
|
26
|
+
require 'chef/knife/chop/chef_part'
|
27
|
+
|
28
|
+
class ::Chef::Role
|
29
|
+
attr_reader :logger
|
30
|
+
|
31
|
+
# --------------------------------------------------------------------------------------------------------------------
|
32
|
+
def generate_json(part)
|
33
|
+
self.to_json
|
34
|
+
end
|
35
|
+
|
36
|
+
# --------------------------------------------------------------------------------------------------------------------
|
37
|
+
def generate_rb(part)
|
38
|
+
src = []
|
39
|
+
src << "name '#{@name}'"
|
40
|
+
src << "description '#{@description}'"
|
41
|
+
src << part.hash_to_rb('default_attributes')
|
42
|
+
src << part.hash_to_rb('override_attributes')
|
43
|
+
src << part.run_lists_to_rb(@run_list) if @run_list
|
44
|
+
src << part.run_lists_to_rb(@env_run_lists,'env_run_lists') if @env_run_lists
|
45
|
+
src.join("\n")
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Christo De Lange (<opscode@dldinternet.com>)
|
3
|
+
# Copyright:: Copyright (c) 2013 DLDInternet, 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
|
+
if ::Chef::Knife.const_defined?('CookbookUpload')
|
19
|
+
class ::Chef::Knife::CookbookUpload
|
20
|
+
if self.const_defined?('CHECKSUM')
|
21
|
+
remove_const(:CHECKSUM)
|
22
|
+
end
|
23
|
+
if self.const_defined?('MATCH_CHECKSUM')
|
24
|
+
remove_const(:MATCH_CHECKSUM)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
require 'chef/knife/cookbook_upload'
|
29
|
+
class ::String
|
30
|
+
def plural(count)
|
31
|
+
count > 1 ? self+'s' : self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ::Chef::Knife::CookbookUpload
|
36
|
+
def run
|
37
|
+
raise StandardError.new("I was crafted from Chef::Knife::VERSION == '11.6.2'. Please verify that #{self.class.name}.run is still relevant in your version '#{Chef::VERSION}'!") unless Chef::VERSION == '11.6.2'
|
38
|
+
# Sanity check before we load anything from the server
|
39
|
+
unless config[:all]
|
40
|
+
if @name_args.empty?
|
41
|
+
show_usage
|
42
|
+
ui.fatal("You must specify the --all flag or at least one cookbook name")
|
43
|
+
exit 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
config[:cookbook_path] ||= Chef::Config[:cookbook_path]
|
48
|
+
|
49
|
+
if @name_args.empty? and ! config[:all]
|
50
|
+
show_usage
|
51
|
+
ui.fatal("You must specify the --all flag or at least one cookbook name")
|
52
|
+
exit 1
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_environment_valid!
|
56
|
+
version_constraints_to_update = {}
|
57
|
+
upload_failures = 0
|
58
|
+
upload_ok = 0
|
59
|
+
|
60
|
+
# Get a list of cookbooks and their versions from the server
|
61
|
+
# to check for the existence of a cookbook's dependencies.
|
62
|
+
@server_side_cookbooks = Chef::CookbookVersion.list_all_versions
|
63
|
+
justify_width = @server_side_cookbooks.map {|name| name.size}.max.to_i + 2
|
64
|
+
if config[:all]
|
65
|
+
cookbook_repo.load_cookbooks
|
66
|
+
cbs = []
|
67
|
+
cookbook_repo.each do |cookbook_name, cookbook|
|
68
|
+
cbs << cookbook
|
69
|
+
cookbook.freeze_version if config[:freeze]
|
70
|
+
version_constraints_to_update[cookbook_name] = cookbook.version
|
71
|
+
end
|
72
|
+
begin
|
73
|
+
upload(cbs, justify_width)
|
74
|
+
rescue ::Chef::Exceptions::CookbookFrozen
|
75
|
+
ui.warn("Not updating version constraints for some cookbooks in the environment as the cookbook is frozen.")
|
76
|
+
end
|
77
|
+
ui.info("Uploaded all cookbooks.")
|
78
|
+
else
|
79
|
+
if @name_args.empty?
|
80
|
+
show_usage
|
81
|
+
ui.error("You must specify the --all flag or at least one cookbook name")
|
82
|
+
exit 1
|
83
|
+
end
|
84
|
+
|
85
|
+
cookbooks_to_upload.each do |cookbook_name, cookbook|
|
86
|
+
cookbook.freeze_version if config[:freeze]
|
87
|
+
begin
|
88
|
+
upload([cookbook], justify_width)
|
89
|
+
upload_ok += 1
|
90
|
+
version_constraints_to_update[cookbook_name] = cookbook.version
|
91
|
+
rescue ::Chef::Exceptions::CookbookNotFoundInRepo => e
|
92
|
+
upload_failures += 1
|
93
|
+
ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
|
94
|
+
Log.debug(e)
|
95
|
+
upload_failures += 1
|
96
|
+
rescue ::Chef::Exceptions::CookbookFrozen
|
97
|
+
ui.warn("Not updating version constraints for #{cookbook_name} in the environment as the cookbook is frozen.")
|
98
|
+
upload_failures += 1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
upload_skips = @name_args.length - @cookbooks_to_upload.length
|
103
|
+
|
104
|
+
if upload_failures == 0
|
105
|
+
if upload_skips == 0
|
106
|
+
ui.step "Uploaded #{upload_ok} cookbook".plural(upload_ok)+"."
|
107
|
+
elsif upload_skips > 0 && upload_ok > 0
|
108
|
+
ui.step "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} ok but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included."
|
109
|
+
elsif upload_ok == 0
|
110
|
+
ui.error "Did not upload any cookbooks."
|
111
|
+
exit 1
|
112
|
+
end
|
113
|
+
elsif upload_failures > 0 && upload_ok > 0
|
114
|
+
if upload_skips == 0
|
115
|
+
ui.warn "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} ok but #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
|
116
|
+
elsif upload_skips > 0
|
117
|
+
ui.warn "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} ok but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included and #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
|
118
|
+
end
|
119
|
+
elsif upload_failures > 0 && upload_ok == 0
|
120
|
+
ui.error "Failed to upload #{upload_failures} #{'cookbook'.plural(upload_failures)}."
|
121
|
+
exit 1
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
unless version_constraints_to_update.empty?
|
126
|
+
update_version_constraints(version_constraints_to_update) if config[:environment]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def upload(cookbooks, justify_width)
|
131
|
+
cookbooks.each do |cb|
|
132
|
+
ui.step("Uploading #{cb.name.to_s.ljust(justify_width + 10)} [#{cb.version}]")
|
133
|
+
check_for_broken_links!(cb)
|
134
|
+
check_for_dependencies!(cb)
|
135
|
+
end
|
136
|
+
::Chef::CookbookUploader.new(cookbooks, config[:cookbook_path], :force => config[:force]).upload_cookbooks
|
137
|
+
rescue ::Chef::Exceptions::CookbookFrozen => e
|
138
|
+
ui.error e
|
139
|
+
raise
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|