Class ShadowPuppet::Manifest

  1. lib/shadow_puppet/manifest.rb (view online)
Parent: Object

A Manifest is an executable collection of Puppet Resources.

Example

class ManifestExample < ShadowPuppet::Manifest
  recipe :sample
  recipe :lamp, :ruby               # queue calls to self.lamp and
                                    # self.ruby when executing

  recipe :mysql, {                  # queue a call to self.mysql
    :root_password => 'OMGSEKRET'   # passing the provided hash
  }                                 # as an option

  def sample
    exec :foo, :command => 'echo "foo" > /tmp/foo.txt'

    package :foo, :ensure => :installed

    file '/tmp/example.txt',
      :ensure   => :present,
      :contents => Facter.to_hash_inspect,
      :require  => package(:foo)
  end

  def lamp
    #install a basic LAMP stack
  end

  def ruby
    #install a ruby interpreter and tools
  end

  def mysql(options)
     #install a mysql server and set the root password to options[:root_password]
  end

end

To execute the above manifest, instantiate it and call execute on it:

m = ManifestExample.new
m.execute

As shown in the sample method in ManifestExample above, instance methods are created for each Puppet::Type available on your system. These methods behave identally to the Puppet Resources methods. See here for documentation on these methods.

To view a list of all defined methods on your system, run:

ruby -rubygems -e 'require "shadow_puppet";puts ShadowPuppet::Manifest.puppet_type_methods'

The use of methods (sample, lamp, ruby, and mysql above) as a container for resources facilitates recipie re-use through the use of Ruby Modules. For example:

module ApachePuppet
  # Required options:
  #   domain
  #   path
  def php_vhost(options)
    #...
  end
 end

class MyWebMainfest < ShadowPuppet::Manifest
  include ApachePuppet
  recipe :php_vhost, {
    :domain => 'foo.com',
    :path => '/var/www/apps/foo'
  }
end

Attributes

puppet_resources [R]

Public class methods

configuration ()

A hash describing any configuration that has been performed on the class. Modify this hash by calling configure:

class SampleManifest < ShadowPuppet::Manifest
  configure(:name => 'test')
end

>> SampleManifest.configuration
=> {:name => 'test'}

All keys on this hash are coerced into symbols for ease of access.

Subclasses of the Manifest class properly inherit the parent classes’ configuration.

[show source]
# File lib/shadow_puppet/manifest.rb, line 136
    def self.configuration
      __config__.deep_symbolize_keys
    end
configure (hash)

Define configuration on this manifest. This is useful for storing things such as hostnames, password, or usernames that may change between different implementations of a shared manifest. Access this hash by calling configuration:

class SampleManifest < ShadowPuppet::Manifest
  configure('name' => 'test')
end

>> SampleManifest.configuration
=> {:name => 'test'}

All keys on this hash are coerced into symbols for ease of access.

Subsequent calls to configure perform a deep_merge of the provided hash into the pre-existing configuration.

[show source]
# File lib/shadow_puppet/manifest.rb, line 168
    def self.configure(hash)
      __config__.deep_merge!(hash)
    end
new (config = {})

Initialize a new instance of this manifest. This can take a config hash, which is immediately passed on to the configure method

[show source]
# File lib/shadow_puppet/manifest.rb, line 85
    def initialize(config = {})
      if Process.uid == 0
        Puppet[:confdir] = File.expand_path("/etc/shadow_puppet")
        Puppet[:vardir] = File.expand_path("/var/shadow_puppet")
      else
        Puppet[:confdir] = File.expand_path("~/.shadow_puppet")
        Puppet[:vardir] = File.expand_path("~/.shadow_puppet/var")
      end
      Puppet[:user] = Process.uid
      Puppet[:group] = Process.gid
      Puppet::Util::Log.newdestination(:console)

      configure(config)
      @executed = false
      @puppet_resources = Hash.new do |hash, key|
        hash[key] = {}
      end
    end
puppet_type_methods ()

An array of all methods defined for creation of Puppet Resources

[show source]
# File lib/shadow_puppet/manifest.rb, line 187
    def self.puppet_type_methods
      Puppet::Type.eachtype { |t| t.name }.keys.map { |n| n.to_s }.sort.inspect
    end
recipe (*methods)

