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



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

def initialize(builder)
  super
end

Instance Method Details

#createClassesFileObject


noinspection RubyHashKeysTypesInspection


107
108
109
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
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 107

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 43

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



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

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


161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 161

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


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

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



269
270
271
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
# File 'lib/cicd/builder/manifest/mixlib/repo/artifactory.rb', line 269

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



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

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


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

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



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

def uploadToRepo(artifacts)
  @manifest = {}
  super
  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