chef 17.0.242-universal-mingw32 → 17.4.25-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (254) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -14
  3. data/Rakefile +2 -2
  4. data/chef.gemspec +10 -20
  5. data/lib/chef/action_collection.rb +6 -26
  6. data/lib/chef/application/base.rb +15 -0
  7. data/lib/chef/application.rb +4 -2
  8. data/lib/chef/chef_fs/file_pattern.rb +2 -2
  9. data/lib/chef/client.rb +7 -1
  10. data/lib/chef/compliance/default_attributes.rb +5 -3
  11. data/lib/chef/compliance/reporter/automate.rb +1 -1
  12. data/lib/chef/compliance/runner.rb +17 -3
  13. data/lib/chef/cookbook/cookbook_version_loader.rb +3 -3
  14. data/lib/chef/cookbook/gem_installer.rb +5 -1
  15. data/lib/chef/cookbook_version.rb +26 -4
  16. data/lib/chef/data_collector/run_end_message.rb +1 -1
  17. data/lib/chef/data_collector.rb +0 -1
  18. data/lib/chef/deprecated.rb +14 -4
  19. data/lib/chef/dsl/declare_resource.rb +5 -10
  20. data/lib/chef/dsl/render_helpers.rb +44 -0
  21. data/lib/chef/dsl/secret.rb +64 -0
  22. data/lib/chef/dsl/toml.rb +116 -0
  23. data/lib/chef/dsl/universal.rb +5 -0
  24. data/lib/chef/dsl.rb +1 -0
  25. data/lib/chef/event_dispatch/base.rb +2 -1
  26. data/lib/chef/exceptions.rb +23 -0
  27. data/lib/chef/formatters/doc.rb +2 -1
  28. data/lib/chef/formatters/error_mapper.rb +2 -2
  29. data/lib/chef/handler/slow_report.rb +66 -0
  30. data/lib/chef/handler.rb +46 -8
  31. data/lib/chef/json_compat.rb +1 -1
  32. data/lib/chef/node.rb +21 -20
  33. data/lib/chef/policy_builder/policyfile.rb +88 -45
  34. data/lib/chef/provider/directory.rb +6 -6
  35. data/lib/chef/provider/execute.rb +1 -1
  36. data/lib/chef/provider/file.rb +2 -2
  37. data/lib/chef/provider/group/dscl.rb +1 -1
  38. data/lib/chef/provider/launchd.rb +6 -6
  39. data/lib/chef/provider/link.rb +1 -1
  40. data/lib/chef/provider/lwrp_base.rb +1 -1
  41. data/lib/chef/provider/package/dnf/dnf_helper.py +11 -10
  42. data/lib/chef/provider/package/dnf/python_helper.rb +9 -8
  43. data/lib/chef/provider/package/habitat.rb +168 -0
  44. data/lib/chef/provider/package/powershell.rb +5 -0
  45. data/lib/chef/provider/package/yum/python_helper.rb +15 -10
  46. data/lib/chef/provider/package/yum/yum_helper.py +46 -62
  47. data/lib/chef/provider/package/yum.rb +1 -4
  48. data/lib/chef/provider/registry_key.rb +1 -1
  49. data/lib/chef/provider/service/systemd.rb +1 -1
  50. data/lib/chef/provider/subversion.rb +4 -4
  51. data/lib/chef/provider/support/yum_repo.erb +1 -1
  52. data/lib/chef/provider/support/zypper_repo.erb +4 -2
  53. data/lib/chef/provider/systemd_unit.rb +18 -17
  54. data/lib/chef/provider/template/content.rb +1 -1
  55. data/lib/chef/provider/user/mac.rb +3 -3
  56. data/lib/chef/provider/windows_script.rb +1 -1
  57. data/lib/chef/provider/yum_repository.rb +27 -43
  58. data/lib/chef/provider/zypper_repository.rb +30 -34
  59. data/lib/chef/provider.rb +26 -1
  60. data/lib/chef/provider_resolver.rb +8 -2
  61. data/lib/chef/providers.rb +1 -0
  62. data/lib/chef/resource/alternatives.rb +5 -5
  63. data/lib/chef/resource/apt_preference.rb +2 -2
  64. data/lib/chef/resource/apt_repository.rb +2 -2
  65. data/lib/chef/resource/apt_update.rb +4 -4
  66. data/lib/chef/resource/build_essential.rb +1 -1
  67. data/lib/chef/resource/chef_client_config.rb +10 -5
  68. data/lib/chef/resource/chef_client_cron.rb +3 -3
  69. data/lib/chef/resource/chef_client_launchd.rb +3 -3
  70. data/lib/chef/resource/chef_client_scheduled_task.rb +15 -15
  71. data/lib/chef/resource/chef_client_systemd_timer.rb +3 -3
  72. data/lib/chef/resource/chef_client_trusted_certificate.rb +2 -2
  73. data/lib/chef/resource/chef_handler.rb +2 -2
  74. data/lib/chef/resource/chef_sleep.rb +1 -1
  75. data/lib/chef/resource/chef_vault_secret.rb +2 -2
  76. data/lib/chef/resource/chocolatey_feature.rb +2 -2
  77. data/lib/chef/resource/chocolatey_source.rb +1 -1
  78. data/lib/chef/resource/cron/cron_d.rb +4 -6
  79. data/lib/chef/resource/cron_access.rb +1 -1
  80. data/lib/chef/resource/dmg_package.rb +1 -1
  81. data/lib/chef/resource/dsc_resource.rb +1 -1
  82. data/lib/chef/resource/execute.rb +5 -5
  83. data/lib/chef/resource/gem_package.rb +2 -1
  84. data/lib/chef/resource/group.rb +4 -4
  85. data/lib/chef/resource/habitat/_habitat_shared.rb +28 -0
  86. data/lib/chef/resource/habitat/habitat_package.rb +129 -0
  87. data/lib/chef/resource/habitat/habitat_sup.rb +329 -0
  88. data/lib/chef/resource/habitat/habitat_sup_systemd.rb +67 -0
  89. data/lib/chef/resource/habitat/habitat_sup_windows.rb +90 -0
  90. data/lib/chef/resource/habitat_config.rb +107 -0
  91. data/lib/chef/resource/habitat_install.rb +247 -0
  92. data/lib/chef/resource/habitat_service.rb +451 -0
  93. data/lib/chef/resource/habitat_user_toml.rb +92 -0
  94. data/lib/chef/resource/homebrew_cask.rb +18 -7
  95. data/lib/chef/resource/homebrew_package.rb +1 -1
  96. data/lib/chef/resource/homebrew_tap.rb +4 -3
  97. data/lib/chef/resource/homebrew_update.rb +2 -2
  98. data/lib/chef/resource/hostname.rb +49 -7
  99. data/lib/chef/resource/inspec_waiver_file_entry.rb +156 -0
  100. data/lib/chef/resource/kernel_module.rb +6 -6
  101. data/lib/chef/resource/launchd.rb +3 -3
  102. data/lib/chef/resource/locale.rb +1 -1
  103. data/lib/chef/resource/lwrp_base.rb +18 -3
  104. data/lib/chef/resource/macos_userdefaults.rb +2 -2
  105. data/lib/chef/resource/ohai_hint.rb +2 -6
  106. data/lib/chef/resource/openbsd_package.rb +17 -0
  107. data/lib/chef/resource/openssl_dhparam.rb +1 -2
  108. data/lib/chef/resource/openssl_ec_private_key.rb +1 -3
  109. data/lib/chef/resource/openssl_ec_public_key.rb +1 -3
  110. data/lib/chef/resource/openssl_rsa_private_key.rb +1 -3
  111. data/lib/chef/resource/openssl_rsa_public_key.rb +1 -3
  112. data/lib/chef/resource/openssl_x509_certificate.rb +1 -4
  113. data/lib/chef/resource/openssl_x509_crl.rb +1 -3
  114. data/lib/chef/resource/openssl_x509_request.rb +1 -3
  115. data/lib/chef/resource/osx_profile.rb +3 -3
  116. data/lib/chef/resource/plist.rb +1 -1
  117. data/lib/chef/resource/powershell_package_source.rb +2 -4
  118. data/lib/chef/resource/reboot.rb +38 -9
  119. data/lib/chef/resource/remote_directory.rb +2 -2
  120. data/lib/chef/resource/remote_file.rb +3 -3
  121. data/lib/chef/resource/rhsm_errata.rb +0 -2
  122. data/lib/chef/resource/rhsm_errata_level.rb +1 -5
  123. data/lib/chef/resource/rhsm_repo.rb +15 -0
  124. data/lib/chef/resource/rhsm_subscription.rb +5 -5
  125. data/lib/chef/resource/ruby_block.rb +100 -0
  126. data/lib/chef/resource/scm/subversion.rb +1 -1
  127. data/lib/chef/resource/ssh_known_hosts_entry.rb +4 -7
  128. data/lib/chef/resource/sudo.rb +2 -6
  129. data/lib/chef/resource/support/HabService.dll.config.erb +19 -0
  130. data/lib/chef/resource/support/client.erb +8 -1
  131. data/lib/chef/resource/support/sup.toml.erb +179 -0
  132. data/lib/chef/resource/swap_file.rb +2 -6
  133. data/lib/chef/resource/sysctl.rb +2 -2
  134. data/lib/chef/resource/systemd_unit.rb +3 -3
  135. data/lib/chef/resource/timezone.rb +1 -1
  136. data/lib/chef/resource/user_ulimit.rb +2 -2
  137. data/lib/chef/resource/windows_ad_join.rb +2 -2
  138. data/lib/chef/resource/windows_audit_policy.rb +2 -2
  139. data/lib/chef/resource/windows_auto_run.rb +2 -2
  140. data/lib/chef/resource/windows_certificate.rb +1 -1
  141. data/lib/chef/resource/windows_defender.rb +163 -0
  142. data/lib/chef/resource/windows_defender_exclusion.rb +125 -0
  143. data/lib/chef/resource/windows_dfs_folder.rb +2 -2
  144. data/lib/chef/resource/windows_dfs_namespace.rb +2 -2
  145. data/lib/chef/resource/windows_dns_record.rb +2 -2
  146. data/lib/chef/resource/windows_dns_zone.rb +2 -2
  147. data/lib/chef/resource/windows_env.rb +1 -1
  148. data/lib/chef/resource/windows_feature.rb +3 -3
  149. data/lib/chef/resource/windows_feature_dism.rb +3 -5
  150. data/lib/chef/resource/windows_feature_powershell.rb +3 -3
  151. data/lib/chef/resource/windows_firewall_profile.rb +2 -2
  152. data/lib/chef/resource/windows_firewall_rule.rb +20 -6
  153. data/lib/chef/resource/windows_font.rb +2 -2
  154. data/lib/chef/resource/windows_pagefile.rb +104 -65
  155. data/lib/chef/resource/windows_path.rb +4 -4
  156. data/lib/chef/resource/windows_printer.rb +80 -61
  157. data/lib/chef/resource/windows_printer_port.rb +48 -65
  158. data/lib/chef/resource/windows_security_policy.rb +7 -4
  159. data/lib/chef/resource/windows_share.rb +2 -2
  160. data/lib/chef/resource/windows_shortcut.rb +1 -1
  161. data/lib/chef/resource/windows_task.rb +2 -2
  162. data/lib/chef/resource/windows_uac.rb +3 -5
  163. data/lib/chef/resource/windows_update_settings.rb +259 -0
  164. data/lib/chef/resource/windows_user_privilege.rb +2 -2
  165. data/lib/chef/resource/windows_workgroup.rb +2 -2
  166. data/lib/chef/resource/yum_package.rb +11 -15
  167. data/lib/chef/resource/zypper_package.rb +4 -4
  168. data/lib/chef/resource/zypper_repository.rb +28 -8
  169. data/lib/chef/resource.rb +20 -25
  170. data/lib/chef/resource_builder.rb +8 -2
  171. data/lib/chef/resource_inspector.rb +6 -2
  172. data/lib/chef/resource_reporter.rb +0 -1
  173. data/lib/chef/resources.rb +13 -1
  174. data/lib/chef/run_lock.rb +1 -1
  175. data/lib/chef/runner.rb +1 -1
  176. data/lib/chef/secret_fetcher/aws_secrets_manager.rb +65 -0
  177. data/lib/chef/secret_fetcher/azure_key_vault.rb +78 -0
  178. data/lib/chef/secret_fetcher/base.rb +76 -0
  179. data/lib/chef/secret_fetcher/example.rb +46 -0
  180. data/lib/chef/secret_fetcher.rb +55 -0
  181. data/lib/chef/shell/ext.rb +3 -3
  182. data/lib/chef/version.rb +1 -1
  183. data/lib/chef/win32/api.rb +9 -2
  184. data/spec/data/knife-home/.chef/plugins/knife/example_home_subcommand.rb +0 -0
  185. data/spec/data/knife-site-subcommands/plugins/knife/example_subcommand.rb +0 -0
  186. data/spec/data/knife_subcommand/test_explicit_category.rb +7 -0
  187. data/spec/data/knife_subcommand/test_name_mapping.rb +4 -0
  188. data/spec/data/knife_subcommand/test_yourself.rb +21 -0
  189. data/spec/functional/mixin/from_file_spec.rb +1 -1
  190. data/spec/functional/resource/dnf_package_spec.rb +857 -537
  191. data/spec/functional/resource/group_spec.rb +1 -1
  192. data/spec/functional/resource/link_spec.rb +1 -1
  193. data/spec/functional/resource/remote_file_spec.rb +1 -1
  194. data/spec/functional/resource/windows_env_spec.rb +2 -2
  195. data/spec/functional/resource/windows_hostname_spec.rb +91 -0
  196. data/spec/functional/resource/windows_pagefile_spec.rb +98 -0
  197. data/spec/functional/resource/yum_package_spec.rb +495 -428
  198. data/spec/integration/client/client_spec.rb +0 -20
  199. data/spec/integration/compliance/compliance_spec.rb +1 -0
  200. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -1
  201. data/spec/integration/recipes/resource_action_spec.rb +6 -6
  202. data/spec/integration/recipes/unified_mode_spec.rb +70 -0
  203. data/spec/spec_helper.rb +3 -0
  204. data/spec/support/chef_helpers.rb +1 -1
  205. data/spec/support/shared/functional/execute_resource.rb +1 -1
  206. data/spec/support/shared/functional/knife.rb +37 -0
  207. data/spec/support/shared/integration/knife_support.rb +192 -0
  208. data/spec/support/shared/unit/knife_shared.rb +39 -0
  209. data/spec/support/shared/unit/provider/file.rb +3 -9
  210. data/spec/unit/chef_fs/file_system/repository/directory_spec.rb +1 -1
  211. data/spec/unit/compliance/runner_spec.rb +46 -2
  212. data/spec/unit/cookbook_version_spec.rb +52 -0
  213. data/spec/unit/data_collector_spec.rb +47 -1
  214. data/spec/unit/dsl/render_helpers_spec.rb +102 -0
  215. data/spec/unit/dsl/secret_spec.rb +71 -0
  216. data/spec/unit/handler_spec.rb +8 -2
  217. data/spec/unit/policy_builder/dynamic_spec.rb +0 -5
  218. data/spec/unit/policy_builder/policyfile_spec.rb +144 -56
  219. data/spec/unit/provider/apt_update_spec.rb +3 -1
  220. data/spec/unit/provider/link_spec.rb +1 -1
  221. data/spec/unit/provider/mount/aix_spec.rb +1 -1
  222. data/spec/unit/provider/package/dnf/python_helper_spec.rb +1 -0
  223. data/spec/unit/provider/package/powershell_spec.rb +74 -12
  224. data/spec/unit/provider/package/yum/python_helper_spec.rb +1 -0
  225. data/spec/unit/provider/service/systemd_service_spec.rb +2 -2
  226. data/spec/unit/provider/systemd_unit_spec.rb +2 -2
  227. data/spec/unit/provider/zypper_repository_spec.rb +3 -10
  228. data/spec/unit/provider_spec.rb +23 -0
  229. data/spec/unit/resource/homebrew_cask_spec.rb +29 -11
  230. data/spec/unit/resource/inspec_waiver_file_entry_spec.rb +80 -0
  231. data/spec/unit/resource/rhsm_subscription_spec.rb +50 -3
  232. data/spec/unit/resource/systemd_unit_spec.rb +1 -1
  233. data/spec/unit/resource/windows_defender_exclusion_spec.rb +62 -0
  234. data/spec/unit/resource/windows_defender_spec.rb +71 -0
  235. data/spec/unit/resource/windows_firewall_rule_spec.rb +12 -7
  236. data/spec/unit/resource/windows_pagefile_spec.rb +4 -9
  237. data/spec/unit/resource/windows_update_settings_spec.rb +64 -0
  238. data/spec/unit/resource/zypper_repository_spec.rb +1 -1
  239. data/spec/unit/resource_spec.rb +19 -8
  240. data/spec/unit/secret_fetcher/aws_secrets_manager_spec.rb +70 -0
  241. data/spec/unit/secret_fetcher/azure_key_vault_spec.rb +70 -0
  242. data/spec/unit/secret_fetcher_spec.rb +82 -0
  243. data/tasks/rspec.rb +4 -9
  244. metadata +65 -161
  245. data/lib/chef/provider/package/yum/simplejson/LICENSE.txt +0 -79
  246. data/lib/chef/provider/package/yum/simplejson/__init__.py +0 -318
  247. data/lib/chef/provider/package/yum/simplejson/__init__.pyc +0 -0
  248. data/lib/chef/provider/package/yum/simplejson/decoder.py +0 -354
  249. data/lib/chef/provider/package/yum/simplejson/decoder.pyc +0 -0
  250. data/lib/chef/provider/package/yum/simplejson/encoder.py +0 -440
  251. data/lib/chef/provider/package/yum/simplejson/encoder.pyc +0 -0
  252. data/lib/chef/provider/package/yum/simplejson/scanner.py +0 -65
  253. data/lib/chef/provider/package/yum/simplejson/scanner.pyc +0 -0
  254. data/lib/chef/provider/package/yum/simplejson/tool.py +0 -37
