lib/cicd/builder/mixlib/repo/artifactory.rb in cicd-builder-0.9.31 vs lib/cicd/builder/mixlib/repo/artifactory.rb in cicd-builder-0.9.32

- old
+ new

@@ -73,21 +73,19 @@ @vars[:build_mdd].each do |k,v| @properties_matrix["build.#{k.downcase}"] = v end # matrix = properties.map{|k,v| (v.nil? or v.empty?) ? nil : "#{k}=#{v}"}.join("\;").gsub(%r'^\;*(.*?)\;*$', '\1') # @client.endpoint += ";#{matrix}" - manifest = {} + @manifest = {} artifacts.each{|art| data = art[:data] - objects = maybeArtifactoryObject(data) - upload = false if data.has_key?(:data) tempArtifactFile("manifest-#{data[:name]}", data) end if data.has_key?(:file) - sha1 = Digest::SHA1.file(data[:file]).hexdigest - md5 = Digest::MD5.file(data[:file]).hexdigest + data[:sha1] = Digest::SHA1.file(data[:file]).hexdigest + data[:md5] = Digest::MD5.file(data[:file]).hexdigest else raise 'Artifact does not have file or data?' end file_name = File.basename(data[:file]) if file_name =~ %r'^#{data[:name]}' @@ -101,94 +99,159 @@ file_name.gsub!(%r'\.*#{file_ext}$','') end if file_name =~ %r'\.+' raise "Unable to parse out file name in #{data[:file]}" end - unless file_name.empty? or file_name.match(%r'^-') - file_name = "-#{file_name}" + unless file_name.empty? + file_name = '_'+file_name.gsub(%r'^(\.|-|)(\w)', '\2').gsub(%r'(\.|-)+', '_') end - artifact_name = "#{data[:name]}-#{data[:version]}#{file_name}-#{@vars[:variant]}.#{file_ext}" # -#{data[:build]} - artifact_path = "#{artifactory_org_path()}/#{data[:name]}/#{data[:version]}-#{@vars[:variant]}/#{artifact_name}" - manifest[data[:name]] = artifact_path - if objects.nil? or objects.size == 0 - upload = true - else - @logger.info "#{artifactory_endpoint()}/#{artifactory_repo()}/#{artifact_path} exists - #{objects.size} results" - # Check the checksum of the artifact - matched = false - objects.each do |artifact| - @logger.debug "\tChecking: #{artifact.attributes.ai} for #{artifact_path}" - if artifact.uri.match(%r'#{artifact_path}$') - matched = true - @logger.info "\tMatched: #{artifact.attributes.select{|k,_| k != :client}.ai}" - if artifact.md5 != md5 or artifact.sha1 != sha1 - upload = true - end - end - end - upload ||= (not matched) + maybeUploadArtifactoryObject(data, data[:name], data[:version] || @vars[:version], file_ext, file_name) # -#{@vars[:variant] + break unless @vars[:return_code] == 0 + } + if @vars[:return_code] == 0 + manifest_data = '' + @manifest.each do |k,v| + manifest_data += "#{k}=#{v}\n" end + data = { data: manifest_data, version: @vars[:build_ver], build: @vars[:build_num], properties: @properties_matrix } + tempArtifactFile('manifest', data) + data[:sha1] = Digest::SHA1.file(data[:file]).hexdigest + data[:md5 ] = Digest::MD5.file(data[:file]).hexdigest + data[:name] = artifactory_manifest_name + maybeUploadArtifactoryObject(data, artifactory_manifest_name, data[:version] || @vars[:version], 'properties', '') # -#{@vars[:variant]} + end + @vars[:return_code] + end - if upload - data[:properties] = @properties_matrix - uploadArtifact(artifact_path, data, md5, sha1) - else - @logger.info "Keep existing #{artifactory_endpoint()}/#{artifact_path}" + def maybeUploadArtifactoryObject(data, artifact_module, artifact_version, file_ext, file_name) + 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) + 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 "\t#{objects.map{|o| o.attributes[:uri]}.join("\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 - if data[:temp] - File.unlink(data[:file]) - end - } - manifest_data = '' - manifest.each do |k,v| - manifest_data += "#{k}=#{v}\n" + data[:properties] = properties_matrix.merge(@properties_matrix) + objects = uploadArtifact(artifact_module, artifact_version, artifact_path, data) + matched = matchArtifactoryObjects(artifact_path, data, objects) + else + @logger.info "Keep existing #{matched.map{|o| o.attributes[:uri]}.join("\t")}" end - data = { data: manifest_data, version: @vars[:build_ver], build: @vars[:build_num], properties: @properties_matrix } - tempArtifactFile('manifest', data) - sha1 = Digest::SHA1.file(data[:file]).hexdigest - md5 = Digest::MD5.file(data[:file]).hexdigest - artifact_name = "#{artifactory_manifest_name}-#{data[:version]}-#{data[:variant]}.properties" - artifact_path = "#{artifactory_org_path()}/#{artifactory_manifest_module}/#{data[:version]}-#{@vars[:variant]}/#{artifact_name}" - uploadArtifact(artifact_path, data, md5, sha1) if data[:temp] File.unlink(data[:file]) end - 0 + @vars[:return_code] = Errors::ARTIFACT_NOT_UPLOADED unless matched.size > 0 + if @vars[:return_code] == 0 + artifact_version += "-#{data[:build] || @vars[:build_num]}" + artifact_name = getArtifactName(artifact_module, file_name, artifact_version, file_ext, ) + artifact_path = getArtifactPath(artifact_module, artifact_version, artifact_name) + copies = maybeArtifactoryObject(artifact_module, artifact_version, false) + 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) + unless copied.size > 0 + @vars[:return_code] = Errors::ARTIFACT_NOT_COPIED + break + end + end + else + @logger.info "Keep existing #{matched.map{|o| o.attributes[:uri]}.join("\t")}" + end + @manifest[data[:name]] = artifact_version + end + + @vars[:return_code] end - def uploadArtifact(artifact_path, data, md5, sha1) + def matchArtifactoryObjects(artifact_path, data, objects) + # matched = false + objects.select do |artifact| + @logger.debug "\tChecking: #{artifact.attributes.ai} for #{artifact_path}" + # if artifact.uri.match(%r'#{artifact_path}$') + # @logger.info "\tMatched: #{artifact.attributes.select { |k, _| k != :client }.ai}" + # end + matched = (artifact.md5.eql?(data[:md5]) or artifact.sha1.eql?(data[:sha1])) + matched + end + end + + def getArtifactPath(artifact_module, artifact_version, artifact_name) + artifact_path = "#{artifactory_org_path()}/#{artifact_module}/#{artifact_version}/#{artifact_name}" + end + + 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) + begin + # Get a list of matching artifacts in this repository + result = @client.artifact_gavc_search(group: artifactory_org_path(), name: artifact_name, version: "#{artifact_version}", repos: [artifactory_repo()]) + if result.size > 0 + @logger.info "Artifactory gavc_search match g=#{artifactory_org_path()},a=#{artifact_name},v=#{artifact_version},r=#{artifactory_repo()}: #{result}" + # raise "GAVC started working: #{result.ai}" + elsif wide + @logger.warn 'GAVC search came up empty!' + result = @client.artifact_search(name: artifact_name, repos: [artifactory_repo()]) + @logger.info "Artifactory search match a=#{artifact_name},r=#{artifactory_repo()}: #{result}" + end + result + rescue Exception => e + @logger.error "Artifactory error: #{e.class.name} #{e.message}" + raise e + end + end + + def uploadArtifact(artifact_module, artifact_version, artifact_path, data) data[:size] = File.size(data[:file]) - @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Start upload #{artifact_path} = #{data[:size]} bytes" artifact = ::Artifactory::Resource::Artifact.new(local_path: data[:file], client: @client) # noinspection RubyStringKeysInHashInspection artifact.checksums = { - 'md5' => md5, - 'sha1' => sha1 + '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" result = artifact.upload(artifactory_repo(), "#{artifact_path}", data[:properties] || {}) @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Uploaded: #{result.attributes.select { |k, _| k != :client }.ai}" - artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :sha1, sha1) - artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :md5, md5) - objects = maybeArtifactoryObject(data, false) + artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :sha1, data[:sha1]) + artifact.upload_checksum(artifactory_repo(), "#{artifact_path}", :md5, data[:md5]) + objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + unless objects.size > 0 + objects = maybeArtifactoryObject(artifact_module, artifact_version, true) + end raise "Failed to upload '#{artifact_path}'" unless objects.size > 0 + objects end - def maybeArtifactoryObject(data,wide=true) + def copyArtifact(artifact_module, artifact_version, artifact_path, artifact) begin - # Get a list of matching artifacts in this repository - result = @client.artifact_gavc_search(group: artifactory_org_path(), name: data[:name], version: "#{data[:version]}-#{@vars[:variant]}", repos: [artifactory_repo()]) - if result.size > 0 - @logger.info "Artifactory gavc_search match g=#{artifactory_org_path()},a=#{data[:name]},v=#{data[:version]}-#{@vars[:variant]},r=#{artifactory_repo()}: #{result}" - # raise "GAVC started working: #{result.ai}" - elsif wide - @logger.warn 'GAVC search came up empty!' - result = @client.artifact_search(name: data[:name], repos: [artifactory_repo()]) - @logger.info "Artifactory search match a=#{data[:name]},r=#{artifactory_repo()}: #{result}" + if artifact.attributes[:uri].eql?(File.join(artifactory_endpoint, 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" + result = artifact.copy("#{artifactory_repo()}/#{artifact_path}") + @logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}] Copied: #{result.ai}" end - result + objects = maybeArtifactoryObject(artifact_module, artifact_version, false) + raise "Failed to copy '#{artifact_path}'" unless objects.size > 0 + objects rescue Exception => e - @logger.error "Artifactory error: #{e.class.name} #{e.message}" + @logger.error "Failed to copy #{artifact_path}: #{e.class.name} #{e.message}" raise e end end # --------------------------------------------------------------------------------------------------------------- \ No newline at end of file