chef 12.5.0.alpha.1 → 12.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +21 -25
  3. data/Gemfile +46 -0
  4. data/README.md +4 -4
  5. data/Rakefile +5 -110
  6. data/distro/common/html/knife_cookbook_site.html +18 -18
  7. data/distro/common/man/man1/knife-cookbook-site.1 +11 -11
  8. data/lib/chef/application.rb +1 -1
  9. data/lib/chef/application/apply.rb +19 -1
  10. data/lib/chef/application/client.rb +11 -5
  11. data/lib/chef/application/knife.rb +2 -2
  12. data/lib/chef/application/windows_service_manager.rb +9 -7
  13. data/lib/chef/chef_class.rb +39 -0
  14. data/lib/chef/chef_fs/data_handler/client_data_handler.rb +3 -1
  15. data/lib/chef/chef_fs/file_system/acl_dir.rb +3 -4
  16. data/lib/chef/chef_fs/file_system/acls_dir.rb +5 -1
  17. data/lib/chef/chef_fs/file_system/base_fs_dir.rb +0 -5
  18. data/lib/chef/chef_fs/file_system/base_fs_object.rb +5 -2
  19. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb +2 -9
  20. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb +2 -9
  21. data/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb +10 -17
  22. data/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb +1 -12
  23. data/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb +15 -11
  24. data/lib/chef/chef_fs/file_system/chef_server_root_dir.rb +8 -2
  25. data/lib/chef/chef_fs/file_system/cookbook_dir.rb +3 -4
  26. data/lib/chef/chef_fs/file_system/cookbooks_acl_dir.rb +1 -1
  27. data/lib/chef/chef_fs/file_system/cookbooks_dir.rb +3 -11
  28. data/lib/chef/chef_fs/file_system/data_bags_dir.rb +3 -5
  29. data/lib/chef/chef_fs/file_system/environments_dir.rb +1 -1
  30. data/lib/chef/chef_fs/file_system/file_system_entry.rb +7 -4
  31. data/lib/chef/chef_fs/file_system/memory_dir.rb +2 -3
  32. data/lib/chef/chef_fs/file_system/multiplexed_dir.rb +15 -0
  33. data/lib/chef/chef_fs/file_system/nodes_dir.rb +1 -1
  34. data/lib/chef/chef_fs/file_system/organization_members_entry.rb +2 -2
  35. data/lib/chef/chef_fs/file_system/rest_list_dir.rb +4 -9
  36. data/lib/chef/client.rb +2 -3
  37. data/lib/chef/config.rb +34 -2
  38. data/lib/chef/cookbook/metadata.rb +25 -3
  39. data/lib/chef/cookbook/synchronizer.rb +1 -1
  40. data/lib/chef/cookbook_site_streaming_uploader.rb +1 -1
  41. data/lib/chef/cookbook_version.rb +3 -3
  42. data/lib/chef/deprecation/mixin/template.rb +1 -2
  43. data/lib/chef/deprecation/provider/cookbook_file.rb +1 -1
  44. data/lib/chef/deprecation/provider/file.rb +1 -1
  45. data/lib/chef/deprecation/provider/remote_directory.rb +52 -0
  46. data/lib/chef/deprecation/provider/remote_file.rb +1 -2
  47. data/lib/chef/deprecation/provider/template.rb +1 -1
  48. data/lib/chef/deprecation/warnings.rb +3 -4
  49. data/lib/chef/dsl/reboot_pending.rb +3 -2
  50. data/lib/chef/dsl/recipe.rb +6 -5
  51. data/lib/chef/dsl/resources.rb +2 -2
  52. data/lib/chef/event_dispatch/base.rb +12 -7
  53. data/lib/chef/event_dispatch/dispatcher.rb +21 -6
  54. data/lib/chef/exceptions.rb +22 -0
  55. data/lib/chef/file_content_management/tempfile.rb +1 -1
  56. data/lib/chef/formatters/base.rb +3 -0
  57. data/lib/chef/formatters/doc.rb +53 -5
  58. data/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +36 -0
  59. data/lib/chef/formatters/minimal.rb +2 -2
  60. data/lib/chef/knife.rb +35 -55
  61. data/lib/chef/knife/bootstrap.rb +41 -0
  62. data/lib/chef/knife/bootstrap/chef_vault_handler.rb +1 -0
  63. data/lib/chef/knife/bootstrap/client_builder.rb +16 -0
  64. data/lib/chef/knife/bootstrap/templates/README.md +3 -4
  65. data/lib/chef/knife/cookbook_create.rb +1 -1
  66. data/lib/chef/knife/cookbook_site_download.rb +1 -1
  67. data/lib/chef/knife/cookbook_site_install.rb +1 -1
  68. data/lib/chef/knife/cookbook_site_share.rb +6 -6
  69. data/lib/chef/knife/cookbook_site_unshare.rb +2 -2
  70. data/lib/chef/knife/core/bootstrap_context.rb +12 -4
  71. data/lib/chef/knife/core/custom_manifest_loader.rb +69 -0
  72. data/lib/chef/knife/core/gem_glob_loader.rb +138 -0
  73. data/lib/chef/knife/core/hashed_command_loader.rb +80 -0
  74. data/lib/chef/knife/core/node_presenter.rb +24 -1
  75. data/lib/chef/knife/core/object_loader.rb +1 -0
  76. data/lib/chef/knife/core/subcommand_loader.rb +131 -146
  77. data/lib/chef/knife/node_run_list_remove.rb +12 -1
  78. data/lib/chef/knife/null.rb +10 -0
  79. data/lib/chef/knife/rehash.rb +62 -0
  80. data/lib/chef/knife/search.rb +3 -3
  81. data/lib/chef/knife/ssh.rb +52 -30
  82. data/lib/chef/local_mode.rb +5 -0
  83. data/lib/chef/log.rb +5 -1
  84. data/lib/chef/mixin/deprecation.rb +8 -8
  85. data/lib/chef/mixin/params_validate.rb +2 -2
  86. data/lib/chef/mixin/template.rb +48 -0
  87. data/lib/chef/mixin/which.rb +1 -1
  88. data/lib/chef/mixin/wide_string.rb +72 -0
  89. data/lib/chef/mixin/windows_architecture_helper.rb +15 -39
  90. data/lib/chef/mixin/windows_env_helper.rb +4 -1
  91. data/lib/chef/monkey_patches/webrick-utils.rb +51 -0
  92. data/lib/chef/monkey_patches/win32/registry.rb +72 -0
  93. data/lib/chef/node.rb +97 -3
  94. data/lib/chef/node_map.rb +2 -2
  95. data/lib/chef/platform/provider_mapping.rb +5 -6
  96. data/lib/chef/platform/query_helpers.rb +46 -4
  97. data/lib/chef/platform/service_helpers.rb +30 -32
  98. data/lib/chef/policy_builder.rb +1 -8
  99. data/lib/chef/policy_builder/dynamic.rb +186 -0
  100. data/lib/chef/policy_builder/expand_node_object.rb +30 -15
  101. data/lib/chef/policy_builder/policyfile.rb +155 -18
  102. data/lib/chef/property.rb +38 -9
  103. data/lib/chef/provider.rb +104 -12
  104. data/lib/chef/provider/batch.rb +8 -0
  105. data/lib/chef/provider/deploy.rb +2 -2
  106. data/lib/chef/provider/directory.rb +14 -2
  107. data/lib/chef/provider/dsc_resource.rb +5 -9
  108. data/lib/chef/provider/group/pw.rb +1 -1
  109. data/lib/chef/provider/ifconfig.rb +2 -2
  110. data/lib/chef/provider/mount.rb +7 -3
  111. data/lib/chef/provider/package/dpkg.rb +5 -11
  112. data/lib/chef/provider/package/rpm.rb +2 -2
  113. data/lib/chef/provider/package/rubygems.rb +1 -1
  114. data/lib/chef/provider/package/windows/msi.rb +2 -2
  115. data/lib/chef/provider/package/yum.rb +17 -5
  116. data/lib/chef/provider/powershell_script.rb +27 -4
  117. data/lib/chef/provider/remote_directory.rb +190 -102
  118. data/lib/chef/provider/service.rb +12 -2
  119. data/lib/chef/provider/service/aix.rb +1 -1
  120. data/lib/chef/provider/service/debian.rb +3 -5
  121. data/lib/chef/provider/service/freebsd.rb +1 -1
  122. data/lib/chef/provider/service/gentoo.rb +3 -3
  123. data/lib/chef/provider/service/init.rb +3 -3
  124. data/lib/chef/provider/service/insserv.rb +2 -4
  125. data/lib/chef/provider/service/invokercd.rb +2 -4
  126. data/lib/chef/provider/service/openbsd.rb +2 -1
  127. data/lib/chef/provider/service/redhat.rb +52 -16
  128. data/lib/chef/provider/service/simple.rb +2 -2
  129. data/lib/chef/provider/service/systemd.rb +3 -5
  130. data/lib/chef/provider/service/upstart.rb +4 -6
  131. data/lib/chef/provider/subversion.rb +13 -7
  132. data/lib/chef/provider/template/content.rb +16 -6
  133. data/lib/chef/provider/user/solaris.rb +32 -4
  134. data/lib/chef/provider/windows_script.rb +3 -5
  135. data/lib/chef/provider_resolver.rb +2 -2
  136. data/lib/chef/resource.rb +85 -27
  137. data/lib/chef/resource/action_class.rb +83 -0
  138. data/lib/chef/resource/chef_gem.rb +3 -3
  139. data/lib/chef/resource/deploy.rb +8 -2
  140. data/lib/chef/resource/dsc_script.rb +2 -0
  141. data/lib/chef/resource/file/verification.rb +1 -1
  142. data/lib/chef/resource/registry_key.rb +1 -1
  143. data/lib/chef/resource/service.rb +10 -2
  144. data/lib/chef/resource/subversion.rb +5 -0
  145. data/lib/chef/resource/windows_script.rb +6 -2
  146. data/lib/chef/resource/yum_package.rb +10 -1
  147. data/lib/chef/resource_resolver.rb +3 -3
  148. data/lib/chef/run_context.rb +6 -8
  149. data/lib/chef/run_list/versioned_recipe_list.rb +15 -0
  150. data/lib/chef/run_lock.rb +30 -21
  151. data/lib/chef/util/powershell/ps_credential.rb +4 -0
  152. data/lib/chef/util/windows.rb +0 -32
  153. data/lib/chef/util/windows/net_group.rb +85 -106
  154. data/lib/chef/util/windows/net_use.rb +35 -71
  155. data/lib/chef/util/windows/net_user.rb +0 -1
  156. data/lib/chef/util/windows/volume.rb +19 -19
  157. data/lib/chef/version.rb +3 -3
  158. data/lib/chef/win32/api.rb +1 -0
  159. data/lib/chef/win32/api/file.rb +20 -0
  160. data/lib/chef/win32/api/net.rb +163 -43
  161. data/lib/chef/win32/api/registry.rb +51 -0
  162. data/lib/chef/win32/api/system.rb +23 -0
  163. data/lib/chef/win32/api/unicode.rb +0 -43
  164. data/lib/chef/win32/crypto.rb +2 -1
  165. data/lib/chef/win32/file.rb +28 -3
  166. data/lib/chef/win32/mutex.rb +1 -2
  167. data/lib/chef/win32/net.rb +162 -8
  168. data/lib/chef/win32/process.rb +13 -0
  169. data/lib/chef/win32/registry.rb +25 -28
  170. data/lib/chef/win32/security.rb +1 -1
  171. data/lib/chef/win32/security/token.rb +1 -1
  172. data/lib/chef/win32/system.rb +62 -0
  173. data/lib/chef/win32/unicode.rb +7 -2
  174. data/lib/chef/win32/version.rb +0 -4
  175. data/spec/data/cookbooks/openldap/templates/default/helpers.erb +14 -0
  176. data/spec/data/cookbooks/openldap/templates/default/nested_openldap_partials.erb +1 -0
  177. data/spec/data/cookbooks/openldap/templates/default/nested_partial.erb +1 -0
  178. data/spec/data/dsc_lcm.pfx +0 -0
  179. data/spec/functional/dsl/reboot_pending_spec.rb +33 -43
  180. data/spec/functional/knife/cookbook_delete_spec.rb +17 -7
  181. data/spec/functional/knife/ssh_spec.rb +16 -0
  182. data/spec/functional/resource/deploy_revision_spec.rb +1 -1
  183. data/spec/functional/resource/dsc_resource_spec.rb +2 -0
  184. data/spec/functional/resource/dsc_script_spec.rb +91 -2
  185. data/spec/functional/resource/group_spec.rb +67 -44
  186. data/spec/functional/resource/powershell_script_spec.rb +104 -15
  187. data/spec/functional/resource/windows_service_spec.rb +1 -1
  188. data/spec/functional/run_lock_spec.rb +368 -189
  189. data/spec/functional/win32/{registry_helper_spec.rb → registry_spec.rb} +4 -23
  190. data/spec/integration/client/client_spec.rb +51 -0
  191. data/spec/integration/knife/download_spec.rb +4 -0
  192. data/spec/integration/knife/list_spec.rb +8 -0
  193. data/spec/integration/knife/upload_spec.rb +1 -1
  194. data/spec/integration/recipes/recipe_dsl_spec.rb +1 -1
  195. data/spec/integration/recipes/remote_directory.rb +74 -0
  196. data/spec/integration/recipes/resource_action_spec.rb +42 -22
  197. data/spec/integration/recipes/resource_converge_if_changed_spec.rb +423 -0
  198. data/spec/integration/recipes/resource_load_spec.rb +206 -0
  199. data/spec/spec_helper.rb +8 -0
  200. data/spec/support/platform_helpers.rb +13 -0
  201. data/{lib/chef/mixin/wstring.rb → spec/support/shared/context/win32.rb} +15 -12
  202. data/spec/support/shared/functional/windows_script.rb +63 -26
  203. data/spec/support/shared/unit/mock_shellout.rb +46 -0
  204. data/spec/support/shared/unit/provider/file.rb +10 -4
  205. data/spec/unit/application/client_spec.rb +16 -3
  206. data/spec/unit/application/knife_spec.rb +2 -2
  207. data/spec/unit/chef_class_spec.rb +0 -4
  208. data/spec/unit/client_spec.rb +6 -1
  209. data/spec/unit/config_spec.rb +31 -0
  210. data/spec/unit/cookbook/metadata_spec.rb +23 -3
  211. data/spec/unit/cookbook/syntax_check_spec.rb +3 -0
  212. data/spec/unit/deprecation_spec.rb +3 -6
  213. data/spec/unit/dsl/reboot_pending_spec.rb +12 -6
  214. data/spec/unit/event_dispatch/dispatcher_spec.rb +65 -3
  215. data/spec/unit/event_dispatch/dsl_spec.rb +0 -4
  216. data/spec/unit/formatters/doc_spec.rb +32 -0
  217. data/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb +26 -0
  218. data/spec/unit/knife/bootstrap/client_builder_spec.rb +27 -0
  219. data/spec/unit/knife/bootstrap_spec.rb +55 -3
  220. data/spec/unit/knife/cookbook_site_share_spec.rb +3 -3
  221. data/spec/unit/knife/core/bootstrap_context_spec.rb +21 -4
  222. data/spec/unit/knife/core/custom_manifest_loader_spec.rb +41 -0
  223. data/spec/unit/knife/core/gem_glob_loader_spec.rb +210 -0
  224. data/spec/unit/knife/core/hashed_command_loader_spec.rb +93 -0
  225. data/spec/unit/knife/core/subcommand_loader_spec.rb +16 -192
  226. data/spec/unit/knife/node_run_list_remove_spec.rb +17 -0
  227. data/spec/unit/mixin/enforce_ownership_and_permissions_spec.rb +10 -10
  228. data/spec/unit/mixin/template_spec.rb +5 -1
  229. data/spec/unit/mixin/windows_architecture_helper_spec.rb +13 -8
  230. data/spec/unit/node_spec.rb +213 -0
  231. data/spec/unit/platform/query_helpers_spec.rb +146 -3
  232. data/spec/unit/policy_builder/dynamic_spec.rb +275 -0
  233. data/spec/unit/policy_builder/expand_node_object_spec.rb +37 -38
  234. data/spec/unit/policy_builder/policyfile_spec.rb +260 -46
  235. data/spec/unit/property/validation_spec.rb +5 -0
  236. data/spec/unit/property_spec.rb +152 -26
  237. data/spec/unit/provider/deploy_spec.rb +5 -5
  238. data/spec/unit/provider/directory_spec.rb +35 -0
  239. data/spec/unit/provider/dsc_resource_spec.rb +3 -10
  240. data/spec/unit/provider/ifconfig_spec.rb +22 -2
  241. data/spec/unit/provider/mount/aix_spec.rb +2 -1
  242. data/spec/unit/provider/mount/mount_spec.rb +6 -0
  243. data/spec/unit/provider/mount/windows_spec.rb +14 -0
  244. data/spec/unit/provider/mount_spec.rb +12 -1
  245. data/spec/unit/provider/package/dpkg_spec.rb +8 -1
  246. data/spec/unit/provider/package/rpm_spec.rb +18 -1
  247. data/spec/unit/provider/package/rubygems_spec.rb +18 -0
  248. data/spec/unit/provider/package/yum_spec.rb +97 -24
  249. data/spec/unit/provider/powershell_script_spec.rb +52 -26
  250. data/spec/unit/provider/remote_directory_spec.rb +1 -2
  251. data/spec/unit/provider/service/aix_service_spec.rb +3 -3
  252. data/spec/unit/provider/service/gentoo_service_spec.rb +4 -4
  253. data/spec/unit/provider/service/openbsd_service_spec.rb +10 -8
  254. data/spec/unit/provider/service/redhat_spec.rb +88 -8
  255. data/spec/unit/provider/service/upstart_service_spec.rb +11 -7
  256. data/spec/unit/provider/service/windows_spec.rb +211 -200
  257. data/spec/unit/provider/subversion_spec.rb +50 -31
  258. data/spec/unit/provider/template/content_spec.rb +93 -2
  259. data/spec/unit/provider/user/solaris_spec.rb +66 -9
  260. data/spec/unit/provider_resolver_spec.rb +707 -650
  261. data/spec/unit/resource/deploy_spec.rb +7 -1
  262. data/spec/unit/resource/dsc_script_spec.rb +4 -0
  263. data/spec/unit/resource/file/verification_spec.rb +1 -1
  264. data/spec/unit/resource/powershell_script_spec.rb +17 -13
  265. data/spec/unit/resource/service_spec.rb +4 -4
  266. data/spec/unit/resource/subversion_spec.rb +4 -0
  267. data/spec/unit/resource/yum_package_spec.rb +10 -1
  268. data/spec/unit/run_list/versioned_recipe_list_spec.rb +5 -0
  269. data/spec/unit/win32/registry_spec.rb +394 -0
  270. data/tasks/external_tests.rb +47 -23
  271. data/tasks/maintainers.rb +155 -14
  272. metadata +51 -26
  273. data/lib/chef/knife/bootstrap/templates/archlinux-gems.erb +0 -76
  274. data/lib/chef/knife/bootstrap/templates/chef-aix.erb +0 -72
  275. data/spec/unit/registry_helper_spec.rb +0 -390
