Class: Puppet_X::Binford2k::Itemize::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/binford2k/itemize/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet_x/binford2k/itemize/runner.rb', line 7

def initialize(options = {})
  Puppet::Util::Log.newdestination(:console)

  @paths     = expand(Array(options[:manifests]))
  @options   = options
  @results   = {}

  if options[:manifests].size == 1
    root = options[:manifests].first
    path = [File.expand_path("#{root}/metadata.json"),
            File.expand_path("#{root}/../metadata.json")].select { |p| File.exist? p }.first

    if path
      @metadata  = JSON.parse(File.read(File.expand_path("#{path}/../metadata.json")))
      @namespace = @metadata['name'].split(/[-\/]/).last

      # we can only use the module name part of this, not the user namespace
      @dependencies = @metadata['dependencies'].map do |dep|
        dep['name'].split(/[-\/]/).last
      end
      # inject a few mocked dependencies that we can assume will always exist
      @dependencies << @namespace
      @dependencies << 'puppet_enterprise'
    else
      # what error to display here?
    end
  end
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/puppet_x/binford2k/itemize/runner.rb', line 6

def results
  @results
end

Instance Method Details

#dump!Object



72
73
74
75
# File 'lib/puppet_x/binford2k/itemize/runner.rb', line 72

def dump!
  require 'json'
  puts JSON.pretty_generate(@results)
end

#expand(paths) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet_x/binford2k/itemize/runner.rb', line 36

def expand(paths)
  paths.map do |path|
    path = File.expand_path(path)

    if File.file? path
      path
    elsif File.directory? path
      Dir.glob("#{path}/**/*.pp")
    else
      raise "Path '#{path}' doesn't appear to be a file or directory"
    end
  end.flatten
end

#run!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet_x/binford2k/itemize/runner.rb', line 50

def run!
  @paths.each do |path|
    Puppet.debug "Itemizing #{path}"
    parser = Puppet_X::Binford2k::Itemize::Parser.new(path, @options).parse!
    parser.results.each do |kind, counts|
      @results[kind] ||= {}

      counts.each do |name, count|
        segments = name.split('::')
        if @dependencies and segments.size > 1
          Puppet.warn_once(:dependency, name, "Undeclared module dependancy: #{name}", :default, :default) unless @dependencies.include? segments.first
        end
        next if @options[:external] and segments.first == @namespace

        @results[kind][name] ||= 0
        @results[kind][name]  += count
      end
    end
  end
  self
end