class KnifePlayground::PgClientnodeDelete::PgGitCookbookUpload

Most of this code comes from Opscode Knife cookbook_upload.rb plugin:

github.com/opscode/chef/blob/master/chef/lib/chef/knife/cookbook_upload.rb

Minor modifications to add Git support added

Constants

CHECKSUM
MATCH_CHECKSUM

Public Instance Methods

cookbook_repo() click to toggle source
# File lib/chef/knife/pg.rb, line 160
def cookbook_repo
  @cookbook_loader ||= begin
    Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest, config[:cookbook_path]) }
    Chef::CookbookLoader.new(config[:cookbook_path])
  end
end
environment() click to toggle source
# File lib/chef/knife/pg.rb, line 175
def environment
  @environment ||= config[:environment] ? Environment.load(config[:environment]) : nil
end
run() click to toggle source
# File lib/chef/knife/pg.rb, line 106
def run
  git_urls = @name_args.dup
  git_urls.each do |n| 
    if n =~ /^git:\/\//
      @name_args.delete n
      git_repo = n
      git_clone(git_repo)
    end
  end
  
  config[:cookbook_path] ||= Chef::Config[:cookbook_path]

  assert_environment_valid!
  version_constraints_to_update = {}
  # Get a list of cookbooks and their versions from the server
  # for checking existence of dependending cookbooks.
  @server_side_cookbooks = Chef::CookbookVersion.list

  if config[:all]
    justify_width = cookbook_repo.cookbook_names.map {|name| name.size}.max.to_i + 2
    cookbook_repo.each do |cookbook_name, cookbook|
      cookbook.freeze_version if config[:freeze]
      upload(cookbook, justify_width)
      version_constraints_to_update[cookbook_name] = cookbook.version
    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
    justify_width = @name_args.map {|name| name.size }.max.to_i + 2
    @name_args.each do |cookbook_name|
      begin
        cookbook = cookbook_repo[cookbook_name]
        if config[:depends]
          cookbook.metadata.dependencies.each do |dep, versions|
            @name_args.push dep
          end
        end
        cookbook.freeze_version if config[:freeze]
        upload(cookbook, justify_width)
        version_constraints_to_update[cookbook_name] = cookbook.version
      rescue Chef::Exceptions::CookbookNotFoundInRepo => e
        ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
        Chef::Log.debug(e)
      end
    end
  end

  ui.info "upload complete"
  update_version_constraints(version_constraints_to_update) if config[:environment]
end
update_version_constraints(new_version_constraints) click to toggle source
# File lib/chef/knife/pg.rb, line 167
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

Private Instance Methods

assert_environment_valid!() click to toggle source
# File lib/chef/knife/pg.rb, line 195
def assert_environment_valid!
  environment
rescue Net::HTTPServerException => e
  if e.response.code.to_s == "404"
    ui.error "The environment #{config[:environment]} does not exist on the server, aborting."
    Log.debug(e)
    exit 1
  else
    raise
  end
end
check_dependencies(cookbook) click to toggle source
# File lib/chef/knife/pg.rb, line 241
def check_dependencies(cookbook)
  # for each dependency, check if the version is on the server, or
  # the version is in the cookbooks being uploaded. If not, exit and warn the user.
  cookbook.metadata.dependencies.each do |cookbook_name, version|
    unless check_server_side_cookbooks(cookbook_name, version) || check_uploading_cookbooks(cookbook_name, version)
      # warn the user and exit
      ui.error "Cookbook #{cookbook.name} depends on cookbook #{cookbook_name} version #{version},"
      ui.error "which is not currently being uploaded and cannot be found on the server."
      exit 1
    end
  end
end
check_server_side_cookbooks(cookbook_name, version) click to toggle source
# File lib/chef/knife/pg.rb, line 254
def check_server_side_cookbooks(cookbook_name, version)
  if @server_side_cookbooks[cookbook_name].nil?
    false
  else
    @server_side_cookbooks[cookbook_name]["versions"].each do |versions_hash|
      return true if Chef::VersionConstraint.new(version).include?(versions_hash["version"])
    end
    false
  end
end
check_uploading_cookbooks(cookbook_name, version) click to toggle source
# File lib/chef/knife/pg.rb, line 265
def check_uploading_cookbooks(cookbook_name, version)
  if config[:all]
    # check from all local cookbooks in the path
    unless cookbook_repo[cookbook_name].nil?
      return Chef::VersionConstraint.new(version).include?(cookbook_repo[cookbook_name].version)
    end
  else
    # check from only those in the command argument
    if @name_args.include?(cookbook_name)
      return Chef::VersionConstraint.new(version).include?(cookbook_repo[cookbook_name].version)
    end
  end
  false
end
git_clone(url, opts = {}) click to toggle source
# File lib/chef/knife/pg.rb, line 181
def git_clone(url, opts = {})
  repo = File.basename(URI.parse(url).path.split('/').last, '.git')
  @name_args << repo
  cbpath = Chef::Config[:cookbook_path].first rescue '/var/chef/cookbooks'
  path = File.join(cbpath.first, repo)
  # Error if previous checkout exist
  if File.directory?(path + '/.git')
    ui.info "Cookbook #{repo} already downloaded."
  else
    ui.info "Downloading cookbook from #{url}"
    Git.clone url, path, opts
  end
end
upload(cookbook, justify_width) click to toggle source
# File lib/chef/knife/pg.rb, line 207
def upload(cookbook, justify_width)
  ui.info("Uploading #{cookbook.name.to_s.ljust(justify_width + 10)} [#{cookbook.version}]")

  check_for_broken_links(cookbook)
  check_dependencies(cookbook)
  Chef::CookbookUploader.new(cookbook, config[:cookbook_path], :force => config[:force]).upload_cookbook
rescue Net::HTTPServerException => e
  case e.response.code
  when "409"
    ui.error "Version #{cookbook.version} of cookbook #{cookbook.name} is frozen. Use --force to override."
    Log.debug(e)
  else
    raise
  end
end