Class: CiCd::Builder::Manifest::Repo::Artifactory

Inherits:
Repo::Artifactory
  • Object
show all
Defined in:
lib/cicd/builder/manifest/mixlib/repo/artifactory.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ Artifactory




14
15
16
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 14

def initialize(builder)
  super
end

Instance Method Details

#createClassesFileObject


noinspection RubyHashKeysTypesInspection



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 110

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

#handleManifestsObject




46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 46

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
end

#handleProjectsFileObject




32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 32

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
end

#loadConfigFile(file) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 164

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

#loadProjectNames(fresh = false) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 176

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

#maybeUpdateManifest(data, man, assemble, supman = nil) ⇒ Object




272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 272

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

#maybeUploadArtifactoryObject(args) ⇒ Object




329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 329

def maybeUploadArtifactoryObject(args)
  super
  if @vars[:return_code] == 0
    data             = args[:data]
    artifact_module  = args[:artifact_module]
    artifact_version = args[:artifact_version]
    # file_ext         = args[:file_ext]
    # file_name        = args[:file_name]
    if @manifest[artifact_module].nil?
      @manifest[artifact_module] = {}
      file_name = artifact_module
    else
      file_name, _ = get_artifact_file_name_ext(data)
      if file_name.empty?
        file_name = artifact_module
      else
        file_name = "#{artifact_module}#{file_name}"
      end
    end
    @manifest[artifact_module][file_name] = artifact_version
  end

  @vars[:return_code]
end

#saveConfigFile(file, projects) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 151

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

#uploadToRepo(artifacts) ⇒ Object




19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 19

def uploadToRepo(artifacts)
  @manifest = {}
  super
  if @vars[:return_code] == 0
    handleManifests()
    # If we are asked to produce a PROJECTS_FILE then we do that from the manifest and components.
    handleProjectsFile()
    cleanupTempFiles()
  end
  @vars[:return_code]
end