chef 16.5.64 → 16.8.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (317) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -14
  3. data/README.md +1 -1
  4. data/Rakefile +21 -14
  5. data/chef-universal-mingw32.gemspec +1 -1
  6. data/chef.gemspec +3 -2
  7. data/lib/chef/application.rb +2 -2
  8. data/lib/chef/application/base.rb +1 -1
  9. data/lib/chef/application/client.rb +7 -2
  10. data/lib/chef/application/knife.rb +1 -1
  11. data/lib/chef/application/solo.rb +1 -1
  12. data/lib/chef/chef_fs/chef_fs_data_store.rb +1 -1
  13. data/lib/chef/chef_fs/data_handler/cookbook_data_handler.rb +1 -1
  14. data/lib/chef/chef_fs/file_pattern.rb +1 -1
  15. data/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb +1 -1
  16. data/lib/chef/client.rb +6 -32
  17. data/lib/chef/compliance/default_attributes.rb +89 -0
  18. data/lib/chef/compliance/fetcher/automate.rb +69 -0
  19. data/lib/chef/compliance/fetcher/chef_server.rb +134 -0
  20. data/lib/chef/compliance/reporter/automate.rb +202 -0
  21. data/lib/chef/compliance/reporter/chef_server_automate.rb +94 -0
  22. data/lib/chef/compliance/reporter/compliance_enforcer.rb +20 -0
  23. data/lib/chef/compliance/reporter/json_file.rb +19 -0
  24. data/lib/chef/compliance/runner.rb +261 -0
  25. data/lib/chef/cookbook/gem_installer.rb +1 -1
  26. data/lib/chef/cookbook_manifest.rb +2 -1
  27. data/lib/chef/cookbook_site_streaming_uploader.rb +1 -1
  28. data/lib/chef/cookbook_version.rb +2 -5
  29. data/lib/chef/data_collector.rb +1 -1
  30. data/lib/chef/encrypted_data_bag_item/assertions.rb +1 -1
  31. data/lib/chef/environment.rb +1 -1
  32. data/lib/chef/event_loggers/windows_eventlog.rb +1 -1
  33. data/lib/chef/exceptions.rb +5 -1
  34. data/lib/chef/file_access_control/windows.rb +1 -4
  35. data/lib/chef/file_content_management/tempfile.rb +1 -1
  36. data/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +2 -2
  37. data/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +5 -5
  38. data/lib/chef/formatters/indentable_output_stream.rb +2 -2
  39. data/lib/chef/guard_interpreter/resource_guard_interpreter.rb +28 -39
  40. data/lib/chef/http.rb +2 -12
  41. data/lib/chef/http/basic_client.rb +1 -1
  42. data/lib/chef/http/http_request.rb +1 -1
  43. data/lib/chef/http/socketless_chef_zero_client.rb +1 -1
  44. data/lib/chef/http/ssl_policies.rb +6 -0
  45. data/lib/chef/json_compat.rb +2 -7
  46. data/lib/chef/key.rb +1 -1
  47. data/lib/chef/knife/bootstrap.rb +2 -1
  48. data/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb +2 -2
  49. data/lib/chef/knife/bootstrap/train_connector.rb +1 -1
  50. data/lib/chef/knife/config_show.rb +1 -1
  51. data/lib/chef/knife/core/cookbook_scm_repo.rb +1 -1
  52. data/lib/chef/knife/core/gem_glob_loader.rb +1 -1
  53. data/lib/chef/knife/core/ui.rb +5 -2
  54. data/lib/chef/knife/core/windows_bootstrap_context.rb +7 -4
  55. data/lib/chef/knife/node_policy_set.rb +2 -2
  56. data/lib/chef/knife/node_run_list_add.rb +1 -1
  57. data/lib/chef/knife/node_run_list_remove.rb +1 -1
  58. data/lib/chef/knife/node_run_list_set.rb +1 -1
  59. data/lib/chef/knife/role_env_run_list_add.rb +1 -1
  60. data/lib/chef/knife/role_env_run_list_set.rb +1 -1
  61. data/lib/chef/knife/role_run_list_add.rb +1 -1
  62. data/lib/chef/knife/role_run_list_set.rb +1 -1
  63. data/lib/chef/knife/search.rb +0 -1
  64. data/lib/chef/knife/ssh.rb +5 -3
  65. data/lib/chef/knife/tag_create.rb +1 -1
  66. data/lib/chef/knife/tag_delete.rb +1 -1
  67. data/lib/chef/local_mode.rb +1 -1
  68. data/lib/chef/mixin/convert_to_class_name.rb +0 -56
  69. data/lib/chef/mixin/openssl_helper.rb +1 -1
  70. data/lib/chef/mixin/powershell_exec.rb +24 -10
  71. data/lib/chef/mixin/powershell_out.rb +12 -5
  72. data/lib/chef/mixin/properties.rb +2 -0
  73. data/lib/chef/mixin/template.rb +1 -1
  74. data/lib/chef/mixin/unformatter.rb +1 -1
  75. data/lib/chef/mixin/uris.rb +3 -1
  76. data/lib/chef/node/attribute_collections.rb +2 -6
  77. data/lib/chef/node/mixin/immutablize_hash.rb +2 -0
  78. data/lib/chef/node_map.rb +2 -2
  79. data/lib/chef/platform/query_helpers.rb +4 -4
  80. data/lib/chef/policy_builder/dynamic.rb +2 -0
  81. data/lib/chef/powershell.rb +10 -4
  82. data/lib/chef/property.rb +1 -1
  83. data/lib/chef/provider.rb +1 -1
  84. data/lib/chef/provider/cron.rb +2 -13
  85. data/lib/chef/provider/dsc_resource.rb +12 -24
  86. data/lib/chef/provider/dsc_script.rb +16 -20
  87. data/lib/chef/provider/file.rb +1 -1
  88. data/lib/chef/provider/git.rb +5 -5
  89. data/lib/chef/provider/group.rb +14 -6
  90. data/lib/chef/provider/group/windows.rb +12 -1
  91. data/lib/chef/provider/ifconfig.rb +8 -8
  92. data/lib/chef/provider/ifconfig/debian.rb +38 -22
  93. data/lib/chef/provider/ifconfig/redhat.rb +54 -18
  94. data/lib/chef/provider/launchd.rb +1 -11
  95. data/lib/chef/provider/link.rb +0 -9
  96. data/lib/chef/provider/mount.rb +18 -1
  97. data/lib/chef/provider/mount/linux.rb +4 -0
  98. data/lib/chef/provider/mount/mount.rb +41 -43
  99. data/lib/chef/provider/package.rb +3 -0
  100. data/lib/chef/provider/package/apt.rb +1 -1
  101. data/lib/chef/provider/package/chocolatey.rb +6 -6
  102. data/lib/chef/provider/package/dpkg.rb +3 -12
  103. data/lib/chef/provider/package/freebsd/base.rb +3 -2
  104. data/lib/chef/provider/package/freebsd/pkgng.rb +1 -1
  105. data/lib/chef/provider/package/homebrew.rb +1 -1
  106. data/lib/chef/provider/package/ips.rb +1 -1
  107. data/lib/chef/provider/package/powershell.rb +2 -3
  108. data/lib/chef/provider/package/rubygems.rb +1 -1
  109. data/lib/chef/provider/package/snap.rb +1 -3
  110. data/lib/chef/provider/package/solaris.rb +0 -2
  111. data/lib/chef/provider/package/yum/rpm_utils.rb +1 -1
  112. data/lib/chef/provider/package/zypper.rb +98 -71
  113. data/lib/chef/provider/powershell_script.rb +12 -1
  114. data/lib/chef/provider/registry_key.rb +4 -3
  115. data/lib/chef/provider/route.rb +2 -2
  116. data/lib/chef/provider/service/debian.rb +2 -1
  117. data/lib/chef/provider/service/redhat.rb +1 -1
  118. data/lib/chef/provider/user.rb +17 -9
  119. data/lib/chef/provider/user/aix.rb +1 -1
  120. data/lib/chef/provider/user/dscl.rb +2 -2
  121. data/lib/chef/provider/user/mac.rb +14 -6
  122. data/lib/chef/provider/user/solaris.rb +1 -1
  123. data/lib/chef/provider/user/windows.rb +10 -3
  124. data/lib/chef/providers.rb +0 -3
  125. data/lib/chef/pwsh.rb +71 -0
  126. data/lib/chef/resource.rb +1 -1
  127. data/lib/chef/resource/apt_repository.rb +6 -5
  128. data/lib/chef/resource/bash.rb +119 -1
  129. data/lib/chef/resource/batch.rb +1 -1
  130. data/lib/chef/resource/breakpoint.rb +3 -1
  131. data/lib/chef/resource/build_essential.rb +5 -8
  132. data/lib/chef/resource/chef_client_config.rb +313 -0
  133. data/lib/chef/resource/chef_client_cron.rb +5 -5
  134. data/lib/chef/resource/chef_client_scheduled_task.rb +4 -4
  135. data/lib/chef/resource/chef_client_systemd_timer.rb +5 -5
  136. data/lib/chef/resource/chef_handler.rb +1 -0
  137. data/lib/chef/resource/chef_sleep.rb +1 -1
  138. data/lib/chef/resource/cron/_cron_shared.rb +1 -0
  139. data/lib/chef/resource/cron/cron_d.rb +2 -2
  140. data/lib/chef/resource/csh.rb +2 -2
  141. data/lib/chef/resource/dsc_script.rb +8 -1
  142. data/lib/chef/resource/execute.rb +6 -4
  143. data/lib/chef/resource/file.rb +2 -2
  144. data/lib/chef/resource/homebrew_update.rb +4 -1
  145. data/lib/chef/resource/hostname.rb +5 -5
  146. data/lib/chef/resource/ifconfig.rb +52 -5
  147. data/lib/chef/resource/kernel_module.rb +1 -1
  148. data/lib/chef/resource/ksh.rb +3 -3
  149. data/lib/chef/resource/launchd.rb +15 -15
  150. data/lib/chef/resource/lwrp_base.rb +3 -5
  151. data/lib/chef/resource/mount.rb +8 -2
  152. data/lib/chef/resource/perl.rb +2 -2
  153. data/lib/chef/resource/plist.rb +2 -6
  154. data/lib/chef/resource/powershell_package_source.rb +19 -18
  155. data/lib/chef/resource/powershell_script.rb +14 -11
  156. data/lib/chef/resource/python.rb +2 -2
  157. data/lib/chef/resource/registry_key.rb +93 -2
  158. data/lib/chef/resource/route.rb +1 -1
  159. data/lib/chef/resource/ruby.rb +2 -2
  160. data/lib/chef/resource/scm/_scm.rb +2 -1
  161. data/lib/chef/resource/scm/git.rb +82 -1
  162. data/lib/chef/resource/scm/subversion.rb +12 -0
  163. data/lib/chef/resource/script.rb +2 -2
  164. data/lib/chef/resource/solaris_package.rb +0 -2
  165. data/lib/chef/resource/sudo.rb +1 -1
  166. data/lib/chef/resource/support/client.erb +64 -0
  167. data/lib/chef/resource/systemd_unit.rb +42 -1
  168. data/lib/chef/resource/template.rb +2 -2
  169. data/lib/chef/resource/windows_ad_join.rb +9 -9
  170. data/lib/chef/resource/windows_audit_policy.rb +26 -24
  171. data/lib/chef/resource/windows_certificate.rb +13 -7
  172. data/lib/chef/resource/windows_dfs_server.rb +7 -4
  173. data/lib/chef/resource/windows_env.rb +173 -0
  174. data/lib/chef/resource/windows_feature.rb +2 -0
  175. data/lib/chef/resource/windows_firewall_profile.rb +7 -12
  176. data/lib/chef/resource/windows_firewall_rule.rb +9 -11
  177. data/lib/chef/resource/windows_font.rb +1 -1
  178. data/lib/chef/resource/windows_package.rb +1 -0
  179. data/lib/chef/resource/windows_path.rb +38 -0
  180. data/lib/chef/resource/windows_security_policy.rb +5 -5
  181. data/lib/chef/resource/windows_service.rb +108 -0
  182. data/lib/chef/resource/windows_share.rb +18 -18
  183. data/lib/chef/resource/windows_task.rb +629 -28
  184. data/lib/chef/resource/windows_workgroup.rb +6 -4
  185. data/lib/chef/resource/yum_repository.rb +1 -1
  186. data/lib/chef/resource_collection/resource_set.rb +2 -6
  187. data/lib/chef/resource_inspector.rb +77 -75
  188. data/lib/chef/resource_reporter.rb +0 -2
  189. data/lib/chef/resources.rb +1 -0
  190. data/lib/chef/run_lock.rb +2 -2
  191. data/lib/chef/search/query.rb +3 -1
  192. data/lib/chef/server_api.rb +0 -4
  193. data/lib/chef/shell/ext.rb +1 -1
  194. data/lib/chef/util/backup.rb +1 -1
  195. data/lib/chef/util/dsc/configuration_generator.rb +52 -11
  196. data/lib/chef/util/dsc/lcm_output_parser.rb +4 -7
  197. data/lib/chef/util/dsc/local_configuration_manager.rb +18 -15
  198. data/lib/chef/util/dsc/resource_store.rb +5 -11
  199. data/lib/chef/version.rb +1 -1
  200. data/lib/chef/win32/api/file.rb +4 -0
  201. data/lib/chef/win32/file.rb +1 -1
  202. data/lib/chef/win32/security/sid.rb +1 -1
  203. data/lib/chef/win32/unicode.rb +1 -1
  204. data/spec/functional/mixin/powershell_out_spec.rb +11 -3
  205. data/spec/functional/resource/apt_package_spec.rb +4 -6
  206. data/spec/functional/resource/chocolatey_package_spec.rb +3 -3
  207. data/spec/functional/resource/cron_spec.rb +3 -3
  208. data/spec/functional/resource/dsc_script_spec.rb +6 -9
  209. data/spec/functional/resource/mount_spec.rb +10 -2
  210. data/spec/functional/resource/powershell_package_source_spec.rb +107 -0
  211. data/spec/functional/resource/powershell_script_spec.rb +57 -14
  212. data/spec/functional/resource/windows_certificate_spec.rb +10 -6
  213. data/spec/functional/resource/windows_firewall_rule_spec.rb +93 -0
  214. data/spec/functional/resource/windows_package_spec.rb +36 -10
  215. data/spec/functional/resource/windows_share_spec.rb +103 -0
  216. data/spec/functional/resource/windows_task_spec.rb +2 -3
  217. data/spec/functional/resource/zypper_package_spec.rb +11 -0
  218. data/spec/integration/client/client_spec.rb +2 -1
  219. data/spec/integration/compliance/compliance_spec.rb +81 -0
  220. data/spec/integration/knife/client_key_create_spec.rb +1 -1
  221. data/spec/integration/knife/node_create_spec.rb +1 -1
  222. data/spec/integration/knife/node_environment_set_spec.rb +1 -1
  223. data/spec/integration/knife/node_run_list_add_spec.rb +4 -4
  224. data/spec/integration/knife/node_run_list_remove_spec.rb +1 -1
  225. data/spec/integration/knife/node_run_list_set_spec.rb +1 -1
  226. data/spec/integration/knife/node_show_spec.rb +1 -1
  227. data/spec/integration/recipes/notifies_spec.rb +1 -1
  228. data/spec/integration/recipes/provider_choice.rb +2 -2
  229. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -0
  230. data/spec/spec_helper.rb +3 -4
  231. data/spec/support/lib/chef/resource/cat.rb +1 -1
  232. data/spec/support/lib/chef/resource/one_two_three_four.rb +1 -1
  233. data/spec/support/mock/platform.rb +24 -16
  234. data/spec/support/platform_helpers.rb +11 -4
  235. data/spec/support/shared/unit/knife_shared.rb +1 -1
  236. data/spec/support/shared/unit/script_resource.rb +4 -4
  237. data/spec/support/shared/unit/windows_script_resource.rb +1 -1
  238. data/spec/unit/chef_fs/config_spec.rb +1 -1
  239. data/spec/unit/chef_fs/data_handler/data_bag_item_data_handler.rb +1 -1
  240. data/spec/unit/client_spec.rb +17 -0
  241. data/spec/unit/compliance/fetcher/automate_spec.rb +134 -0
  242. data/spec/unit/compliance/fetcher/chef_server_spec.rb +93 -0
  243. data/spec/unit/compliance/reporter/automate_spec.rb +427 -0
  244. data/spec/unit/compliance/reporter/chef_server_automate_spec.rb +177 -0
  245. data/spec/unit/compliance/reporter/compliance_enforcer_spec.rb +48 -0
  246. data/spec/unit/compliance/runner_spec.rb +140 -0
  247. data/spec/unit/data_collector_spec.rb +0 -4
  248. data/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb +11 -11
  249. data/spec/unit/http/ssl_policies_spec.rb +11 -0
  250. data/spec/unit/knife/client_create_spec.rb +2 -2
  251. data/spec/unit/knife/configure_client_spec.rb +5 -5
  252. data/spec/unit/knife/configure_spec.rb +3 -3
  253. data/spec/unit/knife/cookbook_delete_spec.rb +2 -2
  254. data/spec/unit/knife/cookbook_download_spec.rb +2 -2
  255. data/spec/unit/knife/cookbook_list_spec.rb +2 -2
  256. data/spec/unit/knife/cookbook_metadata_spec.rb +3 -3
  257. data/spec/unit/knife/core/node_editor_spec.rb +1 -1
  258. data/spec/unit/knife/environment_compare_spec.rb +3 -3
  259. data/spec/unit/knife/supermarket_download_spec.rb +8 -8
  260. data/spec/unit/knife/supermarket_list_spec.rb +3 -3
  261. data/spec/unit/knife/supermarket_search_spec.rb +1 -1
  262. data/spec/unit/knife/tag_create_spec.rb +1 -1
  263. data/spec/unit/knife/tag_delete_spec.rb +1 -1
  264. data/spec/unit/knife/user_create_spec.rb +1 -1
  265. data/spec/unit/mixin/powershell_exec_spec.rb +41 -4
  266. data/spec/unit/mixin/powershell_out_spec.rb +14 -0
  267. data/spec/unit/mixin/which.rb +1 -1
  268. data/spec/unit/platform/query_helpers_spec.rb +11 -12
  269. data/spec/unit/provider/dsc_resource_spec.rb +10 -27
  270. data/spec/unit/provider/dsc_script_spec.rb +1 -1
  271. data/spec/unit/provider/group/windows_spec.rb +6 -0
  272. data/spec/unit/provider/group_spec.rb +1 -1
  273. data/spec/unit/provider/mount/linux_spec.rb +10 -0
  274. data/spec/unit/provider/mount/mount_spec.rb +21 -10
  275. data/spec/unit/provider/mount/solaris_spec.rb +1 -1
  276. data/spec/unit/provider/mount/windows_spec.rb +1 -0
  277. data/spec/unit/provider/mount_spec.rb +31 -0
  278. data/spec/unit/provider/package/chocolatey_spec.rb +1 -2
  279. data/spec/unit/provider/package/powershell_spec.rb +87 -95
  280. data/spec/unit/provider/package/zypper_spec.rb +0 -25
  281. data/spec/unit/provider/package_spec.rb +2 -2
  282. data/spec/unit/provider/powershell_script_spec.rb +11 -0
  283. data/spec/unit/provider/subversion_spec.rb +0 -3
  284. data/spec/unit/provider/systemd_unit_spec.rb +1 -1
  285. data/spec/unit/provider/user_spec.rb +7 -1
  286. data/spec/unit/provider/windows_env_spec.rb +18 -34
  287. data/spec/unit/provider/windows_path_spec.rb +6 -11
  288. data/spec/unit/provider/windows_task_spec.rb +7 -6
  289. data/spec/unit/resource/breakpoint_spec.rb +1 -1
  290. data/spec/unit/resource/build_essential_spec.rb +0 -12
  291. data/spec/unit/resource/chef_client_config_spec.rb +137 -0
  292. data/spec/unit/resource/ifconfig_spec.rb +2 -10
  293. data/spec/unit/resource/mount_spec.rb +18 -5
  294. data/spec/unit/resource/powershell_package_source_spec.rb +20 -20
  295. data/spec/unit/resource/powershell_script_spec.rb +4 -74
  296. data/spec/unit/resource/service_spec.rb +2 -2
  297. data/spec/unit/resource/solaris_package_spec.rb +8 -10
  298. data/spec/unit/resource/windows_certificate_spec.rb +12 -0
  299. data/spec/unit/resource_inspector_spec.rb +3 -3
  300. data/spec/unit/shell_spec.rb +2 -2
  301. data/spec/unit/util/dsc/configuration_generator_spec.rb +79 -0
  302. data/spec/unit/util/dsc/local_configuration_manager_spec.rb +27 -35
  303. data/tasks/rspec.rb +1 -1
  304. metadata +47 -24
  305. data/lib/chef/monkey_patches/net_http.rb +0 -22
  306. data/lib/chef/provider/windows_env.rb +0 -210
  307. data/lib/chef/provider/windows_path.rb +0 -61
  308. data/lib/chef/provider/windows_task.rb +0 -631
  309. data/lib/chef/util/powershell/cmdlet.rb +0 -175
  310. data/lib/chef/util/powershell/cmdlet_result.rb +0 -61
  311. data/spec/functional/util/powershell/cmdlet_spec.rb +0 -111
  312. data/spec/support/mock/constant.rb +0 -52
  313. data/spec/unit/monkey_patches/uri_spec.rb +0 -34
  314. data/spec/unit/provider_resolver_spec.rb +0 -885
  315. data/spec/unit/resource/data/InstallHistory_with_CLT.plist +0 -92
  316. data/spec/unit/resource/data/InstallHistory_without_CLT.plist +0 -38
  317. data/spec/unit/util/powershell/cmdlet_spec.rb +0 -106
