class Chef::Resource::WindowsFeatureDism
Public Instance Methods
parse the feature string and add the values to the appropriate array in the strips trailing whitespace characters then split on n number of spaces + | + n number of spaces @return [void]
# File lib/chef/resource/windows_feature_dism.rb, line 211 def add_to_feature_mash(feature_type, feature_string) feature_details = feature_string.strip.split(/\s+[|]\s+/).first # dism isn't case sensitive so it's best to compare lowercase lists so the # user input doesn't need to be case sensitive feature_details.downcase! node.override["dism_features_cache"][feature_type] << feature_details end
@return [Array] features the user has requested to delete which need deleting
# File lib/chef/resource/windows_feature_dism.rb, line 146 def features_to_delete # the intersection of the features to remove & enabled/disabled features are what needs removing @remove ||= begin all_available = node["dism_features_cache"]["enabled"] + node["dism_features_cache"]["disabled"] new_resource.feature_name & all_available end end
@return [Array] features the user has requested to install which need installation
# File lib/chef/resource/windows_feature_dism.rb, line 126 def features_to_install @install ||= begin # disabled features are always available to install available_for_install = node["dism_features_cache"]["disabled"].dup # removed features are also available for installation available_for_install.concat(node["dism_features_cache"]["removed"]) # the intersection of the features to install & disabled/removed features are what needs installing new_resource.feature_name & available_for_install end end
@return [Array] features the user has requested to remove which need removing
# File lib/chef/resource/windows_feature_dism.rb, line 140 def features_to_remove # the intersection of the features to remove & enabled features are what needs removing @remove ||= new_resource.feature_name & node["dism_features_cache"]["enabled"] end
run dism.exe to get a list of all available features and their state and save that to the node at node.override level. We do this because getting a list of features in dism takes at least a second and this data will be persisted across multiple resource runs which gives us a much faster run when no features actually need to be installed / removed. @return [void]
# File lib/chef/resource/windows_feature_dism.rb, line 183 def reload_cached_dism_data logger.trace("Caching Windows features available via dism.exe.") node.override["dism_features_cache"] = Mash.new node.override["dism_features_cache"]["enabled"] = [] node.override["dism_features_cache"]["disabled"] = [] node.override["dism_features_cache"]["removed"] = [] # Grab raw feature information from dism command line raw_list_of_features = shell_out("dism.exe /Get-Features /Online /Format:Table /English").stdout # Split stdout into an array by windows line ending features_list = raw_list_of_features.split("\r\n") features_list.each do |feature_details_raw| case feature_details_raw when /Payload Removed/ # matches 'Disabled with Payload Removed' add_to_feature_mash("removed", feature_details_raw) when /Enable/ # matches 'Enabled' and 'Enable Pending' aka after reboot add_to_feature_mash("enabled", feature_details_raw) when /Disable/ # matches 'Disabled' and 'Disable Pending' aka after reboot add_to_feature_mash("disabled", feature_details_raw) end end logger.trace("The cache contains\n#{node["dism_features_cache"]}") end
# File lib/chef/resource/windows_feature_dism.rb, line 220 def required_parent_feature?(error_message) error_message.include?("Error: 50") && error_message.include?("required parent feature") end
@return [Array] lowercase the array
# File lib/chef/resource/windows_feature_dism.rb, line 62 def to_formatted_array(x) x = x.split(/\s*,\s*/) if x.is_a?(String) # split multiple forms of a comma separated list x.map(&:downcase) end