From cf4848a5672d4ba8788e58c9c435ef53669d33ea Mon Sep 17 00:00:00 2001 From: dave-q <24652224+dave-q@users.noreply.github.com> Date: Thu, 1 Apr 2021 18:07:29 +0100 Subject: [PATCH] [GH-476] Feature names translated into powershell format when 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> --- libraries/windowsfeature_helper.rb | 42 ++++++++++++ recipes/mod_application_initialization.rb | 2 +- recipes/mod_aspnet.rb | 2 +- recipes/mod_aspnet45.rb | 2 +- recipes/mod_auth_basic.rb | 2 +- recipes/mod_auth_digest.rb | 2 +- recipes/mod_auth_windows.rb | 2 +- recipes/mod_cgi.rb | 2 +- recipes/mod_compress_dynamic.rb | 2 +- recipes/mod_compress_static.rb | 2 +- recipes/mod_ftp.rb | 2 +- recipes/mod_iis6_metabase_compat.rb | 2 +- recipes/mod_isapi.rb | 2 +- recipes/mod_logging.rb | 2 +- recipes/mod_management.rb | 2 +- recipes/mod_security.rb | 2 +- recipes/mod_tracing.rb | 2 +- resources/install.rb | 2 +- resources/windows_feature_wrapper.rb | 81 +++++++++++++++++++++++ 19 files changed, 140 insertions(+), 17 deletions(-) create mode 100644 libraries/windowsfeature_helper.rb create mode 100644 resources/windows_feature_wrapper.rb diff --git a/libraries/windowsfeature_helper.rb b/libraries/windowsfeature_helper.rb new file mode 100644 index 00000000..86fa3a3f --- /dev/null +++ b/libraries/windowsfeature_helper.rb @@ -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 diff --git a/recipes/mod_application_initialization.rb b/recipes/mod_application_initialization.rb index c791f8dd..fbd61c18 100644 --- a/recipes/mod_application_initialization.rb +++ b/recipes/mod_application_initialization.rb @@ -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 diff --git a/recipes/mod_aspnet.rb b/recipes/mod_aspnet.rb index 1349a0ff..1d9eaf92 100644 --- a/recipes/mod_aspnet.rb +++ b/recipes/mod_aspnet.rb @@ -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? diff --git a/recipes/mod_aspnet45.rb b/recipes/mod_aspnet45.rb index b0eb533e..345ff718 100644 --- a/recipes/mod_aspnet45.rb +++ b/recipes/mod_aspnet45.rb @@ -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 diff --git a/recipes/mod_auth_basic.rb b/recipes/mod_auth_basic.rb index 81b6ad6b..dd1a8688 100644 --- a/recipes/mod_auth_basic.rb +++ b/recipes/mod_auth_basic.rb @@ -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 diff --git a/recipes/mod_auth_digest.rb b/recipes/mod_auth_digest.rb index acfaa8ae..dcb31a99 100644 --- a/recipes/mod_auth_digest.rb +++ b/recipes/mod_auth_digest.rb @@ -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 diff --git a/recipes/mod_auth_windows.rb b/recipes/mod_auth_windows.rb index e733c34f..01717e42 100644 --- a/recipes/mod_auth_windows.rb +++ b/recipes/mod_auth_windows.rb @@ -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 diff --git a/recipes/mod_cgi.rb b/recipes/mod_cgi.rb index 994f4e56..17264af1 100644 --- a/recipes/mod_cgi.rb +++ b/recipes/mod_cgi.rb @@ -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 diff --git a/recipes/mod_compress_dynamic.rb b/recipes/mod_compress_dynamic.rb index 245212f0..cd48b57e 100644 --- a/recipes/mod_compress_dynamic.rb +++ b/recipes/mod_compress_dynamic.rb @@ -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 diff --git a/recipes/mod_compress_static.rb b/recipes/mod_compress_static.rb index 32c654df..f7dded0c 100644 --- a/recipes/mod_compress_static.rb +++ b/recipes/mod_compress_static.rb @@ -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 diff --git a/recipes/mod_ftp.rb b/recipes/mod_ftp.rb index deebb703..de3d9772 100644 --- a/recipes/mod_ftp.rb +++ b/recipes/mod_ftp.rb @@ -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 diff --git a/recipes/mod_iis6_metabase_compat.rb b/recipes/mod_iis6_metabase_compat.rb index bbe6be04..6714b685 100644 --- a/recipes/mod_iis6_metabase_compat.rb +++ b/recipes/mod_iis6_metabase_compat.rb @@ -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 diff --git a/recipes/mod_isapi.rb b/recipes/mod_isapi.rb index 44babf70..9b7f470e 100644 --- a/recipes/mod_isapi.rb +++ b/recipes/mod_isapi.rb @@ -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 diff --git a/recipes/mod_logging.rb b/recipes/mod_logging.rb index a9f03230..199d2654 100644 --- a/recipes/mod_logging.rb +++ b/recipes/mod_logging.rb @@ -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 diff --git a/recipes/mod_management.rb b/recipes/mod_management.rb index ac59c2e4..956bdad8 100644 --- a/recipes/mod_management.rb +++ b/recipes/mod_management.rb @@ -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 diff --git a/recipes/mod_security.rb b/recipes/mod_security.rb index 7a7dbf8a..1380c1d5 100644 --- a/recipes/mod_security.rb +++ b/recipes/mod_security.rb @@ -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 diff --git a/recipes/mod_tracing.rb b/recipes/mod_tracing.rb index 76647f57..82e3f779 100644 --- a/recipes/mod_tracing.rb +++ b/recipes/mod_tracing.rb @@ -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 diff --git a/resources/install.rb b/resources/install.rb index 3d15203e..0c899a4d 100644 --- a/resources/install.rb +++ b/resources/install.rb @@ -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? diff --git a/resources/windows_feature_wrapper.rb b/resources/windows_feature_wrapper.rb new file mode 100644 index 00000000..502c8dff --- /dev/null +++ b/resources/windows_feature_wrapper.rb @@ -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