diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-09-04 18:02:34 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-09-04 18:02:34 -0700 |
commit | a2f52aecdcc2e31e1ffcd85f4aa1f61ddcc9e770 (patch) | |
tree | bab6939145c01d1ae9285c63ef31181be94de275 /lib/chef | |
parent | 4bdc0a4e18b4b3b8107581853b81d7a2826e8f30 (diff) | |
download | chef-a2f52aecdcc2e31e1ffcd85f4aa1f61ddcc9e770.tar.gz |
Adding script support!
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/config.rb | 1 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 3 | ||||
-rw-r--r-- | lib/chef/mixin/command.rb | 37 | ||||
-rw-r--r-- | lib/chef/platform.rb | 8 | ||||
-rw-r--r-- | lib/chef/provider/script.rb | 35 | ||||
-rw-r--r-- | lib/chef/resource/bash.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/csh.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/perl.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/python.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/ruby.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/script.rb | 49 |
11 files changed, 287 insertions, 11 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb index dd44baf220..ea1d8650f4 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -52,6 +52,7 @@ class Chef :couchdb_database => "chef", :openid_store_path => "/var/chef/openid/db", :openid_cstore_path => "/var/chef/openid/cstore", + :executable_path => ENV['PATH'] ? ENV['PATH'].split(File::PATH_SEPARATOR) : [] } class << self diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 2aad51096b..5513d42364 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -18,6 +18,7 @@ class Chef class Exception class SearchIndex < RuntimeError; end - class Exec < RuntimeError; end + class Exec < RuntimeError; end + class FileNotFound < RuntimeError; end end end
\ No newline at end of file diff --git a/lib/chef/mixin/command.rb b/lib/chef/mixin/command.rb index b513abb0eb..e2b61cadde 100644 --- a/lib/chef/mixin/command.rb +++ b/lib/chef/mixin/command.rb @@ -15,13 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# This is taken directly from Ara T Howard's Open4 library, and then -# modified to suit the needs of Chef. Any bugs here are most likely -# my own, and not Ara. -# -# The original appears in external/open4.rb in it's unmodified form. -# -# Thanks, Ara. + require 'tmpdir' require 'fcntl' @@ -31,6 +25,24 @@ class Chef module Mixin module Command + def whereis(command, path=nil) + raise ArgumentError "Path must be an array!" unless path.kind_of?(Array) + + search_path = Chef::Config[:executable_path] + if path + search_path.unshift(*path) + end + + search_path.each do |sp| + potential_file = File.join(sp, command) + if File.exists?(potential_file) && File.executable?(potential_file) + return potential_file + end + end + + raise Chef::Exception::FileNotFound, "Cannot find #{command} in #{search_path.join(File::PATH_SEPARATOR)}" + end + def run_command(args={}) if args.has_key?(:creates) if File.exists?(args[:creates]) @@ -95,14 +107,21 @@ class Chef if status.exitstatus != args[:returns] raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}" else - Chef::Log.debug("Ran #{args[:command_string]} returned #{status.exitstatus}") + Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}") end end status end module_function :run_command - + + # This is taken directly from Ara T Howard's Open4 library, and then + # modified to suit the needs of Chef. Any bugs here are most likely + # my own, and not Ara. + # + # The original appears in external/open4.rb in it's unmodified form. + # + # Thanks, Ara. def popen4(cmd, args={}, &b) args[:user] ||= nil diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb index e947328c9a..3e8bb6809c 100644 --- a/lib/chef/platform.rb +++ b/lib/chef/platform.rb @@ -37,7 +37,13 @@ class Chef :remote_file => Chef::Provider::RemoteFile, :remote_directory => Chef::Provider::RemoteDirectory, :sysctl => Chef::Provider::Sysctl, - :execute => Chef::Provider::Execute + :execute => Chef::Provider::Execute, + :script => Chef::Provider::Script, + :perl => Chef::Provider::Script, + :python => Chef::Provider::Script, + :ruby => Chef::Provider::Script, + :bash => Chef::Provider::Script, + :csh => Chef::Provider::Script } } diff --git a/lib/chef/provider/script.rb b/lib/chef/provider/script.rb new file mode 100644 index 0000000000..4669361142 --- /dev/null +++ b/lib/chef/provider/script.rb @@ -0,0 +1,35 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'tempfile' + +class Chef + class Provider + class Script < Chef::Provider::Execute + + def action_run + tf = Tempfile.new("chef-script") + tf.puts(@new_resource.code) + tf.close + @new_resource.command("#{@new_resource.interpreter} #{tf.path}") + super + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/bash.rb b/lib/chef/resource/bash.rb new file mode 100644 index 0000000000..7af5f9756a --- /dev/null +++ b/lib/chef/resource/bash.rb @@ -0,0 +1,33 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "script") + +class Chef + class Resource + class Bash < Chef::Resource::Script + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :bash + @interpreter = "bash" + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/csh.rb b/lib/chef/resource/csh.rb new file mode 100644 index 0000000000..29de9777b9 --- /dev/null +++ b/lib/chef/resource/csh.rb @@ -0,0 +1,33 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "script") + +class Chef + class Resource + class Csh < Chef::Resource::Script + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :csh + @interpreter = "csh" + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/perl.rb b/lib/chef/resource/perl.rb new file mode 100644 index 0000000000..a9b891ed5c --- /dev/null +++ b/lib/chef/resource/perl.rb @@ -0,0 +1,33 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "script") + +class Chef + class Resource + class Perl < Chef::Resource::Script + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :perl + @interpreter = "perl" + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/python.rb b/lib/chef/resource/python.rb new file mode 100644 index 0000000000..ac2b27c00f --- /dev/null +++ b/lib/chef/resource/python.rb @@ -0,0 +1,33 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "script") + +class Chef + class Resource + class Python < Chef::Resource::Script + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :python + @interpreter = "python" + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/ruby.rb b/lib/chef/resource/ruby.rb new file mode 100644 index 0000000000..591f1a658b --- /dev/null +++ b/lib/chef/resource/ruby.rb @@ -0,0 +1,33 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "script") + +class Chef + class Resource + class Ruby < Chef::Resource::Script + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :ruby + @interpreter = "ruby" + end + + end + end +end
\ No newline at end of file diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb new file mode 100644 index 0000000000..a99088d09b --- /dev/null +++ b/lib/chef/resource/script.rb @@ -0,0 +1,49 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class Chef + class Resource + class Script < Chef::Resource::Execute + + def initialize(name, collection=nil, node=nil) + super(name, collection, node) + @resource_name = :script + @command = name + @code = nil + @interpreter = nil + end + + def code(arg=nil) + set_or_return( + :code, + arg, + :kind_of => [ String ] + ) + end + + def interpreter(arg=nil) + set_or_return( + :interpreter, + arg, + :kind_of => [ String ] + ) + end + + end + end +end
\ No newline at end of file |