Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using puppet inside ruby
require 'puppet'
require 'pp'
prefix="demo-puppet"
Puppet[:modulepath]=File.join(prefix,"modules")
Puppet[:manifestdir]=File.join(prefix,"manifests")
Puppet[:manifest]=File.join(prefix,"manifests","roles","logger.pp")
#Puppet[:manifest]=File.join(prefix,"manifests","site.pp")
code="import '#{Puppet[:manifest]}'\n"
Puppet[:code]=code
name="ec2-1.internal"
node=Puppet::Node.new(name)
#https://github.com/puppetlabs/puppet/blob/master/lib/puppet/resource/catalog.rb
catalog=Puppet::Resource::Catalog.indirection.find(name, :use_node => node).to_ral
puts "====================== CATALOG ---------------"
pp catalog
puts "====================== CATALOG Classes ---------------"
pp catalog.classes
puts "====================== CATALOG Resources ---------------"
catalog.classes.each do |c|
r=catalog.resource(:class,c)
# https://github.com/puppetlabs/puppet/blob/master/lib/puppet/resource.rb
pp r.class
pp r.instance_variables
def print_resource(res)
puts res.title
puts res.type
pp res.original_parameters
end
puts "====================== RESOUCE #{r.name} ---------------"
puts "#### Depends on #{r.name}"
catalog.dependents(r).each do |resource|
print_resource(resource)
end
puts "====================== RESOUCE #{r.name}---------------"
puts "#### #{r.name} depends on:"
catalog.dependencies(r) do |resource|
print_resource(resource)
end
end
puts "====================== Graph ---------------"
# https://github.com/puppetlabs/puppet/blob/master/lib/puppet/simple_graph.rb
# http://groups.google.com/group/puppet-dev/browse_thread/thread/f47fef57400d2d65
graph=catalog.relationship_graph
#puts graph.to_dot
puts graph.size
#pp graph
puts "====================== Adding resouces ---------------"
#http://www.devco.net/archives/2010/07/07/puppet_resources_on_demand.php
params = { :name => "patrick",
:comment => "Patrick Debois",
:password => '......' }
newuser = Puppet::Type.type(:user).new(params)
catalog.add_resource newuser
user=catalog.resource(:user,'patrick')
if user.nil?
puts "User not found"
else
puts "Title:",user.title
puts "Name:",user.name
puts "Comment:",user.parameters[:comment].should
puts "Password:",user.parameters[:password].should
end
puts "====================== Facts ---------------"
require 'facter'
puts Facter.hostname
puts "knowfacts:"
facts=[]
Facter.each do |name,value|
facts << name
end
puts facts.join(",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.