lib/cicd/builder/manifest/mixlib/repo/artifactory.rb in manifest-builder-0.6.2 vs lib/cicd/builder/manifest/mixlib/repo/artifactory.rb in manifest-builder-0.6.4
- old
+ new
@@ -16,43 +16,42 @@
# ---------------------------------------------------------------------------------------------------------------
def uploadToRepo(artifacts)
@manifest = {}
super
- if @vars[:return_code] == 0
- # Preserve the manifest (maybeUploadArtifactoryObject will add everything we upload to the instance var)
- manifest = @manifest.dup
- # Create a manifest for each product and store it.
- createProductManifests(manifest)
- # Get a super manifest of all products and store as learning-manifest
- createSuperManifest(manifest) # -#{@vars[:variant]}
- @manifest = manifest
- end
+ handleManifests()
# If we are asked to produce a PROJECTS_FILE then we do that from the manifest and components.
+ handleProjectsFile()
+ cleanupTempFiles()
+ @vars[:return_code]
+ end
+
+ # ---------------------------------------------------------------------------------------------------------------
+ def handleProjectsFile
unless ENV['PROJECTS_FILE'].nil?
if @vars[:return_code] == 0
if File.directory?(File.realdirpath(File.dirname(ENV['PROJECTS_FILE'])))
createProjectsFile
else
@logger.error "The path to the PROJECTS_FILE (#{File.dirname(ENV['PROJECTS_FILE'])}) does not exist!"
@vars[:return_code] = Errors::NO_PROJECTS_PATH
end
end
end
- @vars[:artifacts].each do |art|
- if art[:data][:temp].is_a?(FalseClass)
- if File.exists?(art[:data][:file])
- File.unlink(art[:data][:file]) if File.exists?(art[:data][:file])
- art[:data].delete(:file)
- art[:data].delete(:temp)
- else
- @logger.warn "Temporary file disappeared: #{data.ai}"
- @vars[:return_code] = Errors::TEMP_FILE_MISSING
- end
- end
+ end
+
+ # ---------------------------------------------------------------------------------------------------------------
+ def handleManifests
+ if @vars[:return_code] == 0
+ # Preserve the manifest (maybeUploadArtifactoryObject will add everything we upload to the instance var)
+ manifest = @manifest.dup
+ # Create a manifest for each product and store it.
+ createProductManifests(manifest)
+ # Get a super manifest of all products and store as learning-manifest
+ createSuperManifest(manifest) # -#{@vars[:variant]}
+ @manifest = manifest
end
- @vars[:return_code]
end
# ---------------------------------------------------------------------------------------------------------------
def createProjectsFile
@logger.info __method__.to_s
@@ -189,16 +188,20 @@
end
@project_names
end
def createSuperManifest(manifest)
- manifest_data = ''
- manifest.each do |mod, man|
- man.each do |k, v|
- manifest_data += "#{k}=#{v}\n"
+ assemble = lambda{|mani|
+ manifest_data = ''
+ mani.each do |_, man|
+ man.each do |k, v|
+ manifest_data += "#{k}=#{v}\n"
+ end
end
- end
+ manifest_data
+ }
+ manifest_data = assemble.call(manifest)
amn = artifactory_manifest_name # Just using a local iso invoking method_missing repeatedly ... ;)
data = {module: amn, data: manifest_data, version: @vars[:build_ver], build: @vars[:build_num], properties: @properties_matrix}
# tempArtifactFile(amn, data)
data[:file] = Dir::Tmpname.create(amn) do |tmpnam, n, opts|
@@ -213,32 +216,35 @@
data[:name] = amn
@vars[:artifacts] << {
key: amn,
data: data.dup,
}
- # manifest[amn]={ amn => "#{@vars[:build_ver]}-#{@vars[:build_num]}" }
-
- maybeUploadArtifactoryObject(data: data, artifact_module: amn, artifact_version: data[:version] || @vars[:version], file_name: '', file_ext: 'properties')
+ manifest = maybeUpdateManifest(data, manifest, assemble, amn)
+ if 0 == @vars[:return_code]
+ maybeUploadArtifactoryObject(data: data, artifact_module: amn, artifact_version: data[:version] || @vars[:version], file_name: '', file_ext: 'properties')
+ end
end
-
# ---------------------------------------------------------------------------------------------------------------
def createProductManifests(manifest)
manifest.dup.each do |mod, man|
- manifest_data = ''
- man.each do |k, v|
- manifest_data += "#{k}=#{v}\n"
- end
+ assemble = lambda{ |mani|
+ manifest_data = ''
+ mani.each do |k, v|
+ manifest_data += "#{k}=#{v}\n"
+ end
+ manifest_data
+ }
+ manifest_data = assemble.call(man)
data = {
name: "#{mod}-manifest",
module: "#{mod}-manifest",
component: mod,
data: manifest_data,
version: @vars[:build_ver],
build: @vars[:build_num],
properties: @properties_matrix
}
- # tempArtifactFile("#{mod}-manifest", data)
data[:file] = Dir::Tmpname.create("#{mod}-manifest") do |tmpnam, n, opts|
mode = File::RDWR|File::CREAT|File::EXCL
perm = 0600
opts = perm
@@ -246,21 +252,79 @@
IO.write(data[:file], data[:data])
data[:temp] = false
data[:sha1] = Digest::SHA1.file(data[:file]).hexdigest
data[:md5] = Digest::MD5.file(data[:file]).hexdigest
@vars[:artifacts] << {
- key: "#{mod}-manifest",
- data: data.dup,
- }
- # noinspection RubyStringKeysInHashInspection
- # manifest["#{mod}-manifest"]={ "#{mod}-manifest" => "#{@vars[:build_ver]}-#{@vars[:build_num]}" }
+ key: "#{mod}-manifest",
+ data: data.dup,
+ }
- maybeUploadArtifactoryObject(data: data, artifact_module: data[:name], artifact_version: data[:version] || @vars[:version], file_name: '', file_ext: 'properties') # -#{@vars[:variant]}
+ maybeUpdateManifest(data, man, assemble)
+ if 0 == @vars[:return_code]
+ maybeUploadArtifactoryObject(data: data, artifact_module: data[:name], artifact_version: data[:version] || @vars[:version], file_name: '', file_ext: 'properties') # -#{@vars[:variant]}
+ end
end
end
# ---------------------------------------------------------------------------------------------------------------
+ def maybeUpdateManifest(data, man, assemble, supman=nil)
+ artifact_name = getArtifactName(data[:name], '', data[:version], 'properties')
+ artifact_path = getArtifactPath(data[:module], data[:version], data[:name])
+ objects = maybeArtifactoryObject(data[:module], @vars[:build_ver], false)
+ unless objects.nil? or objects.size == 0
+ @logger.info "#{artifact_name}(#{artifactory_endpoint()}/#{artifactory_repo()}/#{artifact_path}) exists - #{objects.size} results"
+ @logger.info "\t#{objects.map { |o| o.attributes[:uri] }.join("\n\t")}"
+ if objects.size > 1
+ @logger.error "Matched more than one artifact: #{artifact_path}/#{artifact_name}"
+ @vars[:return_code] = Errors::ARTIFACT_MULTI_MATCH
+ else
+ # Download over our temp file
+ local_dir, local_fil = File.split(data[:file])
+ @logger.info "Download #{data[:name]} to #{local_fil}"
+ @arti_download_artifact = false
+ 3.times{
+ monitor(30){
+ IO.write(data[:file], '')
+ objects[0].download(local_dir, filename: local_fil)
+ @arti_download_artifact = true
+ }
+ break if @arti_download_artifact
+ }
+ # Add our manifest
+ data[:data] = IO.read(data[:file])
+ raise "Failed to download #{data[:name]}" if data[:data] == '' or not @arti_download_artifact
+ manifest = {}
+ data[:data].split(/\n+/).each do |line|
+ k, v = line.chomp.split(/=/)
+ manifest[k] = v #unless man.has_key?(k)
+ end
+ man = if supman
+ manifest.delete(supman)
+ man.each do |_,m|
+ m.each do |k,v|
+ manifest[k] = v
+ end
+ end
+ { supman => manifest }
+ else
+ man.each do |k,v|
+ manifest[k] = v #unless man.has_key?(k)
+ end
+ manifest
+ end
+ data[:data] = assemble.call(man)
+ # Save the new file
+ IO.write(data[:file], data[:data])
+ data[:temp] = false
+ data[:sha1] = Digest::SHA1.file(data[:file]).hexdigest
+ data[:md5] = Digest::MD5.file(data[:file]).hexdigest
+ end
+ end
+ man
+ end
+
+ # ---------------------------------------------------------------------------------------------------------------
def maybeUploadArtifactoryObject(args)
super
if @vars[:return_code] == 0
data = args[:data]
artifact_module = args[:artifact_module]
@@ -282,10 +346,10 @@
end
@vars[:return_code]
end
- private :createProductManifests, :createProjectsFile, :createSuperManifest
+ protected :createProductManifests, :createProjectsFile, :createSuperManifest
end
end
end
end
end