Class: Puppetfiler::Puppetfile

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetfiler/puppetfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'Puppetfile') ⇒ Puppetfile

Returns a new instance of Puppetfile.



10
11
12
13
14
15
16
17
18
19
# File 'lib/puppetfiler/puppetfile.rb', line 10

def initialize(path = 'Puppetfile')
    @modules    = {}
    @repos      = {}
    @puppetfile = path

    @maxlen_name = 0
    @maxlen_ver  = 0

    self.evaluate
end

Instance Attribute Details

#maxlen_nameObject (readonly)

Returns the value of attribute maxlen_name.



8
9
10
# File 'lib/puppetfiler/puppetfile.rb', line 8

def maxlen_name
  @maxlen_name
end

#modulesObject (readonly)

Returns the value of attribute modules.



5
6
7
# File 'lib/puppetfiler/puppetfile.rb', line 5

def modules
  @modules
end

#puppetfileObject (readonly)

Returns the value of attribute puppetfile.



7
8
9
# File 'lib/puppetfiler/puppetfile.rb', line 7

def puppetfile
  @puppetfile
end

#reposObject (readonly)

Returns the value of attribute repos.



6
7
8
# File 'lib/puppetfiler/puppetfile.rb', line 6

def repos
  @repos
end

Instance Method Details

#evaluateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppetfiler/puppetfile.rb', line 21

def evaluate
    if not File.exists?(@puppetfile)
        STDERR.puts "Puppetfile not found at path '#{@puppetfile}'"
        return nil
    end

    begin
        self.instance_eval(File.read(@puppetfile))
    rescue SyntaxError => error
        STDERR.puts "Puppetfile at path '#{@puppetfile}' is invalid:"
        STDERR.puts error
        return nil
    end
end

#fixture(modifiers = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puppetfiler/puppetfile.rb', line 58

def fixture(modifiers = {})
    fixtures = {
        'forge_modules' => {},
        'repositories'  => {},
    }

    fixtures.each do |k, v|
        modifiers[k] = {} if not modifiers.has_key?(k)
    end

    @modules.each do |name, version|
        short = name.split('/')[1]
        value = {
            'repo' => name,
            'ref'  => version,
        }

        modifiers['forge_modules'].each do |modifier, merger|
            # TODO use x.match?(y) on ruby 2.4
            value.merge!(merger) if name =~ /#{modifier}/
        end

        fixtures['forge_modules'][short] = value
    end

    @repos.each do |name, hash|
        if hash.has_key?(:ref)
            content = {
                'repo' => hash[:uri],
                'ref'  => hash[:ref],
            }

            modifiers['repositories'].each do |modifier, merger|
                content.merge!(merger) if name =~ /#{modifier}/
            end
        else
            content = hash[:uri]

            modifiers['repositories'].each do |modifier, merger|
                if name =~ /#{modifier}/
                    if merger.is_a?(String)
                        content = merger
                    else
                        content = {
                            'repo' => hash[:uri],
                        }

                        content.merge!(merger)
                    end
                end
            end
        end

        fixtures['repositories'][name] = content
    end

    { 'fixtures' => fixtures }
end

#maxlen_verObject



36
37
38
# File 'lib/puppetfiler/puppetfile.rb', line 36

def maxlen_ver
    [@maxlen_ver, 'current'.length].max
end

#updatesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppetfiler/puppetfile.rb', line 40

def updates
    updates = {}

    @modules.each do |name, version|
        current = SemanticPuppet::Version.parse(version)
        newest = Puppetfiler::Mod.newest(name)

        if not newest.eql?(current)
            updates[name] = {
                :current => current,
                :newest  => newest,
            }
        end
    end

    return updates
end