chef 17.1.35 → 17.2.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/lib/chef/action_collection.rb +6 -26
  4. data/lib/chef/application.rb +1 -1
  5. data/lib/chef/application/base.rb +15 -0
  6. data/lib/chef/client.rb +6 -0
  7. data/lib/chef/cookbook_version.rb +26 -4
  8. data/lib/chef/data_bag.rb +2 -1
  9. data/lib/chef/data_bag_item.rb +2 -1
  10. data/lib/chef/data_collector.rb +0 -1
  11. data/lib/chef/data_collector/run_end_message.rb +1 -1
  12. data/lib/chef/deprecated.rb +4 -0
  13. data/lib/chef/event_dispatch/base.rb +2 -1
  14. data/lib/chef/exceptions.rb +3 -0
  15. data/lib/chef/handler.rb +46 -8
  16. data/lib/chef/handler/slow_report.rb +66 -0
  17. data/lib/chef/node.rb +20 -19
  18. data/lib/chef/provider/support/zypper_repo.erb +4 -2
  19. data/lib/chef/provider/zypper_repository.rb +27 -31
  20. data/lib/chef/resource/alternatives.rb +5 -5
  21. data/lib/chef/resource/apt_preference.rb +2 -2
  22. data/lib/chef/resource/apt_repository.rb +2 -2
  23. data/lib/chef/resource/apt_update.rb +4 -4
  24. data/lib/chef/resource/build_essential.rb +1 -1
  25. data/lib/chef/resource/chef_client_config.rb +3 -3
  26. data/lib/chef/resource/chef_client_cron.rb +2 -2
  27. data/lib/chef/resource/chef_client_launchd.rb +2 -2
  28. data/lib/chef/resource/chef_client_scheduled_task.rb +14 -14
  29. data/lib/chef/resource/chef_client_systemd_timer.rb +2 -2
  30. data/lib/chef/resource/chef_handler.rb +2 -2
  31. data/lib/chef/resource/chef_sleep.rb +1 -1
  32. data/lib/chef/resource/chocolatey_feature.rb +2 -2
  33. data/lib/chef/resource/chocolatey_source.rb +1 -1
  34. data/lib/chef/resource/cron/cron_d.rb +4 -6
  35. data/lib/chef/resource/cron_access.rb +1 -1
  36. data/lib/chef/resource/dmg_package.rb +1 -1
  37. data/lib/chef/resource/group.rb +4 -4
  38. data/lib/chef/resource/homebrew_cask.rb +17 -6
  39. data/lib/chef/resource/homebrew_package.rb +1 -1
  40. data/lib/chef/resource/homebrew_tap.rb +4 -3
  41. data/lib/chef/resource/homebrew_update.rb +2 -2
  42. data/lib/chef/resource/hostname.rb +49 -7
  43. data/lib/chef/resource/inspec_waiver_file_entry.rb +6 -5
  44. data/lib/chef/resource/kernel_module.rb +6 -6
  45. data/lib/chef/resource/locale.rb +1 -1
  46. data/lib/chef/resource/macos_userdefaults.rb +2 -2
  47. data/lib/chef/resource/ohai_hint.rb +2 -6
  48. data/lib/chef/resource/openbsd_package.rb +17 -0
  49. data/lib/chef/resource/openssl_dhparam.rb +1 -2
  50. data/lib/chef/resource/openssl_ec_private_key.rb +1 -3
  51. data/lib/chef/resource/openssl_ec_public_key.rb +1 -3
  52. data/lib/chef/resource/openssl_rsa_private_key.rb +1 -3
  53. data/lib/chef/resource/openssl_rsa_public_key.rb +1 -3
  54. data/lib/chef/resource/openssl_x509_certificate.rb +1 -4
  55. data/lib/chef/resource/openssl_x509_crl.rb +1 -3
  56. data/lib/chef/resource/openssl_x509_request.rb +1 -3
  57. data/lib/chef/resource/osx_profile.rb +3 -3
  58. data/lib/chef/resource/plist.rb +1 -1
  59. data/lib/chef/resource/powershell_package_source.rb +2 -4
  60. data/lib/chef/resource/reboot.rb +38 -9
  61. data/lib/chef/resource/remote_directory.rb +2 -2
  62. data/lib/chef/resource/rhsm_errata.rb +0 -2
  63. data/lib/chef/resource/rhsm_errata_level.rb +1 -5
  64. data/lib/chef/resource/rhsm_repo.rb +15 -0
  65. data/lib/chef/resource/ssh_known_hosts_entry.rb +4 -7
  66. data/lib/chef/resource/sudo.rb +2 -6
  67. data/lib/chef/resource/swap_file.rb +2 -6
  68. data/lib/chef/resource/sysctl.rb +2 -2
  69. data/lib/chef/resource/timezone.rb +1 -1
  70. data/lib/chef/resource/user_ulimit.rb +2 -2
  71. data/lib/chef/resource/windows_ad_join.rb +2 -2
  72. data/lib/chef/resource/windows_audit_policy.rb +2 -2
  73. data/lib/chef/resource/windows_auto_run.rb +2 -2
  74. data/lib/chef/resource/windows_certificate.rb +1 -1
  75. data/lib/chef/resource/windows_dfs_folder.rb +2 -2
  76. data/lib/chef/resource/windows_dfs_namespace.rb +2 -2
  77. data/lib/chef/resource/windows_dns_record.rb +2 -2
  78. data/lib/chef/resource/windows_dns_zone.rb +2 -2
  79. data/lib/chef/resource/windows_feature.rb +3 -3
  80. data/lib/chef/resource/windows_feature_dism.rb +3 -5
  81. data/lib/chef/resource/windows_feature_powershell.rb +3 -3
  82. data/lib/chef/resource/windows_firewall_profile.rb +2 -2
  83. data/lib/chef/resource/windows_firewall_rule.rb +20 -6
  84. data/lib/chef/resource/windows_font.rb +1 -1
  85. data/lib/chef/resource/windows_pagefile.rb +103 -64
  86. data/lib/chef/resource/windows_path.rb +2 -2
  87. data/lib/chef/resource/windows_printer.rb +5 -20
  88. data/lib/chef/resource/windows_printer_port.rb +48 -65
  89. data/lib/chef/resource/windows_security_policy.rb +2 -2
  90. data/lib/chef/resource/windows_share.rb +2 -2
  91. data/lib/chef/resource/windows_shortcut.rb +1 -1
  92. data/lib/chef/resource/windows_task.rb +1 -1
  93. data/lib/chef/resource/windows_uac.rb +3 -5
  94. data/lib/chef/resource/windows_user_privilege.rb +2 -2
  95. data/lib/chef/resource/windows_workgroup.rb +2 -2
  96. data/lib/chef/resource/yum_package.rb +10 -10
  97. data/lib/chef/resource/zypper_package.rb +4 -4
  98. data/lib/chef/resource/zypper_repository.rb +28 -8
  99. data/lib/chef/resource_reporter.rb +0 -1
  100. data/lib/chef/version.rb +1 -1
  101. data/spec/functional/resource/windows_hostname_spec.rb +91 -0
  102. data/spec/functional/resource/windows_pagefile_spec.rb +98 -0
  103. data/spec/unit/cookbook_version_spec.rb +52 -0
  104. data/spec/unit/data_bag_item_spec.rb +2 -2
  105. data/spec/unit/data_bag_spec.rb +1 -1
  106. data/spec/unit/data_collector_spec.rb +47 -1
  107. data/spec/unit/handler_spec.rb +8 -2
  108. data/spec/unit/provider/zypper_repository_spec.rb +3 -10
  109. data/spec/unit/resource/windows_firewall_rule_spec.rb +12 -7
  110. data/spec/unit/resource/windows_pagefile_spec.rb +4 -9
  111. data/spec/unit/resource/zypper_repository_spec.rb +1 -1
  112. metadata +9 -6