@@ -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
@@ -43,9 +43,9 @@ class Chef
43
43
 
44
44
  property :source, String,
45
45
  description: "A local filesystem path or URI that is used to source the font file.",
46
- coerce: proc { |x| /^.:.*/.match?(x) ? x.tr('\\', "/").gsub("//", "/") : x }
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
@@ -56,7 +66,7 @@ class Chef
56
66
  DOC
57
67
 
58
68
  property :path, String,
59
- coerce: proc { |x| x.tr("/", '\\') },
69
+ coerce: proc { |x| x.tr("/", "\\") },
60
70
  description: "An optional property to set the pagefile name if it differs from the resource block's name.",
61
71
  name_property: true
62
72
 
@@ -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
@@ -64,7 +64,7 @@ class Chef
64
64
  end
65
65
  end
66
66
 
67
- action :add, description: "Add an item to the system path" do
67
+ action :add, description: "Add an item to the system path." do
68
68
  # The windows Env provider does not correctly expand variables in
69
69
  # the PATH environment variable. Ruby expects these to be expanded.
70
70
  #
@@ -72,11 +72,11 @@ class Chef
72
72
  env "path" do
73
73
  action :modify
74
74
  delim ::File::PATH_SEPARATOR
75
- value path.tr("/", '\\')
75
+ value path.tr("/", "\\")
76
76
  end
