Class: Simp::Metadata::Component

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simp/metadata/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, name, version) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
# File 'lib/simp/metadata/component.rb', line 8

def initialize(engine, name, version)
  @engine = engine
  @name = name
  @release_version = version
end

Instance Attribute Details

#engineObject

Returns the value of attribute engine.



5
6
7
# File 'lib/simp/metadata/component.rb', line 5

def engine
  @engine
end

#release_versionObject

Returns the value of attribute release_version.



6
7
8
# File 'lib/simp/metadata/component.rb', line 6

def release_version
  @release_version
end

Instance Method Details

#[](index) ⇒ Object



127
128
129
# File 'lib/simp/metadata/component.rb', line 127

def [](index)
  send index.to_sym
end

#asset_nameObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/simp/metadata/component.rb', line 156

def asset_name
  if real_asset_name.nil?
    case component_type
    when 'puppet-module'
      splitted = name.split('-')
      splitted[splitted.size - 1]
    else
      name
    end
  else
    real_asset_name
  end
end

#authoritativeObject



222
223
224
# File 'lib/simp/metadata/component.rb', line 222

def authoritative
  get_from_component['authoritative']
end

#authoritative?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/simp/metadata/component.rb', line 218

def authoritative?
  get_from_component['authoritative']
end

#binarynameObject



396
397
398
# File 'lib/simp/metadata/component.rb', line 396

def binaryname
  "#{asset_name}-#{version}.#{extension}"
end

#branchObject



263
264
265
# File 'lib/simp/metadata/component.rb', line 263

def branch
  get_from_release['branch']
end

#branch=(value) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/simp/metadata/component.rb', line 267

def branch=(value)
  release = release_source.releases[release_version]
  unless release.nil?
    if release.key?(name)
      release[name]['branch'] = value
    else
      release[name] = { 'branch' => value }
    end
  end
  release_source.dirty = true
end

#build(destination) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/simp/metadata/component.rb', line 463

def build(destination)
  currentdir = Dir.pwd
  destination = currentdir if destination.nil?
  abort(Simp::Metadata.critical("File #{rpm_name} already exists in #{destination}. Please delete this file and re-run the command if you wish to replace it.")[0]) if File.exist?("#{destination}/#{rpm_name}")

  # Create tmp dir and clone source
  Dir.mktmpdir do |dir|
    Dir.chdir(dir.to_s) do
      system("git clone #{url} source > /dev/null")
      Dir.chdir('source') do
        system("git checkout #{version}")

        # sanitize
        excludes = %w(.git .gitignore)
        if File.exist?('./.simp.yml')
          config = YAML.load_file('.simp.yml')
          if config.key?('sanitize')
            sanitize = config['sanitize']
            excludes = excludes + ['.simp.yml'] + sanitize['exclude'] if sanitize.key?('exclude')
            if sanitize.key?('scripts')
              sanitize['scripts'].each do |command|
                puts `#{command}`
              end
            end
          end
        end

        # Make build dirs
        FileUtils.makedirs "#{dir}/usr/share/simp/modules/#{module_name}"

        # Create tarball and extract to tmp/usr/share/simp/#{module_name}
        errorcode = Simp::Metadata.run("tar -cf - --exclude=./#{excludes.join(' --exclude=./')} . | tar -xvpf - -C #{dir}/usr/share/simp/modules/#{module_name}")
        abort(Simp::Metadata.critical("Failed to create and extract tarball for #{name}")) unless errorcode == 0

        # Set RPM build options
         = JSON.parse File.read('metadata.json')

        heredoc = <<-HEREDOC
-s dir
-t rpm
--name '#{rpm_basename}'
--rpm-summary '#{['name'].split('-')[1].capitalize} Puppet Module'
--description '#{['summary']}'
--maintainer '[email protected]'
--category Applications/System
--prefix 'usr/share/simp/modules'
--url '#{['source']}'
--vendor "Onyx Point, Inc"
--license '#{['license']}'
--package '#{currentdir}/#{rpm_name}'
--version '#{['version']}'
--iteration '#{revision}'
--architecture '#{target}'
--verbose
-C #{dir}/usr/share/simp/modules
--rpm-digest sha512 -d 'simp-adapter'
--directories=/usr/share/simp/modules/#{module_name}
        HEREDOC

        options = heredoc.tr("\n", ' ')

        # Create RPM
        Dir.chdir(dir.to_s) do
          errorcode = Simp::Metadata.run("fpm #{options} 2> /dev/null")
          abort(Simp::Metadata.critical("Failed to create RPM for #{module_name}")[0]) unless errorcode == 0
          puts "RPM #{rpm_name} built successfully" if File.exist?("#{currentdir}/#{rpm_name}")
        end
      end
    end
  end
  FileUtils.move "#{currentdir}/#{rpm_name}", destination
