summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2012-06-21 11:49:55 -0700
committerBryan McLellan <btm@opscode.com>2012-06-21 11:49:55 -0700
commit1afcbe6530f560e7ee99ad5dabd6cf8e9a5f21dd (patch)
tree99416a461184eef53fab79ae8c4b17b63bfbc7a6
parent29ec163a6e613b2ebe0e9193f7b90f878f5bc84a (diff)
parente95340ed3a73fc84d3faf8b35c45ad9cc1ac2a69 (diff)
downloadchef-1afcbe6530f560e7ee99ad5dabd6cf8e9a5f21dd.tar.gz
Merge branch 'CHEF-2739'
-rw-r--r--chef/lib/chef/config.rb3
-rw-r--r--chef/lib/chef/knife/exec.rb39
2 files changed, 40 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..4908bfc203 100644
--- a/chef/lib/chef/knife/exec.rb
+++ b/chef/lib/chef/knife/exec.rb
@@ -27,11 +27,23 @@ 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] ||= Array(Chef::Config[:script_path])
+
+ # Default script paths are chef-repo/.chef/scripts and ~/.chef/scripts
+ config[:script_path] << File.join(Chef::Knife.chef_config_dir, 'scripts') if Chef::Knife.chef_config_dir
+ config[:script_path] << File.join(ENV['HOME'], '.chef', 'scripts') if ENV['HOME']
+
scripts = Array(name_args)
context = Object.new
Shef::Extensions.extend_context_object(context)
@@ -39,7 +51,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 +59,28 @@ 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.
+ script = File.expand_path(x)
+ return script if File.exists?(script)
+
+ # Failing that, try searching the script path. If we can't find
+ # anything, fail gracefully.
+ Chef::Log.debug("Searching script_path: #{config[:script_path].inspect}")
+
+ config[:script_path].each do |path|
+ path = File.expand_path(path)
+ test = File.join(path, x)
+ Chef::Log.debug("Testing: #{test}")
+ if File.exists?(test)
+ script = test
+ Chef::Log.debug("Found: #{test}")
+ return script
+ end
+ end
+ ui.error("\"#{x}\" not found in current directory or script_path, giving up.")
+ exit(1)
+ end
+
end