Permalink
Browse files

first commit of create_resources module.

it was called accepts.

I added the ability to specify classes or definitions.
  • Loading branch information...
0 parents commit 9a4fef865f90bdb59dd3aca831cd16d92e5e573d @bodepd bodepd committed Feb 25, 2011
Showing with 107 additions and 0 deletions.
  1. +29 −0 README
  2. +32 −0 lib/puppet/parser/functions/create_resources.rb
  3. +7 −0 manifests/init.pp
  4. +7 −0 manifests/user.pp
  5. +32 −0 tests/users.pp
@@ -0,0 +1,29 @@
+- License - GPL2
+- Copyright - Puppetlabs 2011
+
+This module contains a custom function for puppet that can be used to dynamically add resources to the catalog.
+
+I wrote this to use with an external node classifier that consumes YAML.
+
+The yaml specifies classes and passes hashes to those classes as parameters
+
+classes:
+ webserver::instances:
+ instances:
+ instance1:
+ foo: bar
+ instance2:
+ foo: blah
+
+Then puppet code can consume the hash parameters and convert then into resources
+
+class webserver::instances (
+ $instances = {}
+) {
+ create_resources('webserver::instance', $instances)
+}
+
+Now I can dynamically determine how webserver instances are deployed to nodes
+by updating the YAML files.
+
+
@@ -0,0 +1,32 @@
+Puppet::Parser::Functions::newfunction(:create_resources, :doc => '
+Converts a hash into resources and adds them to the catalog.
+Takes two parameters:
+ create_resource($type, $resources)
+ Creates resources of type $type from the $resources hash. Assumes that
+ hash is in the following form:
+ {title=>{attr=>value}}
+') do |args|
+ raise ArgumentError, 'requires resource type and param hash' if args.size < 2
+ args[1].each do |title, params|
+ # TODO - add argument to specify constraints on parameters
+ raise ArgumentError, 'params should not contain title' if(params['title'])
+ if type = Puppet::Type.type(args[0].to_sym)
+ resource = type.hash2resource(params.merge(:title => title))
+ catalog.add_resource(resource)
+ elsif args[0].downcase == 'class'# || args[0].downcase == 'node'
+ klass = find_hostclass(title)
+ klass.ensure_in_catalog(self, params)
+ compiler.catalog.add_class([title])
+ else
+ # TODO - use ensure_in_catalog when it supports definitions
+ # we assume that if nothing else mathces is must be a defined resource type
+ resource = find_definition(args[0])
+ p_resource = Puppet::Parser::Resource.new(args[0], title, :scope => self, :source => resource)
+ params.merge(:name => title).each do |k,v|
+ p_resource.set_parameter(k,v)
+ end
+ resource.instantiate_resource(self, p_resource)
+ compiler.add_resource(self, p_resource)
+ end
+ end
+end
@@ -0,0 +1,7 @@
+class create_resources(
+ $ensure,
+ $user_name=$name
+){
+ user{$name: ensure => $ensure}
+ notify{$user_name:}
+}
@@ -0,0 +1,7 @@
+define create_resources::user(
+ $ensure,
+ $user_name=$operatingsystem
+){
+ user{$name: ensure => $ensure}
+ notify{$user_name:}
+}
@@ -0,0 +1,32 @@
+$users3 = {
+ 'dannyboy454' => {
+ 'user_name'=>'dlfjdslkf',
+ 'ensure'=>present,
+ 'require' => 'User[foobar]',
+ 'before' => 'User[sally-mae]'
+ },
+ 'bobby-joe' => {'ensure'=>present}
+}
+create_resources('create_resources::user', $users3)
+
+# TODO - types are not applied in main stage
+$users = {
+ 'sally-mae' =>
+ {'ensure' => 'present',
+ 'require' => 'User[bobby-joe]'
+ }
+}
+create_resources('user', $users)
+
+user { 'foobar':
+ ensure => present,
+ require => User['bobby-joe']
+}
+
+$classes = {
+ 'create_resources' => {
+ 'ensure' => 'present'
+ }
+}
+
+create_resources('class', $classes)

0 comments on commit 9a4fef8

Please sign in to comment.