end

#buildinfo(type = nil) ⇒ Object



455
456
457
458
459
460
461
# File 'lib/simp/metadata/component.rb', line 455

def buildinfo(type = nil)
  if type.nil?
    {}
  else
    Simp::Metadata::Buildinfo.new(self, type)
  end
end

#compiled?Boolean

Returns:

  • (Boolean)


388
389
390
391
392
393
394
# File 'lib/simp/metadata/component.rb', line 388

def compiled?
  if get_from_release.key?('compiled')
    get_from_release['compiled']
  else
    false
  end
end

#component_sourceObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simp/metadata/component.rb', line 35

def component_source
  retval = engine.sources['bootstrap_metadata']
  engine.sources.each do |_name, source|
    next if source.components.nil?
    if source.components.key?(name)
      retval = source
      break
    end
  end
  retval
end

#component_typeObject



214
215
216
# File 'lib/simp/metadata/component.rb', line 214

def component_type
  get_from_component['component-type']
end

#component_versionObject



334
335
336
337
338
339
340
# File 'lib/simp/metadata/component.rb', line 334

def component_version
  if version =~ /^[v][0-9]/
    version.split('v')[1]
  else
    version
  end
end

#diff(component, attribute) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/simp/metadata/component.rb', line 426

def diff(component, attribute)
  diff = {}

  if attribute.nil?
    current_hash = {}
    comp_hash = {}
    each do |attr, value|
      current_hash.merge!(attr => value)
    end
    component.each do |attr, value|
      comp_hash.merge!(attr => value)
    end
    unless current_hash == comp_hash
      current_hash.each do |attr, value|
        diff[attr] = { 'original' => (current_hash[attr]).to_s,
                       'changed' => (comp_hash[attr]).to_s } if comp_hash[attr] != value
      end
    end
    diff
  else
    v1 = self[attribute.to_s]
    v2 = component[attribute.to_s]
    unless v1 == v2
      diff[attribute] = { 'original' => v1.to_s, 'changed' => v2.to_s }
      diff
    end
  end
end

#download(destination, src) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/simp/metadata/component.rb', line 536

def download(destination, src)
  destination = Dir.pwd if destination.nil?
  rpm_name = self.rpm_name
  return if File.exist?("#{destination}/#{rpm_name}")
  el_version = platform.split('el')[1]
  if src.any?
    sources = src
  else
    sources = ["https://download.simp-project.com/SIMP/yum/simp6/el/#{el_version}/x86_64", "https://download.simp-project.com/SIMP/yum/unstable/el/#{el_version}/x86_64"]
  end
  sources.each do |source|
    if source =~ /^https?:/
      file_check = `curl -sLI #{source}/#{rpm_name} | head -n 1 | awk '{print $2}'`.chomp
      `wget -q #{source}/#{rpm_name}` if file_check == '200'
    elsif File.exist?("#{source}/#{rpm_name}")
      FileUtils.cp "#{source}/#{rpm_name}", destination
    end
    puts "Copied #{rpm_name} from #{source}" if File.exist?("#{destination}/#{rpm_name}")
    return if File.exist?("#{destination}/#{rpm_name}")
  end
  puts "Unable to find #{rpm_name} from #{sources}"
end

#eachObject



131
132
133
134
135
# File 'lib/simp/metadata/component.rb', line 131

def each
  keys.each do |key|
    yield key, self[key]
  end
end

#extensionObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/simp/metadata/component.rb', line 102

def extension
  if real_extension.nil?
    case component_type
    when 'simp-metadata'
      'tgz'
    when 'logstash-filter'
      'gem'
    when 'rubygem'
      'gem'
    when 'grafana-plugin'
      'zip'
    when 'puppet-module'
      'tgz'
    else
      ''
    end
  else
    real_extension
  end
end

#extractObject



198
199
200
# File 'lib/simp/metadata/component.rb', line 198

def extract
  locations.primary.extract
end

