79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/chef/knife/cookbook_upload.rb', line 79
def run
if ! config[:all] && @name_args.empty?
show_usage
ui.fatal("You must specify the --all flag or at least one cookbook name")
exit 1
end
config[:cookbook_path] ||= Chef::Config[:cookbook_path]
assert_environment_valid!
version_constraints_to_update = {}
upload_failures = 0
upload_ok = 0
cookbooks = []
cookbooks_to_upload.each do |cookbook_name, cookbook|
raise Chef::Exceptions::MetadataNotFound.new(cookbook.root_paths[0], cookbook_name) unless cookbook.has_metadata_file?
if cookbook.metadata.name.nil?
message = "Cookbook loaded at path [#{cookbook.root_paths[0]}] has invalid metadata: #{cookbook.metadata.errors.join("; ")}"
raise Chef::Exceptions::MetadataNotValid, message
end
cookbooks << cookbook
end
if cookbooks.empty?
cookbook_path = config[:cookbook_path].respond_to?(:join) ? config[:cookbook_path].join(", ") : config[:cookbook_path]
ui.warn("Could not find any cookbooks in your cookbook path: '#{File.expand_path(cookbook_path)}'. Use --cookbook-path to specify the desired path.")
else
Chef::CookbookLoader.copy_to_tmp_dir_from_array(cookbooks) do |tmp_cl|
tmp_cl.load_cookbooks
tmp_cl.compile_metadata
tmp_cl.freeze_versions if config[:freeze]
cookbooks_for_upload = []
tmp_cl.each do |cookbook_name, cookbook|
cookbooks_for_upload << cookbook
version_constraints_to_update[cookbook_name] = cookbook.version
end
if config[:all]
if cookbooks_for_upload.any?
begin
upload(cookbooks_for_upload)
rescue Chef::Exceptions::CookbookFrozen
ui.warn("Not updating version constraints for some cookbooks in the environment as the cookbook is frozen.")
ui.error("Uploading of some of the cookbooks must be failed. Remove cookbook whose version is frozen from your cookbooks repo OR use --force option.")
upload_failures += 1
rescue SystemExit => e
raise exit e.status
end
ui.info("Uploaded all cookbooks.") if upload_failures == 0
end
else
tmp_cl.each do |cookbook_name, cookbook|
upload([cookbook])
upload_ok += 1
rescue Exceptions::CookbookNotFoundInRepo => e
upload_failures += 1
ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
Log.debug(e)
upload_failures += 1
rescue Exceptions::CookbookFrozen
ui.warn("Not updating version constraints for #{cookbook_name} in the environment as the cookbook is frozen.")
upload_failures += 1
rescue SystemExit => e
raise exit e.status
end
if upload_failures == 0
ui.info "Uploaded #{upload_ok} cookbook#{upload_ok == 1 ? "" : "s"}."
elsif upload_failures > 0 && upload_ok > 0
ui.warn "Uploaded #{upload_ok} cookbook#{upload_ok == 1 ? "" : "s"} ok but #{upload_failures} " +
"cookbook#{upload_failures == 1 ? "" : "s"} upload failed."
elsif upload_failures > 0 && upload_ok == 0
ui.error "Failed to upload #{upload_failures} cookbook#{upload_failures == 1 ? "" : "s"}."
exit 1
end
end
unless version_constraints_to_update.empty?
update_version_constraints(version_constraints_to_update) if config[:environment]
end
end
end
end
|