@@ -19,6 +19,7 @@
19
19
  # XXX: mixing shellout into a mixin into classes has to be code smell
20
20
  require 'chef/mixin/shell_out'
21
21
  require 'chef/mixin/which'
22
+ require 'chef/chef_class'
22
23
 
23
24
  class Chef
24
25
  class Platform
@@ -42,56 +43,56 @@ class Chef
42
43
  # different services is NOT a design concern of this module.
43
44
  #
44
45
  def service_resource_providers
45
- @service_resource_providers ||= [].tap do |service_resource_providers|
46
+ providers = []
46
47
 
47
- if ::File.exist?("/usr/sbin/update-rc.d")
48
- service_resource_providers << :debian
49
- end
50
-
51
- if ::File.exist?("/usr/sbin/invoke-rc.d")
52
- service_resource_providers << :invokercd
53
- end
48
+ if ::File.exist?(Chef.path_to("/usr/sbin/update-rc.d"))
49
+ providers << :debian
50
+ end
54
51
 
55
- if ::File.exist?("/sbin/insserv")
56
- service_resource_providers << :insserv
57
- end
52
+ if ::File.exist?(Chef.path_to("/usr/sbin/invoke-rc.d"))
53
+ providers << :invokercd
54
+ end
58
55
 
59
- # debian >= 6.0 has /etc/init but does not have upstart
60
- if ::File.exist?("/etc/init") && ::File.exist?("/sbin/start")
61
- service_resource_providers << :upstart
62
- end
56
+ if ::File.exist?(Chef.path_to("/sbin/insserv"))
57
+ providers << :insserv
58
+ end
63
59
 
