Class: ShadowPuppet::Manifest
- Inherits:
-
Object
- Object
- ShadowPuppet::Manifest
- Defined in:
- lib/shadow_puppet/test.rb,
lib/shadow_puppet/manifest.rb
Overview
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()
#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()
#...
end
end
class MyWebMainfest < ShadowPuppet::Manifest
include ApachePuppet
recipe :php_vhost, {
:domain => 'foo.com',
:path => '/var/www/apps/foo'
}
end
Direct Known Subclasses
Defined Under Namespace
Classes: Setup
Instance Attribute Summary collapse
-
#puppet_resources ⇒ Object
readonly
Returns the value of attribute puppet_resources.
Class Method Summary collapse
-
.configuration ⇒ Object
A hash describing any configuration that has been performed on the class.
-
.configure(hash) ⇒ Object
Define configuration on this manifest.
-
.puppet_type_methods ⇒ Object
An array of all methods defined for creation of Puppet Resources.
-
.recipe(*methods) ⇒ Object
Declares that the named method or methods will be called whenever execute is called on an instance of this class.
-
.register_puppet_types ⇒ Object
Create an instance method for every type that either creates or references a resource.
-
.register_puppet_types_for_testing ⇒ Object
Creates an instance method for every puppet type that either creates or references a resource.
Instance Method Summary collapse
-
#configuration ⇒ Object
Access to the configuration of the class of this instance.
-
#configure(hash) ⇒ Object
(also: #configuration=)
Update the configuration of this manifest instance’s class.
-
#executable? ⇒ Boolean
Returns true if this Manifest
respond_to?
all methods named by calls to recipe, and if this Manifest has not been executed before. -
#execute(force = false) ⇒ Object
Execute this manifest, applying all resources defined.
-
#execute!(force = false) ⇒ Object
Execute this manifest, applying all resources defined.
-
#initialize(config = {}) ⇒ Manifest
constructor
Initialize a new instance of this manifest.
- #missing_recipes ⇒ Object
- #name ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Manifest
Initialize a new instance of this manifest. This can take a config hash, which is immediately passed on to the configure method
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/shadow_puppet/manifest.rb', line 85 def initialize(config = {}) if Process.uid == 0 Puppet[:confdir] = File.("/etc/shadow_puppet") Puppet[:vardir] = File.("/var/shadow_puppet") else Puppet[:confdir] = File.("~/.shadow_puppet") Puppet[:vardir] = File.("~/.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 |
Instance Attribute Details
#puppet_resources ⇒ Object (readonly)
Returns the value of attribute puppet_resources.
78 79 80 |
# File 'lib/shadow_puppet/manifest.rb', line 78 def puppet_resources @puppet_resources end |
Class Method Details
.configuration ⇒ Object
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.
136 137 138 |
# File 'lib/shadow_puppet/manifest.rb', line 136 def self.configuration __config__.deep_symbolize_keys end |
.configure(hash) ⇒ Object
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.
168 169 170 |
# File 'lib/shadow_puppet/manifest.rb', line 168 def self.configure(hash) __config__.deep_merge!(hash) end |
.puppet_type_methods ⇒ Object
An array of all methods defined for creation of Puppet Resources
187 188 189 |
# 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) ⇒ Object
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.
112 113 114 115 116 117 118 119 120 |
# File 'lib/shadow_puppet/manifest.rb', line 112 def self.recipe(*methods) return nil if methods.nil? || methods == [] = methods. methods.each do |meth| = configuration[meth.to_sym] if == {} ||= {} recipes << [meth.to_sym, ] end end |
.register_puppet_types ⇒ Object
Create an instance method for every type that either creates or references a resource
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/shadow_puppet/manifest.rb', line 197 def self.register_puppet_types Puppet::Type.loadall Puppet::Type.eachtype do |type| #remove the method rdoc placeholders remove_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 |
.register_puppet_types_for_testing ⇒ Object
Creates an instance method for every puppet type that either creates or references a resource
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/shadow_puppet/test.rb', line 55 def self.register_puppet_types_for_testing Puppet::Type.loadall Puppet::Type.eachtype do |type| plural_type = type.name.to_s.downcase.pluralize #undefine the method rdoc placeholders undef_method(plural_type) rescue nil define_method(plural_type) do |*args| puppet_resources[type] end end end |
Instance Method Details
#configuration ⇒ Object
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"
148 149 150 |
# File 'lib/shadow_puppet/manifest.rb', line 148 def configuration self.class.configuration end |
#configure(hash) ⇒ Object Also known as: configuration=
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"
181 182 183 |
# File 'lib/shadow_puppet/manifest.rb', line 181 def configure(hash) self.class.configure(hash) end |
#executable? ⇒ Boolean
Returns true if this Manifest respond_to?
all methods named by calls to recipe, and if this Manifest has not been executed before.
215 216 217 218 219 220 221 |
# 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) ⇒ Object
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.
233 234 235 236 237 238 239 240 241 242 243 |
# 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) ⇒ Object
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.
249 250 251 252 253 254 255 256 257 258 259 |
# 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 ⇒ Object
223 224 225 226 227 |
# 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 ⇒ Object
191 192 193 |
# File 'lib/shadow_puppet/manifest.rb', line 191 def name @name ||= "#{self.class}##{self.object_id}" end |