summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Chisamore <schisamo@opscode.com>2011-09-27 03:23:02 -0400
committerSeth Chisamore <schisamo@opscode.com>2011-09-27 03:23:02 -0400
commitb0ee07e01b786be361bb40501847ea03910c981a (patch)
tree6471b43874578dbd2dfbc486547dfb5cd81733db
parent9ff858747cda76bb57104f0e0ac1497b332f4e4e (diff)
downloadmixlib-shellout-b0ee07e01b786be361bb40501847ea03910c981a.tar.gz
added a windows_batch resource/provider for running batch scripts remotely
-rw-r--r--README.md37
-rw-r--r--providers/batch.rb62
-rw-r--r--resources/batch.rb36
3 files changed, 135 insertions, 0 deletions
diff --git a/README.md b/README.md
index 939f0d5..4020a4c 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,43 @@ Resource/Provider
end
+`windows_batch`
+------------
+Execute a batch script using the cmd.exe interpreter (much like the script resources for bash, csh, powershell, perl, python and ruby). A temporary file is created and executed like other script resources, rather than run inline. By their nature, Script resources are not idempotent, as they are completely up to the user's imagination. Use the `not_if` or `only_if` meta parameters to guard the resource for idempotence.
+
+### Actions
+
+- :run: run the batch file
+
+### Attribute Parameters
+
+- command: name attribute. Name of the command to execute.
+- code: quoted string of code to execute.
+- creates: a file this command creates - if the file exists, the command will not be run.
+- cwd: current working directory to run the command from.
+- flags: command line flags to pass to the interpreter when invoking.
+- user: A user name or user ID that we should change to before running this command.
+- group: A group name or group ID that we should change to before running this command.
+
+### Examples
+
+ windows_batch "unzip_and_move_ruby" do
+ code <<-EOH
+ 7z.exe x #{Chef::Config[:file_cache_path]}/ruby-1.8.7-p352-i386-mingw32.7z -oC:\\source -r -y
+ xcopy C:\\source\\ruby-1.8.7-p352-i386-mingw32 C:\\ruby /e /y
+ EOH
+ end
+
+ windows_batch "echo some env vars" do
+ code <<-EOH
+ echo %TEMP%
+ echo %SYSTEMDRIVE%
+ echo %PATH%
+ echo %WINDIR%
+ EOH
+ end
+
+
`windows_feature`
-----------------
diff --git a/providers/batch.rb b/providers/batch.rb
new file mode 100644
index 0000000..9aa347c
--- /dev/null
+++ b/providers/batch.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Cookbook Name:: windws
+# Provider:: batch
+#
+# Copyright:: 2011, Opscode, Inc.
+#
+# 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'
+require 'chef/resource/execute'
+
+action :run do
+ begin
+ script_file.puts(@new_resource.code)
+ script_file.close
+ set_owner_and_group
+
+ # cwd hax...shell_out on windows needs to support proper 'cwd'
+ # follow CHEF-2357 for more
+ cwd = @new_resource.cwd ? "cd \"#{@new_resource.cwd}\" & " : ""
+
+ r = Chef::Resource::Execute.new(@new_resource.name, run_context)
+ r.user(@new_resource.user)
+ r.group(@new_resource.group)
+ r.command("#{cwd}call \"#{script_file.path}\" #{@new_resource.flags}")
+ r.creates(@new_resource.creates)
+ r.returns(@new_resource.returns)
+ r.run_action(:run)
+
+ @new_resource.updated_by_last_action(r.updated_by_last_action?)
+ ensure
+ unlink_script_file
+ end
+end
+
+private
+def set_owner_and_group
+ # FileUtils itself implements a no-op if +user+ or +group+ are nil
+ # You can prove this by running FileUtils.chown(nil,nil,'/tmp/file')
+ # as an unprivileged user.
+ FileUtils.chown(@new_resource.user, @new_resource.group, script_file.path)
+end
+
+def script_file
+ @script_file ||= Tempfile.open(['chef-script', '.bat'])
+end
+
+def unlink_script_file
+ @script_file && @script_file.close!
+end
diff --git a/resources/batch.rb b/resources/batch.rb
new file mode 100644
index 0000000..7d4e917
--- /dev/null
+++ b/resources/batch.rb
@@ -0,0 +1,36 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Cookbook Name:: windows
+# Resource:: batch
+#
+# Copyright:: 2011, Opscode, Inc.
+#
+# 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.
+#
+
+actions :run
+
+attribute :command, :kind_of => String, :name_attribute => true
+attribute :cwd, :kind_of => String, :default => nil
+attribute :code, :kind_of => String, :default => nil
+attribute :user, :kind_of => [ String, Integer ], :default => nil
+attribute :group, :kind_of => [ String, Integer ], :default => nil
+attribute :creates, :kind_of => [ String ], :default => nil
+attribute :flags, :kind_of => [ String ], :default => nil
+attribute :returns, :kind_of => [Integer, Array], :default => 0
+
+def initialize(name, run_context=nil)
+ super
+ @action = :run
+ @command = name
+end