64
- if ::File.exist?("/sbin/chkconfig")
65
- service_resource_providers << :redhat
66
- end
60
+ # debian >= 6.0 has /etc/init but does not have upstart
61
+ if ::File.exist?(Chef.path_to("/etc/init")) && ::File.exist?(Chef.path_to("/sbin/start"))
62
+ providers << :upstart
63
+ end
67
64
 
68
- if systemd_sanity_check?
69
- service_resource_providers << :systemd
70
- end
65
+ if ::File.exist?(Chef.path_to("/sbin/chkconfig"))
66
+ providers << :redhat
67
+ end
71
68
 
69
+ if systemd_sanity_check?
70
+ providers << :systemd
72
71
  end
72
+
73
+ providers
73
74
  end
74
75
 
75
76
  def config_for_service(service_name)
76
77
  configs = []
77
78
 
78
- if ::File.exist?("/etc/init.d/#{service_name}")
79
+ if ::File.exist?(Chef.path_to("/etc/init.d/#{service_name}"))
79
80
  configs << :initd
80
81
  end
81
82
 
82
- if ::File.exist?("/etc/init/#{service_name}.conf")
83
+ if ::File.exist?(Chef.path_to("/etc/init/#{service_name}.conf"))
83
84
  configs << :upstart
84
85
  end