@@ -97,7 +97,7 @@ class Chef
97
97
  property :sensitive, [TrueClass, FalseClass],
98
98
  default: true, desired_state: false
99
99
 
100
- action :join, description: "Join the Active Directory domain" do
100
+ action :join, description: "Join the Active Directory domain." do
101
101
  unless on_desired_domain?
102
102
  cmd = "$pswd = ConvertTo-SecureString \'#{new_resource.domain_password}\' -AsPlainText -Force;"
103
103
  cmd << "$credential = New-Object System.Management.Automation.PSCredential (\"#{sanitize_usename}\",$pswd);"
@@ -127,7 +127,7 @@ class Chef
127
127
  end
128
128
  end
129
129
 
130
- action :leave, description: "Leave an Active Directory domain and re-join a workgroup" do
130
+ action :leave, description: "Leave an Active Directory domain and re-join a workgroup." do
131
131
  if joined_to_domain?
132
132
  cmd = ""
133
133
  cmd << "$pswd = ConvertTo-SecureString \'#{new_resource.domain_password}\' -AsPlainText -Force;"
@@ -106,7 +106,7 @@ class Chef
106
106
 
107
107
  ```ruby
108
108
  windows_audit_policy "Set Audit Policy for 'Credential Validation' actions to 'Success'" do
109
- subcategory 'Credential Validation'
109
+ subcategory 'Credential Validation'
110
110
  success true
111
111
  failure false
112
112
  action :set
@@ -152,7 +152,7 @@ class Chef
152
152
  property :audit_base_directories, [true, false],
153
153
  description: "Setting this audit policy option to true will force the system to assign a System Access Control List to named objects to enable auditing of container objects such as directories."
154
154
 
155
- action :set, description: "Configure an audit policy" do
155
+ action :set, description: "Configure an audit policy." do
156
156
  unless new_resource.subcategory.nil?
157
157
  new_resource.subcategory.each do |subcategory|
158
158
  next if subcategory_configured?(subcategory, new_resource.success, new_resource.failure)
@@ -57,7 +57,7 @@ class Chef
57
57
 
58
58
  alias_method :program, :path
59
59
 
60
- action :create, description: "Create an item to be run at login" do
60
+ action :create, description: "Create an item to be run at login." do
61
61
 
62
62
  data = "\"#{new_resource.path}\""
63
63
  data << " #{new_resource.args}" if new_resource.args
@@ -72,7 +72,7 @@ class Chef
72
72
  end
73
73
  end
74
74
 
75
- action :remove, description: "Remove an item that was previously configured to run at login" do
75
+ action :remove, description: "Remove an item that was previously configured to run at login." do
76
76
  registry_key registry_path do
77
77
  values [{
78
78
  name: new_resource.program_name,
@@ -160,7 +160,7 @@ class Chef
160
160
  end
161
161
  end
162
162
 
163
- action :verify, description: "Verifies a certificate and logs the result" do
163
+ action :verify, description: "Verifies a certificate and logs the result." do
164
164
  out = verify_cert
165
165
  if !!out == out
166
166
  out = out ? "Certificate is valid" : "Certificate not valid"
@@ -42,7 +42,7 @@ class Chef
42
42
  property :description, String,
43
43
  description: "Description for the share."
44
44
 
45
- action :create, description: "Creates the folder in dfs namespace" do
45
+ action :create, description: "Creates the folder in dfs namespace." do
46
46
  raise "target_path is required for install" unless property_is_set?(:target_path)
47
47
  raise "description is required for install" unless property_is_set?(:description)
48
48
 
@@ -60,7 +60,7 @@ class Chef
60
60
  end
61
61
  end
62
62
 
63
- action :delete, description: "Deletes the folder in the dfs namespace" do
63
+ action :delete, description: "Deletes the folder in the dfs namespace." do
64
64
  powershell_script "Delete DFS Namespace" do
65
65
  code <<-EOH
66
66
  Remove-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -Force
@@ -52,7 +52,7 @@ class Chef
52
52
  description: "The root from which to create the DFS tree. Defaults to C:\\DFSRoots.",
53
53
  default: 'C:\\DFSRoots'
54
54
 
55
- action :create, description: "Creates the dfs namespace on the server" do
55
+ action :create, description: "Creates the dfs namespace on the server." do
56
56
  directory file_path do
57
57
  action :create
58
58
  recursive true
@@ -82,7 +82,7 @@ class Chef
82
82
  end
83
83
  end
84
84
 
85
- action :delete, description: "Deletes a DFS Namespace including the directory on disk" do
85
+ action :delete, description: "Deletes a DFS Namespace including the directory on disk." do
86
86
  powershell_script "Delete DFS Namespace" do
87
87
  code <<-EOH
88
88
  Remove-DfsnRoot -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}' -Force
@@ -49,7 +49,7 @@ class Chef
49
49
  default: "localhost",
50
50
  introduced: "16.3"
51
51
 
52
- action :create, description: "Creates and updates the DNS entry" do
52
+ action :create, description: "Creates and updates the DNS entry." do
53
53
  windows_feature "RSAT-DNS-Server" do
54
54
  not_if new_resource.dns_server.casecmp?("localhost")
55
55
  end
@@ -59,7 +59,7 @@ class Chef
59
59
  run_dsc_resource "Present"
60
60
  end
61
61
 
62
- action :delete, description: "Deletes a DNS entry" do
62
+ action :delete, description: "Deletes a DNS entry." do
63
63
  windows_feature "RSAT-DNS-Server" do
64
64
  not_if new_resource.dns_server.casecmp?("localhost")
65
65
  end
@@ -40,13 +40,13 @@ class Chef
40
40
  description: "The type of DNS server, Domain or Standalone.",
41
41
  default: "Domain", equal_to: %w{Domain Standalone}
42
42
 
43
- action :create, description: "Creates and updates a DNS Zone" do
43
+ action :create, description: "Creates and updates a DNS Zone." do
44
44
  powershell_package "xDnsServer"
45
45
 
46
46
  run_dsc_resource "Present"
47
47
  end
48
48
 
49
- action :delete, description: "Deletes a DNS Zone" do
49
+ action :delete, description: "Deletes a DNS Zone." do
50
50
  powershell_package "xDnsServer"
51
51
 
52
52
  run_dsc_resource "Absent"
@@ -108,15 +108,15 @@ class Chef
108
108
  default: 600,
109
109
  desired_state: false
110
110
 
111
- action :install, description: "Install a Windows role / feature" do
111
+ action :install, description: "Install a Windows role or feature." do
112
112
  run_default_subresource :install
113
113
  end
114
114
 
115
- action :remove, description: "Remove a Windows role / feature" do
115
+ action :remove, description: "Remove a Windows role or feature." do
116
116
  run_default_subresource :remove
117
117
  end
118
118
 
119
- action :delete, description: "Remove a Windows role/feature from the image" do
119
+ action :delete, description: "Remove a Windows role or feature from the image." do
120
120
  run_default_subresource :delete
121
121
  end
122
122
 
@@ -65,9 +65,7 @@ class Chef
65
65
  x.map(&:downcase)
66
66
  end
67
67
 
68
- action :install do
69
- description "Install a Windows role/feature using DISM"
70
-
68
+ action :install, description: "Install a Windows role/feature using DISM." do
71
69
  reload_cached_dism_data unless node["dism_features_cache"]
72
70
  fail_if_unavailable # fail if the features don't exist
73
71
 
@@ -91,7 +89,7 @@ class Chef
91
89
  end
92
90
  end
93
91
 
94
- action :remove, description: "Remove a Windows role / feature using DISM" do
92
+ action :remove, description: "Remove a Windows role or feature using DISM." do
95
93
  reload_cached_dism_data unless node["dism_features_cache"]
96
94
 
97
95
  logger.trace("Windows features needing removal: #{features_to_remove.empty? ? "none" : features_to_remove.join(",")}")
@@ -106,7 +104,7 @@ class Chef
106
104
  end
107
105
  end
108
106
 
109
- action :delete, description: "Remove a Windows role / feature from the image using DISM" do
107
+ action :delete, description: "Remove a Windows role or feature from the image using DISM." do
110
108
  reload_cached_dism_data unless node["dism_features_cache"]
111
109
 
112
110
  fail_if_unavailable # fail if the features don't exist
@@ -87,7 +87,7 @@ class Chef
87
87
  x.map(&:downcase)
88
88
  end
89
89
 
90
- action :install, description: "Install a Windows role / feature using PowerShell" do
90
+ action :install, description: "Install a Windows role or feature using PowerShell." do
91
91
  reload_cached_powershell_data unless node["powershell_features_cache"]
92
92
  fail_if_unavailable # fail if the features don't exist
93
93
  fail_if_removed # fail if the features are in removed state
@@ -108,7 +108,7 @@ class Chef
108
108
  end
109
109
  end
110
110
 
111
- action :remove, description: "Remove a Windows role / feature using PowerShell" do
111
+ action :remove, description: "Remove a Windows role or feature using PowerShell." do
112
112
  reload_cached_powershell_data unless node["powershell_features_cache"]
113
113
 
114
114
  Chef::Log.debug("Windows features needing removal: #{features_to_remove.empty? ? "none" : features_to_remove.join(",")}")
@@ -123,7 +123,7 @@ class Chef
123
123
  end
124
124
  end
125
125
 
126
- action :delete, description: "Delete a Windows role / feature from the image using PowerShell" do
126
+ action :delete, description: "Delete a Windows role or feature from the image using PowerShell." do
127
127
  reload_cached_powershell_data unless node["powershell_features_cache"]
128
128
 
129
129
  fail_if_unavailable # fail if the features don't exist
@@ -121,7 +121,7 @@ class Chef
121
121
  end
122
122
  end
123
123
 
124
- action :enable, description: "Enable and optionally configure a Windows Firewall profile" do
124
+ action :enable, description: "Enable and optionally configure a Windows Firewall profile." do
125
125
  converge_if_changed :default_inbound_action, :default_outbound_action, :allow_inbound_rules, :allow_local_firewall_rules,
126
126
  :allow_local_ipsec_rules, :allow_user_apps, :allow_user_ports, :allow_unicast_response, :display_notification do
127
127
  fw_cmd = firewall_command(new_resource.profile)
@@ -135,7 +135,7 @@ class Chef
135
135
  end
136
136
  end
137
137
 
138
- action :disable, description: "Disable a Windows Firewall profile" do
138
+ action :disable, description: "Disable a Windows Firewall profile." do
139
139
  if firewall_enabled?(new_resource.profile)
140
140
  converge_by "Disable the #{new_resource.profile} Firewall Profile" do
141
141
  cmd = "Set-NetFirewallProfile -Profile #{new_resource.profile} -Enabled \"False\""
@@ -39,6 +39,19 @@ class Chef
39
39
  end
40
40
  ```
