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

#send_log

Constructor Details

#initialize(name, environment = nil) ⇒ Module

Returns a new instance of Module.



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

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:

  • (Boolean)


95
96
97
# File 'lib/puppet/module.rb', line 95

def exist?
  ! path.nil?
end

#file_directoryObject

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



100
101
102
# File 'lib/puppet/module.rb', line 100

def file_directory
  subpath("files")
end

#has_metadata?Boolean

Returns:

  • (Boolean)


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

def has_metadata?
  return false unless 

  FileTest.exist?()
end

#license_fileObject



104
105
106
107
108
109
# File 'lib/puppet/module.rb', line 104

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



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/module.rb', line 111

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.



125
126
127
128
129
130
# File 'lib/puppet/module.rb', line 125

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



132
133
134
135
136
137
# File 'lib/puppet/module.rb', line 132

def 
  return  if defined?()

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

#pathObject

Find this module in the modulepath.



140
141
142
# File 'lib/puppet/module.rb', line 140

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

#plugin_directoryObject

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



145
146
147
# File 'lib/puppet/module.rb', line 145

def plugin_directory
  subpath("plugins")
end

#requires(name, version = nil) ⇒ Object



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

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

#supports(name, version = nil) ⇒ Object



154
155
156
157
# File 'lib/puppet/module.rb', line 154

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

#to_sObject



159
160
161
162
163
# File 'lib/puppet/module.rb', line 159

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

#validate_dependenciesObject



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/puppet/module.rb', line 165

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:



179
180
181
182
# File 'lib/puppet/module.rb', line 179

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