lib/cicd/builder/mixlib/repo/artifactory.rb in cicd-builder-0.9.57 vs lib/cicd/builder/mixlib/repo/artifactory.rb in cicd-builder-0.9.58

- old
+ new

@@ -166,10 +166,11 @@ maybeUploadArtifactoryObject(data: data, artifact_module: data[:module], artifact_version: data[:version] || @vars[:version], file_name: file_name, file_ext: file_ext) # -#{@vars[:variant] break unless @vars[:return_code] == 0 } @vars[:return_code] end + alias_method :cicd_uploadToRepo, :uploadToRepo def get_artifact_file_name_ext(data) file_name = File.basename(data[:file]) if file_name =~ %r'^#{data[:name]}' file_name.gsub!(%r'^#{data[:name]}\.*', '') @@ -191,31 +192,32 @@ data = args[:data] artifact_module = args[:artifact_module] artifact_version = args[:artifact_version] file_ext = args[:file_ext] file_name = args[:file_name] + make_copy = (args[:copy].nil? or args[:copy]) artifact_name = getArtifactName(data[:name], file_name, artifact_version, file_ext) # artifact_path = "#{artifactory_org_path()}/#{data[:name]}/#{data[:version]}-#{@vars[:variant]}/#{artifact_name}" artifact_path = getArtifactPath(artifact_module, artifact_version, artifact_name) - objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + objects = maybeArtifactoryObject(artifact_module, artifact_version, false, args[:repo]) upload = false matched = [] if objects.nil? or objects.size == 0 upload = true else - @logger.info "#{artifactory_endpoint()}/#{artifactory_repo()}/#{artifact_path} exists - #{objects.size} results" + @logger.info "#{artifactory_endpoint()}/#{args[:repo] || artifactory_repo()}/#{artifact_path} exists - #{objects.size} results" @logger.info "\t#{objects.map{|o| o.attributes[:uri]}.join("\n\t")}" matched = matchArtifactoryObjects(artifact_path, data, objects) upload ||= (matched.size == 0) end if upload properties_matrix = {} data.select{|k,_| not k.to_s.eql?('file')}.each do |k,v| properties_matrix["product.#{k}"] = v end data[:properties] = properties_matrix.merge(@properties_matrix) - objects = uploadArtifact(artifact_module, artifact_version, artifact_path, data) + objects = uploadArtifact(artifact_module, artifact_version, artifact_path, data, args[:repo]) matched = matchArtifactoryObjects(artifact_path, data, objects) else @logger.info "Keep existing #{matched.map{|o| o.attributes[:uri]}.join("\t")}" end if data[:temp] @@ -226,20 +228,20 @@ else @logger.warn "Temporary file disappeared: #{data.ai}" end end @vars[:return_code] = Errors::ARTIFACT_NOT_UPLOADED unless matched.size > 0 - if @vars[:return_code] == 0 + if @vars[:return_code] == 0 and make_copy artifact_version += "-#{data[:build] || @vars[:build_num]}" artifact_name = getArtifactName(data[:name], file_name, artifact_version, file_ext, ) artifact_path = getArtifactPath(artifact_module, artifact_version, artifact_name) - copies = maybeArtifactoryObject(artifact_module, artifact_version, false) + copies = maybeArtifactoryObject(artifact_module, artifact_version, false, args[:repo]) matched = matchArtifactoryObjects(artifact_path, data, copies) upload = (matched.size == 0) if upload objects.each do |artifact| - copied = copyArtifact(artifact_module, artifact_version, artifact_path, artifact) + copied = copyArtifact(artifact_module, artifact_version, artifact_path, artifact, args[:repo]) unless copied.size > 0 @vars[:return_code] = Errors::ARTIFACT_NOT_COPIED break end end @@ -253,10 +255,11 @@ args[:file_ext] = file_ext args[:file_name] = file_name @vars[:return_code] end + alias_method :cicd_maybeUploadArtifactoryObject, :maybeUploadArtifactoryObject def matchArtifactoryObjects(artifact_path, data, objects) # matched = false objects.select do |artifact| @logger.debug "\tChecking: #{artifact.attributes.ai} for #{artifact_path}" @@ -275,34 +278,17 @@ def getArtifactName(name, file_name, artifact_version, file_ext) artifact_name = "#{name}#{artifact_version.empty? ? '' : "-#{artifact_version}"}.#{file_ext}" # #{file_name} end # --------------------------------------------------------------------------------------------------------------- - def maybeArtifactoryObject(artifact_name,artifact_version,wide=true) + def maybeArtifactoryObject(artifact_name,artifact_version,wide=true,repo=nil) begin # Get a list of matching artifacts in this repository - @logger.info "Artifactory gavc_search g=#{artifactory_org_path()},a=#{artifact_name},v=#{artifact_version},r=#{artifactory_repo()}" + @logger.info "Artifactory gavc_search g=#{artifactory_org_path()},a=#{artifact_name},v=#{artifact_version},r=#{repo || artifactory_repo()}" @arti_search_result = [] - # results = ::Parallel.map([:search,:progress], preserve_results: true, in_threads: 2) { |task| - # if task == :search - # @logger.debug 'searching ... ' - # @arti_search_result = @client.artifact_gavc_search(group: artifactory_org_path(), name: artifact_name, version: "#{artifact_version}", repos: [artifactory_repo()]) - # @logger.debug 'searching complete!' - # raise ::Parallel::Kill - # # raise ::Parallel::Break # -> stops after all current items are finished - # else - # progressbar = ::ProgressBar.create(:title => 'artifact_gavc_search', progress_mark: '=', length: 30, remainder_mark: '.') - # 30.times { |i| - # @logger.debug i - # sleep 1 - # progressbar.increment - # } - # raise ::Parallel::Kill - # end - # } monitor(30, 'artifact_gavc_search'){ - @arti_search_result = @client.artifact_gavc_search(group: artifactory_org_path(), name: artifact_name, version: "#{artifact_version}", repos: [artifactory_repo()]) + @arti_search_result = @client.artifact_gavc_search(group: artifactory_org_path(), name: artifact_name, version: "#{artifact_version}", repos: [repo || artifactory_repo()]) } # noinspection RubyScope if @arti_search_result.size > 0 @logger.info "\tresult: #{@arti_search_result}" elsif wide @@ -315,10 +301,30 @@ @logger.error "Artifactory error: #{e.class.name} #{e.message}" raise e end end + # --------------------------------------------------------------------------------------------------------------- + def latestArtifactoryVersion(artifact_name, repo=nil) + begin + # Get a list of matching artifacts in this repository + @logger.info "Artifactory latest_version g=#{artifactory_org_path()},a=#{artifact_name},r=#{repo || artifactory_repo()}" + @arti_search_result = [] + monitor(30, 'artifact_latest_version'){ + @arti_search_result = ::Artifactory::Resource::Artifact.latest_version(client: @client, group: artifactory_org_path(), name: artifact_name, repos: [repo || artifactory_repo()]) + } + # noinspection RubyScope + if @arti_search_result.size > 0 + @logger.info "\tresult: #{@arti_search_result}" + end + @arti_search_result + rescue Exception => e + @logger.error "Artifactory error: #{e.class.name} #{e.message}" + raise e + end + end + def monitor(limit,title='Progress') raise 'Must have a block' unless block_given? thread = Thread.new(){ yield } @@ -335,29 +341,29 @@ end end thread.kill if thread.alive? or thread.stop? end - def uploadArtifact(artifact_module, artifact_version, artifact_path, data) + def uploadArtifact(artifact_module, artifact_version, artifact_path, data, repo=nil) data[:size] = File.size(data[:file]) artifact = ::Artifactory::Resource::Artifact.new(local_path: data[:file], client: @client) # noinspection RubyStringKeysInHashInspection artifact.checksums = { 'md5' => data[:md5], 'sha1' => data[:sha1], } artifact.size = data[:size] @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Start upload #{artifact_path} = #{data[:size]} bytes" monitor(30, 'upload') { - @arti_upload_result = artifact.upload(artifactory_repo(), "#{artifact_path}", data[:properties] || {}) + @arti_upload_result = artifact.upload(repo || artifactory_repo(), "#{artifact_path}", data[:properties] || {}) } @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Uploaded: #{@arti_upload_result.attributes.select { |k, _| k != :client }.ai}" 3.times{ @arti_upload_checksum = false monitor(30, 'upload_checksum') { begin - artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :sha1, data[:sha1]) + artifact.upload_checksum(repo || artifactory_repo(), "#{artifact_path}", :sha1, data[:sha1]) @arti_upload_checksum = true rescue Exception => e @logger.fatal "Failed to upload #{artifact_path}: #{e.class.name} #{e.message}" raise e end @@ -367,11 +373,11 @@ raise "Failed to upload SHA1 for #{artifact_path}" unless @arti_upload_checksum 3.times{ @arti_upload_checksum = false monitor(30, 'upload_checksum') { begin - artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :md5, data[:md5]) + artifact.upload_checksum(repo || artifactory_repo(), "#{artifact_path}", :md5, data[:md5]) @arti_upload_checksum = true rescue Exception => e @logger.fatal "Failed to upload #{artifact_path}: #{e.class.name} #{e.message}" raise e end @@ -380,41 +386,41 @@ } raise "Failed to upload MD5 for #{artifact_path}" unless @arti_upload_checksum attempt = 0 objects = [] while attempt < 3 - objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + objects = maybeArtifactoryObject(artifact_module, artifact_version, false, repo) break if objects.size > 0 sleep 2 attempt += 1 end raise "Failed to upload '#{artifact_path}'" unless objects.size > 0 objects end - def copyArtifact(artifact_module, artifact_version, artifact_path, artifact) + def copyArtifact(artifact_module, artifact_version, artifact_path, artifact, repo=nil) begin - if artifact.attributes[:uri].eql?(File.join(artifactory_endpoint, artifactory_repo, artifact_path)) + if artifact.attributes[:uri].eql?(File.join(artifactory_endpoint, repo || artifactory_repo, artifact_path)) @logger.info "Not copying (identical artifact): #{artifact_path}" else @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Start copy #{artifact_path} = #{artifact.attributes[:size]} bytes" copied = false 3.times{ copied = false monitor(30){ - result = artifact.copy("#{artifactory_repo()}/#{artifact_path}") + result = artifact.copy("#{repo || artifactory_repo()}/#{artifact_path}") @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Copied: #{result.ai}" copied = true } break if copied } raise "Failed to copy #{artifact_path}" unless copied end - objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + objects = maybeArtifactoryObject(artifact_module, artifact_version, false, repo) unless objects.size > 0 sleep 10 - objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + objects = maybeArtifactoryObject(artifact_module, artifact_version, false, repo) raise "Failed to copy '#{artifact_path}'" unless objects.size > 0 end objects rescue Exception => e @logger.error "Failed to copy #{artifact_path}: #{e.class.name} #{e.message}" @@ -450,10 +456,10 @@ # if 0 == @vars[:return_code] # @vars[:return_code] = takeInventory() # end @vars[:return_code] rescue => e - @logger.error "#{e.class.name} #{e.message}" + @logger.error "#{e.class.name} #{e.message}\n#{e.backtrace.ai}" @vars[:return_code] = Errors::ARTIFACT_UPLOAD_EXCEPTION raise e end else @vars[:return_code] = Errors::NO_ARTIFACTS \ No newline at end of file