41
41
 
42
+ **Configuring multiple remote-address ports on a rule**:
43
+
44
+ ```ruby
45
+ windows_firewall_rule 'MyRule' do
46
+ description 'Testing out remote address arrays'
47
+ enabled false
48
+ local_port 1434
49
+ remote_address %w(10.17.3.101 172.7.7.53)
50
+ protocol 'TCP'
51
+ action :create
52
+ end
53
+ ```
54
+
42
55
  **Allow protocol ICMPv6 with ICMP Type**:
43
56
 
44
57
  ```ruby
@@ -97,8 +110,9 @@ class Chef
97
110
  coerce: proc { |d| d.is_a?(String) ? d.split(/\s*,\s*/).sort : Array(d).sort.map(&:to_s) },
98
111
  description: "The local port the firewall rule applies to."
99
112
 
100
- property :remote_address, String,
101
- description: "The remote address the firewall rule applies to."
113
+ property :remote_address, [String, Array],
114
+ coerce: proc { |d| d.is_a?(String) ? d.split(/\s*,\s*/).sort : Array(d).sort.map(&:to_s) },
115
+ description: "The remote address(es) the firewall rule applies to."
102
116
 
103
117
  property :remote_port, [String, Integer, Array],
104
118
  # split various formats of comma separated lists and provide a sorted array of strings to match PS output
@@ -172,7 +186,7 @@ class Chef
172
186
  group state["group"]
173
187
  local_address state["local_address"]
174
188
  local_port Array(state["local_port"]).sort
175
- remote_address state["remote_address"]
189
+ remote_address Array(state["remote_address"]).sort
176
190
  remote_port Array(state["remote_port"]).sort
177
191
  direction state["direction"]
178
192
  protocol state["protocol"]
@@ -185,7 +199,7 @@ class Chef
185
199
  enabled state["enabled"]
186
200
  end
187
201
 
188
- action :create, description: "Create a Windows firewall entry" do
202
+ action :create, description: "Create a Windows firewall entry." do
189
203
  if current_resource
190
204
  converge_if_changed :rule_name, :description, :displayname, :local_address, :local_port, :remote_address,
191
205
  :remote_port, :direction, :protocol, :icmp_type, :firewall_action, :profile, :program, :service,
@@ -206,7 +220,7 @@ class Chef
206
220
  end
207
221
  end
208
222
 
209
- action :delete, description: "Delete an existing Windows firewall entry" do
223
+ action :delete, description: "Delete an existing Windows firewall entry." do
210
224
  if current_resource
211
225
  converge_by("delete firewall rule #{new_resource.rule_name}") do
212
226
  powershell_exec!("Remove-NetFirewallRule -Name '#{new_resource.rule_name}'")
@@ -227,7 +241,7 @@ class Chef
227
241
  cmd << " -Description '#{new_resource.description}'" if new_resource.description
228
242
  cmd << " -LocalAddress '#{new_resource.local_address}'" if new_resource.local_address
229
243
  cmd << " -LocalPort '#{new_resource.local_port.join("', '")}'" if new_resource.local_port
230
- cmd << " -RemoteAddress '#{new_resource.remote_address}'" if new_resource.remote_address
244
+ cmd << " -RemoteAddress '#{new_resource.remote_address.join("', '")}'" if new_resource.remote_address
231
245
  cmd << " -RemotePort '#{new_resource.remote_port.join("', '")}'" if new_resource.remote_port
232
246
  cmd << " -Direction '#{new_resource.direction}'" if new_resource.direction
233
247
  cmd << " -Protocol '#{new_resource.protocol}'" if new_resource.protocol
@@ -45,7 +45,7 @@ class Chef
45
45
  description: "A local filesystem path or URI that is used to source the font file.",
46
46
  coerce: proc { |x| /^.:.*/.match?(x) ? x.tr("\\", "/").gsub("//", "/") : x }
47
47
 
48
- action :install, description: "Install a font to the system fonts directory" do
48
+ action :install, description: "Install a font to the system fonts directory." do
49
49
  if font_exists?
50
50
  logger.debug("Not installing font: #{new_resource.font_name} as font already installed.")
51
51
  else
@@ -39,16 +39,26 @@ class Chef
39
39
 
40
40
  ```ruby
