diff options
author | John McCrae <john.mccrae@progress.com> | 2021-06-02 09:40:11 -0700 |
---|---|---|
committer | John McCrae <john.mccrae@progress.com> | 2021-06-02 09:40:11 -0700 |
commit | 17f55412e2aa7e2c0dc9f16f7893b09cb0f95a2d (patch) | |
tree | 2cfb6576947d5027e6e5298b213da941fbe922cd /lib/chef | |
parent | c76c7f7aaf136896df9b8c836597a0e5a1653463 (diff) | |
download | chef-17f55412e2aa7e2c0dc9f16f7893b09cb0f95a2d.tar.gz |
Updated the Windows Pagefile resource to use PowerShell over WMI, added a corresponding test file
Signed-off-by: John McCrae <john.mccrae@progress.com>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/resource/windows_pagefile.rb | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/chef/resource/windows_pagefile.rb b/lib/chef/resource/windows_pagefile.rb index 0227b50255..8c4272ed38 100644 --- a/lib/chef/resource/windows_pagefile.rb +++ b/lib/chef/resource/windows_pagefile.rb @@ -66,7 +66,7 @@ class Chef DOC property :path, String, - coerce: proc { |x| x[0] }, + coerce: proc { |x| x.tr("/", "\\") }, description: "An optional property to set the pagefile name if it differs from the resource block's name.", name_property: true @@ -83,7 +83,7 @@ class Chef description: "Maximum size of the pagefile in megabytes." action :set, description: "Configures the default pagefile, creating if it doesn't exist." do - pagefile = (new_resource.path[0] + ":\\pagefile.sys") + pagefile = clarify_pagefile_name initial_size = new_resource.initial_size maximum_size = new_resource.maximum_size system_managed = new_resource.system_managed @@ -96,6 +96,7 @@ class Chef else # the method below is designed to raise an exception if the drive you are trying to create a pagefile for doesn't exist. # PowerShell will happily let you create a pagefile called h:\pagefile.sys even though you don't have an H:\ drive. + pagefile_drive_exist?(pagefile) create(pagefile) unless exists?(pagefile) @@ -112,13 +113,30 @@ class Chef end action :delete, description: "Deletes the specified pagefile." do - pagefile = (new_resource.path[0] + ":\\pagefile.sys") + pagefile = clarify_pagefile_name delete(pagefile ) if exists?(pagefile ) end action_class do private + def clarify_pagefile_name + if new_resource.path.length < 4 + (new_resource.path[0] + ":\\pagefile.sys") + elsif new_resource.path !~ /^.:.*/ + "C:\\pagefile.sys" + else + validate_name + new_resource.path + end + end + + def validate_name + return if /^.:.*.sys/.match?(new_resource.path) + + raise "#{new_resource.path} does not match the format DRIVE:\\path\\pagefile.sys for pagefiles. Example: C:\\pagefile.sys" + end + # raise an exception if the target drive location is invalid def pagefile_drive_exist?(pagefile) if ::Dir.exist?(pagefile[0] + ":\\") == false @@ -164,9 +182,7 @@ class Chef converge_by("create pagefile #{pagefile}") do logger.trace("Running New-CimInstance -ClassName Win32_PageFileSetting to create new pagefile : #{pagefile}") powershell_exec! <<~ELM - $page_file = "#{pagefile}" - $driveLetter = $page_file.split(':')[0] - New-CimInstance -ClassName Win32_PageFileSetting -Property @{Name = "$($DriveLetter)\:\\pagefile.sys"} + New-CimInstance -ClassName Win32_PageFileSetting -Property @{Name = "#{pagefile}"} ELM end end |