85
86
 
86
- if ::File.exist?("/etc/xinetd.d/#{service_name}")
87
+ if ::File.exist?(Chef.path_to("/etc/xinetd.d/#{service_name}"))
87
88
  configs << :xinetd
88
89
  end
89
90
 
90
- if ::File.exist?("/etc/rc.d/#{service_name}")
91
+ if ::File.exist?(Chef.path_to("/etc/rc.d/#{service_name}"))
91
92
  configs << :etc_rcd
92
93
  end
93
94
 
94
- if ::File.exist?("/usr/local/etc/rc.d/#{service_name}")
95
+ if ::File.exist?(Chef.path_to("/usr/local/etc/rc.d/#{service_name}"))
95
96
  configs << :usr_local_etc_rcd
96
97
  end
97
98
 
@@ -105,14 +106,11 @@ class Chef
105
106
  private
106
107
 
107
108
  def systemctl_path
108
- if @systemctl_path.nil?
109
- @systemctl_path = which("systemctl")
110
- end
111
- @systemctl_path
109
+ which("systemctl")
112
110
  end
113
111
 
114
112
  def systemd_sanity_check?
115
- systemctl_path && File.exist?("/proc/1/comm") && File.open("/proc/1/comm").gets.chomp == "systemd"
113
+ systemctl_path && File.exist?(Chef.path_to("/proc/1/comm")) && File.open(Chef.path_to("/proc/1/comm")).gets.chomp == "systemd"
116
114
  end
