class Chef::Knife::CookbookUpload

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/chop/cookbook_upload.rb, line 36
def run
  unless Chef::VERSION.match(%r/^(11|12)\./)
    raise StandardError.new("I was crafted from Chef::VERSION == '11.x'. Please verify that #{self.class.name}.run is still relevant in your version == '#{Chef::VERSION}'!")
  end
  # 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? and ! 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
    cbs = []
    cookbook_repo.each do |cookbook_name, cookbook|
      cbs << cookbook
      cookbook.freeze_version if config[:freeze]
      version_constraints_to_update[cookbook_name] = cookbook.version
    end
    begin
      upload(cbs, justify_width)
    rescue ::Chef::Exceptions::CookbookFrozen
      ui.warn("Not updating version constraints for some cookbooks in the environment as the cookbook is frozen.")
    end
    ui.step("Uploaded all cookbooks.")
  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 ::Chef::Exceptions::CookbookNotFoundInRepo => e
        upload_failures += 1
        ui.fatal("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
        Log.debug(e)
        upload_failures += 1
      rescue ::Chef::Exceptions::CookbookFrozen
        ui.error("Not updating version constraints for #{cookbook_name} in the environment as the cookbook is frozen.")
        upload_failures += 1
      end
    end

    # BEGIN changes DLDInternet
    # upload_failures += @name_args.length - @cookbooks_to_upload.length
    #
    # 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" : ""} 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
    upload_skips = @name_args.length - @cookbooks_to_upload.length

    if upload_failures == 0
      if upload_skips == 0
        ui.step "Uploaded #{upload_ok} cookbook".plural(upload_ok)+"."
      elsif upload_skips > 0 && upload_ok > 0
        ui.step "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included."
      elsif upload_ok == 0
        ui.fatal "Did not upload any cookbooks."
        exit 1
      end
    elsif upload_failures > 0 && upload_ok > 0
      if upload_skips == 0
        ui.error "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
      elsif upload_skips > 0
        ui.error "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included and #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
      end
    elsif upload_failures > 0 && upload_ok == 0
      ui.fatal "Failed to upload #{upload_failures} #{'cookbook'.plural(upload_failures)}."
      exit 1
    end
                      # END Changes DLDInternet
  end

  unless version_constraints_to_update.empty?
    update_version_constraints(version_constraints_to_update) if config[:environment]
  end
end
upload(cookbooks, justify_width) click to toggle source

DLDInternet monkey patch of original

# File lib/chef/knife/chop/cookbook_upload.rb, line 148
def upload(cookbooks, justify_width)
  cookbooks.each do |cb|
    # BEGIN changes DLDInternet
    ui.step("Uploading #{cb.name.to_s.ljust(justify_width + 10)} [#{cb.version}]")
    # END changes DLDInternet
    check_for_broken_links!(cb)
    check_for_dependencies!(cb)
  end
  ::Chef::CookbookUploader.new(cookbooks, config[:cookbook_path], :force => config[:force]).upload_cookbooks
rescue ::Chef::Exceptions::CookbookFrozen => e
  ui.error e
  raise
end