Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 7 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 10, 2012
@schisamo schisamo Refactor the powershell resource from a core-Chef monkey-patch into a…
… proper LWRP.
b58518b
@schisamo schisamo Take advantage of native Win32 support for `cwd` and `environment` in…
… Chef 0.10.8+.
3f595ab
@schisamo schisamo [COOK-630] force powershell scripts to terminate immediately and retu…
…rn an error code on failure
914095a
@schisamo schisamo ensure more sane default options are set on PowerShell process e27ec1b
Commits on Apr 11, 2012
@schisamo schisamo update README with latest LWRP attribute usage 72bf198
Commits on Apr 16, 2012
@schisamo schisamo Merge branch 'improvements' f9226c2
@schisamo schisamo powershell v1.0.6
* Refactor the powershell resource from a core-Chef monkey-patch
into a proper LWRP.
* Take advantage of native Win32 support for `cwd` and `environment`
in Chef 0.10.8+.
* [COOK-630] force powershell scripts to terminate immediately and
return an error code on failure
* ensure more sane default options are set on PowerShell process
7d8850a
View
7 CHANGELOG.md
@@ -1,3 +1,10 @@
+## v1.0.6:
+
+* Refactor the powershell resource from a core-Chef monkey-patch into a proper LWRP.
+* Take advantage of native Win32 support for `cwd` and `environment` in Chef 0.10.8+.
+* [COOK-630] force powershell scripts to terminate immediately and return an error code on failure
+* ensure more sane default options are set on PowerShell process
+
## v1.0.4:
* [COOK-988] - Powershell never exists on the powershell resource
View
27 README.md
@@ -1,7 +1,8 @@
Description
===========
-Installs and configures PowerShell 2.0. Also includes a resource/provider for executing scripts using the PowerShell interpreter.
+Installs and configures PowerShell 2.0. Also includes a resource/provider for
+executing scripts using the PowerShell interpreter.
Requirements
============
@@ -23,7 +24,11 @@ Resource/Provider
`powershell`
------------
-Execute a script using the powershell interpreter (much like the script resources for bash, csh, 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.
+Execute a script using the powershell interpreter (much like the script resources
+for bash, csh, 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
@@ -39,6 +44,8 @@ Execute a script using the powershell interpreter (much like the script resource
- environment: A hash of environment variables to set before running this command.
- 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.
+- returns: The return value of the command (may be an array of accepted values) - this resource raises an exception if the return value(s) do not match.
+- timeout: How many seconds to let the command run before timing it out.
### Examples
@@ -79,7 +86,7 @@ Execute a script using the powershell interpreter (much like the script resource
# cwd to a winodws env variable
powershell "cwd-to-win-env-var" do
- cwd "%TEMP%"
+ cwd ENV['TEMP']
code <<-EOH
$stream = [System.IO.StreamWriter] "./temp-write-from-chef.txt"
$stream.WriteLine("chef on windows rox yo!")
@@ -93,7 +100,7 @@ Execute a script using the powershell interpreter (much like the script resource
environment ({'foo' => 'BAZ'})
code <<-EOH
$stream = [System.IO.StreamWriter] "./test-read-env-var.txt"
- $stream.WriteLine("FOO is $foo")
+ $stream.WriteLine("FOO is $env:foo")
$stream.close()
EOH
end
@@ -106,27 +113,31 @@ default
Include the default recipe in a run list, to ensure PowerShell 2.0 is installed.
-On the following versions of Windows the PowerShell 2.0 package will be downloaded from Microsoft and installed:
+On the following versions of Windows the PowerShell 2.0 package will be
+downloaded from Microsoft and installed:
* Windows XP
* Windows Server 2003
* Windows Server 2008 R1
* Windows Vista
-On the following versions of Windows, PowerShell 2.0 is present and must just be enabled:
+On the following versions of Windows, PowerShell 2.0 is present and must just be
+enabled:
* Windows 7
* Windows Server 2008 R2
* Windows Server 2008 R2 Core
-**PLEASE NOTE** - The installation may require a restart of the node being configured before PowerShell (or the powershell script resource) can be used (yeah Windows!).
+**PLEASE NOTE** - The installation may require a restart of the node being
+configured before PowerShell (or the powershell script resource) can be used
+(yeah Windows!).
License and Author
==================
Author:: Seth Chisamore (<schisamo@opscode.com>)
-Copyright:: 2011, Opscode, Inc
+Copyright:: 2011-2012, Opscode, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
View
117 libraries/core.rb
@@ -1,117 +0,0 @@
-#
-# Author:: Seth Chisamore (<schisamo@opscode.com>)
-# Copyright:: Copyright (c) 2011 Opscode, Inc.
-# 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 'chef/resource/execute'
-require 'chef/resource/script'
-
-# PLEASE NOTE - This is not a good example of shipping a Resource/Provider
-# with a cookbook. Please check the Chef Wiki for more traditional DSL driven
-# LWRPs. This 'heavyweight' resource will eventually be moved into Core Chef.
-
-class Chef
- class Resource
- class Powershell < Chef::Resource::Script
-
- def initialize(name, run_context=nil)
- super
- @resource_name = :powershell
- @provider = Chef::Provider::PowershellScript
- @returns = [0,42] # successful commands return exit code 42
- end
-
- def interpreter
- # force 64-bit powershell from 32-bit ruby process
- if ::File.exist?("#{ENV['WINDIR']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
- "#{ENV['WINDIR']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
- elsif ::File.exist?("#{ENV['WINDIR']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
- "#{ENV['WINDIR']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
- else
- "powershell.exe"
- end
- end
- end
- end
-
- class Provider
- class PowershellScript < Chef::Provider::Execute
-
- def action_run
- script_file.puts(@new_resource.code)
- script_file.close
- set_owner_and_group
-
- # always set the ExecutionPolicy flag
- # see http://technet.microsoft.com/en-us/library/ee176961.aspx
- # Powershell will hang if stdin is redirected
- # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
- @new_resource.flags("#{@new_resource.flags} -ExecutionPolicy RemoteSigned -inputformat none -Command".strip)
-
- # cwd hax...shell_out on windows needs to support proper 'cwd'
- # follow CHEF-2357 for more
- cwd = @new_resource.cwd ? "cd #{@new_resource.cwd} & " : ""
- @new_resource.command("#{cwd}#{@new_resource.interpreter} #{@new_resource.flags} \"#{build_powershell_scriptblock}\"")
- super
- ensure
- unlink_script_file
- end
-
- 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', '.ps1'])
- end
-
- def unlink_script_file
- @script_file && @script_file.close!
- end
-
- private
- # take advantage of PowerShell scriptblocks
- # to pass scoped environment variables to the
- # command
- def build_powershell_scriptblock
- # environment var hax...shell_out on windows needs to support proper 'environment'
- # follow CHEF-2358 for more
- env_string = if @new_resource.environment
- @new_resource.environment.inject("") {|a, (k,v)| a << "$#{k} = '#{v}'; "; a}
- else
- ""
- end
- "& { #{env_string}#{ensure_windows_friendly_path(script_file.path)} }"
- end
- end
- end
-
- module Mixin
- module Language
- def ensure_windows_friendly_path(path)
- if path
- path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)
- else
- path
- end
- end
- end
- end
-end
-Chef::Platform.platforms[:default].merge! :powershell => Chef::Provider::PowershellScript
View
2  metadata.rb
@@ -3,7 +3,7 @@
license "Apache 2.0"
description "Installs/Configures PowerShell 2.0 on the Windows platform"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
-version "1.0.4"
+version "1.0.6"
recipe "powershell::default", "Installs and configures PowerShell 2.0"
View
110 providers/default.rb
@@ -0,0 +1,110 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Copyright:: Copyright (c) 2011-2012 Opscode, Inc.
+# 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.
+#
+
+action :run do
+ begin
+ # force our script to terminate and return an error code on failure
+ # http://blogs.msdn.com/b/powershell/archive/2006/04/25/583241.aspx
+ script_file.puts("$ErrorActionPreference = 'Stop'")
+ script_file.puts(@new_resource.code)
+ script_file.close
+
+ # default flags
+ flags = [
+ # Hides the copyright banner at startup.
+ "-NoLogo",
+ # Does not present an interactive prompt to the user.
+ "-NonInteractive",
+ # Does not load the Windows PowerShell profile.
+ "-NoProfile",
+ # always set the ExecutionPolicy flag
+ # see http://technet.microsoft.com/en-us/library/ee176961.aspx
+ "-ExecutionPolicy RemoteSigned",
+ # Powershell will hang if STDIN is redirected
+ # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
+ "-InputFormat None"
+ ]
+
+ # user-provided flags
+ unless @new_resource.flags.nil? || @new_resource.flags.empty?
+ flags << @new_resource.flags.strip
+ end
+
+ cwd = ensure_windows_friendly_path(@new_resource.cwd)
+ prefix = @new_resource.interpreter
+ command = ensure_windows_friendly_path(script_file.path)
+
+ # Chef::Resource::Execute in Chef >= 0.10.8 has first-class Win32 support
+ if Gem::Version.create(Chef::VERSION) >= Gem::Version.create("0.10.8")
+ execute.cwd(cwd)
+ execute.environment(@new_resource.environment)
+ else
+ # we have to fake `cwd` and `environment` on older versions of Chef
+ prefix = "cd #{@new_resource.cwd} & #{prefix}" if @new_resource.cwd
+ command = create_env_wrapper(command, @new_resource.environment)
+ end
+
+ command = "#{prefix} #{flags.join(' ')} -Command \"#{command}\""
+
+ execute.command(command)
+ execute.creates(@new_resource.creates)
+ execute.user(@new_resource.user)
+ execute.group(@new_resource.group)
+ execute.timeout(@new_resource.timeout)
+ execute.returns(@new_resource.returns)
+ execute.run_action(:run)
+ ensure
+ unlink_script_file
+ end
+end
+
+private
+
+def execute
+ @execute ||= Chef::Resource::Execute.new(@new_resource.name, run_context)
+end
+
+def script_file
+ @script_file ||= Tempfile.open(['chef-script', '.ps1'])
+end
+
+def unlink_script_file
+ @script_file && @script_file.close!
+end
+
+# take advantage of PowerShell scriptblocks
+# to pass scoped environment variables to the
+# command. This is mainly only useful for versions
+# of Chef < 0.10.8 when Chef::Resource::Execute
+# did not support the 'environment' attribute.
+def create_env_wrapper(command, environment)
+ if environment
+ env_string = environment.map{ |k,v| "$env:#{k}='#{v}'" }.join('; ')
+ "& { #{env_string}; #{command} }"
+ else
+ command
+ end
+end
+
+def ensure_windows_friendly_path(path)
+ if path
+ path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)
+ else
+ path
+ end
+end
View
49 resources/default.rb
@@ -0,0 +1,49 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Copyright:: Copyright (c) 2011-2012 Opscode, Inc.
+# 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.
+#
+
+actions :run
+
+# Chef::Resource::Execute
+attribute :command, :kind_of => String, :name_attribute => true
+attribute :creates, :kind_of => String
+attribute :cwd, :kind_of => String
+attribute :environment, :kind_of => Hash
+attribute :user, :kind_of => [ String, Integer ]
+attribute :group, :kind_of => [ String, Integer ]
+attribute :returns, :kind_of => [ Integer, Array ]
+attribute :timeout, :kind_of => Integer
+
+# Chef::Resource::Script
+attribute :code, :kind_of => String
+attribute :flags, :kind_of => String
+
+def initialize(*args)
+ super
+ @action = :run
+end
+
+def interpreter
+ # force 64-bit powershell from 32-bit ruby process
+ if ::File.exist?("#{ENV['WINDIR']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
+ "#{ENV['WINDIR']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
+ elsif ::File.exist?("#{ENV['WINDIR']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
+ "#{ENV['WINDIR']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
+ else
+ "powershell.exe"
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.