41
41
  windows_pagefile 'Delete the pagefile' do
42
- path 'C:\pagefile.sys'
42
+ path 'C'
43
43
  action :delete
44
44
  end
45
45
  ```
46
46
 
47
+ **Switch to system managed pagefiles**:
48
+
49
+ ```ruby
50
+ windows_pagefile 'Change the pagefile to System Managed' do
51
+ path 'E:\'
52
+ system_managed true
53
+ action :set
54
+ end
55
+ ```
56
+
47
57
  **Create a pagefile with an initial and maximum size**:
48
58
 
49
59
  ```ruby
50
- windows_pagefile 'create the pagefile' do
51
- path 'C:\pagefile.sys'
60
+ windows_pagefile 'create the pagefile with these sizes' do
61
+ path 'f:\'
52
62
  initial_size 100
53
63
  maximum_size 200
54
64
  end
@@ -64,8 +74,7 @@ class Chef
64
74
  description: "Configures whether the system manages the pagefile size."
65
75
 
66
76
  property :automatic_managed, [TrueClass, FalseClass],
67
- description: "Enable automatic management of pagefile initial and maximum size. Setting this to true ignores `initial_size` and `maximum_size` properties.",
68
- default: false
77
+ description: "Enable automatic management of pagefile initial and maximum size. Setting this to true ignores `initial_size` and `maximum_size` properties."
69
78
 
