Skip to content

Commit

Permalink
[sous-chefsGH-476] Feature names translated into powershell format wh…
Browse files Browse the repository at this point in the history
…en install method is powershell

- Using powershell to install features requires passing in a different feature name
- To prevent having to do this look up for every windows_feature call
I created a wrapper resource that is identical to windows_feature expect
it will look up the correct  format of the feature to install

Signed-off-by: dave-q <24652224+dave-q@users.noreply.github.com>
  • Loading branch information
dave-q committed Apr 1, 2021
1 parent 973ddf0 commit 642ec01
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 17 deletions.
42 changes: 42 additions & 0 deletions libraries/windowsfeature_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Cookbook:: iis
# Library:: section-helper
#
# Copyright:: 2017-2021, Chef Software, 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.
#

module IISCookbook
module WindowsFeatureHelper
def transform_feature_name(install_method, *names)
features = names
if install_method.to_sym == :windows_feature_powershell
translated_features = []
names.each do |name|
cmd = "Get-WindowsFeature | Where-Object {$_.AdditionalInfo.InstallName -Eq #{name}} | Select -Expand Name"
result = powershell_out cmd
if result.stderr.empty?
translated_features.push(result.stdout)
else
Chef::Log.warn("Unable to translate feature #{name}")
Chef::Log.warn(result.stderr)
translated_features.push(name)
end
end
features = translated_features
end
features
end
end
end
2 changes: 1 addition & 1 deletion recipes/mod_application_initialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-ApplicationInit' do
iis_windows_feature_wrapper 'IIS-ApplicationInit' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_aspnet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
include_recipe 'iis'
include_recipe 'iis::mod_isapi'

windows_feature %w(IIS-NetFxExtensibility IIS-ASPNET) do
iis_windows_feature_wrapper %w(IIS-NetFxExtensibility IIS-ASPNET) do
action :install
all !IISCookbook::Helper.older_than_windows2012?
source node['iis']['source'] unless node['iis']['source'].nil?
Expand Down
2 changes: 1 addition & 1 deletion recipes/mod_aspnet45.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
include_recipe 'iis'
include_recipe 'iis::mod_isapi'

windows_feature %w(NetFx4Extended-ASPNET45 IIS-NetFxExtensibility45 IIS-ASPNET45) do
iis_windows_feature_wrapper %w(NetFx4Extended-ASPNET45 IIS-NetFxExtensibility45 IIS-ASPNET45) do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_auth_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

include_recipe 'iis'

windows_feature 'IIS-BasicAuthentication' do
iis_windows_feature_wrapper 'IIS-BasicAuthentication' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end

Expand Down
2 changes: 1 addition & 1 deletion recipes/mod_auth_digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

include_recipe 'iis'

windows_feature 'IIS-DigestAuthentication' do
iis_windows_feature_wrapper 'IIS-DigestAuthentication' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end

Expand Down
2 changes: 1 addition & 1 deletion recipes/mod_auth_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

include_recipe 'iis'

windows_feature 'IIS-WindowsAuthentication' do
iis_windows_feature_wrapper 'IIS-WindowsAuthentication' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end

Expand Down
2 changes: 1 addition & 1 deletion recipes/mod_cgi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-CGI' do
iis_windows_feature_wrapper 'IIS-CGI' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_compress_dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-HttpCompressionDynamic' do
iis_windows_feature_wrapper 'IIS-HttpCompressionDynamic' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_compress_static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-HttpCompressionStatic' do
iis_windows_feature_wrapper 'IIS-HttpCompressionStatic' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature %w(IIS-FTPServer IIS-FTPSvc IIS-FTPExtensibility) do
iis_windows_feature_wrapper %w(IIS-FTPServer IIS-FTPSvc IIS-FTPExtensibility) do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_iis6_metabase_compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature %w(IIS-IIS6ManagementCompatibility IIS-Metabase) do
iis_windows_feature_wrapper %w(IIS-IIS6ManagementCompatibility IIS-Metabase) do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_isapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature %w(IIS-ISAPIFilter IIS-ISAPIExtensions) do
iis_windows_feature_wrapper %w(IIS-ISAPIFilter IIS-ISAPIExtensions) do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-CustomLogging' do
iis_windows_feature_wrapper 'IIS-CustomLogging' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

include_recipe 'iis'

windows_feature %w(IIS-ManagementConsole IIS-ManagementService) do
iis_windows_feature_wrapper %w(IIS-ManagementConsole IIS-ManagementService) do
action :install
all !IISCookbook::Helper.older_than_windows2012?
install_method node['iis']['windows_feature_install_method']&.to_sym
Expand Down
2 changes: 1 addition & 1 deletion recipes/mod_security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature %w(IIS-URLAuthorization IIS-RequestFiltering IIS-IPSecurity) do
iis_windows_feature_wrapper %w(IIS-URLAuthorization IIS-RequestFiltering IIS-IPSecurity) do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion recipes/mod_tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

include_recipe 'iis'

windows_feature 'IIS-HttpTracing' do
iis_windows_feature_wrapper 'IIS-HttpTracing' do
install_method node['iis']['windows_feature_install_method']&.to_sym
end
2 changes: 1 addition & 1 deletion resources/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
action :install do
features_to_install = ['IIS-WebServerRole'].concat new_resource.additional_components

windows_feature 'Install IIS and additional components' do
iis_windows_feature_wrapper 'Install IIS and additional components' do
feature_name features_to_install
action :install
all !IISCookbook::Helper.older_than_windows2012?
Expand Down
81 changes: 81 additions & 0 deletions resources/windows_feature_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Cookbook:: iis
# Resource:: install
#
# Copyright:: 2018-2019, Chef Software, 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.
#

include IISCookbook::WindowsFeatureHelper

property :feature_name, [Array, String],
description: "The name of the feature(s) or role(s) to install if they differ from the resource block's name. The same feature may have different names depending on the underlying installation method being used (ie DHCPServer vs DHCP; DNS-Server-Full-Role vs DNS).",
name_property: true

property :source, String,
description: 'Specify a local repository for the feature install.'

property :all, [true, false],
description: 'Install all sub-features.',
default: false

property :management_tools, [true, false],
description: 'Install all applicable management tools for the roles, role services, or features (PowerShell-only).',
default: false

property :install_method, Symbol,
description: 'The underlying installation method to use for feature installation. Specify `:windows_feature_dism` for DISM or `:windows_feature_powershell` for PowerShell.',
equal_to: %i(windows_feature_dism windows_feature_powershell windows_feature_servermanagercmd),
default: :windows_feature_dism

property :timeout, Integer,
description: 'Specifies a timeout (in seconds) for the feature installation.',
default: 600,
desired_state: false

action :delete do
feature = transform_feature_name(new_resource.install_method, *new_resource.feature_name)
windows_feature feature do
source new_resource.source
all new_resource.all
management_tools new_resource.management_tools
install_method new_resource.install_method
timeout new_resource.timeout
action :delete
end
end

action :install do
feature = transform_feature_name(new_resource.install_method, *new_resource.feature_name)
windows_feature feature do
source new_resource.source
all new_resource.all
management_tools new_resource.management_tools
install_method new_resource.install_method
timeout new_resource.timeout
action :install
end
end

action :remove do
feature = transform_feature_name(new_resource.install_method, *new_resource.feature_name)
windows_feature feature do
source new_resource.source
all new_resource.all
management_tools new_resource.management_tools
install_method new_resource.install_method
timeout new_resource.timeout
action :remove
end
end

0 comments on commit 642ec01

Please sign in to comment.