77
77
  end
78
78
 
79
- action :remove, description: "Remove an item from the system path" do
79
+ action :remove, description: "Remove an item from the system path." do
80
80
  # The windows Env provider does not correctly expand variables in
81
81
  # the PATH environment variable. Ruby expects these to be expanded.
82
82
  #
@@ -84,7 +84,7 @@ class Chef
84
84
  env "path" do
85
85
  action :delete
86
86
  delim ::File::PATH_SEPARATOR
87
- value path.tr("/", '\\')
87
+ value path.tr("/", "\\")
88
88
  end
89
89
  end
90
90
  end
@@ -1,6 +1,7 @@
1
1
  #
2
2
  # Author:: Doug Ireton (<doug@1strategy.com>)
3
3
  # Copyright:: 2012-2018, Nordstrom, Inc.
4
+ # Copyright:: Chef Software, Inc.
4
5
  #
5
6
  # Licensed under the Apache License, Version 2.0 (the "License");
6
7
  # you may not use this file except in compliance with the License.
@@ -21,6 +22,10 @@ require_relative "../resource"
21
22
 
22
23
  class Chef
23
24
  class Resource
25
+ # @todo
26
+ # 1. Allow updating the printer properties
27
+ # 2. Fail with a warning if the port can't be found and create_port is false
28
+ # 3. Fail with helpful messaging if the printer driver can't be installed
24
29
  class WindowsPrinter < Chef::Resource
