lib/cicd/builder/manifest/mixlib/repo/artifactory.rb in manifest-builder-0.6.0 vs lib/cicd/builder/manifest/mixlib/repo/artifactory.rb in manifest-builder-0.6.2

- old
+ new

@@ -55,40 +55,35 @@ # --------------------------------------------------------------------------------------------------------------- def createProjectsFile @logger.info __method__.to_s projects = {} - project_names = {} - unless ENV['PROJECT_NAMES'].nil? - if File.exists?(ENV['PROJECT_NAMES']) - @logger.info "Load PROJECT_NAMES: #{ENV['PROJECT_NAMES']}" - project_names = JSON.load(IO.read(ENV['PROJECT_NAMES'])) || {} - else - @logger.error "The PROJECT_NAMES file (#{ENV['PROJECT_NAMES']}) does not exist!" - @vars[:return_code] = Errors::NO_PROJECT_NAMES - end - end + project_names = loadProjectNames() exts = {} exts = Hash[@vars[:artifacts].map { |a| [a[:data][:name], File.basename(a[:data][:file]).match(CiCd::Builder::Manifest::Build::EXT_RGX)[1]] }] + + createClassesFile() + @vars[:artifacts].each do |art| prod = art[:data][:name] mod = art[:data][:module] projects[prod] = { - name: project_names[prod] || prod, - module: mod, - ext: exts[prod], - class_filter: '', - } + name: project_names[prod] || prod, + module: mod, + ext: exts[prod], + class_filter: @vars[:filters][prod] || @vars[:filters][prod.gsub(/-manifest$/, '')], + } end require 'chef/mash' require 'chef/mixin/deep_merge' - old_projects = Chef::Mash.new(File.exists?(ENV['PROJECTS_FILE']) ? JSON.load(IO.read(ENV['PROJECTS_FILE'])) : {}) - projects = Chef::Mash.new(projects) - projects = Chef::Mash.new(Chef::Mixin::DeepMerge.deep_merge(projects, old_projects)) - @logger.info "Save PROJECTS_FILE: #{ENV['PROJECTS_FILE']}" - IO.write(ENV['PROJECTS_FILE'], JSON.pretty_generate(projects.to_hash, {indent: "\t", space: ' '})) + + projects_hash = File.exists?(ENV['PROJECTS_FILE']) ? loadConfigFile(ENV['PROJECTS_FILE']) : {} + old_projects = ::Chef::Mash.new(projects_hash) + projects = ::Chef::Mash.new(projects) + projects = ::Chef::Mash.new(::Chef::Mixin::DeepMerge.deep_merge(projects, old_projects)) + saveConfigFile(ENV['PROJECTS_FILE'], projects) data = { name: 'projects-file', module: 'projects-file', file: ENV['PROJECTS_FILE'], version: @vars[:build_ver], @@ -97,13 +92,106 @@ temp: false, sha1: Digest::SHA1.file(ENV['PROJECTS_FILE']).hexdigest, md5: Digest::MD5.file(ENV['PROJECTS_FILE']).hexdigest, } - maybeUploadArtifactoryObject(data: data, artifact_module: data[:name], artifact_version: data[:version] || @vars[:version], file_name: '', file_ext: 'json') + maybeUploadArtifactoryObject( + data: data, + artifact_module: data[:name], + artifact_version: data[:version] || @vars[:version], + file_name: '', + file_ext: (ENV['PROJECTS_FILE'] and ENV['PROJECTS_FILE'].downcase.match(/\.ya?ml$/)) ? 'yaml' : 'json' + ) end + # --------------------------------------------------------------------------------------------------------------- + # noinspection RubyHashKeysTypesInspection + def createClassesFile() + @logger.info __method__.to_s + project_names = loadProjectNames() + + @vars[:classes] = YAML.load(IO.read(ENV['CLASSES_MANIFEST_FILE'])) + # keys = Hash[classes.keys.map.with_index.to_a].keys.sort + + @vars[:filters] = {} + filters = {} + @vars[:classes].each do |role,apps| + apps.map{ |app| + filters[app] ||= [] + filters[app] << role + } + end + filters.each do |app,roles| + @vars[:filters][app] = Hash[roles.map.with_index.to_a].keys.join('|') + end + + saveConfigFile(ENV['CLASSES_FILE'],@vars[:classes]) + data = { + name: 'classes-file', + module: 'classes-file', + file: ENV['CLASSES_FILE'], + version: @vars[:build_ver], + build: @vars[:build_num], + properties: @properties_matrix, + temp: false, + sha1: Digest::SHA1.file(ENV['CLASSES_FILE']).hexdigest, + md5: Digest::MD5.file(ENV['CLASSES_FILE']).hexdigest, + } + + maybeUploadArtifactoryObject( + data: data, + artifact_module: data[:name], + artifact_version: data[:version] || @vars[:version], + file_name: '', + file_ext: 'yaml' + ) + end + + def saveConfigFile(file, projects) + @logger.info "Save config file: #{file}" + ext = file.gsub(/\.(\w+)$/, '\1') + IO.write(file, case ext.downcase + when /ya?ml/ + projects.to_hash.to_yaml line_width: 1024, indentation: 4, canonical: false + when /json|js/ + JSON.pretty_generate(projects.to_hash, {indent: "\t", space: ' '}) + else + raise "Unsupported extension: #{ext}" + end) + end + + def loadConfigFile(file) + ext = file.gsub(/\.(\w+)$/, '\1') + hash = case ext.downcase + when /ya?ml/ + YAML.load_file(ENV['PROJECTS_FILE']) + when /json|js/ + JSON.load(IO.read(ENV['PROJECTS_FILE'])) + else + raise "Unsupported extension: #{ext}" + end + end + + def loadProjectNames(fresh=false) + if fresh + @project_names = nil + end + unless @project_names + @project_names = {} + unless ENV['PROJECT_NAMES'].nil? + if File.exists?(ENV['PROJECT_NAMES']) + @logger.info "Load PROJECT_NAMES: #{ENV['PROJECT_NAMES']}" + @project_names = JSON.load(IO.read(ENV['PROJECT_NAMES'])) || {} + else + @logger.error "The PROJECT_NAMES file (#{ENV['PROJECT_NAMES']}) does not exist!" + @vars[:return_code] = Errors::NO_PROJECT_NAMES + end + end + end + @project_names + end + def createSuperManifest(manifest) manifest_data = '' manifest.each do |mod, man| man.each do |k, v| manifest_data += "#{k}=#{v}\n" @@ -140,9 +228,10 @@ manifest_data += "#{k}=#{v}\n" end data = { name: "#{mod}-manifest", module: "#{mod}-manifest", + component: mod, data: manifest_data, version: @vars[:build_ver], build: @vars[:build_num], properties: @properties_matrix }