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, FaultyMetadata, IncompatibleModule, IncompatiblePlatform, InvalidFilePattern, InvalidName, MissingMetadata, MissingModule, UnsupportedPlatform

Constant Summary collapse

FILETYPES =
{
  "manifests" => "manifests",
  "files" => "files",
  "templates" => "templates",
  "plugins" => "lib",
  "pluginfacts" => "facts.d",
}

Constants included from Util::Logging

Util::Logging::FILE_AND_LINE, Util::Logging::FILE_NO_LINE, Util::Logging::MM, Util::Logging::NO_FILE_LINE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#clear_deprecation_warnings, #debug, #deprecation_warning, #format_exception, #get_deprecation_offender, #log_and_raise, #log_deprecations_to_file, #log_exception, #puppet_deprecation_warning, #send_log, setup_facter_logging!, #warn_once

Constructor Details

#initialize(name, path, environment) ⇒ Module

Returns a new instance of Module.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/module.rb', line 62

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

  assert_validity

   if has_metadata?

  @absolute_path_to_manifests = Puppet::FileSystem::PathPattern.absolute(manifests)
end

Instance Attribute Details

#authorObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def author
  @author
end

#dependenciesObject



59
60
61
# File 'lib/puppet/module.rb', line 59

def dependencies
  @dependencies
end

#descriptionObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def description
  @description
end

#environmentObject



56
57
58
# File 'lib/puppet/module.rb', line 56

def environment
  @environment
end

#forge_nameObject



59
60
61
# File 'lib/puppet/module.rb', line 59

def forge_name
  @forge_name
end

#licenseObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def license
  @license
end

#metadataObject (readonly)



56
57
58
# File 'lib/puppet/module.rb', line 56

def 
  @metadata
end

#nameObject (readonly)



56
57
58
# File 'lib/puppet/module.rb', line 56

def name
  @name
end

#pathObject (readonly)



56
57
58
# File 'lib/puppet/module.rb', line 56

def path
  @path
end

#project_pageObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def project_page
  @project_page
end

#sourceObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def source
  @source
end

#summaryObject



60
61
62
# File 'lib/puppet/module.rb', line 60

def summary
  @summary
end

#versionObject



60
61
62
# File 'lib/puppet/module.rb', line 60

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



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

def self.find(modname, environment = nil)
  return nil unless modname
  # Unless a specific environment is given, use the current environment
  env = environment ? Puppet.lookup(:environments).get!(environment) : Puppet.lookup(:current_environment)
  env.module(modname)
end

.is_module_directory?(name, path) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.is_module_directory?(name, path)
  # it must be a directory
  fullpath = File.join(path, name)
  return false unless Puppet::FileSystem.directory?(fullpath)
  return is_module_directory_name?(name)
end

.is_module_directory_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.is_module_directory_name?(name)
  # it must match an installed module name according to forge validator
  return true if name =~ /^[a-z][a-z0-9_]*$/
  return false
end

.is_module_namespaced_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/puppet/module.rb', line 50

def self.is_module_namespaced_name?(name)
  # it must match the full module name according to forge validator
  return true if name =~ /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/
  return false
end

Instance Method Details

#==(other) ⇒ Object



331
332
333
334
335
336
# File 'lib/puppet/module.rb', line 331

def ==(other)
  self.name == other.name &&
  self.version == other.version &&
  self.path == other.path &&
  self.environment == other.environment
end

#all_manifestsObject



196
197
198
199
200
# File 'lib/puppet/module.rb', line 196

def all_manifests
  return [] unless Puppet::FileSystem.exist?(manifests)

  Dir.glob(File.join(manifests, '**', '*.pp'))
end

#dependencies_as_modulesObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/puppet/module.rb', line 237

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

#has_external_facts?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/puppet/module.rb', line 222

def has_external_facts?
  File.directory?(plugin_fact_directory)
end

#has_metadata?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/module.rb', line 88

def has_metadata?
  return false unless 

  return false unless Puppet::FileSystem.exist?()

  begin
     =  JSON.parse(File.read(, :encoding => 'utf-8'))
  rescue JSON::JSONError => e
    msg = "#{name} has an invalid and unparsable metadata.json file. The parse error: #{e.message}"
    case Puppet[:strict]
    when :off, :warning
      Puppet.warning(msg)
    when :error
      raise FaultyMetadata, msg
    end
    return false
  end

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

#license_fileObject



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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppet/module.rb', line 154

def 
  @metadata = data = JSON.parse(File.read(, :encoding => 'utf-8'))
  @forge_name = data['name'].gsub('-', '/') if data['name']

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

    if attr == :dependencies
      unless value.is_a?(Array)
        raise MissingMetadata, "The value for the key dependencies in the file metadata.json of the module #{self.name} must be an array, not: '#{value}'"
      end
      value.each do |dep|
        name = dep['name']
        dep['name'] = name.tr('-', '/') unless name.nil?
        dep['version_requirement'] ||= '>= 0.0.0'
      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’ for empty modules.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/module.rb', line 180

def match_manifests(rest)
  if rest
    wanted_manifests = wanted_manifests_from(rest)
    searched_manifests = wanted_manifests.glob.reject { |f| FileTest.directory?(f) }
  else
    searched_manifests = []
  end

  # (#4220) Always ensure init.pp in case class is defined there.
  init_manifest = manifest("init.pp")
  if !init_manifest.nil? && !searched_manifests.include?(init_manifest)
    searched_manifests.unshift(init_manifest)
  end
  searched_manifests
end

#metadata_fileObject



202
203
204
205
206
207
# File 'lib/puppet/module.rb', line 202

def 
  return @metadata_file if defined?(@metadata_file)

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

#modulepathObject



209
210
211
# File 'lib/puppet/module.rb', line 209

def modulepath
  File.dirname(path) if path
end

#plugin_directoryObject

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



214
215
216
# File 'lib/puppet/module.rb', line 214

def plugin_directory
  subpath("lib")
end

#plugin_fact_directoryObject



218
219
220
# File 'lib/puppet/module.rb', line 218

def plugin_fact_directory
  subpath("facts.d")
end

#puppetversionObject

Deprecated.

The puppetversion module metadata field is no longer used.



75
76
77
# File 'lib/puppet/module.rb', line 75

def puppetversion
  nil
end

#puppetversion=(something) ⇒ Object

Deprecated.

The puppetversion module metadata field is no longer used.



80
81
# File 'lib/puppet/module.rb', line 80

def puppetversion=(something)
end

#required_byObject



248
249
250
# File 'lib/puppet/module.rb', line 248

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

#supports(name, version = nil) ⇒ Object



226
227
228
229
# File 'lib/puppet/module.rb', line 226

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

#to_sObject



231
232
233
234
235
# File 'lib/puppet/module.rb', line 231

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'
    }
  }
]


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/puppet/module.rb', line 278

def unmet_dependencies
  unmet_dependencies = []
  return unmet_dependencies unless dependencies

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

    dep_mod = begin
      environment.module_by_forge_name(name)
    rescue
      nil
    end

    error_details = {
      :name => 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

Deprecated.

The puppetversion module metadata field is no longer used.



84
85
86
# File 'lib/puppet/module.rb', line 84

def validate_puppet_version
  return
end