25
30
  unified_mode true
26
31
 
@@ -28,7 +33,7 @@ class Chef
28
33
 
29
34
  provides(:windows_printer) { true }
30
35
 
31
- description "Use the **windows_printer** resource to setup Windows printers. Note that this doesn't currently install a printer driver. You must already have the driver installed on the system."
36
+ description "Use the **windows_printer** resource to setup Windows printers. This resource will automatically install the driver specified in the `driver_name` property and will automatically create a printer port using either the `ipv4_address` property or the `port_name property."
32
37
  introduced "14.0"
33
38
  examples <<~DOC
34
39
  **Create a printer**:
@@ -49,6 +54,23 @@ class Chef
49
54
  action :delete
50
55
  end
51
56
  ```
57
+
58
+ **Create a printer port and a printer that uses that port (new in 17.3)**
59
+
60
+ ```ruby
61
+ windows_printer_port '10.4.64.39' do
62
+ port_name 'My awesome printer port'
63
+ snmp_enabled true
64
+ port_protocol 2
65
+ end
66
+
67
+ windows_printer 'HP LaserJet 5th Floor' do
68
+ driver_name 'HP LaserJet 4100 Series PCL6'
69
+ port_name 'My awesome printer port'
70
+ ipv4_address '10.4.64.38'
71
+ create_port false
72
+ end
73
+ ```
52
74
  DOC
53
75
 
54
76
  property :device_id, String,
@@ -83,82 +105,79 @@ class Chef
83
105
  proc { |v| v.match(Resolv::IPv4::Regex) },
84
106
  }
85
107
 
86
- PRINTERS_REG_KEY = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\\'.freeze unless defined?(PRINTERS_REG_KEY)
108
+ property :create_port, [TrueClass, FalseClass],
109
+ description: "Create a printer port for the printer. Set this to false and specify the `port_name` property if using the `windows_printer_port` resource to create the port instead.",
110
+ introduced: "17.3",
111
+ default: true, desired_state: false
87
112
 
88
- # @todo Set @current_resource printer properties from registry
89
- load_current_value do |new_resource|
90
- name new_resource.name
91
- end
113
+ property :port_name, String,
114
+ description: "The port name.",
115
+ default: lazy { |x| "IP_#{x.ipv4_address}" },
116
+ introduced: "17.3",
117
+ default_description: "The resource block name or the ipv4_address prepended with IP_."
92
118
 
93
- action :create, description: "Create a new printer and printer port, if one doesn't already" do
94
- description "Create a new printer and a printer port if one doesn't already exist."
119
+ load_current_value do |new_resource|
120
+ printer_data = powershell_exec(%Q{Get-WmiObject -Class Win32_Printer -Filter "Name='#{new_resource.device_id}'"}).result
95
121
 
96
- if printer_exists?
97
- Chef::Log.info "#{@new_resource} already exists - nothing to do."
122
+ if printer_data.empty?
123
+ current_value_does_not_exist!
98
124
  else
99
- converge_by("Create #{@new_resource}") do
100
- create_printer
125
+ device_id new_resource.device_id
126
+ comment printer_data["Comment"]
127
+ default printer_data["Default"]
128
+ location printer_data["Location"]
129
+ shared printer_data["Shared"]
130
+ share_name printer_data["ShareName"]
131
+ port_name printer_data["PortName"]
132
+
133
+ driver_data = powershell_exec(%Q{Get-PrinterDriver -Name="#{new_resource.driver_name}"}).result
134
+ unless driver_data.empty?
135
+ driver_name new_resource.driver_name
101
136
  end
102
137
  end
103
138
  end
104
139
 
105
- action :delete, description: "Delete an existing printer. Note that this resource does not delete the associated printer port." do
106
- description "Delete an existing printer. Note this does not delete the associated printer port."
107
-
108
- if printer_exists?
109
- converge_by("Delete #{@new_resource}") do
110
- delete_printer
111
- end
140
+ action :create, description: "Create a new printer and printer port, if one doesn't already." do
141
+ if current_resource
142
+ Chef::Log.info "#{@new_resource} already exists - nothing to do."
112
143
  else
113
- Chef::Log.info "#{@current_resource} doesn't exist - can't delete."
114
- end
115
- end
144
+ # Create the printer port first unless the property is set to false
145
+ if new_resource.create_port
146
+ windows_printer_port new_resource.port_name do
147
+ ipv4_address new_resource.ipv4_address
148
+ port_name new_resource.port_name
149
+ end
150
+ end
116
151
 
117
- action_class do
118
- private
119
-
120
- # does the printer exist
121
- #
122
- # @param [String] name the name of the printer
123
- # @return [Boolean]
124
- def printer_exists?
125
- printer_reg_key = PRINTERS_REG_KEY + new_resource.name
126
- logger.trace "Checking to see if this reg key exists: '#{printer_reg_key}'"
127
- registry_key_exists?(printer_reg_key)
128
- end
152
+ converge_by("install driver #{new_resource.driver_name}") do
153
+ powershell_exec!("Add-PrinterDriver -Name '#{new_resource.driver_name}'")
154
+ end
129
155
 
130
- # creates the printer port and then the printer
131
- def create_printer
132
- # Create the printer port first
133
- windows_printer_port new_resource.ipv4_address
134
-
135
- port_name = "IP_#{new_resource.ipv4_address}"
136
-
137
- declare_resource(:powershell_script, "Creating printer: #{new_resource.device_id}") do
138
- code <<-EOH
139
-
140
- Set-WmiInstance -class Win32_Printer `
141
- -EnableAllPrivileges `
142
- -Argument @{ DeviceID = "#{new_resource.device_id}";
143
- Comment = "#{new_resource.comment}";
144
- Default = "$#{new_resource.default}";
145
- DriverName = "#{new_resource.driver_name}";
146
- Location = "#{new_resource.location}";
147
- PortName = "#{port_name}";
148
- Shared = "$#{new_resource.shared}";
149
- ShareName = "#{new_resource.share_name}";
150
- }
156
+ converge_by("create #{@new_resource.device_id}") do
157
+ powershell_exec! <<-EOH
158
+ Set-WmiInstance -class Win32_Printer `
159
+ -EnableAllPrivileges `
160
+ -Argument @{ DeviceID = "#{new_resource.device_id}";
161
+ Comment = "#{new_resource.comment}";
162
+ Default = "$#{new_resource.default}";
163
+ DriverName = "#{new_resource.driver_name}";
164
+ Location = "#{new_resource.location}";
165
+ PortName = "#{new_resource.port_name}";
166
+ Shared = "$#{new_resource.shared}";
167
+ ShareName = "#{new_resource.share_name}";
168
+ }
151
169
  EOH
152
170
  end
153
171
  end
172
+ end
154
173
 
155
- def delete_printer
156
- declare_resource(:powershell_script, "Deleting printer: #{new_resource.device_id}") do
157
- code <<-EOH
158
- $printer = Get-WMIObject -class Win32_Printer -EnableAllPrivileges -Filter "name = '#{new_resource.device_id}'"
159
- $printer.Delete()
160
- EOH
174
+ action :delete, description: "Delete an existing printer. Note that this resource does not delete the associated printer port." do
175
+ if current_resource
176
+ converge_by("Delete #{new_resource.device_id}") do
177
+ powershell_exec!("Remove-Printer -Name '#{new_resource.device_id}'")
161
178
  end
179
+ else
180
+ Chef::Log.info "#{new_resource.device_id} doesn't exist - can't delete."
162
181
  end
163
182
  end
164
183
  end