class Chef::Knife::ArtifactoryShare

Public Instance Methods

cookbook_version() click to toggle source

@example

cookbook_version #=> 1.0.1

@example

cookbook_version #=> 0.1.0

@example

cookbook_version #=> nil

@return [String, nil] version of cookbook present in cookbook path or nil if no

coobook versions exist locally.
# File lib/chef/knife/artifactory_share.rb, line 81
def cookbook_version
  cl = Chef::CookbookLoader.new(cookbook_path)
  if cl.cookbook_exists?(cookbook_name)
    cookbook = cl[cookbook_name]
    cookbook.version
  end
end
cookbook_versions_in_artifactory() click to toggle source

@example

cookbook_versions_in_artifactory #=> ["1.0.0", "1.0.1", "1.1.0", "1.2.0"]

@return [Array<String>] versions in artifactory

# File lib/chef/knife/artifactory_share.rb, line 61
def cookbook_versions_in_artifactory
  return [] if cookbook_name.nil?

  data = noauth_rest.get("#{supermarket_uri}/cookbooks/#{cookbook_name}")
  data["metrics"]["downloads"]["versions"].keys
end
orig_run()
Alias for: run
run() click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 34
def run
  # I'm forced to use threadlocal until we find a better solution... can't really find a way to pass configuration
  # down to the Chef::CookbookUploader, Chef::ServerAPI, Chef::HTTP or Chef::HTTP::Authenticator
  # (which are created one after another starting) with CookbookUploader to make it skip the signing key verification.
  # Can make the authenticator skip by passing load_signing_key(nil, nil) and opts[:sign_request] => false
  Thread.current[:artifactory_deploy] = "yes"
  # Send artifactory deploy flag to super
  config[:artifactory_deploy] = true
  Chef::Log.debug("[KNIFE-ART] running site share with config: #{config}")

  if !overwrite_cookbook? && cookbook_versions_in_artifactory.include?(cookbook_version)
    ui.info("Cookbook version already exists, skipping upload.")
    exit(0)
  end

  orig_run
ensure
    # always cleanup threadlocal
  Thread.current[:artifactory_deploy] = nil
end
Also aliased as: orig_run

Private Instance Methods

cookbook_name() click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 91
def cookbook_name
  @name_args[0] if @name_args.length >= 1
end
cookbook_path() click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 95
def cookbook_path
  config[:cookbook_path] ||= Chef::Config[:cookbook_path]
end
do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename) click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 129
def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename)
  # Use Artifactory deployment logic only if flag sent by Artifactory plugin
  unless config[:artifactory_deploy]
    Chef::Log.debug("[KNIFE-ART] ArtifactoryShare::do_upload called without artifactory flag, delegating to super")
    orig_do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename)
    return
  end
  # cookbook_filename is set as tempDir/cookbook_name in parent
  cookbook_name = cookbook_filename.split("/")[-1]
  uri = "#{config[:supermarket_site]}/api/v1/cookbooks/#{cookbook_name}"
  uri += "?category=#{cookbook_category}" if cookbook_category
  Chef::Log.debug("[KNIFE-ART] Deploying cookbook #{cookbook_name} to Artifactory url at #{uri}")
  # This guy throws an exception and consumes the request body upon non-ok http code, and deprives us of the
  # ability to do anything with the response itself... i'm letting the parent catch it and terminate.
  # debug log will be able to show the response Artifactory returned in case of errors.
  file_contents = File.open(cookbook_filename, "rb") { |f| f.read }
  # no need to send auth header here, 'normal' HTTP client uses url with credentials from config
  rest.post(uri, file_contents, { "content-type" => "application/x-binary" })
end
Also aliased as: orig_do_upload
get_category(cookbook_name) click to toggle source

Pretty much copy paste of the original, just with authentication on the rest client…

# File lib/chef/knife/artifactory_share.rb, line 108
def get_category(cookbook_name)
  # Use Artifactory deployment logic only if flag sent by Artifactory plugin
  unless config[:artifactory_deploy]
    Chef::Log.debug("[KNIFE-ART] ArtifactoryShare::get_category called without artifactory flag, delegating to super")
    return orig_get_category(cookbook_name)
  end
  begin
    data = noauth_rest.get("#{config[:supermarket_site]}/api/v1/cookbooks/#{@name_args[0]}")
    if data.nil?
      return data["category"]
    else
      return "Other"
    end
  rescue => e
    return "Other" if e.kind_of?(Net::HTTPServerException) && e.response.code == "404"
    ui.fatal("Unable to reach Supermarket: #{e.message}. Increase log verbosity (-VV) for more information.")
    Chef::Log.debug("\n#{e.backtrace.join("\n")}")
    exit(1)
  end
end
Also aliased as: orig_get_category
orig_do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename)
Alias for: do_upload
orig_get_category(cookbook_name)
Alias for: get_category
overwrite_cookbook?() click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 103
def overwrite_cookbook?
  config[:overwrite_cookbook]
end
supermarket_uri() click to toggle source
# File lib/chef/knife/artifactory_share.rb, line 99
def supermarket_uri
  "#{config[:supermarket_site]}/api/v1"
end