Class: Chef::Knife::CookbookUpload

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/cookbook_upload.rb

Constant Summary collapse

CHECKSUM =
"checksum"
MATCH_CHECKSUM =
/[0-9a-f]{32,}/

Constants inherited from Chef::Knife

OFFICIAL_PLUGINS

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#cookbook_repoObject


187
188
189
190
191
192
# File 'lib/chef/knife/cookbook_upload.rb', line 187

def cookbook_repo
  @cookbook_loader ||= begin
    Chef::Cookbook::FileVendor.fetch_from_disk(config[:cookbook_path])
    Chef::CookbookLoader.new(config[:cookbook_path])
  end
end

#cookbooks_to_uploadObject


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chef/knife/cookbook_upload.rb', line 164

def cookbooks_to_upload
  @cookbooks_to_upload ||=
    if config[:all]
      cookbook_repo.load_cookbooks_without_shadow_warning
    else
      upload_set = {}
      @name_args.each do |cookbook_name|
        begin
          if ! upload_set.has_key?(cookbook_name)
            upload_set[cookbook_name] = cookbook_repo[cookbook_name]
            if config[:depends]
              upload_set[cookbook_name]..dependencies.each { |dep, ver| @name_args << dep }
            end
          end
        rescue Exceptions::CookbookNotFoundInRepo => e
          ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
          Log.debug(e)
        end
      end
      upload_set
    end
end

#environmentObject


201
202
203
# File 'lib/chef/knife/cookbook_upload.rb', line 201

def environment
  @environment ||= config[:environment] ? Environment.load(config[:environment]) : nil
end

#runObject


77
78
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
# File 'lib/chef/knife/cookbook_upload.rb', line 77

def run
  # Sanity check before we load anything from the server
  unless config[:all]
    if @name_args.empty?
      show_usage
      ui.fatal("You must specify the --all flag or at least one cookbook name")
      exit 1
    end
  end

  config[:cookbook_path] ||= Chef::Config[:cookbook_path]

  if @name_args.empty? && ! config[:all]
    show_usage
    ui.fatal("You must specify the --all flag or at least one cookbook name")
    exit 1
  end

  assert_environment_valid!
  warn_about_cookbook_shadowing
  version_constraints_to_update = {}
  upload_failures = 0
  upload_ok = 0

  # Get a list of cookbooks and their versions from the server
  # to check for the existence of a cookbook's dependencies.
  @server_side_cookbooks = Chef::CookbookVersion.list_all_versions
  justify_width = @server_side_cookbooks.map { |name| name.size }.max.to_i + 2
  if config[:all]
    cookbook_repo.load_cookbooks_without_shadow_warning
    cookbooks_for_upload = []
    cookbook_repo.each do |cookbook_name, cookbook|
      cookbooks_for_upload << cookbook
      cookbook.freeze_version if config[:freeze]
      version_constraints_to_update[cookbook_name] = cookbook.version
    end
    if cookbooks_for_upload.any?
      begin
        upload(cookbooks_for_upload, justify_width)
      rescue Exceptions::CookbookFrozen
        ui.warn("Not updating version constraints for some cookbooks in the environment as the cookbook is frozen.")
      end
      ui.info("Uploaded all cookbooks.")
    else
      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: #{cookbook_path}. Use --cookbook-path to specify the desired path.")
    end
  else
    if @name_args.empty?
      show_usage
      ui.error("You must specify the --all flag or at least one cookbook name")
      exit 1
    end

    cookbooks_to_upload.each do |cookbook_name, cookbook|
      cookbook.freeze_version if config[:freeze]
      begin
        upload([cookbook], justify_width)
        upload_ok += 1
        version_constraints_to_update[cookbook_name] = cookbook.version
      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
      end
    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

#update_version_constraints(new_version_constraints) ⇒ Object


194
195
196
197
198
199
# File 'lib/chef/knife/cookbook_upload.rb', line 194

def update_version_constraints(new_version_constraints)
  new_version_constraints.each do |cookbook_name, version|
    environment.cookbook_versions[cookbook_name] = "= #{version}"
  end
  environment.save
end

#warn_about_cookbook_shadowingObject


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/chef/knife/cookbook_upload.rb', line 205

def warn_about_cookbook_shadowing
  # because cookbooks are lazy-loaded, we have to force the loader
  # to load the cookbooks the user intends to upload here:
  cookbooks_to_upload

  unless cookbook_repo.merged_cookbooks.empty?
    ui.warn "* " * 40
    ui.warn(<<-WARNING)
The cookbooks: #{cookbook_repo.merged_cookbooks.join(', ')} exist in multiple places in your cookbook_path.
A composite version of these cookbooks has been compiled for uploading.

#{ui.color('IMPORTANT:', :red, :bold)} In a future version of Chef, this behavior will be removed and you will no longer
be able to have the same version of a cookbook in multiple places in your cookbook_path.
WARNING
    ui.warn "The affected cookbooks are located:"
    ui.output ui.format_for_display(cookbook_repo.merged_cookbook_paths)
    ui.warn "* " * 40
  end
end