Module: FalkorLib::Puppet::Modules

Defined in:
lib/falkorlib/puppet/modules.rb

Overview

Management of Puppet Modules operations

Class Method Summary collapse

Class Method Details

._get_classdefs(moduledir = Dir.pwd, type = 'classes') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/falkorlib/puppet/modules.rb', line 58

def _get_classdefs(moduledir = Dir.pwd, type = 'classes')
 name     = File.basename( moduledir )
 error "The module #{name} does not exist" unless File.directory?( moduledir )
 t = case type
     when /class*/i 
      'class'
     when /def*/    
      'define'
     else 
      ''		            
 end
 error "Undefined type #{type}" if t.empty?	            
    result = []
    Dir["#{moduledir}/manifests/**/*.pp"].each do |ppfile|
  File.read(ppfile).scan(/^[ \t]*#{t}[\s]+([0-9a-zA-z:]+).*$/).each do |line|
            result << line[0]
        end
    end
    result.uniq!
    result
end

.classes(moduledir = Dir.pwd) ⇒ Object

Find the classes of a given module



206
207
208
# File 'lib/falkorlib/puppet/modules.rb', line 206

def classes(moduledir = Dir.pwd)
 _get_classdefs(moduledir, 'classes')
end

.definitions(moduledir = Dir.pwd) ⇒ Object

Find the definitions of a given module



213
214
215
# File 'lib/falkorlib/puppet/modules.rb', line 213

def definitions(moduledir = Dir.pwd)
 _get_classdefs(moduledir, 'definitions')
end

.deps(moduledir = Dir.pwd) ⇒ Object

Find the dependencies of a given module



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/falkorlib/puppet/modules.rb', line 220

def deps(moduledir = Dir.pwd)
 name     = File.basename( moduledir )
 error "The module #{name} does not exist" unless File.directory?( moduledir )

 result    = Array.new
 result2   = Array.new
 resulttmp = Array.new

 result << name

 while result != result2 do
  resulttmp = result.dup
  (result - result2).each do |x|
   Dir["#{moduledir}/**/*.pp"].each do |ppfile|
    File.read(ppfile).scan(/^[ \t]*include.*$|^[ \t]*require.*$/).each do |line|
     if line.scan(">").length == 0
      result << line.gsub(/^[ \t]*(include|require) ([\"']|)([0-9a-zA-Z:{$}\-]*)([\"']|)/, '\3').split("::").first
     end
    end
   end
  end
  result.uniq!
  result2 = resulttmp.dup
 end
 result.delete "#{name}"
 result
end

.init(rootdir = Dir.pwd, name = '', options = {}) ⇒ Object

Initialize a new Puppet Module named name in rootdir. Supported options:

  • :no_iteraction [boolean]


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
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
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/falkorlib/puppet/modules.rb', line 87

def init(rootdir = Dir.pwd, name = '', options = {})
    config = {}
     = `whoami`.chomp
    config[:name] = name unless name.empty?
 moduledir = name.empty? ? rootdir : File.join(rootdir, name)
    FalkorLib::Config::Puppet::Modules::DEFAULTS[:metadata].each do |k,v|
        next if v.kind_of?(Array) or k == :license
        next if k == :name and ! name.empty?
        default_answer = case k
                         when :project_page
                             config[:source].nil? ? v : config[:source]
                         when :name
                             File.basename(rootdir).gsub(/^puppet-/, '')
                         when :issue_url
                             config[:project_page].nil? ? v : "#{config[:project_page]}/issues"
                         when :description
                             config[:summary].nil? ? v : "#{config[:summary]}"
                         when :source
                             v.empty? ? "https://github.com/#{`whoami`.chomp}/#{config[:name]}" : v
                         else
                             v
                         end
        config[k.to_sym] = ask( "\t" + sprintf("%-20s", "Module #{k}"), default_answer)
    end
    name = config[:name].gsub(/.*-/, '')
 tags = ask("\tKeywords (comma-separated list of tags)", name)
    config[:tags] = tags.split(',')
    license = select_from(FalkorLib::Config::Puppet::Modules::DEFAULTS[:licenses],
                          'Select the license index for the Puppet module:',
                          1)
    config[:license] = license.downcase unless license.empty?
    puts "\t" + sprintf("%-20s", "Module License:") + config[:license]

    #ap config
    # Bootstrap the directory
    templatedir = File.join( FalkorLib.templates, 'puppet', 'modules')
 init_from_template(templatedir, moduledir, config, {
                           :erb_exclude => [ 'templates\/[^\/]*\.erb$' ]
                       })
    # Rename the files / element templatename
    Dir["#{moduledir}/**/*"].each do |e|
        next unless e =~ /templatename/
        info "renaming #{e}"
        newname = e.gsub(/templatename/, "#{name}")
        run %{ mv #{e} #{newname} }
    end

    info "Generating the License file"
    authors = config[:author].empty? ? 'UNKNOWN' : config[:author]
    Dir.chdir(moduledir) do
        run %{ licgen #{config[:license]} #{authors} }
    end
    info "Initialize RVM"
    init_rvm(moduledir)
    unless FalkorLib::GitFlow.init?(moduledir)
        warn "Git [Flow] is not initialized in #{moduledir}."
        a = ask("Proceed to git-flow initialization (Y|n)", 'Yes')
        FalkorLib::GitFlow.init(moduledir) unless a =~ /n.*/i
    end

    # Propose to commit the key files
    if FalkorLib::Git.init?(moduledir)
        if FalkorLib::GitFlow.init?(moduledir)
            info "=> preparing git-flow feature for the newly created module '#{config[:name]}'"
            FalkorLib::GitFlow.start('feature', "init_#{name}", moduledir)
        end
        [ 'metadata.json', 'LICENSE', '.gitignore', 'Gemfile', 'Rakefile'].each do |f|
            FalkorLib::Git.add(File.join(moduledir, f))
        end
    end
end

.parse(moduledir = Dir.pwd) ⇒ Object

Parse a given modules to collect information



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/falkorlib/puppet/modules.rb', line 162

def parse(moduledir = Dir.pwd)
    name     = File.basename( moduledir )
    error "The module #{name} does not exist" unless File.directory?( moduledir )
 jsonfile = File.join( moduledir, 'metadata.json')
 error "Unable to find #{jsonfile}" unless File.exist?( jsonfile ) 
  = JSON.parse( IO.read( jsonfile ) )
 ["classes"]     = classes(moduledir)
 ["definitions"] = definitions(moduledir)
 deps        = deps(moduledir)
 listed_deps = ["dependencies"]
 missed_deps = []
 ["dependencies"].each do |dep|
  lib = dep["name"].gsub(/^[^\/-]+[\/-]/,'')
  if deps.include?( lib )
   deps.delete( lib ) 
  else 
   unless lib =~ /stdlib/
    warn "The library '#{dep["name"]}' is not analyzed as part of the #{name} module" 
    missed_deps << dep
   end 
  end 
 end
 if ! deps.empty?
  deps.each do |l| 
   warn "The module '#{l}' is missing in the dependencies thus added"
    = ask("[Github] login for the module '#{lib}'")
   version = ask("Version requirement (ex: '>=1.0.0 <2.0.0' or '1.2.3' or '1.x')")
   ["dependencies"] << {
    "name"                => "#{}/#{lib}",
    "version_requirement" => "#{version}"
   }
  end
  warn "About to commit these changes in the '#{name}/metadata.json' file"
  really_continue?
  File.open(jsonfile,"w") do |f|
   f.write JSON.pretty_generate(  )
  end
 end
end