Skip to content

Commit

Permalink
Convert to separate type & provider
Browse files Browse the repository at this point in the history
Prior to this commit, the puppet_module repository offered a provider
for the package type for managing Puppet Modules from the Forge.

This provider was limited in what information it could accept from
the user because the type (package) controls the user interface.
Because of this, a dedicated type is appropriate once you want to
do things like specify a modulepath.

This commit refactors the work to introduce a 'module' type and a
forge provider. At present, this code will install, remove, upgrade
and otherwise help you manage modules from the Forge as resources.
It should still be considered experimental.
  • Loading branch information
Ryan Coleman committed Jan 8, 2013
1 parent 89115ad commit 7777e12
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
require 'puppet/provider/package'
require 'puppet/face'
require 'uri'

Puppet::Type.type(:package).provide :puppet_module, :parent => Puppet::Provider::Package do
desc "A Package provider for Puppet Modules"

has_feature :versionable
Puppet::Type.type(:module).provide :forge do
desc "A Forge provider for Puppet Modules"

@@module = Puppet::Face[:module, :current]

def self.installed
@@module.list.map do |module_path, modules|
modules.map do |mod|
{
:name => mod.forge_name,
:ensure => mod.version,
:modulepath => module_path,
:provider => :puppet_module,
}
end
end.flatten
end

def self.instances
installed.map { |x| new(x) }
end

def install
return self.update unless query.nil?
def create
return self.update unless exists?.nil?
options = {}
options[:version] = resource[:ensure] unless resource[:ensure].is_a? Symbol

# Allow modulepath to be set
options[:modulepath] = resource[:modulepath]

if resource[:source] =~ /$https?:/
options[:module_repository] = resource[:source]
Expand All @@ -49,11 +32,32 @@ def latest
@@module.search(resource[:name])[:answers][0]['version']
end

def query
def exists?
self.class.installed.find { |x| x[:name] == resource[:name] }
end

def uninstall
def self.installed
@@module.list.map do |module_path, modules|

modules.map do |mod|
#puts mod.inspect
{
:name => mod.forge_name,
:ensure => mod.version,
:modulepath => module_path,
:provider => :puppet_module,
}
end

end.flatten
end

def self.instances
installed.map { |x| new(x) }
end


def destroy
begin
output = @@module.uninstall(resource[:name])
raise output[:error][:oneline] if output.key?(:error)
Expand All @@ -63,9 +67,12 @@ def uninstall
end

def update
return self.install if query.nil?
return self.install if exists?.nil?
options = {}
options[:version] = resource[:ensure] unless resource[:ensure].is_a? Symbol

# Allow modulepath to be set
options[:modulepath] = resource[:modulepath]

if resource[:source] =~ /$https?:/
options[:module_repository] = resource[:source]
Expand Down
31 changes: 31 additions & 0 deletions lib/puppet/type/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Puppet::Type.newtype(:module) do

ensurable do

attr_accessor :latest

newvalue(:present, :event => :module_installed) do
provider.create
end

newvalue(:absent, :event => :module_removed) do
provider.destroy
end

newvalue(/./) do
provider.create
end

defaultto :present
end


newparam(:name, :namevar => true)

newparam(:version)

newparam(:modulepath)

newparam(:source)

end

0 comments on commit 7777e12

Please sign in to comment.