117
115
 
118
116
  def extract_systemd_services(command)
@@ -126,7 +124,7 @@ class Chef
126
124
  # this splits off the suffix after the last dot to return "sshd"
127
125
  services += services.select {|s| s.match(/\.service$/) }.map { |s| s.sub(/(.*)\.service$/, '\1') }
128
126
  rescue Mixlib::ShellOut::ShellCommandFailed
129
- false
127
+ []
130
128
  end
131
129
 
132
130
  def platform_has_systemd_unit?(service_name)
@@ -18,6 +18,7 @@
18
18
 
19
19
  require 'chef/policy_builder/expand_node_object'
20
20
  require 'chef/policy_builder/policyfile'
21
+ require 'chef/policy_builder/dynamic'
21
22
 
22
23
  class Chef
23
24
 
@@ -37,13 +38,5 @@ class Chef
37
38
  # * cookbook_hash is stored in run_context
38
39
  module PolicyBuilder
39
40
 
40
- def self.strategy
41
- if Chef::Config[:use_policyfile]
42
- Policyfile
43
- else
44
- ExpandNodeObject
45
- end
46
- end
47
-
48
41
  end
49
42
  end
@@ -0,0 +1,186 @@
1
+ #
2
+ # Author:: Daniel DeLeo (<dan@chef.io>)
3
+ # Copyright:: Copyright 2015 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 'forwardable'
20
+
21
+ require 'chef/log'
22
+ require 'chef/rest'
23
+ require 'chef/run_context'
24
+ require 'chef/config'
25
+ require 'chef/node'
26
+ require 'chef/exceptions'
27
+
28
+ class Chef
29
+ module PolicyBuilder
30
+
31
+ # PolicyBuilder that selects either a Policyfile or non-Policyfile
32
+ # implementation based on the content of the node object.
33
+ class Dynamic
34
+
35
+ extend Forwardable
36
+
37
+ attr_reader :node
38
+ attr_reader :node_name
39
+ attr_reader :ohai_data
40
+ attr_reader :json_attribs
41
+ attr_reader :override_runlist
42
+ attr_reader :events
43
+
44
+ def initialize(node_name, ohai_data, json_attribs, override_runlist, events)
45
+ @implementation = nil
46
+
47
+ @node_name = node_name
48
+ @ohai_data = ohai_data
49
+ @json_attribs = json_attribs
50
+ @override_runlist = override_runlist
51
+ @events = events
52
+
53
+ @node = nil
54
+ end
55
+
56
+ ## PolicyBuilder API ##
57
+
58
+ # Loads the node state from the server, then picks the correct
59
+ # implementation class based on the node and json_attribs.
60
+ #
61
+ # Calls #finish_load_node on the implementation object to complete the
62
+ # loading process. All subsequent lifecycle calls are delegated.
63
+ #
64
+ # @return [Chef::Node] the loaded node.
65
+ def load_node
66
+ events.node_load_start(node_name, config)
67
+ Chef::Log.debug("Building node object for #{node_name}")
68
+
69
+ @node =
70
+ if Chef::Config[:solo]
71
+ Chef::Node.build(node_name)
72
+ else
73
+ Chef::Node.find_or_create(node_name)
74
+ end
75
+ select_implementation(node)
76
+ implementation.finish_load_node(node)
77
+ node
78
+ rescue Exception => e
79
+ events.node_load_failed(node_name, e, config)
80
+ raise
81
+ end
82
+
83
+ ## Delegated Public API Methods ##
84
+
85
+ ### Accessors ###
86
+
87
+ def_delegator :implementation, :original_runlist
88
+ def_delegator :implementation, :run_context
89
+ def_delegator :implementation, :run_list_expansion
90
+
91
+ ### Lifecycle Methods ###
92
+
93
+ # @!method build_node
94
+ #
95
+ # Applies external attributes (e.g., from JSON file, environment,
96
+ # policyfile, etc.) and determines the correct expanded run list for the
97
+ # run.
98
+ #
99
+ # @return [Chef::Node]
100
+ def_delegator :implementation, :build_node
101
+
102
+ # @!method setup_run_context
103
+ #
104
+ # Synchronizes cookbooks and initializes the run context object for the
105
+ # run.
106
+ #
107
+ # @return [Chef::RunContext]
108
+ def_delegator :implementation, :setup_run_context
109
+
110
+ # @!method expanded_run_list
111
+ #
112
+ # Resolves the run list to a form containing only recipes and sets the
113
+ # `roles` and `recipes` automatic attributes on the node.
114
+ #
115
+ # @return [#recipes, #roles] A RunListExpansion or duck-type.
116
+ def_delegator :implementation, :expand_run_list
117
+
118
+ # @!method sync_cookbooks
119
+ #
120
+ # Synchronizes cookbooks. In a normal chef-client run, this is handled by
121
+ # #setup_run_context, but may be called directly in some circumstances.
122
+ #
123
+ # @return [Hash{String => Chef::CookbookManifest}] A map of
124
+ # CookbookManifest objects by cookbook name.
125
+ def_delegator :implementation, :sync_cookbooks
126
+
127
+ # @!method temporary_policy?
128
+ #
129
+ # Indicates whether the policy is temporary, which means an
130
+ # override_runlist was provided. Chef::Client uses this to decide whether
131
+ # to do the final node save at the end of the run or not.
132
+ #
133
+ # @return [true,false]
134
+ def_delegator :implementation, :temporary_policy?
135
+
136
+ ## Internal Public API ##
137
+
138
+ # Returns the selected implementation, or raises if not set. The
139
+ # implementation is set when #load_node is called.
140
+ #
141
+ # @return [PolicyBuilder::Policyfile, PolicyBuilder::ExpandNodeObject]
142
+ def implementation
143
+ @implementation or raise Exceptions::InvalidPolicybuilderCall, "#load_node must be called before other policy builder methods"
144
+ end
145
+
146
+ # @api private
147
+ #
148
+ # Sets the implementation based on the content of the node, node JSON
149
+ # (i.e., the `-j JSON_FILE` data), and config. This is only public for
150
+ # testing purposes; production code should call #load_node instead.
151
+ def select_implementation(node)
152
+ if policyfile_set_in_config? ||
153
+ policyfile_attribs_in_node_json? ||
154
+ node_has_policyfile_attrs?(node) ||
155
+ policyfile_compat_mode_config?
156
+ @implementation = Policyfile.new(node_name, ohai_data, json_attribs, override_runlist, events)
157
+ else
158
+ @implementation = ExpandNodeObject.new(node_name, ohai_data, json_attribs, override_runlist, events)
159
+ end
160
+ end
161
+
162
+ def config
163
+ Chef::Config
164
+ end
165
+
166
+ private
167
+
168
+ def node_has_policyfile_attrs?(node)
169
+ node.policy_name || node.policy_group
170
+ end
171
+
172
+ def policyfile_attribs_in_node_json?
173
+ json_attribs.key?("policy_name") || json_attribs.key?("policy_group")
174
+ end
175
+
176
+ def policyfile_set_in_config?
177
+ config[:use_policyfile] || config[:policy_name] || config[:policy_group]
178
+ end
179
+
180
+ def policyfile_compat_mode_config?
181
+ config[:deployment_group] && !config[:policy_document_native_api]
182
+ end
183
+
184
+ end
185
+ end
186
+ end
@@ -33,6 +33,9 @@ class Chef
33
33
  # expands the run_list on a node object and then queries the chef-server
