diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-03-10 00:38:43 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-03-10 00:38:43 -0700 |
commit | 3dd77b260393bae93bb27677e3f8a45f407981c1 (patch) | |
tree | d017427ddcd5733caf7c0ceeec8ea72abbb107fa /lib | |
parent | b4beed548959ca63365ca3d799c59a155047b5ff (diff) | |
download | chef-3dd77b260393bae93bb27677e3f8a45f407981c1.tar.gz |
Added rcov coverage, lots of tests, definitions, node support
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef.rb (renamed from lib/marionette.rb) | 4 | ||||
-rw-r--r-- | lib/chef/config.rb | 69 | ||||
-rw-r--r-- | lib/chef/mixin/check_helper.rb (renamed from lib/marionette/mixin/check_arguments.rb) | 16 | ||||
-rw-r--r-- | lib/chef/mixin/from_file.rb | 32 | ||||
-rw-r--r-- | lib/chef/node.rb | 91 | ||||
-rw-r--r-- | lib/chef/recipe.rb | 96 | ||||
-rw-r--r-- | lib/chef/resource.rb | 123 | ||||
-rw-r--r-- | lib/chef/resource/file.rb (renamed from lib/marionette/resource/file.rb) | 9 | ||||
-rw-r--r-- | lib/chef/resource_collection.rb | 155 | ||||
-rw-r--r-- | lib/chef/resource_definition.rb | 63 | ||||
-rw-r--r-- | lib/marionette/mixin/graph_resources.rb | 85 | ||||
-rw-r--r-- | lib/marionette/recipe.rb | 74 | ||||
-rw-r--r-- | lib/marionette/resource.rb | 154 |
13 files changed, 644 insertions, 327 deletions
diff --git a/lib/marionette.rb b/lib/chef.rb index 9ad45671f8..dc61e950a0 100644 --- a/lib/marionette.rb +++ b/lib/chef.rb @@ -19,8 +19,8 @@ require 'rubygems' -Dir[File.join(File.dirname(__FILE__), 'marionette/**/*.rb')].sort.each { |lib| require lib } +Dir[File.join(File.dirname(__FILE__), 'chef/**/*.rb')].sort.each { |lib| require lib } -class Marionette +class Chef VERSION = '0.0.1' end diff --git a/lib/chef/config.rb b/lib/chef/config.rb new file mode 100644 index 0000000000..6c0402bdda --- /dev/null +++ b/lib/chef/config.rb @@ -0,0 +1,69 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "check_helper") + +class Chef + class Config + + include Chef::Mixin::CheckHelper + + def initialize + set_defaults + end + + def self.load_file(file) + config = Chef::Config.new + if File.exists?(file) && File.readable?(file) + begin + config.instance_eval(IO.read(file), file, 1) + rescue NoMethodError => e + new_message = "You probably tried to use a config variable that doesn't exist!\n" + new_message += e.message + raise e.exception(new_message) + end + else + raise IOError, "Cannot find or read #{file}!" + end + config + end + + def cookbook_path(*args) + if args.length == 0 + @cookbook_path + else + flat_args = args.flatten + flat_args.each do |a| + unless a.kind_of?(String) + raise ArgumentError, "You must pass strings to cookbook_path!" + end + end + @cookbook_path = flat_args + end + end + + def set_defaults + @cookbook_path = [ + "/etc/chef/site-cookbook", + "/etc/chef/cookbook", + ] + end + end +end
\ No newline at end of file diff --git a/lib/marionette/mixin/check_arguments.rb b/lib/chef/mixin/check_helper.rb index ba38e3e734..a4208c7e49 100644 --- a/lib/marionette/mixin/check_arguments.rb +++ b/lib/chef/mixin/check_helper.rb @@ -17,17 +17,17 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # -class Marionette +class Chef module Mixin - module CheckArguments - def check_symbol_or_string(to_check, field_name) - case to_check - when Symbol, String - true + module CheckHelper + def set_if_args(thing, arguments) + raise ArgumentError, "Must call set_if_args with a block!" unless Kernel.block_given? + if arguments != nil + yield(arguments) else - raise ArgumentError, "you must pass a symbol or string to #{field_name}!" + thing end end end end -end
\ No newline at end of file +end diff --git a/lib/chef/mixin/from_file.rb b/lib/chef/mixin/from_file.rb new file mode 100644 index 0000000000..68f614c07a --- /dev/null +++ b/lib/chef/mixin/from_file.rb @@ -0,0 +1,32 @@ +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +class Chef + module Mixin + module FromFile + def from_file(filename) + if File.exists?(filename) && File.readable?(filename) + self.instance_eval(IO.read(filename), filename, 1) + else + raise IOError, "Cannot open or read #{filename}!" + end + end + end + end +end diff --git a/lib/chef/node.rb b/lib/chef/node.rb new file mode 100644 index 0000000000..bcda384d09 --- /dev/null +++ b/lib/chef/node.rb @@ -0,0 +1,91 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "check_helper") +require File.join(File.dirname(__FILE__), "mixin", "from_file") + +class Chef + class Node + + attr_accessor :attribute, :recipe_list + + include Chef::Mixin::CheckHelper + include Chef::Mixin::FromFile + + def initialize() + @name = nil + @attribute = Hash.new + @recipe_list = Array.new + end + + def name(arg=nil) + set_if_args(@name, arg) do |a| + case a + when String + @name = a + else + raise ArgumentError, "The nodes name must be a string" + end + end + end + + def [](attrib) + if @attribute.has_key?(attrib) + @attribute[attrib] + elsif @attribute.has_key?(attrib.to_s) + @attribute[attrib.to_s] + else + nil + end + end + + def attribute?(attrib) + result = false + result = @attribute.has_key?(attrib) + return result if result + return @attribute.has_key?(attrib.to_sym) + end + + def recipe?(recipe_name) + @recipe_list.detect { |r| r == recipe_name } ? true : false + end + + def recipes(*args) + if args.length > 0 + @recipe_list = args.flatten + else + @recipe_list + end + end + + def method_missing(symbol, *args) + if args.length != 0 + @attribute[symbol] = args.length == 1 ? args[0] : args + else + if self[symbol] + @attribute[symbol] + else + raise ArgumentError, "Attribute #{symbol.to_s} is not defined!" + end + end + end + + end +end
\ No newline at end of file diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb new file mode 100644 index 0000000000..17d14ef0bf --- /dev/null +++ b/lib/chef/recipe.rb @@ -0,0 +1,96 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "from_file") + +class Chef + class Recipe + + include Chef::Mixin::FromFile + + attr_accessor :module_name, :recipe_name, :recipe, :node, :collection, + :definitions, :config, :params + + def initialize(module_name, recipe_name, node, collection=nil, definitions=nil, config=nil) + @module_name = module_name + @recipe_name = recipe_name + @node = node + if collection + @collection = collection + else + @collection = Chef::ResourceCollection.new() + end + if config + @config = config + else + @config = Chef::Config.new() + end + if definitions + @definitions = definitions + else + @definitions = Hash.new + end + @params = Hash.new + end + + def resources(*args) + @collection.resources(*args) + end + + def method_missing(method_symbol, *args, &block) + resource = nil + # If we have a definition that matches, we want to use that instead. This should + # let you do some really crazy over-riding of "native" types, if you really want + # to. + if @definitions.has_key?(method_symbol) + new_def = @definitions[method_symbol].dup + new_def.instance_eval(&block) + new_recipe = Chef::Recipe.new(@module_name, @recipe_name, @node, @collection, @definitions, @config) + new_recipe.params = new_def.params + new_recipe.instance_eval(&new_def.recipe) + else + method_name = method_symbol.to_s + # Otherwise, we're rocking the regular resource call route. + rname = nil + case method_name + when /^(.+)_(.+)$/ + rname = "Chef::Resource::#{$1.capitalize}#{$2.capitalize}" + when /^(.+)$/ + rname = "Chef::Resource::#{$1.capitalize}" + end + begin + args << @collection + args << @config + resource = eval(rname).new(*args) + resource.params = @params + resource.instance_eval(&block) + rescue Exception => e + if e.kind_of?(NameError) && e.to_s =~ /Chef::Resource/ + raise NameError, "Cannot find #{rname} for #{method_name}\nOriginal: #{e.to_s}" + else + raise e + end + end + @collection << resource + resource + end + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb new file mode 100644 index 0000000000..2d7b90ed7f --- /dev/null +++ b/lib/chef/resource.rb @@ -0,0 +1,123 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "check_helper") +require 'yaml' + +class Chef + class Resource + + include Chef::Mixin::CheckHelper + + attr_accessor :tag, :actions, :config, :params + attr_reader :name, :noop, :resource_name, :collection, :notifies, :subscribes + + def initialize(name, collection=nil, config=nil) + @name = name + if collection + @collection = collection + else + @collection = Chef::ResourceCollection.new() + end + if config + @config = config + else + @config = Chef::Config.new() + end + @tag = [ name.to_s ] + @noop = nil + @before = nil + @actions = Hash.new + @params = Hash.new + end + + def name(name=nil) + set_if_args(@name, name) do + raise ArgumentError, "name must be a string!" unless name.kind_of?(String) + @name = name + end + end + + def noop(tf=nil) + set_if_args(@noop, tf) do + raise ArgumentError, "noop must be true or false!" unless tf == true || tf == false + @noop = tf + end + end + + def notifies(action, resources, timing=:delayed) + timing = check_timing(timing) + rarray = resources.kind_of?(Array) ? resources : [ resources ] + rarray.each do |resource| + action_sym = action.to_sym + if @actions.has_key?(action_sym) + @actions[action_sym][timing] << resource + else + @actions[action_sym] = Hash.new + @actions[action_sym][:delayed] = Array.new + @actions[action_sym][:immediate] = Array.new + @actions[action_sym][timing] << resource + end + end + true + end + + def resources(*args) + @collection.resources(*args) + end + + def subscribes(action, resources, timing=:delayed) + timing = check_timing(timing) + rarray = resources.kind_of?(Array) ? resources : [ resources ] + rarray.each do |resource| + action_sym = action.to_sym + if resource.actions.has_key?(action_sym) + resource.actions[action_sym][timing] << self + else + resource.actions[action_sym] = Hash.new + resource.actions[action_sym][:delayed] = Array.new + resource.actions[action_sym][:immediate] = Array.new + resource.actions[action_sym][timing] << self + end + end + true + end + + def is(*args) + return *args + end + + def to_s + "#{@resource_name}[#{@name}]" + end + + private + + def check_timing(timing) + unless timing == :delayed || timing == :immediate || timing == :immediately + raise ArgumentError, "Timing must be :delayed or :immediate(ly), you said #{timing}" + end + if timing == :immediately + timing = :immediate + end + timing + end + end +end
\ No newline at end of file diff --git a/lib/marionette/resource/file.rb b/lib/chef/resource/file.rb index f19b8f7b17..0a2edb56a5 100644 --- a/lib/marionette/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -1,3 +1,4 @@ +# # Author:: Adam Jacob (<adam@hjksolutions.com>) # Copyright:: Copyright (c) 2008 HJK Solutions, LLC # License:: GNU General Public License version 2 or later @@ -17,14 +18,14 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # -class Marionette +class Chef class Resource - class File < Marionette::Resource + class File < Chef::Resource attr_reader :backup, :checksum, :insure, :group, :mode, :owner, :path - def initialize(name, dg=nil, deps=nil) + def initialize(name, collection=nil, config=nil) @resource_name = :file - super(name, dg) + super(name, collection, config) @path = name @backup = true @checksum = "md5sum" diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb new file mode 100644 index 0000000000..dc93cfb4fa --- /dev/null +++ b/lib/chef/resource_collection.rb @@ -0,0 +1,155 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +class Chef + class ResourceCollection + include Enumerable + + def initialize + @resources = Array.new + @resources_by_name = Hash.new + end + + def [](index) + @resources[index] + end + + def []=(index, arg) + is_chef_resource(arg) + @resources[index] = arg + @resources_by_name[arg.to_s] = index + end + + def <<(*args) + args.flatten.each do |a| + is_chef_resource(a) + @resources << a + @resources_by_name[a.to_s] = @resources.length - 1 + end + end + + def push(*args) + args.flatten.each do |a| + is_chef_resource(a) + @resources.push(a) + @resources_by_name[a.to_s] = @resources.length - 1 + end + end + + def each + @resources.each do |r| + yield r + end + end + + def each_index + @resources.each_index do |i| + yield i + end + end + + def lookup(resource) + lookup_by = nil + if resource.kind_of?(Chef::Resource) + lookup_by = resource.to_s + elsif resource.kind_of?(String) + lookup_by = resource + else + raise ArgumentError, "Must pass a Chef::Resource or String to lookup" + end + res = @resources_by_name[lookup_by] + unless res + raise ArgumentError, "Cannot find a resource matching #{lookup_by} (did you define it first?)" + end + @resources[res] + end + + # Find existing resources by searching the list of existing resources. Possible + # forms are: + # + # resources(:file => "foobar") + # resources(:file => [ "foobar", "baz" ]) + # resources("file[foobar]", "file[baz]") + # resources("file[foobar,baz]") + # + # Returns the matching resource, or an Array of matching resources. + # + # Raises an ArgumentError if you feed it bad lookup information + # Raises a Runtime Error if it can't find the resources you are looking for. + def resources(*args) + results = Array.new + args.each do |arg| + case arg + when Hash + results << find_resource_by_hash(arg) + when String + results << find_resource_by_string(arg) + else + raise ArgumentError, "resources takes arguments as a hash or strings!" + end + end + flat_results = results.flatten + flat_results.length == 1 ? flat_results[0] : flat_results + end + + private + + def find_resource_by_hash(arg) + results = Array.new + arg.each do |resource_name, name_list| + names = name_list.kind_of?(Array) ? name_list : [ name_list ] + names.each do |name| + res_name = "#{resource_name.to_s}[#{name}]" + results << lookup(res_name) + end + end + return results + end + + def find_resource_by_string(arg) + results = Array.new + case arg + when /^(.+)\[(.+?),(.+)\]$/ + resource_type = $1 + arg =~ /^.+\[(.+)\]$/ + resource_list = $1 + resource_list.split(",").each do |name| + resource_name = "#{resource_type}[#{name}]" + results << lookup(resource_name) + end + when /^(.+)\[(.+)\]$/ + resource_type = $1 + name = $2 + resource_name = "#{resource_type}[#{name}]" + results << lookup(resource_name) + else + raise ArgumentError, "You must have a string like resource_type[name]!" + end + return results + end + + def is_chef_resource(arg) + unless arg.kind_of?(Chef::Resource) + raise ArgumentError, "Members must be Chef::Resource's" + end + true + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource_definition.rb b/lib/chef/resource_definition.rb new file mode 100644 index 0000000000..ddc529b7d2 --- /dev/null +++ b/lib/chef/resource_definition.rb @@ -0,0 +1,63 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: GNU General Public License version 2 or later +# +# This program and entire repository is free software; you can +# redistribute it and/or modify it under the terms of the GNU +# General Public License as published by the Free Software +# Foundation; either version 2 of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +require File.join(File.dirname(__FILE__), "mixin", "from_file") + +class Chef + class ResourceDefinition + + include Chef::Mixin::FromFile + + attr_accessor :name, :params, :recipe + + def initialize + @name = nil + @params = Hash.new + @recipe = nil + end + + def define(resource_name, prototype_params=nil, &block) + unless resource_name.kind_of?(Symbol) + raise ArgumentError, "You must use a symbol when defining a new resource!" + end + @name = resource_name + if prototype_params + unless prototype_params.kind_of?(Hash) + raise ArgumentError, "You must pass a hash as the prototype parameters for a definition." + end + @params = prototype_params + end + if Kernel.block_given? + @recipe = block + else + raise ArgumentError, "You must pass a block to a definition." + end + true + end + + # When we do the resource definition, we're really just setting new values for + # the paramaters we prototyped at the top. This method missing is as simple as + # it gets. + def method_missing(symbol, *args) + @params[symbol] = args.length == 1 ? args[0] : args + end + + end +end
\ No newline at end of file diff --git a/lib/marionette/mixin/graph_resources.rb b/lib/marionette/mixin/graph_resources.rb deleted file mode 100644 index 7721b5d91a..0000000000 --- a/lib/marionette/mixin/graph_resources.rb +++ /dev/null @@ -1,85 +0,0 @@ -# Author:: Adam Jacob (<adam@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 HJK Solutions, LLC -# License:: GNU General Public License version 2 or later -# -# This program and entire repository is free software; you can -# redistribute it and/or modify it under the terms of the GNU -# General Public License as published by the Free Software -# Foundation; either version 2 of the License, or any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# - -require 'rubygems' -require 'rgl/adjacency' -require 'rgl/topsort' -require 'rgl/dot' - -class Marionette - module Mixin - module GraphResources - # Find existing resources by searching the list of existing resources. Takes - # a hash of :resource_name => name, or :resource_name => [ name1, name2 ]. - # - # Returns one or an Array of matching resources. - # - # Raises a Runtime Error if it can't find the resources you are looking for. - def resources(args) - unless args.kind_of?(Hash) - raise ArgumentError, "resource requires a hash of :resources => names" - end - - to_find = Array.new - args.each do |resource_name, name_matches| - names = name_matches.kind_of?(Array) ? name_matches : [ name_matches ] - names.each do |name| - to_find.push({ - :resource_name => resource_name, - :name => name, - :found => false, - :object => nil} - ) - end - end - - @dg.each_vertex do |vres| - next if vres == :top - to_find.each do |resource| - if vres.resource_name == resource[:resource_name] - if vres.name == resource[:name] - resource[:found] = true - resource[:object] = vres - break - end - end - end - end - - results = Array.new - errors = Array.new - to_find.each do |r| - if r[:found] - results.push(r) - else - errors.push(r) - end - end - if errors.length > 0 - msg = "Cannot find resources (maybe not evaluated yet?):\n" - errors.each do |e| - msg << " #{e[:resource_name].to_s}: #{e[:name].to_s}\n" - end - raise RuntimeError, msg - end - results.length == 1 ? results[0][:object] : results.collect { |r| r[:object] } - end - end - end -end diff --git a/lib/marionette/recipe.rb b/lib/marionette/recipe.rb deleted file mode 100644 index e008400d55..0000000000 --- a/lib/marionette/recipe.rb +++ /dev/null @@ -1,74 +0,0 @@ -# -# Author:: Adam Jacob (<adam@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 HJK Solutions, LLC -# License:: GNU General Public License version 2 or later -# -# This program and entire repository is free software; you can -# redistribute it and/or modify it under the terms of the GNU -# General Public License as published by the Free Software -# Foundation; either version 2 of the License, or any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# - -class Marionette - class Recipe - - include Marionette::Mixin::GraphResources - - attr_accessor :module_name, :recipe_name, :recipe, :node, :dg, :deps - - def initialize(module_name, recipe_name, node, dg=nil, deps=nil) - @module_name = module_name - @recipe_name = recipe_name - @node = node - if dg - @dg = dg - else - @dg = RGL::DirectedAdjacencyGraph.new() - @dg.add_vertex(:top) - end - if deps - @deps = deps - else - @deps = RGL::DirectedAdjacencyGraph.new() - end - @last_resource = :top - @in_order = Array.new - end - - def method_missing(method_symbol, *args, &block) - method_name = method_symbol.to_s - resource = nil - rname = nil - case method_name - when /^(.+)_(.+)$/ - rname = "Marionette::Resource::#{$1.capitalize}#{$2.capitalize}" - when /^(.+)$/ - rname = "Marionette::Resource::#{$1.capitalize}" - end - begin - args << @dg - args << @deps - resource = eval(rname).new(*args) - resource.instance_eval(&block) - rescue Exception => e - if e.kind_of?(NameError) && e.to_s =~ /Marionette::Resource/ - raise NameError, "Cannot find #{rname} for #{method_name}\nOriginal: #{e.to_s}" - else - raise e - end - end - @dg.add_vertex(resource) - @dg.add_edge(@last_resource, resource) - @last_resource = resource - end - end -end
\ No newline at end of file diff --git a/lib/marionette/resource.rb b/lib/marionette/resource.rb deleted file mode 100644 index 3100c2d7cb..0000000000 --- a/lib/marionette/resource.rb +++ /dev/null @@ -1,154 +0,0 @@ -# Author:: Adam Jacob (<adam@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 HJK Solutions, LLC -# License:: GNU General Public License version 2 or later -# -# This program and entire repository is free software; you can -# redistribute it and/or modify it under the terms of the GNU -# General Public License as published by the Free Software -# Foundation; either version 2 of the License, or any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# - -require 'rubygems' -require 'yaml' - -class Marionette - class Resource - - include Marionette::Mixin::GraphResources - - attr_accessor :tag, :actions - attr_reader :name, :noop, :tag, :resource_name, :dg, :deps, :notifies, :subscribes - - def initialize(name, dg=nil, deps=nil) - @name = name - if dg - @dg = dg - else - @dg = RGL::DirectedAdjacencyGraph.new() - end - if deps - @deps = deps - else - @deps = RGL::DirectedAdjacencyGraph.new() - end - @tag = [ name.to_s ] - @noop = nil - @tag = nil - @before = nil - @actions = Hash.new - end - - def name(name=nil) - set_if_args(@name, name) do - raise ArgumentError, "name must be a string!" unless name.kind_of?(String) - @name = name - end - end - - def noop(tf=nil) - set_if_args(@noop, tf) do - raise ArgumentError, "noop must be true or false!" unless tf == true || tf == false - @noop = tf - end - end - - def requires(resources=nil) - rarray = resources.kind_of?(Array) ? resources : [ resources ] - rarray.each do |resource| - @deps.add_vertex(self) - @deps.add_vertex(resource) - @deps.add_edge(resource, self) - end - true - end - - def before(resources) - rarray = resources.kind_of?(Array) ? resources : [ resources ] - rarray.each do |resource| - @deps.add_vertex(self) - @deps.add_vertex(resource) - @deps.add_edge(self, resource) - end - true - end - - def notifies(*notify_actions) - resources = notify_actions.pop - rarray = resources.kind_of?(Array) ? resources : [ resources ] - rarray.each do |resource| - @deps.add_vertex(self) - @deps.add_vertex(resource) - @deps.add_edge(self, resource) - notify_actions.each do |action| - action_sym = action.to_sym - if @actions.has_key?(action_sym) - @actions[action_sym] << resource - else - @actions[action_sym] = [ resource ] - end - end - end - true - end - - def subscribes(*subscribe_actions) - resources = subscribe_actions.pop - rarray = resources.kind_of?(Array) ? resources : [ resources ] - rarray.each do |resource| - @deps.add_vertex(self) - @deps.add_vertex(resource) - @deps.add_edge(resource, self) - subscribe_actions.each do |action| - action_sym = action.to_sym - if @actions.has_key?(action_sym) - resource.actions[action_sym] << self - else - resource.actions[action_sym] = [ self ] - end - end - end - true - end - - def tag(args=nil) - set_if_args(@tag, args) do - if args.kind_of?(Array) - args.each do |t| - @tag << t - end - else - @tag << args - end - @tag - end - end - - def to_s - "#{@resource_name} #{@name}" - end - - def valid?() - return false unless self.name - true - end - - private - def set_if_args(thing, arguments) - raise ArgumentError, "Must call set_if_args with a block!" unless Kernel.block_given? - if arguments != nil - yield(arguments) - else - thing - end - end - end -end
\ No newline at end of file |