Class: Puppet::Module

Inherits:
Object show all
Includes:
Util::Logging
Defined in:
lib/vendor/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, options = {}) ⇒ Module

Returns a new instance of Module.



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

def initialize(name, options = {})
  @name = name
  @path = options[:path]

  assert_validity

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

   if has_metadata?

  validate_puppet_version
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



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

def author
  @author
end

#dependenciesObject

Returns the value of attribute dependencies.



35
36
37
# File 'lib/vendor/puppet/module.rb', line 35

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#forge_nameObject

Returns the value of attribute forge_name.



35
36
37
# File 'lib/vendor/puppet/module.rb', line 35

def forge_name
  @forge_name
end

#licenseObject

Returns the value of attribute license.



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

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#project_pageObject

Returns the value of attribute project_page.



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

def project_page
  @project_page
end

#puppetversionObject

Returns the value of attribute puppetversion.



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

def puppetversion
  @puppetversion
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#versionObject

Returns the value of attribute version.



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

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



27
28
29
30
# File 'lib/vendor/puppet/module.rb', line 27

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

Instance Method Details

#dependencies_as_modulesObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/vendor/puppet/module.rb', line 179

def dependencies_as_modules
  dependent_modules = []
  dependencies and dependencies.each do |dep|
    author, dep_name = dep["name"].split('/')
    found_module = environment.module(dep_name)
    dependent_modules << found_module if found_module
  end

  dependent_modules
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  ! path.nil?
end

#file_directoryObject

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



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

def file_directory
  subpath("files")
end

#has_local_changes?Boolean

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/vendor/puppet/module.rb', line 194

def has_local_changes?
  changes = Puppet::ModuleTool::Applications::Checksummer.run(path)
  !changes.empty?
end

#has_metadata?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/vendor/puppet/module.rb', line 38

def has_metadata?
  return false unless 

  return false unless FileTest.exist?()

   = PSON.parse File.read()


  return .is_a?(Hash) && !.keys.empty?
end

#license_fileObject



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

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



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
# File 'lib/vendor/puppet/module.rb', line 112

def 
  data = PSON.parse File.read()
  @forge_name = data['name'].gsub('-', '/') if data['name']

  [:source, :author, :version, :license, :puppetversion, :dependencies].each do |attr|
    unless value = data[attr.to_s]
      unless attr == :puppetversion
        raise MissingMetadata, "No #{attr} module metadata provided for #{self.name}"
      end
    end

    # NOTICE: The fallback to `versionRequirement` is something we'd like to
    # not have to support, but we have a reasonable number of releases that
    # don't use `version_requirement`. When we can deprecate this, we should.
    if attr == :dependencies
      value.tap do |dependencies|
        dependencies.each do |dep|
          dep['version_requirement'] ||= dep['versionRequirement'] || '>= 0.0.0'
        end
      end
    end

    send(attr.to_s + "=", value)
  end
end

#local_changesObject



199
200
201
# File 'lib/vendor/puppet/module.rb', line 199

def local_changes
  Puppet::ModuleTool::Applications::Checksummer.run(path)
end

#match_manifests(rest) ⇒ Object

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



140
141
142
143
144
145
# File 'lib/vendor/puppet/module.rb', line 140

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



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

def 
  return @metadata_file if defined?(@metadata_file)

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

#modulepathObject



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

def modulepath
  File.dirname(path) if path
end

#pathObject

Find this module in the modulepath.



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

def path
  @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.



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

def plugin_directory
  subpath("plugins")
end

#required_byObject



190
191
192
# File 'lib/vendor/puppet/module.rb', line 190

def required_by
  environment.module_requirements[self.forge_name] || {}
end

#supports(name, version = nil) ⇒ Object



168
169
170
171
# File 'lib/vendor/puppet/module.rb', line 168

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

#to_sObject



173
174
175
176
177
# File 'lib/vendor/puppet/module.rb', line 173

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

#unmet_dependenciesObject

Identify and mark unmet dependencies. A dependency will be marked unmet for the following reasons:

* not installed and is thus considered missing
* installed and does not meet the version requirements for this module
* installed and doesn't use semantic versioning

Returns a list of hashes representing the details of an unmet dependency.

Example:

[
  {
    :reason => :missing,
    :name   => 'puppetlabs-mysql',
    :version_constraint => 'v0.0.1',
    :mod_details => {
      :installed_version => '0.0.1'
    }
    :parent => {
      :name    => 'puppetlabs-bacula',
      :version => 'v1.0.0'
    }
  }
]


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/vendor/puppet/module.rb', line 229

def unmet_dependencies
  unmet_dependencies = []
  return unmet_dependencies unless dependencies

  dependencies.each do |dependency|
    forge_name = dependency['name']
    version_string = dependency['version_requirement'] || '>= 0.0.0'

    dep_mod = begin
      environment.module_by_forge_name(forge_name)
    rescue => e
      nil
    end

    error_details = {
      :name => forge_name,
      :version_constraint => version_string.gsub(/^(?=\d)/, "v"),
      :parent => {
        :name => self.forge_name,
        :version => self.version.gsub(/^(?=\d)/, "v")
      },
      :mod_details => {
        :installed_version => dep_mod.nil? ? nil : dep_mod.version
      }
    }

    unless dep_mod
      error_details[:reason] = :missing
      unmet_dependencies << error_details
      next
    end

    if version_string
      begin
        required_version_semver_range = SemVer[version_string]
        actual_version_semver = SemVer.new(dep_mod.version)
      rescue ArgumentError
        error_details[:reason] = :non_semantic_version
        unmet_dependencies << error_details
        next
      end

      unless required_version_semver_range.include? actual_version_semver
        error_details[:reason] = :version_mismatch
        unmet_dependencies << error_details
        next
      end
    end
  end

  unmet_dependencies
end

#validate_puppet_versionObject

Raises:



282
283
284
285
# File 'lib/vendor/puppet/module.rb', line 282

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