34
34
  # to find the correct set of cookbooks, given version constraints of the
35
35
  # node's environment.
36
+ #
37
+ # Note that this class should only be used via PolicyBuilder::Dynamic and
38
+ # not instantiated directly.
36
39
  class ExpandNodeObject
37
40
 
38
41
  attr_reader :events
@@ -55,9 +58,10 @@ class Chef
55
58
  @run_list_expansion = nil
56
59
  end
57
60
 
58
- # This method injects the run_context and provider and resource priority
59
- # maps into the Chef class. The run_context has to be injected here, the provider and
60
- # resource maps could be moved if a better place can be found to do this work.
61
+ # This method injects the run_context and into the Chef class.
62
+ #
63
+ # NOTE: This is duplicated with the Policyfile implementation. If
64
+ # it gets any more complicated, it needs to be moved elsewhere.
61
65
  #
62
66
  # @param run_context [Chef::RunContext] the run_context to inject
63
67
  def setup_chef_class(run_context)
@@ -93,25 +97,36 @@ class Chef
93
97
  run_context
94
98
  end
95
99
 
96
-
97
- # In client-server operation, loads the node state from the server. In
98
- # chef-solo operation, builds a new node object.
100
+ # DEPRECATED: As of Chef 12.5, chef selects either policyfile mode or
101
+ # "expand node" mode dynamically, based on the content of the node
102
+ # object, first boot JSON, and config. This happens in
103
+ # PolicyBuilder::Dynamic, which selects the implementation during
104
+ # #load_node and then delegates to either ExpandNodeObject or Policyfile
105
+ # implementations as appropriate. Tools authors should update their code
106
+ # to create a PolicyBuilder::Dynamc policy builder and allow it to select
107
+ # the proper implementation.
99
108
  def load_node
100
- events.node_load_start(node_name, Chef::Config)
109
+ Chef.log_deprecation("ExpandNodeObject#load_node is deprecated. Please use Chef::PolicyBuilder::Dynamic instead of using ExpandNodeObject directly")
110
+
111
+ events.node_load_start(node_name, config)
101
112
  Chef::Log.debug("Building node object for #{node_name}")
102
113
 
103
- if Chef::Config[:solo]
104
- @node = Chef::Node.build(node_name)
105
- else
106
- @node = Chef::Node.find_or_create(node_name)
107
- end
114
+ @node =
115
+ if Chef::Config[:solo]
116
+ Chef::Node.build(node_name)
117
+ else
118
+ Chef::Node.find_or_create(node_name)
119
+ end
120
+ finish_load_node(node)
121
+ node
108
122
  rescue Exception => e
109
- # TODO: wrap this exception so useful error info can be given to the
110
- # user.
111
- events.node_load_failed(node_name, e, Chef::Config)
123
+ events.node_load_failed(node_name, e, config)
112
124
  raise
113
125
  end
114
126
 
127
+ def finish_load_node(node)
128
+ @node = node
129
+ end
115
130
 
116
131
  # Applies environment, external JSON attributes, and override run list to
117
132
  # the node, Then expands the run_list.
@@ -68,22 +68,20 @@ class Chef
68
68
 
69
69
  @node = nil
70
70
 
71
- Chef::Log.warn("Using experimental Policyfile feature")
72
-
73
71
  if Chef::Config[:solo]
74
- raise UnsupportedFeature, "Policyfile does not support chef-solo at this time."
72
+ raise UnsupportedFeature, "Policyfile does not support chef-solo. Use chef-client local mode instead."
75
73
  end
76
74
 
77
75
  if override_runlist
78
- raise UnsupportedFeature, "Policyfile does not support override run lists at this time"
76
+ raise UnsupportedFeature, "Policyfile does not support override run lists. Use named run_lists instead."
79
77
  end
80
78
 
81
79
  if json_attribs && json_attribs.key?("run_list")
