diff options
| author | Luke Kanies <luke@madstop.com> | 2009-10-02 12:08:56 -0700 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-12-09 02:11:05 +1100 |
| commit | adc211ad191568e84eb3e1f618f1cbf78df95ba9 (patch) | |
| tree | 01e6aab12e10204e75d0b7f947309b4462fb49e9 /lib/puppet/module.rb | |
| parent | bf40f4f3a289aaacba047337a9d18d4a9bc5b997 (diff) | |
| download | puppet-adc211ad191568e84eb3e1f618f1cbf78df95ba9.tar.gz puppet-adc211ad191568e84eb3e1f618f1cbf78df95ba9.tar.xz puppet-adc211ad191568e84eb3e1f618f1cbf78df95ba9.zip | |
Adding module metadata
This is a first version that does very little -
it has a few fields, and allows speciification of
dependencies with other modules as well as compatibility
with individual Puppet versions.
It's not really sufficient, because it only allows specific
versions, rather than a range of versions, but it's a good
demo of what it takes and what we provide.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/module.rb')
| -rw-r--r-- | lib/puppet/module.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb index 9aa634bc5..b0d80caa6 100644 --- a/lib/puppet/module.rb +++ b/lib/puppet/module.rb @@ -2,6 +2,12 @@ require 'puppet/util/logging' # Support for modules class Puppet::Module + class MissingModule < Puppet::Error; end + class IncompatibleModule < Puppet::Error; end + class UnsupportedPlatform < Puppet::Error; end + class IncompatiblePlatform < Puppet::Error; end + class MissingMetadata < Puppet::Error; end + include Puppet::Util::Logging class InvalidName < ArgumentError @@ -35,6 +41,14 @@ class Puppet::Module attr_reader :name, :environment attr_writer :environment + attr_accessor :source, :author, :version, :license, :puppetversion, :summary, :description, :project_page + + def has_metadata? + return false unless metadata_file + + FileTest.exist?(metadata_file) + end + def initialize(name, environment = nil) @name = name @@ -45,6 +59,11 @@ class Puppet::Module else @environment = Puppet::Node::Environment.new(environment) end + + load_metadata if has_metadata? + + validate_puppet_version + validate_dependencies end FILETYPES.each do |type| @@ -86,6 +105,25 @@ class Puppet::Module subpath("files") end + def license_file + return @license_file if defined?(@license_file) + + return @license_file = nil unless path + @license_file = File.join(path, "License") + end + + def load_metadata + data = JSON.parse File.read(metadata_file) + [:source, :author, :version, :license, :puppetversion].each do |attr| + unless value = data[attr.to_s] + unless attr == :puppetversion + raise MissingMetadata, "No #{attr} module metadata provided for #{self.name}" + end + end + send(attr.to_s + "=", value) + end + end + # Return the list of manifests matching the given glob pattern, # defaulting to 'init.pp' for empty modules. def match_manifests(rest) @@ -100,6 +138,13 @@ class Puppet::Module result.flatten.compact end + def metadata_file + return @metadata_file if defined?(@metadata_file) + + return @metadata_file = nil unless path + @metadata_file = File.join(path, "metadata.json") + end + # Find this module in the modulepath. def path environment.modulepath.collect { |path| File.join(path, name) }.find { |d| FileTest.exist?(d) } @@ -110,6 +155,16 @@ class Puppet::Module subpath("plugins") end + def requires(name, version = nil) + @requires ||= [] + @requires << [name, version] + end + + def supports(name, version = nil) + @supports ||= [] + @supports << [name, version] + end + def to_s result = "Module %s" % name if path @@ -118,6 +173,25 @@ class Puppet::Module result end + 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 + + 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 + private def find_init_manifest |