@@ -1,210 +0,0 @@
1
- #
2
- # Author:: Doug MacEachern (<dougm@vmware.com>)
3
- # Copyright:: Copyright 2010-2016, VMware, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require_relative "../provider"
20
- require_relative "../resource/windows_env"
21
- require_relative "../mixin/windows_env_helper"
22
-
23
- class Chef
24
- class Provider
25
- class WindowsEnv < Chef::Provider
26
- include Chef::Mixin::WindowsEnvHelper
27
- attr_accessor :key_exists
28
-
29
- provides :env
30
- provides :windows_env
31
-
32
- def whyrun_supported?
33
- false
34
- end
35
-
36
- def initialize(new_resource, run_context)
37
- super
38
- @key_exists = true
39
- end
40
-
41
- def load_current_resource
42
- @current_resource = Chef::Resource::WindowsEnv.new(new_resource.name)
43
- current_resource.key_name(new_resource.key_name)
44
-
45
- if env_key_exists(new_resource.key_name)
46
- current_resource.value(env_value(new_resource.key_name))
47
- else
48
- @key_exists = false
49
- logger.trace("#{new_resource} key does not exist")
50
- end
51
-
52
- current_resource
53
- end
54
-
55
- def env_key_exists(key_name)
56
- env_value(key_name) ? true : false
57
- end
58
-
59
- # Check to see if value needs any changes
60
- #
61
- # ==== Returns
62
- # <true>:: If a change is required
63
- # <false>:: If a change is not required
64
- def requires_modify_or_create?
65
- if new_resource.delim
66
- # e.g. check for existing value within PATH
67
- new_values.inject(0) do |index, val|
68
- next_index = current_values.find_index val
69
- return true if next_index.nil? || next_index < index
70
-
71
- next_index
72
- end
73
- false
74
- else
75
- new_resource.value != current_resource.value
76
- end
77
- end
78
-
79
- alias_method :compare_value, :requires_modify_or_create?
80
-
81
- action :create do
82
- if @key_exists
83
- if requires_modify_or_create?
84
- modify_env
85
- logger.info("#{new_resource} altered")
86
- new_resource.updated_by_last_action(true)
87
- end
88
- else
89
- create_env
90
- logger.info("#{new_resource} created")
91
- new_resource.updated_by_last_action(true)
92
- end
93
- end
94
-
95
- # e.g. delete a PATH element
96
- #
97
- # ==== Returns
98
- # <true>:: If we handled the element case and caller should not delete the key
99
- # <false>:: Caller should delete the key, either no :delim was specific or value was empty
100
- # after we removed the element.
101
- def delete_element
102
- return false unless new_resource.delim # no delim: delete the key
103
-
104
- needs_delete = new_values.any? { |v| current_values.include?(v) }
105
- if !needs_delete
106
- logger.trace("#{new_resource} element '#{new_resource.value}' does not exist")
107
- true # do not delete the key
108
- else
109
- new_value =
110
- current_values.select do |item|
111
- not new_values.include?(item)
112
- end.join(new_resource.delim)
113
-
114
- if new_value.empty?
115
- false # nothing left here, delete the key
116
- else
117
- old_value = new_resource.value(new_value)
118
- create_env
119
- logger.trace("#{new_resource} deleted #{old_value} element")
120
- new_resource.updated_by_last_action(true)
121
- true # we removed the element and updated; do not delete the key
122
- end
123
- end
124
- end
125
-
126
- action :delete do
127
- if ( ENV[new_resource.key_name] || @key_exists ) && !delete_element
128
- delete_env
129
- logger.info("#{new_resource} deleted")
130
- new_resource.updated_by_last_action(true)
131
- end
132
- end
133
-
134
- action :modify do
135
- if @key_exists
136
- if requires_modify_or_create?
137
- modify_env
138
- logger.info("#{new_resource} modified")
139
- new_resource.updated_by_last_action(true)
140
- end
141
- else
142
- raise Chef::Exceptions::WindowsEnv, "Cannot modify #{new_resource} - key does not exist!"
143
- end
144
- end
145
-
146
- def create_env
147
- obj = env_obj(@new_resource.key_name)
148
- unless obj
149
- obj = WIN32OLE.connect("winmgmts://").get("Win32_Environment").spawninstance_
150
- obj.name = @new_resource.key_name
151
- obj.username = new_resource.user
152
- end
153
- obj.variablevalue = @new_resource.value
154
- obj.put_
155
- value = @new_resource.value
156
- value = expand_path(value) if @new_resource.key_name.casecmp("PATH") == 0
157
- ENV[@new_resource.key_name] = value
158
- broadcast_env_change
159
- end
160
-
161
- def delete_env
162
- obj = env_obj(@new_resource.key_name)
163
- if obj
164
- obj.delete_
165
- broadcast_env_change
166
- end
167
- if ENV[@new_resource.key_name]
168
- ENV.delete(@new_resource.key_name)
169
- end
170
- end
171
-
172
- def modify_env
173
- if new_resource.delim
174
- new_resource.value((new_values + current_values).uniq.join(new_resource.delim))
175
- end
176
- create_env
177
- end
178
-
179
- # Returns the current values to split by delimiter
180
- def current_values
181
- @current_values ||= current_resource.value.split(new_resource.delim)
182
- end
183
-
184
- # Returns the new values to split by delimiter
185
- def new_values
186
- @new_values ||= new_resource.value.split(new_resource.delim)
187
- end
188
-
189
- def env_value(key_name)
190
- obj = env_obj(key_name)
191
- obj.variablevalue if obj
192
- end
193
-
194
- def env_obj(key_name)
195
- return @env_obj if @env_obj
196
-
197
- wmi = WmiLite::Wmi.new
198
- # Note that by design this query is case insensitive with regard to key_name
199
- environment_variables = wmi.query("select * from Win32_Environment where name = '#{key_name}'")
200
- if environment_variables && environment_variables.length > 0
201
- environment_variables.each do |env|
202
- @env_obj = env.wmi_ole_object
203
- return @env_obj if @env_obj.username.split('\\').last.casecmp(new_resource.user) == 0
204
- end
205
- end
206
- @env_obj = nil
207
- end
208
- end
209
- end
210
- end
@@ -1,61 +0,0 @@
1
- #
2
- # Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
3
- # Copyright:: Copyright (c) Chef Software Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require_relative "../mixin/windows_env_helper" if ChefUtils.windows?
20
- require_relative "../mixin/wide_string"
21
- require_relative "../exceptions"
22
-
23
- class Chef
24
- class Provider
25
- class WindowsPath < Chef::Provider
26
- include Chef::Mixin::WindowsEnvHelper if ChefUtils.windows?
27
-
28
- provides :windows_path
29
-
30
- def load_current_resource
31
- @current_resource = Chef::Resource::WindowsPath.new(new_resource.name)
32
- @current_resource.path(new_resource.path)
33
- @current_resource
34
- end
35
-
36
- action :add do
37
- # The windows Env provider does not correctly expand variables in
38
- # the PATH environment variable. Ruby expects these to be expanded.
39
- #
40
- path = expand_path(new_resource.path)
41
- declare_resource(:env, "path") do
42
- action :modify
43
- delim ::File::PATH_SEPARATOR
44
- value path.tr("/", '\\')
45
- end
46
- end
47
-
48
- action :remove do
49
- # The windows Env provider does not correctly expand variables in
50
- # the PATH environment variable. Ruby expects these to be expanded.
51
- #
52
- path = expand_path(new_resource.path)
53
- declare_resource(:env, "path") do
54
- action :delete
55
- delim ::File::PATH_SEPARATOR
56
- value path.tr("/", '\\')
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,631 +0,0 @@
1
- #
2
- # Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
3
- # Copyright:: Copyright (c) Chef Software Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- autoload :ISO8601, "iso8601" if ChefUtils.windows?
20
- require_relative "../provider"
21
- require_relative "../util/path_helper"
22
- require "win32/taskscheduler" if ChefUtils.windows?
23
-
24
- class Chef
25
- class Provider
26
- class WindowsTask < Chef::Provider
27
- if ChefUtils.windows?
28
- include Win32
29
-
30
- provides :windows_task
31
-
32
- MONTHS = {
33
- JAN: TaskScheduler::JANUARY,
34
- FEB: TaskScheduler::FEBRUARY,
35
- MAR: TaskScheduler::MARCH,
36
- APR: TaskScheduler::APRIL,
37
- MAY: TaskScheduler::MAY,
38
- JUN: TaskScheduler::JUNE,
39
- JUL: TaskScheduler::JULY,
40
- AUG: TaskScheduler::AUGUST,
41
- SEP: TaskScheduler::SEPTEMBER,
42
- OCT: TaskScheduler::OCTOBER,
43
- NOV: TaskScheduler::NOVEMBER,
44
- DEC: TaskScheduler::DECEMBER,
45
- }.freeze
46
-
47
- DAYS_OF_WEEK = { MON: TaskScheduler::MONDAY,
48
- TUE: TaskScheduler::TUESDAY,
49
- WED: TaskScheduler::WEDNESDAY,
50
- THU: TaskScheduler::THURSDAY,
51
- FRI: TaskScheduler::FRIDAY,
52
- SAT: TaskScheduler::SATURDAY,
53
- SUN: TaskScheduler::SUNDAY }.freeze
54
-
55
- WEEKS_OF_MONTH = {
56
- FIRST: TaskScheduler::FIRST_WEEK,
57
- SECOND: TaskScheduler::SECOND_WEEK,
58
- THIRD: TaskScheduler::THIRD_WEEK,
59
- FOURTH: TaskScheduler::FOURTH_WEEK,
60
- }.freeze
61
-
62
- DAYS_OF_MONTH = {
63
- 1 => TaskScheduler::TASK_FIRST,
64
- 2 => TaskScheduler::TASK_SECOND,
65
- 3 => TaskScheduler::TASK_THIRD,
66
- 4 => TaskScheduler::TASK_FOURTH,
67
- 5 => TaskScheduler::TASK_FIFTH,
68
- 6 => TaskScheduler::TASK_SIXTH,
69
- 7 => TaskScheduler::TASK_SEVENTH,
70
- 8 => TaskScheduler::TASK_EIGHTH,
71
- # cspell:disable-next-line
72
- 9 => TaskScheduler::TASK_NINETH,
73
- 10 => TaskScheduler::TASK_TENTH,
74
- 11 => TaskScheduler::TASK_ELEVENTH,
75
- 12 => TaskScheduler::TASK_TWELFTH,
76
- 13 => TaskScheduler::TASK_THIRTEENTH,
77
- 14 => TaskScheduler::TASK_FOURTEENTH,
78
- 15 => TaskScheduler::TASK_FIFTEENTH,
79
- 16 => TaskScheduler::TASK_SIXTEENTH,
80
- 17 => TaskScheduler::TASK_SEVENTEENTH,
81
- 18 => TaskScheduler::TASK_EIGHTEENTH,
82
- 19 => TaskScheduler::TASK_NINETEENTH,
83
- 20 => TaskScheduler::TASK_TWENTIETH,
84
- 21 => TaskScheduler::TASK_TWENTY_FIRST,
85
- 22 => TaskScheduler::TASK_TWENTY_SECOND,
86
- 23 => TaskScheduler::TASK_TWENTY_THIRD,
87
- 24 => TaskScheduler::TASK_TWENTY_FOURTH,
88
- 25 => TaskScheduler::TASK_TWENTY_FIFTH,
89
- 26 => TaskScheduler::TASK_TWENTY_SIXTH,
90
- 27 => TaskScheduler::TASK_TWENTY_SEVENTH,
91
- 28 => TaskScheduler::TASK_TWENTY_EIGHTH,
92
- 29 => TaskScheduler::TASK_TWENTY_NINTH,
93
- # cspell:disable-next-line
94
- 30 => TaskScheduler::TASK_THIRTYETH,
95
- 31 => TaskScheduler::TASK_THIRTY_FIRST,
96
- }.freeze
97
-
98
- PRIORITY = { "critical" => 0, "highest" => 1, "above_normal_2" => 2 , "above_normal_3" => 3, "normal_4" => 4,
99
- "normal_5" => 5, "normal_6" => 6, "below_normal_7" => 7, "below_normal_8" => 8, "lowest" => 9, "idle" => 10 }.freeze
100
-
101
- def load_current_resource
102
- @current_resource = Chef::Resource::WindowsTask.new(new_resource.name)
103
- task = TaskScheduler.new(new_resource.task_name, nil, "\\", false)
104
- @current_resource.exists = task.exists?(new_resource.task_name)
105
- if @current_resource.exists
106
- task.get_task(new_resource.task_name)
107
- @current_resource.task = task
108
- pathed_task_name = new_resource.task_name.start_with?('\\') ? new_resource.task_name : "\\#{new_resource.task_name}"
109
- @current_resource.task_name(pathed_task_name)
110
- end
111
- @current_resource
112
- end
113
-
114
- action :create do
115
- set_command_and_arguments if new_resource.command
116
-
117
- if current_resource.exists
118
- logger.trace "#{new_resource} task exist."
119
- unless (task_needs_update?(current_resource.task)) || (new_resource.force)
120
- logger.info "#{new_resource} task does not need updating and force is not specified - nothing to do"
121
- return
122
- end
123
-
124
- # if start_day and start_time is not set by user current date and time will be set while updating any property
125
- set_start_day_and_time unless new_resource.frequency == :none
126
- update_task(current_resource.task)
127
- else
128
- basic_validation
129
- set_start_day_and_time
130
- converge_by("#{new_resource} task created") do
131
- task = TaskScheduler.new
132
- if new_resource.frequency == :none
133
- task.new_work_item(new_resource.task_name, {}, { user: new_resource.user, password: new_resource.password, interactive: new_resource.interactive_enabled })
134
- task.activate(new_resource.task_name)
135
- else
136
- task.new_work_item(new_resource.task_name, trigger, { user: new_resource.user, password: new_resource.password, interactive: new_resource.interactive_enabled })
137
- end
138
- task.application_name = new_resource.command
139
- task.parameters = new_resource.command_arguments if new_resource.command_arguments
140
- task.working_directory = new_resource.cwd if new_resource.cwd
141
- task.configure_settings(config_settings)
142
- task.configure_principals(principal_settings)
143
- task.set_account_information(new_resource.user, new_resource.password, new_resource.interactive_enabled)
144
- task.creator = new_resource.user
145
- task.description = new_resource.description unless new_resource.description.nil?
146
- task.activate(new_resource.task_name)
147
- end
148
- end
149
- end
150
-
151
- action :run do
152
- if current_resource.exists
153
- logger.trace "#{new_resource} task exists"
154
- if current_resource.task.status == "running"
155
- logger.info "#{new_resource} task is currently running, skipping run"
156
- else
157
- converge_by("run scheduled task #{new_resource}") do
158
- current_resource.task.run
159
- end
160
- end
161
- else
162
- logger.warn "#{new_resource} task does not exist - nothing to do"
163
- end
164
- end
165
-
166
- action :delete do
167
- if current_resource.exists
168
- logger.trace "#{new_resource} task exists"
169
- converge_by("delete scheduled task #{new_resource}") do
170
- ts = TaskScheduler.new
171
- ts.delete(current_resource.task_name)
172
- end
173
- else
174
- logger.warn "#{new_resource} task does not exist - nothing to do"
175
- end
176
- end
177
-
178
- action :end do
179
- if current_resource.exists
180
- logger.trace "#{new_resource} task exists"
181
- if current_resource.task.status != "running"
182
- logger.trace "#{new_resource} is not running - nothing to do"
183
- else
184
- converge_by("#{new_resource} task ended") do
185
- current_resource.task.stop
186
- end
187
- end
188
- else
189
- logger.warn "#{new_resource} task does not exist - nothing to do"
190
- end
191
- end
192
-
193
- action :enable do
194
- if current_resource.exists
195
- logger.trace "#{new_resource} task exists"
196
- if current_resource.task.status == "not scheduled"
197
- converge_by("#{new_resource} task enabled") do
198
- # TODO wind32-taskscheduler currently not having any method to handle this so using schtasks.exe here
199
- run_schtasks "CHANGE", "ENABLE" => ""
200
- end
201
- else
202
- logger.trace "#{new_resource} already enabled - nothing to do"
203
- end
204
- else
205
- logger.fatal "#{new_resource} task does not exist - nothing to do"
206
- raise Errno::ENOENT, "#{new_resource}: task does not exist, cannot enable"
207
- end
208
- end
209
-
210
- action :disable do
211
- if current_resource.exists
212
- logger.info "#{new_resource} task exists"
213
- if %w{ready running}.include?(current_resource.task.status)
214
- converge_by("#{new_resource} task disabled") do
215
- # TODO: in win32-taskscheduler there is no method which disables the task so currently calling disable with schtasks.exe
216
- run_schtasks "CHANGE", "DISABLE" => ""
217
- end
218
- else
219
- logger.warn "#{new_resource} already disabled - nothing to do"
220
- end
221
- else
222
- logger.warn "#{new_resource} task does not exist - nothing to do"
223
- end
224
- end
225
-
226
- alias_method :action_change, :action_create
227
-
228
- private
229
-
230
- # separated command arguments from :command property
231
- def set_command_and_arguments
232
- cmd, *args = Chef::Util::PathHelper.split_args(new_resource.command)
233
- new_resource.command = cmd
234
- new_resource.command_arguments = args.join(" ")
235
- end
236
-
237
- def set_start_day_and_time
238
- new_resource.start_day = Time.now.strftime("%m/%d/%Y") unless new_resource.start_day
239
- new_resource.start_time = Time.now.strftime("%H:%M") unless new_resource.start_time
240
- end
241
-
242
- def update_task(task)
243
- converge_by("#{new_resource} task updated") do
244
- task.set_account_information(new_resource.user, new_resource.password, new_resource.interactive_enabled)
245
- task.application_name = new_resource.command if new_resource.command
246
- task.parameters = new_resource.command_arguments if new_resource.command_arguments
247
- task.working_directory = new_resource.cwd if new_resource.cwd
248
- task.trigger = trigger unless new_resource.frequency == :none
249
- task.configure_settings(config_settings)
250
- task.creator = new_resource.user
251
- task.description = new_resource.description unless new_resource.description.nil?
252
- task.configure_principals(principal_settings)
253
- end
254
- end
255
-
256
- def trigger
257
- start_month, start_day, start_year = new_resource.start_day.to_s.split("/")
258
- start_hour, start_minute = new_resource.start_time.to_s.split(":")
259
- # TODO currently end_month, end_year and end_year needs to be set to 0. If not set win32-taskscheduler throwing nil into integer error.
260
- trigger_hash = {
261
- start_year: start_year.to_i,
262
- start_month: start_month.to_i,
263
- start_day: start_day.to_i,
264
- start_hour: start_hour.to_i,
265
- start_minute: start_minute.to_i,
266
- end_month: 0,
267
- end_day: 0,
268
- end_year: 0,
269
- trigger_type: trigger_type,
270
- type: type,
271
- random_minutes_interval: new_resource.random_delay,
272
- }
273
-
274
- if new_resource.frequency == :minute
275
- trigger_hash[:minutes_interval] = new_resource.frequency_modifier
276
- end
277
-
278
- if new_resource.frequency == :hourly
279
- minutes = convert_hours_in_minutes(new_resource.frequency_modifier.to_i)
280
- trigger_hash[:minutes_interval] = minutes
281
- end
282
-
283
- if new_resource.minutes_interval
284
- trigger_hash[:minutes_interval] = new_resource.minutes_interval
285
- end
286
-
287
- if new_resource.minutes_duration
288
- trigger_hash[:minutes_duration] = new_resource.minutes_duration
289
- end
290
-
291
- if trigger_type == TaskScheduler::MONTHLYDOW && frequency_modifier_contains_last_week?(new_resource.frequency_modifier)
292
- trigger_hash[:run_on_last_week_of_month] = true
293
- else
294
- trigger_hash[:run_on_last_week_of_month] = false
295
- end
296
-
297
- if trigger_type == TaskScheduler::MONTHLYDATE && day_includes_last_or_lastday?(new_resource.day)
298
- trigger_hash[:run_on_last_day_of_month] = true
299
- else
300
- trigger_hash[:run_on_last_day_of_month] = false
301
- end
302
- trigger_hash
303
- end
304
-
305
- def frequency_modifier_contains_last_week?(frequency_modifier)
306
- frequency_modifier = frequency_modifier.to_s.split(",")
307
- frequency_modifier.map! { |value| value.strip.upcase }
308
- frequency_modifier.include?("LAST")
309
- end
310
-
311
- def day_includes_last_or_lastday?(day)
312
- day = day.to_s.split(",")
313
- day.map! { |value| value.strip.upcase }
314
- day.include?("LAST") || day.include?("LASTDAY")
315
- end
316
-
317
- def convert_hours_in_minutes(hours)
318
- hours.to_i * 60 if hours
319
- end
320
-
321
- # TODO : Try to optimize this method
322
- # known issue : Since start_day and time is not mandatory while updating weekly frequency for which start_day is not mentioned by user idempotency
323
- # is not getting maintained as new_resource.start_day is nil and we fetch the day of week from start_day to set and its currently coming as nil and don't match with current_task
324
- def task_needs_update?(task)
325
- flag = false
326
- if new_resource.frequency == :none
327
- flag = (task.author != new_resource.user ||
328
- task.application_name != new_resource.command ||
329
- description_needs_update?(task) ||
330
- task.parameters != new_resource.command_arguments.to_s ||
331
- task.principals[:run_level] != run_level ||
332
- task.settings[:disallow_start_if_on_batteries] != new_resource.disallow_start_if_on_batteries ||
333
- task.settings[:stop_if_going_on_batteries] != new_resource.stop_if_going_on_batteries ||
334
- task.settings[:start_when_available] != new_resource.start_when_available)
335
- else
336
- current_task_trigger = task.trigger(0)
337
- new_task_trigger = trigger
338
- flag = (ISO8601::Duration.new(task.idle_settings[:idle_duration])) != (ISO8601::Duration.new(new_resource.idle_time * 60)) if new_resource.frequency == :on_idle
339
- flag = (ISO8601::Duration.new(task.execution_time_limit)) != (ISO8601::Duration.new(new_resource.execution_time_limit * 60)) unless new_resource.execution_time_limit.nil?
340
-
341
- # if trigger not found updating the task to add the trigger
342
- if current_task_trigger.nil?
343
- flag = true
344
- else
345
- flag = true if start_day_updated?(current_task_trigger, new_task_trigger) == true ||
346
- start_time_updated?(current_task_trigger, new_task_trigger) == true ||
347
- current_task_trigger[:trigger_type] != new_task_trigger[:trigger_type] ||
348
- current_task_trigger[:type] != new_task_trigger[:type] ||
349
- current_task_trigger[:random_minutes_interval].to_i != new_task_trigger[:random_minutes_interval].to_i ||
350
- current_task_trigger[:minutes_interval].to_i != new_task_trigger[:minutes_interval].to_i ||
351
- task.author.to_s.casecmp(new_resource.user.to_s) != 0 ||
352
- task.application_name != new_resource.command ||
353
- description_needs_update?(task) ||
354
- task.parameters != new_resource.command_arguments.to_s ||
355
- task.working_directory != new_resource.cwd.to_s ||
356
- task.principals[:logon_type] != logon_type ||
357
- task.principals[:run_level] != run_level ||
358
- PRIORITY[task.priority] != new_resource.priority ||
359
- task.settings[:disallow_start_if_on_batteries] != new_resource.disallow_start_if_on_batteries ||
360
- task.settings[:stop_if_going_on_batteries] != new_resource.stop_if_going_on_batteries ||
361
- task.settings[:start_when_available] != new_resource.start_when_available
362
- if trigger_type == TaskScheduler::MONTHLYDATE
363
- flag = true if current_task_trigger[:run_on_last_day_of_month] != new_task_trigger[:run_on_last_day_of_month]
364
- end
365
-
366
- if trigger_type == TaskScheduler::MONTHLYDOW
367
- flag = true if current_task_trigger[:run_on_last_week_of_month] != new_task_trigger[:run_on_last_week_of_month]
368
- end
369
- end
370
- end
371
- flag
372
- end
373
-
374
- def start_day_updated?(current_task_trigger, new_task_trigger)
375
- ( new_resource.start_day && (current_task_trigger[:start_year].to_i != new_task_trigger[:start_year] ||
376
- current_task_trigger[:start_month].to_i != new_task_trigger[:start_month] ||
377
- current_task_trigger[:start_day].to_i != new_task_trigger[:start_day]) )
378
- end
379
-
380
- def start_time_updated?(current_task_trigger, new_task_trigger)
381
- ( new_resource.start_time && ( current_task_trigger[:start_hour].to_i != new_task_trigger[:start_hour] ||
382
- current_task_trigger[:start_minute].to_i != new_task_trigger[:start_minute] ) )
383
- end
384
-
385
- def trigger_type
386
- case new_resource.frequency
387
- when :once, :minute, :hourly
388
- TaskScheduler::ONCE
389
- when :daily
390
- TaskScheduler::DAILY
391
- when :weekly
392
- TaskScheduler::WEEKLY
393
- when :monthly
394
- # If frequency modifier is set with frequency :monthly we are setting taskscheduler as monthlydow
395
- # Ref https://msdn.microsoft.com/en-us/library/windows/desktop/aa382061(v=vs.85).aspx
396
- new_resource.frequency_modifier.to_i.between?(1, 12) ? TaskScheduler::MONTHLYDATE : TaskScheduler::MONTHLYDOW
397
- when :on_idle
398
- TaskScheduler::ON_IDLE
399
- when :onstart
400
- TaskScheduler::AT_SYSTEMSTART
401
- when :on_logon
402
- TaskScheduler::AT_LOGON
403
- else
404
- raise ArgumentError, "Please set frequency"
405
- end
406
- end
407
-
408
- def type
409
- case trigger_type
410
- when TaskScheduler::ONCE
411
- { once: nil }
412
- when TaskScheduler::DAILY
413
- { days_interval: new_resource.frequency_modifier.to_i }
414
- when TaskScheduler::WEEKLY
415
- { weeks_interval: new_resource.frequency_modifier.to_i, days_of_week: days_of_week.to_i }
416
- when TaskScheduler::MONTHLYDATE
417
- { months: months_of_year.to_i, days: days_of_month.to_i }
418
- when TaskScheduler::MONTHLYDOW
419
- { months: months_of_year.to_i, days_of_week: days_of_week.to_i, weeks_of_month: weeks_of_month.to_i }
420
- when TaskScheduler::ON_IDLE
421
- # TODO: handle option for this trigger
422
- when TaskScheduler::AT_LOGON
423
- # TODO: handle option for this trigger
424
- when TaskScheduler::AT_SYSTEMSTART
425
- # TODO: handle option for this trigger
426
- end
427
- end
428
-
429
- # Deleting last from the array of weeks of month since last week is handled in :run_on_last_week_of_month parameter.
430
- def weeks_of_month
431
- weeks_of_month = []
432
- if new_resource.frequency_modifier
433
- weeks = new_resource.frequency_modifier.split(",")
434
- weeks.map! { |week| week.to_s.strip.upcase }
435
- weeks.delete("LAST") if weeks.include?("LAST")
436
- weeks_of_month = get_binary_values_from_constants(weeks, WEEKS_OF_MONTH)
437
- end
438
- weeks_of_month
439
- end
440
-
441
- # Deleting the "LAST" and "LASTDAY" from days since last day is handled in :run_on_last_day_of_month parameter.
442
- def days_of_month
443
- days_of_month = []
444
- if new_resource.day
445
- days = new_resource.day.to_s.split(",")
446
- days.map! { |day| day.to_s.strip.upcase }
447
- days.delete("LAST") if days.include?("LAST")
448
- days.delete("LASTDAY") if days.include?("LASTDAY")
449
- if days - (1..31).to_a
450
- days.each do |day|
451
- days_of_month << DAYS_OF_MONTH[day.to_i]
452
- end
453
- days_of_month = days_of_month.size > 1 ? days_of_month.inject(:|) : days_of_month[0]
454
- end
455
- else
456
- days_of_month = DAYS_OF_MONTH[1]
457
- end
458
- days_of_month
459
- end
460
-
461
- def days_of_week
462
- if new_resource.day
463
- # this line of code is just to support backward compatibility of wild card *
464
- new_resource.day = "mon, tue, wed, thu, fri, sat, sun" if new_resource.day == "*" && new_resource.frequency == :weekly
465
- days = new_resource.day.to_s.split(",")
466
- days.map! { |day| day.to_s.strip.upcase }
467
- weeks_days = get_binary_values_from_constants(days, DAYS_OF_WEEK)
468
- else
469
- # following condition will make the frequency :weekly idempotent if start_day is not provided by user setting day as the current_resource day
470
- if (current_resource) && (current_resource.task) && (current_resource.task.trigger(0)[:type][:days_of_week]) && (new_resource.start_day.nil?)
471
- weeks_days = current_resource.task.trigger(0)[:type][:days_of_week]
472
- else
473
- day = get_day(new_resource.start_day).to_sym if new_resource.start_day
474
- DAYS_OF_WEEK[day]
475
- end
476
- end
477
- end
478
-
479
- def months_of_year
480
- months_of_year = []
481
- if new_resource.frequency_modifier.to_i.between?(1, 12) && !(new_resource.months)
482
- new_resource.months = set_months(new_resource.frequency_modifier.to_i)
483
- end
484
-
485
- if new_resource.months
486
- # this line of code is just to support backward compatibility of wild card *
487
- new_resource.months = "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec" if new_resource.months == "*" && new_resource.frequency == :monthly
488
- months = new_resource.months.split(",")
489
- months.map! { |month| month.to_s.strip.upcase }
490
- months_of_year = get_binary_values_from_constants(months, MONTHS)
491
- else
492
- MONTHS.each do |key, value|
493
- months_of_year << MONTHS[key]
494
- end
495
- months_of_year = months_of_year.inject(:|)
496
- end
497
- months_of_year
498
- end
499
-
500
- # This values are set for frequency_modifier set as 1-12
501
- # This is to give backward compatibility validated this values with earlier code and running schtask.exe
502
- # Used this as reference https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks#d-dayday--
503
- def set_months(frequency_modifier)
504
- case frequency_modifier
505
- when 1
506
- "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"
507
- when 2
508
- "feb, apr, jun, aug, oct, dec"
509
- when 3
510
- "mar, jun, sep, dec"
511
- when 4
512
- "apr, aug, dec"
513
- when 5
514
- "may, oct"
515
- when 6
516
- "jun, dec"
517
- when 7
518
- "jul"
519
- when 8
520
- "aug"
521
- when 9
522
- "sep"
523
- when 10
524
- "oct"
525
- when 11
526
- "nov"
527
- when 12
528
- "dec"
529
- end
530
- end
531
-
532
- def get_binary_values_from_constants(array_values, constant)
533
- data = []
534
- array_values.each do |value|
535
- value = value.to_sym
536
- data << constant[value]
537
- end
538
- data.size > 1 ? data.inject(:|) : data[0]
539
- end
540
-
541
- def run_level
542
- case new_resource.run_level
543
- when :highest
544
- TaskScheduler::TASK_RUNLEVEL_HIGHEST
545
- when :limited
546
- TaskScheduler::TASK_RUNLEVEL_LUA
547
- end
548
- end
549
-
550
- # TODO: while creating the configuration settings win32-taskscheduler it accepts execution time limit values in ISO8601 format
551
- def config_settings
552
- settings = {
553
- execution_time_limit: new_resource.execution_time_limit,
554
- enabled: true,
555
- }
556
- settings[:idle_duration] = new_resource.idle_time if new_resource.idle_time
557
- settings[:run_only_if_idle] = true if new_resource.idle_time
558
- settings[:priority] = new_resource.priority
559
- settings[:disallow_start_if_on_batteries] = new_resource.disallow_start_if_on_batteries
560
- settings[:stop_if_going_on_batteries] = new_resource.stop_if_going_on_batteries
561
- settings[:start_when_available] = new_resource.start_when_available
562
- settings
563
- end
564
-
565
- def principal_settings
566
- settings = {}
567
- settings[:run_level] = run_level
568
- settings[:logon_type] = logon_type
569
- settings
570
- end
571
-
572
- def description_needs_update?(task)
573
- task.description != new_resource.description unless new_resource.description.nil?
574
- end
575
-
576
- def logon_type
577
- # Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383566(v=vs.85).aspx
578
- # if nothing is passed as logon_type the TASK_LOGON_SERVICE_ACCOUNT is getting set as default so using that for comparison.
579
- user_id = new_resource.user.to_s
580
- password = new_resource.password.to_s
581
- if Chef::ReservedNames::Win32::Security::SID.service_account_user?(user_id)
582
- TaskScheduler::TASK_LOGON_SERVICE_ACCOUNT
583
- elsif Chef::ReservedNames::Win32::Security::SID.group_user?(user_id)
584
- TaskScheduler::TASK_LOGON_GROUP
585
- elsif !user_id.empty? && !password.empty?
586
- if new_resource.interactive_enabled
587
- TaskScheduler::TASK_LOGON_INTERACTIVE_TOKEN
588
- else
589
- TaskScheduler::TASK_LOGON_PASSWORD
590
- end
591
- else
592
- TaskScheduler::TASK_LOGON_INTERACTIVE_TOKEN
593
- end
594
- end
595
-
596
- # This method checks if task and command properties exist since those two are mandatory properties to create a schedules task.
597
- def basic_validation
598
- validate = []
599
- validate << "Command" if new_resource.command.nil? || new_resource.command.empty?
600
- validate << "Task Name" if new_resource.task_name.nil? || new_resource.task_name.empty?
601
- return true if validate.empty?
602
-
603
- raise Chef::Exceptions::ValidationFailed.new "Value for '#{validate.join(", ")}' option cannot be empty"
604
- end
605
-
606
- # rubocop:disable Style/StringLiteralsInInterpolation
607
- def run_schtasks(task_action, options = {})
608
- cmd = "schtasks /#{task_action} /TN \"#{new_resource.task_name}\" "
609
- options.each_key do |option|
610
- unless option == "TR"
611
- cmd += "/#{option} "
612
- cmd += "\"#{options[option].to_s.gsub('"', "\\\"")}\" " unless options[option] == ""
613
- end
614
- end
615
- # Appending Task Run [TR] option at the end since appending causing sometimes to append other options in option["TR"] value
616
- if options["TR"]
617
- cmd += "/TR \"#{options["TR"]} \" " unless task_action == "DELETE"
618
- end
619
- logger.trace("running: ")
620
- logger.trace(" #{cmd}")
621
- shell_out!(cmd, returns: [0])
622
- end
623
- # rubocop:enable Style/StringLiteralsInInterpolation
624
-
625
- def get_day(date)
626
- Date.strptime(date, "%m/%d/%Y").strftime("%a").upcase
627
- end
628
- end
629
- end
630
- end
631
- end