82
- raise UnsupportedFeature, "Policyfile does not support setting the run_list in json data at this time"
80
+ raise UnsupportedFeature, "Policyfile does not support setting the run_list in json data."
83
81
  end
84
82
 
85
83
  if Chef::Config[:environment] && !Chef::Config[:environment].chomp.empty?
86
- raise UnsupportedFeature, "Policyfile does not work with Chef Environments"
84
+ raise UnsupportedFeature, "Policyfile does not work with Chef Environments."
87
85
  end
88
86
  end
89
87
 
@@ -112,18 +110,11 @@ class Chef
112
110
 
113
111
  ## PolicyBuilder API ##
114
112
 
115
- # Loads the node state from the server.
116
- def load_node
117
- events.node_load_start(node_name, Chef::Config)
118
- Chef::Log.debug("Building node object for #{node_name}")
119
-
120
- @node = Chef::Node.find_or_create(node_name)
113
+ def finish_load_node(node)
114
+ @node = node
115
+ select_policy_name_and_group
121
116
  validate_policyfile
122
117
  events.policyfile_loaded(policy)
123
- node
124
- rescue Exception => e
125
- events.node_load_failed(node_name, e, Chef::Config)
126
- raise
127
118
  end
128
119
 
129
120
  # Applies environment, external JSON attributes, and override run list to
@@ -154,25 +145,42 @@ class Chef
154
145
  raise
155
146
  end
156
147
 
148
+ # Synchronizes cookbooks and initializes the run context object for the
149
+ # run.
150
+ #
151
+ # @return [Chef::RunContext]
157
152
  def setup_run_context(specific_recipes=nil)
158
153
  Chef::Cookbook::FileVendor.fetch_from_remote(http_api)
159
154
  sync_cookbooks
160
155
  cookbook_collection = Chef::CookbookCollection.new(cookbooks_to_sync)
161
156
  run_context = Chef::RunContext.new(node, cookbook_collection, events)
162
157
 
158
+ setup_chef_class(run_context)
159
+
163
160
  run_context.load(run_list_expansion_ish)
164
161
 
162
+ setup_chef_class(run_context)
165
163
  run_context
166
164
  end
167
165
 
166
+ # Sets `run_list` on the node from the policy, sets `roles` and `recipes`
167
+ # attributes on the node accordingly.
168
+ #
169
+ # @return [RunListExpansionIsh] A RunListExpansion duck-type.
168
170
  def expand_run_list
171
+ CookbookCacheCleaner.instance.skip_removal = true if named_run_list_requested?
172
+
169
173
  node.run_list(run_list)
170
174
  node.automatic_attrs[:roles] = []
171
175
  node.automatic_attrs[:recipes] = run_list_expansion_ish.recipes
172
176
  run_list_expansion_ish
173
177
  end
174
178
 
175
-
179
+ # Synchronizes cookbooks. In a normal chef-client run, this is handled by
180
+ # #setup_run_context, but may be called directly in some circumstances.
181
+ #
182
+ # @return [Hash{String => Chef::CookbookManifest}] A map of
183
+ # CookbookManifest objects by cookbook name.
176
184
  def sync_cookbooks
177
185
  Chef::Log.debug("Synchronizing cookbooks")
178
186
  synchronizer = Chef::CookbookSynchronizer.new(cookbooks_to_sync, events)
@@ -186,12 +194,18 @@ class Chef
186
194
 
187
195
  # Whether or not this is a temporary policy. Since PolicyBuilder doesn't
188
196
  # support override_runlist, this is always false.
197
+ #
198
+ # @return [false]
189
199
  def temporary_policy?
190
200
  false
191
201
  end
192
202
 
193
203
  ## Internal Public API ##
194
204
 
205
+ # @api private
206
+ #
207
+ # Generates an array of strings with recipe names including version and
208
+ # identifier info.
195
209
  def run_list_with_versions_for_display
196
210
  run_list.map do |recipe_spec|
197
211
  cookbook, recipe = parse_recipe_spec(recipe_spec)
@@ -201,6 +215,11 @@ class Chef
201
215
  end
202
216
  end
203
217
 
218
+ # @api private
219
+ #
220
+ # Sets up a RunListExpansionIsh object so that it can be used in place of
221
+ # a RunListExpansion object, to satisfy the API contract of
222
+ # #expand_run_list
204
223
  def run_list_expansion_ish
205
224
  recipes = run_list.map do |recipe_spec|
206
225
  cookbook, recipe = parse_recipe_spec(recipe_spec)
@@ -209,11 +228,15 @@ class Chef
209
228
  RunListExpansionIsh.new(recipes, [])
210
229
  end
211
230
 
231
+ # @api private
232
+ #
233
+ # Sets attributes from the policyfile on the node, using the role priority.
212
234
  def apply_policyfile_attributes
213
235
  node.attributes.role_default = policy["default_attributes"]
214
236
  node.attributes.role_override = policy["override_attributes"]
215
237
  end
216
238
 
239
+ # @api private
217
240
  def parse_recipe_spec(recipe_spec)
218
241
  rmatch = recipe_spec.match(/recipe\[([^:]+)::([^:]+)\]/)
219
242
  if rmatch.nil?
@@ -223,20 +246,31 @@ class Chef
223
246
  end
224
247
  end
225
248
 
249
+ # @api private
226
250
  def cookbook_lock_for(cookbook_name)
227
251
  cookbook_locks[cookbook_name]
228
252
  end
229
253
 
254
+ # @api private
230
255
  def run_list
231
- policy["run_list"]
256
+ if named_run_list_requested?
257
+ named_run_list or
258
+ raise ConfigurationError,
259
+ "Policy '#{retrieved_policy_name}' revision '#{revision_id}' does not have named_run_list '#{named_run_list_name}'" +
260
+ "(available named_run_lists: [#{available_named_run_lists.join(', ')}])"
261
+ else
262
+ policy["run_list"]
263
+ end
232
264
  end