70
79
  property :initial_size, Integer,
71
80
  description: "Initial size of the pagefile in megabytes."
@@ -73,23 +82,26 @@ class Chef
73
82
  property :maximum_size, Integer,
74
83
  description: "Maximum size of the pagefile in megabytes."
75
84
 
76
- action :set, description: "Configures the default pagefile, creating if it doesn't exist" do
77
- pagefile = new_resource.path
78
- initial_size = new_resource.initial_size
79
- maximum_size = new_resource.maximum_size
80
- system_managed = new_resource.system_managed
85
+ action :set, description: "Configures the default pagefile, creating if it doesn't exist." do
81
86
  automatic_managed = new_resource.automatic_managed
82
87
 
83
88
  if automatic_managed
84
89
  set_automatic_managed unless automatic_managed?
85
- else
90
+ elsif automatic_managed == false
86
91
  unset_automatic_managed if automatic_managed?
92
+ else
93
+ pagefile = clarify_pagefile_name
94
+ initial_size = new_resource.initial_size
95
+ maximum_size = new_resource.maximum_size
96
+ system_managed = new_resource.system_managed
87
97
 
88
- # Check that the resource is not just trying to unset automatic managed, if it is do nothing more
89
- if (initial_size && maximum_size) || system_managed
90
- validate_name
91
- create(pagefile) unless exists?(pagefile)
98
+ # the method below is designed to raise an exception if the drive you are trying to create a pagefile for doesn't exist.
99
+ # PowerShell will happily let you create a pagefile called h:\pagefile.sys even though you don't have an H:\ drive.
100
+
101
+ pagefile_drive_exist?(pagefile)
102
+ create(pagefile) unless exists?(pagefile)
92
103
 
