Class: Mvp::Itemizer

Inherits:
Object
  • Object
show all
Defined in:
lib/mvp/itemizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Itemizer

Returns a new instance of Itemizer.



8
9
10
11
# File 'lib/mvp/itemizer.rb', line 8

def initialize(options = {})
  @useragent = 'Puppet Community Stats Monitor'
  @forge     = options[:forge] ||'https://forge.puppet.com'
end

Instance Method Details

#download(path, modname, version) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/mvp/itemizer.rb', line 38

def download(path, modname, version)
  filename = "#{modname}-#{version}.tar.gz"
  Dir.chdir(path) do
    File.open(filename, "w") do |file|
      file << HTTParty.get( "#{@forge}/v3/files/#{filename}" )
    end
    system("tar -xf #{filename}")
    FileUtils.rm(filename)
  end
end

#itemize(modname, version) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mvp/itemizer.rb', line 49

def itemize(modname, version)
  Dir.mktmpdir('mvp') do |path|
    download(path, modname, version)

    # not all modules have manifests
    manifests = "#{path}/#{modname}-#{version}/manifests"
    next {} unless File.directory?(manifests)

    options = {
      :manifests => [manifests],
      :external  => true,
    }
    runner = Puppet_X::Binford2k::Itemize::Runner.new(options).run!
    runner.results
  end
end

#itemized(mod) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mvp/itemizer.rb', line 30

def itemized(mod)
  modname = mod[:slug]
  version = mod[:version]
  baserow = { :module => modname, :version => version, :kind => 'admin', :element => 'version', :count => 0}

  table(itemize(modname, version), mod) << baserow
end

#run!(data, uploader) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mvp/itemizer.rb', line 13

def run!(data, uploader)
  data.each do |mod|
    modname = mod['slug']
    version = mod['version']
    return if uploader.version_itemized?(modname, version)

    $logger.debug "Itemizing #{modname}-#{version}"
    begin
      rows = table(itemize(modname, version), mod)
      uploader.insert(:itemized, rows) unless rows.empty?
    rescue => e
      $logger.error e.message
      $logger.debug e.backtrace.join("\n")
    end
  end
end

#table(itemized, data) ⇒ Object

Build a table with this schema module | version | source | kind | element | count



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mvp/itemizer.rb', line 68

def table(itemized, data)
  modname      = data[:name]
  slug         = data[:slug]
  version      = data[:version]
  dependencies = data[:dependencies]

  itemized.map do |kind, elements|
    # the kind of element comes pluralized from puppet-itemize
    kind = kind.to_s
    kind = kind.end_with?('ses') ? kind.chomp('es') : kind.chomp('s')
    elements.map do |name, count|
      if name == modname
        depname = name
      else
        # This relies on a little guesswork.
        segments = name.split('::')                       # First see if its already namespaced and we can just use it
        segments = name.split('_') if segments.size == 1  # If not, then maybe it follows the pattern like 'mysql_password'
        depname  = segments.first
      end

      # There's a chance of collisions here. For example, if you depended on a module
      # named 'foobar-notify' and you used a 'notify' resource, then the resource would
      # be improperly linked to that module. That's a pretty small edge case though.
      source  = dependencies.find {|row| row.split('-').last == depname} rescue nil

      { :module => slug, :version => version, :source => source, :kind => kind, :element => name, :count => count }
    end
  end.flatten(1)
end

#testObject



98
99
100
101
# File 'lib/mvp/itemizer.rb', line 98

def test()
  require 'pry'
  binding.pry
end