233
265
 
266
+ # @api private
234
267
  def policy
235
268
  @policy ||= http_api.get(policyfile_location)
236
269
  rescue Net::HTTPServerException => e
237
270
  raise ConfigurationError, "Error loading policyfile from `#{policyfile_location}': #{e.class} - #{e.message}"
238
271
  end
239
272
 
273
+ # @api private
240
274
  def policyfile_location
241
275
  if Chef::Config[:policy_document_native_api]
242
276
  validate_policy_config!
@@ -273,6 +307,7 @@ class Chef
273
307
  end
274
308
  end
275
309
 
310
+ # @api private
276
311
  def validate_recipe_spec(recipe_spec)
277
312
  parse_recipe_spec(recipe_spec)
278
313
  nil
@@ -282,11 +317,13 @@ class Chef
282
317
 
283
318
  class ConfigurationError < StandardError; end
284
319
 
320
+ # @api private
285
321
  def deployment_group
286
322
  Chef::Config[:deployment_group] or
287
323
  raise ConfigurationError, "Setting `deployment_group` is not configured."
288
324
  end
289
325
 
326
+ # @api private
290
327
  def validate_policy_config!
291
328
  policy_group or
292
329
  raise ConfigurationError, "Setting `policy_group` is not configured."
@@ -295,14 +332,75 @@ class Chef
295
332
  raise ConfigurationError, "Setting `policy_name` is not configured."
296
333
  end
297
334
 
335
+ # @api private
298
336
  def policy_group
299
337
  Chef::Config[:policy_group]
300
338
  end
301
339
 
340
+ # @api private
302
341
  def policy_name
303
342
  Chef::Config[:policy_name]
304
343
  end
305
344
 
345
+ # @api private
346
+ #
347
+ # Selects the `policy_name` and `policy_group` from the following sources
348
+ # in priority order:
349
+ #
350
+ # 1. JSON attribs (i.e., `-j JSON_FILE`)
351
+ # 2. `Chef::Config`
352
+ # 3. The node object
353
+ #
354
+ # The selected values are then copied to `Chef::Config` and the node.
355
+ def select_policy_name_and_group
356
+ policy_name_to_set =
357
+ policy_name_from_json_attribs ||
358
+ policy_name_from_config ||
359
+ policy_name_from_node
360
+
361
+ policy_group_to_set =
362
+ policy_group_from_json_attribs ||
363
+ policy_group_from_config ||
364
+ policy_group_from_node
365
+
366
+ node.policy_name = policy_name_to_set
367
+ node.policy_group = policy_group_to_set
368
+
369
+ Chef::Config[:policy_name] = policy_name_to_set
370
+ Chef::Config[:policy_group] = policy_group_to_set
371
+ end
372
+
373
+ # @api private
374
+ def policy_group_from_json_attribs
375
+ json_attribs["policy_group"]
376
+ end
377
+
378
+ # @api private
379
+ def policy_name_from_json_attribs
380
+ json_attribs["policy_name"]
381
+ end
382
+
383
+ # @api private
384
+ def policy_group_from_config
385
+ Chef::Config[:policy_group]
386
+ end
387
+
388
+ # @api private
389
+ def policy_name_from_config
390
+ Chef::Config[:policy_name]
391
+ end
392
+
393
+ # @api private
394
+ def policy_group_from_node
395
+ node.policy_group
396
+ end
397
+
398
+ # @api private
399
+ def policy_name_from_node
400
+ node.policy_name
401
+ end
402
+
403
+ # @api private
306
404
  # Builds a 'cookbook_hash' map of the form
307
405
  # "COOKBOOK_NAME" => "IDENTIFIER"
308
406
  #
@@ -330,6 +428,7 @@ class Chef
330
428
  raise
331
429
  end
332
430
 
431
+ # @api private
333
432
  # Fetches the CookbookVersion object for the given name and identifer
334
433
  # specified in the lock_data.
335
434
  # TODO: This only implements Chef 11 compatibility mode, which means that
@@ -343,20 +442,58 @@ class Chef
343
442
  end
344
443
  end
345
444
 
445
+ # @api private
346
446
  def cookbook_locks
347
447
  policy["cookbook_locks"]
348
448
  end
349
449
 
450
+ # @api private
451
+ def revision_id
452
+ policy["revision_id"]
453
+ end
454
+
455
+ # @api private
350
456
  def http_api
351
457
  @api_service ||= Chef::REST.new(config[:chef_server_url])
352
458
  end
353
459
 
460
+ # @api private
354
461
  def config
355
462
  Chef::Config
356
463
  end
357
464
 
358
465
  private
359
466
 
467
+ # This method injects the run_context and into the Chef class.
468
+ #
469
+ # NOTE: This is duplicated with the ExpandNodeObject implementation. If
470
+ # it gets any more complicated, it needs to be moved elsewhere.
471
+ #
472
+ # @param run_context [Chef::RunContext] the run_context to inject
473
+ def setup_chef_class(run_context)
474
+ Chef.set_run_context(run_context)
475
+ end
476
+
477
+ def retrieved_policy_name
478
+ policy["name"]
479
+ end
480
+
481
+ def named_run_list
482
+ policy["named_run_lists"] && policy["named_run_lists"][named_run_list_name]
483
+ end
484
+
485
+ def available_named_run_lists
486
+ (policy["named_run_lists"] || {}).keys
487
+ end
488
+
489
+ def named_run_list_requested?
490
+ !!Chef::Config[:named_run_list]
491
+ end
492
+
493
+ def named_run_list_name
494
+ Chef::Config[:named_run_list]
495
+ end
496
+
360
497
  def compat_mode_manifest_for(cookbook_name, lock_data)
361
498
  xyz_version = lock_data["dotted_decimal_identifier"]
362
499
  rel_url = "cookbooks/#{cookbook_name}/#{xyz_version}"