104
+ if (initial_size && maximum_size) || system_managed
93
105
  if system_managed
94
106
  set_system_managed(pagefile) unless max_and_min_set?(pagefile, 0, 0)
95
107
  else
@@ -101,21 +113,33 @@ class Chef
101
113
  end
102
114
  end
103
115
 
104
- action :delete, description: "Deletes the specified pagefile" do
105
- validate_name
106
- delete(new_resource.path) if exists?(new_resource.path)
116
+ action :delete, description: "Deletes the specified pagefile." do
117
+ pagefile = clarify_pagefile_name
118
+ delete(pagefile) if exists?(pagefile)
107
119
  end
108
120
 
109
121
  action_class do
110
122
  private
111
123
 
112
- # make sure the provided name property matches the appropriate format
113
- # we do this here and not in the property itself because if automatic_managed
114
- # is set then this validation is not necessary / doesn't make sense at all
115
- def validate_name
116
- return if /^.:.*.sys/.match?(new_resource.path)
124
+ # We are adding support for a number of possibilities for how users will express the drive and location they want the pagefile written to.
125
+ def clarify_pagefile_name
126
+ case new_resource.path
127
+ # user enters C, C:, C:\, C:\\
128
+ when /^[a-zA-Z]/
129
+ new_resource.path[0] + ":\\pagefile.sys"
130
+ # user enters C:\pagefile.sys OR c:\foo\bar\pagefile.sys as the path
131
+ when /^[a-zA-Z]:.*.sys/
132
+ new_resource.path
133
+ else
134
+ raise "#{new_resource.path} does not match the format DRIVE:\\path\\pagefile.sys for pagefiles. Example: C:\\pagefile.sys"
135
+ end
136
+ end
117
137
 