#fetch_data(item) ⇒ Object

Will be used to grab method based data in the future, rather then calling get_from_release or get_from_component directly,

For now, just use it in Simp::Metadata::Buildinfo



70
71
72
73
74
75
76
77
78
# File 'lib/simp/metadata/component.rb', line 70

def fetch_data(item)
  component = get_from_component
  release = get_from_release
  if release.key?(item)
    release[item]
  else
    component[item]
  end
end

#formatObject

XXX: ToDo Generate a filename, and output file type; ie, directory or file



210
211
212
# File 'lib/simp/metadata/component.rb', line 210

def format
  get_from_component['format']
end

#get_from_componentObject



80
81
82
# File 'lib/simp/metadata/component.rb', line 80

def get_from_component
  component_source.components[name]
end

#get_from_releaseObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/simp/metadata/component.rb', line 84

def get_from_release
  retval = {}
  if release_source.releases.key?(release_version)
    if release_source.releases[release_version].key?(name)
      retval = release_source.releases[release_version][name]
    end
  else
    if release_source.release(release_version).key?(name)
      retval = release_source.release(release_version)[name]
    end
  end
  retval
end

#keysObject



123
124
125
# File 'lib/simp/metadata/component.rb', line 123

def keys
  %w(component_type authoritative asset_name extension format module_name type url method extract branch tag ref version release_source component_source target revision)
end

#locationsObject



202
203
204
205
206
# File 'lib/simp/metadata/component.rb', line 202

def locations
  # XXX: ToDo Allow manifest.yaml to override locations
  # XXX: ToDo Use primary_source and mirrors here if locations is empty
  Simp::Metadata::Locations.new({ 'locations' => get_from_component['locations'], 'primary_source' => get_from_component['primary_source'], 'mirrors' => get_from_component['mirrors'] }, self)
end

#methodObject



194
195
196
# File 'lib/simp/metadata/component.rb', line 194

def method
  locations.primary.method
end

#module_nameObject



152
153
154
# File 'lib/simp/metadata/component.rb', line 152

def module_name
  asset_name
end

#name(type = 'component') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simp/metadata/component.rb', line 18

def name(type = 'component')
  case type
  when 'component'
    @name
  when 'puppetfile'
    if component_type == 'rubygem'
      "rubygem-#{@name.tr('-', '_')}"
    elsif component_type == 'puppet-module'
      @name.gsub(/pupmod-/, '')
    else
      @name
    end
  else
    abort(Simp::Metadata.critical("Expected type to be 'component' or 'puppetfile'")[0])
  end
end

#output_filenameObject



178
179
180
181
182
183
184
# File 'lib/simp/metadata/component.rb', line 178

def output_filename
  if compiled?
    "#{name}-#{version}.#{extension}"
  else
    name
  end
end

#output_typeObject



170
171
172
173
174
175
176
# File 'lib/simp/metadata/component.rb', line 170

def output_type
  if compiled?
    :file
  else
    :directory
  end
end

#platformObject



371
372
373
374
375
376
377
378
# File 'lib/simp/metadata/component.rb', line 371

def platform
  platform = engine.options['platform']
  if platform.nil?
    'el7'
  else
    platform
  end
end

#primaryObject



186
187
188
# File 'lib/simp/metadata/component.rb', line 186

def primary
  locations.primary
end

#real_asset_nameObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/simp/metadata/component.rb', line 141

def real_asset_name
  case component_type
  when 'puppet-module'
    get_from_component['module_name']
  when 'rubygem'
    get_from_component['gem_name']
  else
    get_from_component['asset_name']
  end
end

#real_extensionObject



137
138
139
# File 'lib/simp/metadata/component.rb', line 137

def real_extension
  get_from_component['extension']
end

#refObject



247
248
249
# File 'lib/simp/metadata/component.rb', line 247

def ref
  get_from_release['ref']
end

#ref=(value) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/simp/metadata/component.rb', line 251

def ref=(value)
  release = release_source.releases[release_version]
  unless release.nil?
    if release.key?(name)
      release[name]['ref'] = value
    else
      release[name] = { 'ref' => value }
    end
  end
  release_source.dirty = true
end

#release_sourceObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simp/metadata/component.rb', line 47

