class Chef::Resource::WindowsPagefile
Public Instance Methods
see if the pagefile is automatically managed by Windows
@return [Boolean]
# File lib/chef/resource/windows_pagefile.rb, line 144 def automatic_managed? @automatic_managed ||= begin logger.trace("Checking if pagefiles are automatically managed") cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" get AutomaticManagedPagefile /format:list") cmd.stderr.empty? && (cmd.stdout =~ /AutomaticManagedPagefile=TRUE/i) end end
raise if there's an error on stderr on a shellout
# File lib/chef/resource/windows_pagefile.rb, line 200 def check_for_errors(stderr) raise stderr.chomp unless stderr.empty? end
create a pagefile
@param [String] pagefile path to the pagefile
# File lib/chef/resource/windows_pagefile.rb, line 122 def create(pagefile) converge_by("create pagefile #{pagefile}") do logger.trace("Running wmic.exe pagefileset create name=\"#{pagefile}\"") cmd = shell_out("wmic.exe pagefileset create name=\"#{pagefile}\"") check_for_errors(cmd.stderr) end end
delete a pagefile
@param [String] pagefile path to the pagefile
# File lib/chef/resource/windows_pagefile.rb, line 133 def delete(pagefile) converge_by("remove pagefile #{pagefile}") do logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" delete") cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" delete") check_for_errors(cmd.stderr) end end
See if the pagefile exists
@param [String] pagefile path to the pagefile @return [Boolean]
# File lib/chef/resource/windows_pagefile.rb, line 97 def exists?(pagefile) @exists ||= begin logger.trace("Checking if #{pagefile} exists by runing: wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list") cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list", returns: [0]) cmd.stderr.empty? && (cmd.stdout =~ /SettingID=#{get_setting_id(pagefile)}/i) end end
# File lib/chef/resource/windows_pagefile.rb, line 194 def get_setting_id(pagefile) split_path = pagefile.split('\\') "#{split_path[1]} @ #{split_path[0]}" end
is the max/min pagefile size set?
@param [String] pagefile path to the pagefile @param [String] min the minimum size of the pagefile @param [String] max the minimum size of the pagefile @return [Boolean]
# File lib/chef/resource/windows_pagefile.rb, line 111 def max_and_min_set?(pagefile, min, max) @max_and_min_set ||= begin logger.trace("Checking if #{pagefile} min: #{min} and max #{max} are set") cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list", returns: [0]) cmd.stderr.empty? && (cmd.stdout =~ /InitialSize=#{min}/i) && (cmd.stdout =~ /MaximumSize=#{max}/i) end end
turn on automatic management of all pagefiles by Windows
# File lib/chef/resource/windows_pagefile.rb, line 153 def set_automatic_managed converge_by("set pagefile to Automatic Managed") do logger.trace("Running wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=True") cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=True") check_for_errors(cmd.stderr) end end
set a custom size for the pagefile (vs the defaults)
@param [String] pagefile path to the pagefile @param [String] min the minimum size of the pagefile @param [String] max the minimum size of the pagefile
# File lib/chef/resource/windows_pagefile.rb, line 175 def set_custom_size(pagefile, min, max) converge_by("set #{pagefile} to InitialSize=#{min} & MaximumSize=#{max}") do logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=#{min},MaximumSize=#{max}") cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=#{min},MaximumSize=#{max}", returns: [0]) check_for_errors(cmd.stderr) end end
set a pagefile size to be system managed
@param [String] pagefile path to the pagefile
# File lib/chef/resource/windows_pagefile.rb, line 186 def set_system_managed(pagefile) converge_by("set #{pagefile} to System Managed") do logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=0,MaximumSize=0") cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=0,MaximumSize=0", returns: [0]) check_for_errors(cmd.stderr) end end
turn off automatic management of all pagefiles by Windows
# File lib/chef/resource/windows_pagefile.rb, line 162 def unset_automatic_managed converge_by("set pagefile to User Managed") do logger.trace("Running wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=False") cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=False") check_for_errors(cmd.stderr) end end
make sure the provided name property matches the appropriate format we do this here and not in the property itself because if automatic_managed is set then this validation is not necessary / doesn't make sense at all
# File lib/chef/resource/windows_pagefile.rb, line 88 def validate_name return if /^.:.*.sys/ =~ new_resource.path raise "#{new_resource.path} does not match the format DRIVE:\\path\\file.sys for pagefiles. Example: C:\\pagefile.sys" end