118
- raise "#{new_resource.path} does not match the format DRIVE:\\path\\file.sys for pagefiles. Example: C:\\pagefile.sys"
138
+ # raise an exception if the target drive location is invalid
139
+ def pagefile_drive_exist?(pagefile)
140
+ if ::Dir.exist?(pagefile[0] + ":\\") == false
141
+ raise "You are trying to create a pagefile on a drive that does not exist!"
142
+ end
119
143
  end
120
144
 
121
145
  # See if the pagefile exists
@@ -124,9 +148,11 @@ class Chef
124
148
  # @return [Boolean]
125
149
  def exists?(pagefile)
126
150
  @exists ||= begin
127
- logger.trace("Checking if #{pagefile} exists by running: wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list")
128
- cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list", returns: [0])
129
- cmd.stderr.empty? && (cmd.stdout =~ /SettingID=#{get_setting_id(pagefile)}/i)
151
+ logger.trace("Checking if #{pagefile} exists by running: Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($pagefile)} ")
152
+ cmd = "$page_file_name = '#{pagefile}';"
153
+ cmd << "$pagefile = Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($page_file_name)};"
154
+ cmd << "if ([string]::IsNullOrEmpty($pagefile)) { return $false } else { return $true }"
155
+ powershell_exec!(cmd).result
130
156
  end
131
157
  end
132
158
 
@@ -137,11 +163,14 @@ class Chef
137
163
  # @param [String] max the minimum size of the pagefile
138
164
  # @return [Boolean]
139
165
  def max_and_min_set?(pagefile, min, max)
140
- @max_and_min_set ||= begin
141
- logger.trace("Checking if #{pagefile} min: #{min} and max #{max} are set")
142
- cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" list /format:list", returns: [0])
143
- cmd.stderr.empty? && (cmd.stdout =~ /InitialSize=#{min}/i) && (cmd.stdout =~ /MaximumSize=#{max}/i)
144
- end
166
+ logger.trace("Checking if #{pagefile} has max and initial disk size values set")
167
+ cmd = "$page_file = '#{pagefile}';"
168
+ cmd << "$driveLetter = $page_file.split(':')[0];"
169
+ cmd << "$page_file_settings = Get-CimInstance -ClassName Win32_PageFileSetting -Filter \"SettingID='pagefile.sys @ $($driveLetter):'\" -Property * -ErrorAction Stop;"
170
+ cmd << "if ($page_file_settings.InitialSize -eq #{min} -and $page_file_settings.MaximumSize -eq #{max})"
171
+ cmd << "{ return $true }"
172
+ cmd << "else { return $false }"
173
+ powershell_exec!(cmd).result
145
174
  end
146
175
 
147
176
  # create a pagefile
@@ -149,9 +178,10 @@ class Chef
149
178
  # @param [String] pagefile path to the pagefile
150
179
  def create(pagefile)
151
180
  converge_by("create pagefile #{pagefile}") do
152
- logger.trace("Running wmic.exe pagefileset create name=\"#{pagefile}\"")
153
- cmd = shell_out("wmic.exe pagefileset create name=\"#{pagefile}\"")
154
- check_for_errors(cmd.stderr)
181
+ logger.trace("Running New-CimInstance -ClassName Win32_PageFileSetting to create new pagefile : #{pagefile}")
182
+ powershell_exec! <<~ELM
183
+ New-CimInstance -ClassName Win32_PageFileSetting -Property @{Name = "#{pagefile}"}
184
+ ELM
155
185
  end
156
186
  end
157
187
 
@@ -160,9 +190,13 @@ class Chef
160
190
  # @param [String] pagefile path to the pagefile
161
191
  def delete(pagefile)
162
192
  converge_by("remove pagefile #{pagefile}") do
163
- logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" delete")
164
- cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" delete")
165
- check_for_errors(cmd.stderr)
193
+ logger.trace("Running Remove-CimInstance for pagefile : #{pagefile}")
194
+ powershell_exec! <<~EOL
195
+ $page_file = "#{pagefile}"
196
+ $driveLetter = $page_file.split(':')[0]
197
+ $PageFile = (Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop)
198
+ $null = ($PageFile | Remove-CimInstance -ErrorAction SilentlyContinue)
199
+ EOL
166
200
  end