def release_source
  retval = engine.sources['bootstrap_metadata']
  engine.sources.each do |_name, source|
    if source.releases.key?(release_version)
      if source.releases[release_version].key?(name)
        retval = source
        break
      end
    else
      if source.release(release_version).key?(name)
        retval = source
        break
      end
    end
  end
  retval
end

#revisionObject



226
227
228
229
230
231
232
233
# File 'lib/simp/metadata/component.rb', line 226

def revision
  revision = get_from_component['revision']
  if revision.nil?
    '0'
  else
    revision
  end
end

#revision=(value) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/simp/metadata/component.rb', line 235

def revision=(value)
  release = release_source.releases[release_version]
  unless release.nil?
    if release.key?(name)
      release[name]['revision'] = value
    else
      release[name] = { 'revision' => value }
    end
  end
  release_source.dirty = true
end

#rpm_basenameObject



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/simp/metadata/component.rb', line 322

def rpm_basename
  if component_type == 'puppet-module'
    if name =~ /pupmod-*/
      name.to_s
    else
      "pupmod-#{name}"
    end
  else
    name.to_s
  end
end

#rpm_nameObject



380
381
382
383
384
385
386
# File 'lib/simp/metadata/component.rb', line 380

def rpm_name
  if component_type == 'puppet-module'
    "#{rpm_basename}-#{rpm_version}.#{target}.rpm"
  else
    "#{rpm_basename}-#{rpm_version}.#{platform}.#{target}.rpm"
  end
end

#rpm_versionObject



342
343
344
345
346
347
348
# File 'lib/simp/metadata/component.rb', line 342

def rpm_version
  if component_version =~ /^[0-9]+.[0-9]+.[0-9]+.[0-9]+/
    component_version
  else
    "#{component_version}-#{revision}"
  end
end

#tagObject



279
280
281
# File 'lib/simp/metadata/component.rb', line 279

def tag
  get_from_release['tag']
end

#tag=(value) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/simp/metadata/component.rb', line 283

def tag=(value)
  release = release_source.releases[release_version]
  unless release.nil?
    if release.key?(name)
      release[name]['tag'] = value
    else
      release[name] = { 'tag' => value }
    end
  end
  release_source.dirty = true
end

#targetObject



350
351
352
353
354
355
356
357
# File 'lib/simp/metadata/component.rb', line 350

def target
  target = get_from_release['target']
  if target.nil?
    'noarch'
  else
    target
  end
end

#target=(value) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/simp/metadata/component.rb', line 359

def target=(value)
  release = release_source.releases[release_version]
  unless release.nil?
    if release.key?(name)
      release[name]['target'] = value
    else
      release[name] = { 'target' => value }
    end
  end
  release_source.dirty = true
end

#to_sObject



14
15
16
# File 'lib/simp/metadata/component.rb', line 14

def to_s
  name
end

#typeObject



98
99
100
# File 'lib/simp/metadata/component.rb', line 98

def type
  get_from_component['type']
end

#urlObject



190
191
192
# File 'lib/simp/metadata/component.rb', line 190

def url
  locations.primary.url
end

#versionObject



295
296
297
298
299
300
301
302
303
304
# File 'lib/simp/metadata/component.rb', line 295

def version
  ver = ''
  %w(version tag ref branch).each do |item|
    unless get_from_release[item].nil?
      ver = get_from_release[item]
      break
    end
  end
  ver
end

#version?Boolean

Returns:

  • (Boolean)


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/simp/metadata/component.rb', line 306

def version?
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      system("git clone #{url} > /dev/null 2>&1")
      Dir.chdir("./#{module_name}") do
        exitcode = Simp::Metadata.run("git checkout #{version} > /dev/null 2>&1")
        if exitcode == 0
          true
        else
          false
        end
      end
    end
  end
end

#view(attribute) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/simp/metadata/component.rb', line 400

def view(attribute)
  comp = self
  view_hash = {}
  if attribute.nil?
    comp.each do |key, value|
      view_hash[key] = value.to_s unless value.nil? || value == ''
    end
    location_hash = {}
    comp.locations.each do |location|
      location.each do |key, value|
        location_hash.merge!(key => value.to_s) unless value.nil?
      end
    end
    buildinfo_hash = {}
    comp.buildinfo.each do |buildinfo|
      # Needs to be fixed/added to
      buildinfo.each do |_key, _value|
      end
    end
    view_hash['location'] = location_hash
  else
    view_hash[attribute] = comp[attribute].to_s
  end
  view_hash
end