diff options
-rw-r--r-- | distro/powershell/chef/Chef.PowerShell.dll | bin | 0 -> 6144 bytes | |||
-rw-r--r-- | distro/powershell/chef/Newtonsoft.Json.dll | bin | 0 -> 664576 bytes | |||
-rw-r--r-- | lib/chef/dsl/universal.rb | 2 | ||||
-rw-r--r-- | lib/chef/mixin/powershell_exec.rb | 39 | ||||
-rw-r--r-- | lib/chef/powershell.rb | 53 | ||||
-rw-r--r-- | omnibus/Gemfile | 2 | ||||
-rw-r--r-- | omnibus/resources/chef/msi/source.wxs.erb | 30 |
7 files changed, 125 insertions, 1 deletions
diff --git a/distro/powershell/chef/Chef.PowerShell.dll b/distro/powershell/chef/Chef.PowerShell.dll Binary files differnew file mode 100644 index 0000000000..9aa1c62850 --- /dev/null +++ b/distro/powershell/chef/Chef.PowerShell.dll diff --git a/distro/powershell/chef/Newtonsoft.Json.dll b/distro/powershell/chef/Newtonsoft.Json.dll Binary files differnew file mode 100644 index 0000000000..1971a35679 --- /dev/null +++ b/distro/powershell/chef/Newtonsoft.Json.dll diff --git a/lib/chef/dsl/universal.rb b/lib/chef/dsl/universal.rb index 6e3d162b6f..f3b79b1d60 100644 --- a/lib/chef/dsl/universal.rb +++ b/lib/chef/dsl/universal.rb @@ -18,6 +18,7 @@ # require "chef/dsl/platform_introspection" +require "chef/mixin/powershell_exec" require "chef/mixin/powershell_out" require "chef/mixin/shell_out" @@ -43,6 +44,7 @@ class Chef # module Universal include Chef::DSL::PlatformIntrospection + include Chef::Mixin::PowershellExec include Chef::Mixin::PowershellOut include Chef::Mixin::ShellOut end diff --git a/lib/chef/mixin/powershell_exec.rb b/lib/chef/mixin/powershell_exec.rb new file mode 100644 index 0000000000..b4ff7b9001 --- /dev/null +++ b/lib/chef/mixin/powershell_exec.rb @@ -0,0 +1,39 @@ +# +# Author:: Stuart Preston (<stuart@chef.io>) +# Copyright:: Copyright 2018, Chef Software, 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/powershell" + +class Chef + module Mixin + module PowershellExec + # Run a command under PowerShell via a managed (.NET) COM interop API. + # This implementation requires the managed dll to be registered on the + # target machine. + # + # Requires: .NET Framework 4.0 or higher on the target machine. + # + # @param script [String] script to run + # @return [Chef::PowerShell] output + def powershell_exec(*command_args) + script = command_args.first + options = command_args.last.is_a?(Hash) ? command_args.last : nil + + Chef::PowerShell.new(script, options) + end + end + end +end diff --git a/lib/chef/powershell.rb b/lib/chef/powershell.rb new file mode 100644 index 0000000000..32a3a2f532 --- /dev/null +++ b/lib/chef/powershell.rb @@ -0,0 +1,53 @@ +# +# Author:: Stuart Preston (<stuart@chef.io>) +# Copyright:: Copyright 2018, Chef Software, 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/json_compat" +require "win32ole" if RUBY_PLATFORM =~ /mswin|mingw32|windows/ + +class Chef + class PowerShell + + attr_reader :result + attr_reader :errors + + # Run a command under PowerShell via a managed (.NET) COM interop API. + # This implementation requires the managed dll to be registered on the + # target machine. + # + # Requires: .NET Framework 4.0 or higher on the target machine. + # + # @param script [String] script to run + # @return [Object] output + def initialize(*command_args) + raise "This class can only be used on the Windows platform." unless RUBY_PLATFORM =~ /mswin|mingw32|windows/ + exec(command_args.first) + end + + def exec(script) + ps = WIN32OLE.new("Chef.PowerShell") + outcome = ps.ExecuteScript(script) + hashed_outcome = Chef::JSONCompat.parse(outcome) + @result = Chef::JSONCompat.parse(hashed_outcome["result"]) + @errors = hashed_outcome["errors"] + end + + def error? + return true if errors.count > 0 + false + end + end +end diff --git a/omnibus/Gemfile b/omnibus/Gemfile index 0de03a5ea2..5457f0f40b 100644 --- a/omnibus/Gemfile +++ b/omnibus/Gemfile @@ -18,7 +18,7 @@ group :development do # Use Test Kitchen with Vagrant for converging the build environment gem "test-kitchen", "~> 1.13" - gem "kitchen-vagrant", "~> 0.19.0" + gem "kitchen-vagrant", "~> 1.3.1" gem "winrm-fs", "~> 1.0" gem "pry" gem "pry-byebug" diff --git a/omnibus/resources/chef/msi/source.wxs.erb b/omnibus/resources/chef/msi/source.wxs.erb index 2249262b1f..7402007257 100644 --- a/omnibus/resources/chef/msi/source.wxs.erb +++ b/omnibus/resources/chef/msi/source.wxs.erb @@ -151,6 +151,35 @@ <Environment Id="ChefPSModulePathEnvironment" Name="PSModulePath" Action="set" Part="last" System="yes" Value="[PSMODULES]" /> </Component> + <Component Id="ChefPowerShellRegistryEntries" Guid="{3715B7AB-C3CA-4C69-8FAF-79C8CF58FD89}"> + <RegistryKey Root="HKCR" Key="Chef.PowerShell"> + <RegistryValue Type="string" Value="Chef.PowerShell" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="Chef.PowerShell\CLSID"> + <RegistryValue Type="string" Value="{9008CA83-83E4-41FF-9C07-696E2CC47B52}" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="CLSID\{9008CA83-83E4-41FF-9C07-696E2CC47B52}"> + <RegistryValue Type="string" Value="Chef.PowerShell" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="CLSID\{9008CA83-83E4-41FF-9C07-696E2CC47B52}\InprocServer32"> + <RegistryValue Type="string" Value="[System64Folder]mscoree.dll" /> + <RegistryValue Type="string" Name="ThreadingModel" Value="Both" /> + <RegistryValue Type="string" Name="Class" Value="Chef.PowerShell" /> + <RegistryValue Type="string" Name="Assembly" Value="Chef.PowerShell, Version=1.0.12.0, Culture=neutral, PublicKeyToken=7def9f799d039a95" /> + <RegistryValue Type="string" Name="RuntimeVersion" Value="v4.0.30319" /> + <RegistryValue Type="string" Name="CodeBase" Value="[PSMODULES]chef\Chef.PowerShell.dll" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="CLSID\{9008CA83-83E4-41FF-9C07-696E2CC47B52}\InprocServer32\1.0.0.0"> + <RegistryValue Type="string" Name="Class" Value="Chef.PowerShell" /> + <RegistryValue Type="string" Name="Assembly" Value="Chef.PowerShell, Version=1.0.12.0, Culture=neutral, PublicKeyToken=7def9f799d039a95" /> + <RegistryValue Type="string" Name="RuntimeVersion" Value="v4.0.30319" /> + <RegistryValue Type="string" Name="CodeBase" Value="[PSMODULES]chef\Chef.PowerShell.dll" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="CLSID\{9008CA83-83E4-41FF-9C07-696E2CC47B52}\ProgId"> + <RegistryValue Type="string" Value="Chef.PowerShell" /> + </RegistryKey> + <RegistryKey Root="HKCR" Key="CLSID\{9008CA83-83E4-41FF-9C07-696E2CC47B52}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}" /> + </Component> </Directory> <Directory Id="EMBEDDED" Name="embedded" > <Directory Id="EMBEDDEDBIN" Name="bin" > @@ -197,6 +226,7 @@ <ComponentRef Id="ChefClientPath" /> <ComponentRef Id="CONFIGLOCATIONDIR" /> <ComponentRef Id="ChefClientLog" /> + <ComponentRef Id="ChefPowerShellRegistryEntries" /> </Feature> <Feature Id="ChefPSModuleFeature" Title="!(loc.FeaturePSModuleName)" Level="1000" AllowAdvertise="no"> |