167
201
  end
168
202
 
@@ -172,26 +206,31 @@ class Chef
172
206
  def automatic_managed?
173
207
  @automatic_managed ||= begin
174
208
  logger.trace("Checking if pagefiles are automatically managed")
175
- cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" get AutomaticManagedPagefile /format:list")
176
- cmd.stderr.empty? && (cmd.stdout =~ /AutomaticManagedPagefile=TRUE/i)
209
+ cmd = "$sys = Get-CimInstance Win32_ComputerSystem -Property *;"
210
+ cmd << "return $sys.AutomaticManagedPagefile"
211
+ powershell_exec!(cmd).result
177
212
  end
178
213
  end
179
214
 
180
215
  # turn on automatic management of all pagefiles by Windows
181
216
  def set_automatic_managed
182
- converge_by("set pagefile to Automatic Managed") do
183
- logger.trace("Running wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=True")
184
- cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=True")
185
- check_for_errors(cmd.stderr)
217
+ converge_by("Set pagefile to Automatic Managed") do
218
+ logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$true} -PassThru")
219
+ powershell_exec! <<~EOH
220
+ $sys = Get-CimInstance Win32_ComputerSystem -Property *
221
+ Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$true} -PassThru
222
+ EOH
186
223
  end
187
224
  end
188
225
 
189
226
  # turn off automatic management of all pagefiles by Windows
190
227
  def unset_automatic_managed
191
- converge_by("set pagefile to User Managed") do
192
- logger.trace("Running wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=False")
193
- cmd = shell_out("wmic.exe computersystem where name=\"%computername%\" set AutomaticManagedPagefile=False")
194
- check_for_errors(cmd.stderr)
228
+ converge_by("Turn off Automatically Managed on pagefiles") do
229
+ logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru")
230
+ powershell_exec! <<~EOH
231
+ $sys = Get-CimInstance Win32_ComputerSystem -Property *
232
+ Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru
233
+ EOH
195
234
  end
196
235
  end
197
236
 
@@ -202,9 +241,14 @@ class Chef
202
241
  # @param [String] max the minimum size of the pagefile
203
242
  def set_custom_size(pagefile, min, max)
204
243
  converge_by("set #{pagefile} to InitialSize=#{min} & MaximumSize=#{max}") do
205
- logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=#{min},MaximumSize=#{max}")
206
- cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=#{min},MaximumSize=#{max}", returns: [0])
207
- check_for_errors(cmd.stderr)
244
+ logger.trace("Set-CimInstance -Property @{InitialSize = #{min} MaximumSize = #{max}")
245
+ powershell_exec! <<~EOD
246
+ $page_file = "#{pagefile}"
247
+ $driveLetter = $page_file.split(':')[0]
248
+ Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{
249
+ InitialSize = #{min}
250
+ MaximumSize = #{max}}
251
+ EOD
208
252
  end
209
253
  end
210
254
 
@@ -213,21 +257,16 @@ class Chef
213
257
  # @param [String] pagefile path to the pagefile
214
258
  def set_system_managed(pagefile)
215
259
  converge_by("set #{pagefile} to System Managed") do
216
- logger.trace("Running wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=0,MaximumSize=0")
217
- cmd = shell_out("wmic.exe pagefileset where SettingID=\"#{get_setting_id(pagefile)}\" set InitialSize=0,MaximumSize=0", returns: [0])
218
- check_for_errors(cmd.stderr)
260
+ logger.trace("Running ")
261
+ powershell_exec! <<~EOM
262
+ $page_file = "#{pagefile}"
263
+ $driveLetter = $page_file.split(':')[0]
264
+ Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{
265
+ InitialSize = 0
266
+ MaximumSize = 0}
267
+ EOM
219
268
  end
220
269
  end
221
-
222
- def get_setting_id(pagefile)
223
- split_path = pagefile.split("\\")
224
- "#{split_path[1]} @ #{split_path[0]}"
225
- end
226
-
227
- # raise if there's an error on stderr on a shellout
228
- def check_for_errors(stderr)
229
- raise stderr.chomp unless stderr.empty?
230
- end
231
270
  end
232
271
  end
233
272
  end