Declares that the named method or methods will be called whenever execute is called on an instance of this class. If the last argument is a Hash, this hash is passed as an argument to all provided methods. If no options hash is provided, each method is passed the contents of configuration[method].

Subclasses of the Manifest class properly inherit the parent classes’ calls to recipe.

[show source]
# File lib/shadow_puppet/manifest.rb, line 112
    def self.recipe(*methods)
      return nil if methods.nil? || methods == []
      options = methods.extract_options!
      methods.each do |meth|
        options = configuration[meth.to_sym] if options == {}
        options ||= {}
        recipes << [meth.to_sym, options]
      end
    end
register_puppet_types ()

Create an instance method for every type that either creates or references a resource

[show source]
# File lib/shadow_puppet/manifest.rb, line 197
    def self.register_puppet_types
      Puppet::Type.loadall
      Puppet::Type.eachtype do |type|
        #undefine the method rdoc placeholders
        undef_method(type.name) rescue nil
        define_method(type.name) do |*args|
          if args && args.flatten.size == 1
            reference(type.name, args.first)
          else
            new_resource(type, args.first, args.last)
          end
        end
      end
    end

Public instance methods

configuration ()

Access to the configuration of the class of this instance.

class SampleManifest < ShadowPuppet::Manifest
  configure(:name => 'test')
end

@manifest = SampleManifest.new
@manifest.configuration[:name] => "test"
[show source]
# File lib/shadow_puppet/manifest.rb, line 148
    def configuration
      self.class.configuration
    end
configuration= (hash)

Alias for configure

configure (hash)

Update the configuration of this manifest instance’s class.

class SampleManifest < ShadowPuppet::Manifest
  configure({})
end

@manifest = SampleManifest.new
@manifest.configure(:name => "test")
@manifest.configuration[:name] => "test"
[show source]
# File lib/shadow_puppet/manifest.rb, line 181
    def configure(hash)
      self.class.configure(hash)
    end
executable? ()

Returns true if this Manifest respond_to? all methods named by calls to recipe, and if this Manifest has not been executed before.

[show source]
# File lib/shadow_puppet/manifest.rb, line 215
    def executable?
      self.class.recipes.each do |meth,args|
        return false unless respond_to?(meth)
      end
      return false if executed?
      true
    end
execute (force=false)

Execute this manifest, applying all resources defined. Execute returns true if successfull, and false if unsucessfull. By default, this will only execute a manifest that has not already been executed?. The force argument, if true, removes this check.

[show source]
# File lib/shadow_puppet/manifest.rb, line 233
    def execute(force=false)
      return false if executed? && !force
      evaluate_recipes
      apply
    rescue Exception => e
      false
    else
      true
    ensure
      @executed = true
    end
execute! (force=false)

Execute this manifest, applying all resources defined. Execute returns true if successfull, and raises an exception if not. By default, this will only execute a manifest that has not already been executed?. The force argument, if true, removes this check.

[show source]
# File lib/shadow_puppet/manifest.rb, line 249
    def execute!(force=false)
      return false if executed? && !force
      evaluate_recipes
      apply
    rescue Exception => e
      raise e
    else
      true
    ensure
      @executed = true
    end
missing_recipes ()
[show source]
# File lib/shadow_puppet/manifest.rb, line 223
    def missing_recipes
      missing = self.class.recipes.each do |meth,args|
        !respond_to?(meth)
      end
    end
name ()
[show source]
# File lib/shadow_puppet/manifest.rb, line 191
    def name
      @name ||= "#{self.class}##{self.object_id}"
    end

Protected instance methods

executed? ()

Has this manifest instance been executed?

[show source]
# File lib/shadow_puppet/manifest.rb, line 264
    def executed?
      @executed
    end
export ()

A Puppet::TransBucket of all defined resoureces.

[show source]
# File lib/shadow_puppet/manifest.rb, line 280
    def export
      transportable_objects = flat_resources.dup.reject { |a| a.nil? }.flatten.collect do |obj|
        obj.to_trans
      end
      b = Puppet::TransBucket.new(transportable_objects)
      b.name = name
      b.type = "class"

      return b
    end
flat_resources ()

An Array of all currently defined resources.

[show source]
# File lib/shadow_puppet/manifest.rb, line 269
    def flat_resources
      a = []
      @puppet_resources.each_value do |by_type|
        by_type.each_value do |by_name|
          a << by_name
        end
      end
      a
    end