diff options
author | KC Braunschweig <kcbraunschweig@gmail.com> | 2011-11-18 17:56:46 -0800 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2012-06-21 11:45:53 -0700 |
commit | c84479a6642e02eed3222e8ec43a84ce5cf98111 (patch) | |
tree | b3c87e892cd56c5b188c266f2fd736b9a1d193fa | |
parent | 29ec163a6e613b2ebe0e9193f7b90f878f5bc84a (diff) | |
download | chef-c84479a6642e02eed3222e8ec43a84ce5cf98111.tar.gz |
Adds script_path feature. Tested, seems to work.
-rw-r--r-- | chef/lib/chef/config.rb | 3 | ||||
-rw-r--r-- | chef/lib/chef/knife/exec.rb | 33 |
2 files changed, 34 insertions, 2 deletions
diff --git a/chef/lib/chef/config.rb b/chef/lib/chef/config.rb index dce963109a..facd7123f9 100644 --- a/chef/lib/chef/config.rb +++ b/chef/lib/chef/config.rb @@ -133,6 +133,9 @@ class Chef cookbook_path [ platform_specific_path("/var/chef/cookbooks"), platform_specific_path("/var/chef/site-cookbooks") ] + # An array of paths to search for knife exec scripts if they aren't in the current directory + script_path [] + # Where files are stored temporarily during uploads sandbox_path "/var/chef/sandboxes" diff --git a/chef/lib/chef/knife/exec.rb b/chef/lib/chef/knife/exec.rb index 44375832a6..86f3f03686 100644 --- a/chef/lib/chef/knife/exec.rb +++ b/chef/lib/chef/knife/exec.rb @@ -27,11 +27,19 @@ class Chef::Knife::Exec < Chef::Knife :long => "--exec CODE", :description => "a string of Chef code to execute" + option :script_path, + :short => "-p PATH:PATH", + :long => "--script-path PATH:PATH", + :description => "A colon-separated path to look for scripts in", + :proc => lambda { |o| o.split(":") } + deps do require 'chef/shef/ext' end def run + config[:script_path] ||= Chef::Config[:script_path] + scripts = Array(name_args) context = Object.new Shef::Extensions.extend_context_object(context) @@ -39,7 +47,7 @@ class Chef::Knife::Exec < Chef::Knife context.instance_eval(config[:exec], "-E Argument", 0) elsif !scripts.empty? scripts.each do |script| - file = File.expand_path(script) + file = find_script(script) context.instance_eval(IO.read(file), file, 0) end else @@ -47,5 +55,26 @@ class Chef::Knife::Exec < Chef::Knife context.instance_eval(script, "STDIN", 0) end end - + + def find_script(x) + # Try to find a script. First try expanding the path given. + # Failing that, try searching the script path. If we can't find + # anything, just return the expanded path of what we were given. + + script = File.expand_path(x) + unless File.exists?(script) + Chef::Log.debug("Searching script_path: #{config[:script_path].inspect}") + config[:script_path].each do |path| + test = File.join(path, x) + Chef::Log.debug("Testing: #{test}") + if File.exists?(test) + script = test + Chef::Log.debug("Found: #{test}") + break + end + end + end + return script + end + end |