| Class | Puppet::Module |
| In: |
lib/puppet/module.rb
|
| Parent: | Object |
Support for modules
| TEMPLATES | = | "templates" |
| FILES | = | "files" |
| MANIFESTS | = | "manifests" |
| name | [R] | |
| path | [R] |
Return an array of the full path of every subdirectory in each directory in the modulepath.
# File lib/puppet/module.rb, line 49
49: def self.all(environment = nil)
50: modulepath(environment).map do |mp|
51: Dir.new(mp).map do |modfile|
52: modpath = File.join(mp, modfile)
53: unless modfile == '.' or modfile == '..' or !File.directory?(modpath)
54: modpath
55: else
56: nil
57: end
58: end
59: end.flatten.compact
60: end
Find and return the module that path belongs to. If path is absolute, or if there is no module whose name is the first component of path, return nil
# File lib/puppet/module.rb, line 34
34: def self.find(modname, environment = nil)
35: if modname =~ %r/^#{File::SEPARATOR}/
36: return nil
37: end
38:
39: modpath = modulepath(environment).collect { |path|
40: File::join(path, modname)
41: }.find { |f| File::directory?(f) }
42: return nil unless modpath
43:
44: return self.new(modname, modpath)
45: end
Return a list of manifests (as absolute filenames) that match pat with the current directory set to cwd. If the first component of pat does not contain any wildcards and is an existing module, return a list of manifests in that module matching the rest of pat Otherwise, try to find manifests matching pat relative to cwd
# File lib/puppet/module.rb, line 115
115: def self.find_manifests(start, options = {})
116: cwd = options[:cwd] || Dir.getwd
117: module_name, pattern = split_path(start)
118: if module_name and mod = find(module_name, options[:environment])
119: return mod.manifests(pattern)
120: else
121: abspat = File::expand_path(start, cwd)
122: files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) }
123: if files.size == 0
124: files = Dir.glob(abspat + ".pp").reject { |f| FileTest.directory?(f) }
125: end
126: return files
127: end
128: end
Find the concrete file denoted by file. If file is absolute, return it directly. Otherwise try to find it as a template in a module. If that fails, return it relative to the templatedir config param. In all cases, an absolute path is returned, which does not necessarily refer to an existing file
# File lib/puppet/module.rb, line 70
70: def self.find_template(template, environment = nil)
71: if template =~ /^#{File::SEPARATOR}/
72: return template
73: end
74:
75: template_paths = templatepath(environment)
76: if template_paths
77: # If we can find the template in :templatedir, we return that.
78: td_file = template_paths.collect { |path|
79: File::join(path, template)
80: }.find { |f| File.exists?(f) }
81:
82: return td_file unless td_file == nil
83: end
84:
85: td_file = find_template_for_module(template, environment)
86:
87: # check in the default template dir, if there is one
88: if td_file.nil?
89: raise Puppet::Error, "No valid template directory found, please check templatedir settings" if template_paths.nil?
90: td_file = File::join(template_paths.first, template)
91: end
92: td_file
93: end
Return an array of paths by splitting the modulepath config parameter. Only consider paths that are absolute and existing directories
# File lib/puppet/module.rb, line 11
11: def self.modulepath(environment = nil)
12: dirs = Puppet.settings.value(:modulepath, environment).split(":")
13: if ENV["PUPPETLIB"]
14: dirs = ENV["PUPPETLIB"].split(":") + dirs
15: else
16: end
17: dirs.select do |p|
18: p =~ /^#{File::SEPARATOR}/ && File::directory?(p)
19: end
20: end
# File lib/puppet/module.rb, line 144
144: def initialize(name, path)
145: @name = name
146: @path = path
147: end
Split the path into the module and the rest of the path. This method can and often does return nil, so anyone calling it needs to handle that.
# File lib/puppet/module.rb, line 133
133: def self.split_path(path)
134: if path =~ %r/^#{File::SEPARATOR}/
135: return nil
136: end
137:
138: modname, rest = path.split(File::SEPARATOR, 2)
139: return nil if modname.nil? || modname.empty?
140: return modname, rest
141: end
Return an array of paths by splitting the templatedir config parameter.
# File lib/puppet/module.rb, line 24
24: def self.templatepath(environment = nil)
25: dirs = Puppet.settings.value(:templatedir, environment).split(":")
26: dirs.select do |p|
27: File::directory?(p)
28: end
29: end
Return the list of manifests matching the given glob pattern, defaulting to ‘init.pp’ for empty modules.
# File lib/puppet/module.rb, line 159
159: def manifests(rest)
160: rest ||= "init.pp"
161: p = File::join(path, MANIFESTS, rest)
162: files = Dir.glob(p).reject { |f| FileTest.directory?(f) }
163: if files.size == 0
164: files = Dir.glob(p + ".pp")
165: end
166: return files
167: end