Class: Puppet::Module

Inherits:
Object show all
Includes:
Util::Logging
Defined in:
lib/puppet/module.rb

Overview

Support for modules

Defined Under Namespace

Classes: Error, IncompatibleModule, IncompatiblePlatform, InvalidName, MissingMetadata, MissingModule, UnsupportedPlatform

Constant Summary collapse

TEMPLATES =
"templates"
FILES =
"files"
MANIFESTS =
"manifests"
PLUGINS =
"plugins"
FILETYPES =
[MANIFESTS, FILES, TEMPLATES, PLUGINS]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#clear_deprecation_warnings, #deprecation_warning, #send_log

Constructor Details

#initialize(name, environment = nil) ⇒ Module

Returns a new instance of Module.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/module.rb', line 51

def initialize(name, environment = nil)
  @name = name

  assert_validity

  if environment.is_a?(Puppet::Node::Environment)
    @environment = environment
  else
    @environment = Puppet::Node::Environment.new(environment)
  end

   if has_metadata?

  validate_puppet_version
  validate_dependencies
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



40
41
42
# File 'lib/puppet/module.rb', line 40

def author
  @author
end

#descriptionObject

Returns the value of attribute description.



40
41
42
# File 'lib/puppet/module.rb', line 40

def description
  @description
end

#environmentObject

Returns the value of attribute environment.



37
38
39
# File 'lib/puppet/module.rb', line 37

def environment
  @environment
end

#licenseObject

Returns the value of attribute license.



40
41
42
# File 'lib/puppet/module.rb', line 40

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/puppet/module.rb', line 37

def name
  @name
end

#project_pageObject

Returns the value of attribute project_page.



40
41
42
# File 'lib/puppet/module.rb', line 40

def project_page
  @project_page
end

#puppetversionObject

Returns the value of attribute puppetversion.



40
41
42
# File 'lib/puppet/module.rb', line 40

def puppetversion
  @puppetversion
end

#sourceObject

Returns the value of attribute source.



40
41
42
# File 'lib/puppet/module.rb', line 40

def source
  @source
end

#summaryObject

Returns the value of attribute summary.



40
41
42
# File 'lib/puppet/module.rb', line 40

def summary
  @summary
end

#versionObject

Returns the value of attribute version.



40
41
42
# File 'lib/puppet/module.rb', line 40

def version
  @version
end

Class Method Details

.find(modname, environment = nil) ⇒ Object

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



32
33
34
35
# File 'lib/puppet/module.rb', line 32

def self.find(modname, environment = nil)
  return nil unless modname
  Puppet::Node::Environment.new(environment).module(modname)
end

.modulepath(environment = nil) ⇒ Object

Return an array of paths by splitting the modulepath config parameter. Only consider paths that are absolute and existing directories



25
26
27
# File 'lib/puppet/module.rb', line 25

def self.modulepath(environment = nil)
  Puppet::Node::Environment.new(environment).modulepath
end

Instance Method Details

#exist?Boolean

Returns:



98
99
100
# File 'lib/puppet/module.rb', line 98

def exist?
  ! path.nil?
end

#file_directoryObject

Find the first ‘files’ directory. This is used by the XMLRPC fileserver.



103
104
105
# File 'lib/puppet/module.rb', line 103

def file_directory
  subpath("files")
end

#has_metadata?Boolean

Returns:



42
43
44
45
46
47
48
49
# File 'lib/puppet/module.rb', line 42

def has_metadata?
  return false unless 

  return false unless FileTest.exist?()

   = PSON.parse File.read()
  return .is_a?(Hash) && !.keys.empty?
end

#license_fileObject



107
108
109
110
111
112
# File 'lib/puppet/module.rb', line 107

def license_file
  return @license_file if defined?(@license_file)

  return @license_file = nil unless path
  @license_file = File.join(path, "License")
end

#load_metadataObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/module.rb', line 114

def 
  data = PSON.parse File.read()
  [:source, :author, :version, :license, :puppetversion].each do |attr|
    unless value = data[attr.to_s]
      unless attr == :puppetversion
        raise , "No #{attr} module metadata provided for #{self.name}"
      end
    end
    send(attr.to_s + "=", value)
  end
end

#match_manifests(rest) ⇒ Object

Return the list of manifests matching the given glob pattern, defaulting to ‘init.pp,rb’ for empty modules.



128
129
130
131
132
133
# File 'lib/puppet/module.rb', line 128

def match_manifests(rest)
  pat = File.join(path, MANIFESTS, rest || 'init')
  [manifest("init.pp"),manifest("init.rb")].compact + Dir.
    glob(pat + (File.extname(pat).empty? ? '.{pp,rb}' : '')).
    reject { |f| FileTest.directory?(f) }
end

#metadata_fileObject



135
136
137
138
139
140
# File 'lib/puppet/module.rb', line 135

def 
  return  if defined?()

  return  = nil unless path
   = File.join(path, "metadata.json")
end

#pathObject

Find this module in the modulepath.



143
144
145
# File 'lib/puppet/module.rb', line 143

def path
  environment.modulepath.collect { |path| File.join(path, name) }.find { |d| FileTest.directory?(d) }
end

#plugin_directoryObject

Find all plugin directories. This is used by the Plugins fileserving mount.



148
149
150
# File 'lib/puppet/module.rb', line 148

def plugin_directory
  subpath("plugins")
end

#requires(name, version = nil) ⇒ Object



152
153
154
155
# File 'lib/puppet/module.rb', line 152

def requires(name, version = nil)
  @requires ||= []
  @requires << [name, version]
end

#supports(name, version = nil) ⇒ Object



157
158
159
160
# File 'lib/puppet/module.rb', line 157

def supports(name, version = nil)
  @supports ||= []
  @supports << [name, version]
end

#to_sObject



162
163
164
165
166
# File 'lib/puppet/module.rb', line 162

def to_s
  result = "Module #{name}"
  result += "(#{path})" if path
  result
end

#validate_dependenciesObject



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/puppet/module.rb', line 168

def validate_dependencies
  return unless defined?(@requires)

  @requires.each do |name, version|
    unless mod = environment.module(name)
      raise MissingModule, "Missing module #{name} required by #{self.name}"
    end

    if version and mod.version != version
      raise IncompatibleModule, "Required module #{name} is version #{mod.version} but #{self.name} requires #{version}"
    end
  end
end

#validate_puppet_versionObject

Raises:



182
183
184
185
# File 'lib/puppet/module.rb', line 182

def validate_puppet_version
  return unless puppetversion and puppetversion != Puppet.version
  raise IncompatibleModule, "Module #{self.name} is only compatible with Puppet version #{puppetversion}, not #{Puppet.version}"
end