Skip to content

Commit

Permalink
[COOK-3714] Powershell features provider and delete support.
Browse files Browse the repository at this point in the history
Signed-off-by: Sean OMeara <[email protected]>
  • Loading branch information
ProTip authored and Sean OMeara committed Dec 21, 2013
1 parent 6346c0d commit e4f37dc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
18 changes: 18 additions & 0 deletions libraries/feature_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def action_remove
end
end

def action_delete
if available?
delete_feature(@new_resource.feature_name)
@new_resource.updated_by_last_action(true)
Chef::Log.info("#{@new_resource} deleted")
else
Chef::Log.debug("#{@new_resource} feature is not installed - nothing to do")
end
end

def install_feature(name)
raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :install"
end
Expand All @@ -31,9 +41,17 @@ def remove_feature(name)
raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :remove"
end

def delete_feature(name)
raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :delete"
end

def installed?
raise Chef::Exceptions::Override, "You must override installed? in #{self.to_s}"
end

def available?
raise Chef::Exceptions::Override, "You must override available? in #{self.to_s}"
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions providers/feature_dism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,28 @@ def remove_feature(name)
shell_out!("#{dism} /online /disable-feature /featurename:#{@new_resource.feature_name} /norestart", {:returns => [0,42,127,3010]})
end

def delete_feature(name)
if win_version.major_version >= 6 and win_version.minor_version >=2
shell_out!("#{dism} /online /disable-feature /featurename:#{@new_resource.feature_name} /Remove /norestart", {:returns => [0,42,127,3010]})
else
raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} :delete action not support on #{win_version.sku}"
end
end

def installed?
@installed ||= begin
cmd = shell_out("#{dism} /online /Get-Features", {:returns => [0,42,127]})
cmd.stderr.empty? && (cmd.stdout =~ /^Feature Name : #{@new_resource.feature_name}.?$\n^State : Enabled.?$/i)
end
end

def available?
@available ||= begin
cmd = shell_out("#{dism} /online /Get-Features", {:returns => [0,42,127]})
cmd.stderr.empty? && (cmd.stdout !~ /^Feature Name : #{@new_resource.feature_name}.?$\n^State : .* with payload removed.?$/i)
end
end

private
# account for File System Redirector
# http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx
Expand Down
38 changes: 38 additions & 0 deletions providers/feature_powershell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Author:: Greg Zapp (<[email protected]>)
# Cookbook Name:: windows
# Provider:: feature_powershell
#

include Chef::Provider::WindowsFeature::Base
include Chef::Mixin::PowershellOut

This comment has been minimized.

Copy link
@tduffield

tduffield Dec 21, 2013

As of Chef 11.8.2, Chef::Mixin::PowershellOut is only available as a library from opscode-cookbook/powershell. The powershell cookbook is not listed as a dependency in the metadata.rb

include Windows::Helper

def install_feature(name)
cmd = powershell_out("Install-WindowsFeature #{@new_resource.feature_name}")
Chef::Log.info(cmd.stdout)
end

def remove_feature(name)
cmd = powershell_out("Uninstall-WindowsFeature #{@new_resource.feature_name}")
Chef::Log.info(cmd.stdout)
end

def delete_feature(name)
cmd = powershell_out("Uninstall-WindowsFeature #{@new_resource.feature_name} -Remove")
Chef::Log.info(cmd.stdout)
end

def installed?
@installed ||= begin
cmd = powershell_out("Get-WindowsFeature #{@new_resource.feature_name} | Select Installed | % { Write-Host $_.Installed }")
cmd.stderr.empty? && cmd.stdout =~ /True/i
end
end

def available?
@available ||= begin
cmd = powershell_out("Get-WindowsFeature #{@new_resource.feature_name}")
cmd.stderr.empty? && cmd.stdout !~ /Removed/i
end
end
6 changes: 4 additions & 2 deletions resources/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

include Windows::Helper

actions :install, :remove
actions :install, :remove, :delete

attribute :feature_name, :kind_of => String, :name_attribute => true

Expand All @@ -32,7 +32,9 @@ def initialize(name, run_context=nil)

private
def locate_default_provider
if ::File.exists?(locate_sysnative_cmd('dism.exe'))
if node['windows'].attribute?(:feature_provider)
"windows_feature_#{node['windows']['feature_provider']}"
elsif ::File.exists?(locate_sysnative_cmd('dism.exe'))
:windows_feature_dism
elsif ::File.exists?(locate_sysnative_cmd('servermanagercmd.exe'))
:windows_feature_servermanagercmd
Expand Down

0 comments on commit e4f37dc

Please sign in to comment.