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
@@ -0,0 +1,138 @@
1
+ # Author:: Christopher Brown (<cb@opscode.com>)
2
+ # Author:: Daniel DeLeo (<dan@opscode.com>)
3
+ # Copyright:: Copyright (c) 2009-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 'chef/version'
20
+ require 'chef/util/path_helper'
21
+ class Chef
22
+ class Knife
23
+ class SubcommandLoader
24
+ class GemGlobLoader < Chef::Knife::SubcommandLoader
25
+ MATCHES_CHEF_GEM = %r{/chef-[\d]+\.[\d]+\.[\d]+}.freeze
26
+ MATCHES_THIS_CHEF_GEM = %r{/chef-#{Chef::VERSION}(-\w+)?(-\w+)?/}.freeze
27
+
28
+ def subcommand_files
29
+ @subcommand_files ||= (gem_and_builtin_subcommands.values + site_subcommands).flatten.uniq
30
+ end
31
+
32
+ # Returns a Hash of paths to knife commands built-in to chef, or installed via gem.
33
+ # If rubygems is not installed, falls back to globbing the knife directory.
34
+ # The Hash is of the form {"relative/path" => "/absolute/path"}
35
+ #--
36
+ # Note: the "right" way to load the plugins is to require the relative path, i.e.,
37
+ # require 'chef/knife/command'
38
+ # but we're getting frustrated by bugs at every turn, and it's slow besides. So
39
+ # subcommand loader has been modified to load the plugins by using Kernel.load
40
+ # with the absolute path.
41
+ def gem_and_builtin_subcommands
42
+ require 'rubygems'
43
+ find_subcommands_via_rubygems
44
+ rescue LoadError
45
+ find_subcommands_via_dirglob
46
+ end
47
+
48
+ def find_subcommands_via_dirglob
49
+ # The "require paths" of the core knife subcommands bundled with chef
50
+ files = Dir[File.join(Chef::Util::PathHelper.escape_glob(File.expand_path('../../../knife', __FILE__)), '*.rb')]
51
+ subcommand_files = {}
52
+ files.each do |knife_file|
53
+ rel_path = knife_file[/#{CHEF_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/,1]
54
+ subcommand_files[rel_path] = knife_file
55
+ end
56
+ subcommand_files
57
+ end
58
+
59
+ def find_subcommands_via_rubygems
60
+ files = find_files_latest_gems 'chef/knife/*.rb'
61
+ subcommand_files = {}
62
+ files.each do |file|
63
+ rel_path = file[/(#{Regexp.escape File.join('chef', 'knife', '')}.*)\.rb/, 1]
64
+
65
+ # When not installed as a gem (ChefDK/appbundler in particular), AND
66
+ # a different version of Chef is installed via gems, `files` will
67
+ # include some files from the 'other' Chef install. If this contains
68
+ # a knife command that doesn't exist in this version of Chef, we will
69
+ # get a LoadError later when we try to require it.
70
+ next if from_different_chef_version?(file)
71
+
72
+ subcommand_files[rel_path] = file
73
+ end
74
+
75
+ subcommand_files.merge(find_subcommands_via_dirglob)
76
+ end
77
+
78
+ private
79
+
80
+ def find_files_latest_gems(glob, check_load_path=true)
81
+ files = []
82
+
83
+ if check_load_path
84
+ files = $LOAD_PATH.map { |load_path|
85
+ Dir["#{File.expand_path glob, Chef::Util::PathHelper.escape_glob(load_path)}#{Gem.suffix_pattern}"]
86
+ }.flatten.select { |file| File.file? file.untaint }
87
+ end
88
+
89
+ gem_files = latest_gem_specs.map do |spec|
90
+ # Gem::Specification#matches_for_glob wasn't added until RubyGems 1.8
91
+ if spec.respond_to? :matches_for_glob
92
+ spec.matches_for_glob("#{glob}#{Gem.suffix_pattern}")
93
+ else
94
+ check_spec_for_glob(spec, glob)
95
+ end
96
+ end.flatten
97
+
98
+ files.concat gem_files
99
+ files.uniq! if check_load_path
100
+
101
+ return files
102
+ end
103
+
104
+ def latest_gem_specs
105
+ @latest_gem_specs ||= if Gem::Specification.respond_to? :latest_specs
106
+ Gem::Specification.latest_specs(true) # find prerelease gems
107
+ else
108
+ Gem.source_index.latest_specs(true)
109
+ end
110
+ end
111
+
112
+ def check_spec_for_glob(spec, glob)
113
+ dirs = if spec.require_paths.size > 1 then
114
+ "{#{spec.require_paths.join(',')}}"
115
+ else
116
+ spec.require_paths.first
117
+ end
118
+
119
+ glob = File.join(Chef::Util::PathHelper.escape_glob(spec.full_gem_path, dirs), glob)
120
+
121
+ Dir[glob].map { |f| f.untaint }
122
+ end
123
+
124
+ def from_different_chef_version?(path)
125
+ matches_any_chef_gem?(path) && !matches_this_chef_gem?(path)
126
+ end
127
+
128
+ def matches_any_chef_gem?(path)
129
+ path =~ MATCHES_CHEF_GEM
130
+ end
131
+
132
+ def matches_this_chef_gem?(path)
133
+ path =~ MATCHES_THIS_CHEF_GEM
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,80 @@
1
+ # Author:: Steven Danna (<steve@chef.io>)
2
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'chef/version'
19
+ class Chef
20
+ class Knife
21
+ class SubcommandLoader
22
+ #
23
+ # Load a subcommand from a pre-computed path
24
+ # for the given command.
25
+ #
26
+ class HashedCommandLoader < Chef::Knife::SubcommandLoader
27
+ KEY = '_autogenerated_command_paths'
28
+
29
+ attr_accessor :manifest
30
+ def initialize(chef_config_dir, plugin_manifest)
31
+ super(chef_config_dir)
32
+ @manifest = plugin_manifest
33
+ end
34
+
35
+ def guess_category(args)
36
+ category_words = positional_arguments(args)
37
+ category_words.map! { |w| w.split('-') }.flatten!
38
+ find_longest_key(manifest[KEY]["plugins_by_category"], category_words, ' ')
39
+ end
40
+
41
+ def list_commands(pref_category=nil)
42
+ if pref_category || manifest[KEY]["plugins_by_category"].key?(pref_category)
43
+ { pref_category => manifest[KEY]["plugins_by_category"][pref_category] }
44
+ else
45
+ manifest[KEY]["plugins_by_category"]
46
+ end
47
+ end
48
+
49
+ def subcommand_files
50
+ manifest[KEY]["plugins_paths"].values.flatten
51
+ end
52
+
53
+ def load_command(args)
54
+ paths = manifest[KEY]["plugins_paths"][subcommand_for_args(args)]
55
+ if paths.nil? || paths.empty? || (! paths.is_a? Array)
56
+ false
57
+ else
58
+ paths.each do |sc|
59
+ if File.exists?(sc)
60
+ Kernel.load sc
61
+ else
62
+ Chef::Log.error "The file #{sc} is missing for subcommand '#{subcommand_for_args(args)}'. Please rehash to update the subcommands cache."
63
+ return false
64
+ end
65
+ end
66
+ true
67
+ end
68
+ end
69
+
70
+ def subcommand_for_args(args)
71
+ if manifest[KEY]["plugins_paths"].key?(args)
72
+ args
73
+ else
74
+ find_longest_key(manifest[KEY]["plugins_paths"], args, "_")
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -67,7 +67,12 @@ class Chef
67
67
  result = {}
68
68
 
69
69
  result["name"] = node.name
70
- result["chef_environment"] = node.chef_environment
70
+ if node.policy_name.nil? && node.policy_group.nil?
71
+ result["chef_environment"] = node.chef_environment
72
+ else
73
+ result["policy_name"] = node.policy_name
74
+ result["policy_group"] = node.policy_group
75
+ end
71
76
  result["run_list"] = node.run_list
72
77
  result["normal"] = node.normal_attrs
73
78
 
@@ -95,11 +100,29 @@ class Chef
95
100
 
96
101
  summarized=<<-SUMMARY
97
102
  #{ui.color('Node Name:', :bold)} #{ui.color(node.name, :bold)}
103
+ SUMMARY
104
+ show_policy = !(node.policy_name.nil? && node.policy_group.nil?)
105
+ if show_policy
106
+ summarized << <<-POLICY
107
+ #{key('Policy Name:')} #{node.policy_name}
108
+ #{key('Policy Group:')} #{node.policy_group}
109
+ POLICY
110
+ else
111
+ summarized << <<-ENV
98
112
  #{key('Environment:')} #{node.chef_environment}
113
+ ENV
114
+ end
115
+ summarized << <<-SUMMARY
99
116
  #{key('FQDN:')} #{node[:fqdn]}
100
117
  #{key('IP:')} #{ip}
101
118
  #{key('Run List:')} #{node.run_list}
119
+ SUMMARY
120
+ unless show_policy
121
+ summarized << <<-ROLES
102
122
  #{key('Roles:')} #{Array(node[:roles]).join(', ')}
123
+ ROLES
124
+ end
125
+ summarized << <<-SUMMARY
103
126
  #{key('Recipes:')} #{Array(node[:recipes]).join(', ')}
104
127
  #{key('Platform:')} #{node[:platform]} #{node[:platform_version]}
105
128
  #{key('Tags:')} #{Array(node[:tags]).join(', ')}
@@ -18,6 +18,7 @@
18
18
 
19
19
  require 'ffi_yajl'
20
20
  require 'chef/util/path_helper'
21
+ require 'chef/data_bag_item'
21
22
 
22
23
  class Chef
23
24
  class Knife
@@ -18,105 +18,120 @@
18
18
 
19
19
  require 'chef/version'
20
20
  require 'chef/util/path_helper'
21
+ require 'chef/knife/core/gem_glob_loader'
22
+ require 'chef/knife/core/hashed_command_loader'
23
+ require 'chef/knife/core/custom_manifest_loader'
24
+
21
25
  class Chef
22
26
  class Knife
27
+ #
28
+ # Public Methods of a Subcommand Loader
29
+ #
30
+ # load_commands - loads all available subcommands
31
+ # load_command(args) - loads subcommands for the given args
32
+ # list_commands(args) - lists all available subcommands,
33
+ # optionally filtering by category
34
+ # subcommand_files - returns an array of all subcommand files
35
+ # that could be loaded
36
+ # commnad_class_from(args) - returns the subcommand class for the
37
+ # user-requested command
38
+ #
23
39
  class SubcommandLoader
24
-
25
- MATCHES_CHEF_GEM = %r{/chef-[\d]+\.[\d]+\.[\d]+}.freeze
26
- MATCHES_THIS_CHEF_GEM = %r{/chef-#{Chef::VERSION}(-\w+)?(-\w+)?/}.freeze
27
-
28
40
  attr_reader :chef_config_dir
29
41
  attr_reader :env
30
42
 
31
- def initialize(chef_config_dir, env=nil)
43
+ # A small factory method. Eventually, this is the only place
44
+ # where SubcommandLoader should know about its subclasses, but
45
+ # to maintain backwards compatibility many of the instance
46
+ # methods in this base class contain default implementations
47
+ # of the functions sub classes should otherwise provide
48
+ # or directly instantiate the appropriate subclass
49
+ def self.for_config(chef_config_dir)
50
+ if autogenerated_manifest?
51
+ Chef::Log.debug("Using autogenerated hashed command manifest #{plugin_manifest_path}")
52
+ Knife::SubcommandLoader::HashedCommandLoader.new(chef_config_dir, plugin_manifest)
53
+ elsif custom_manifest?
54
+ Chef.log_deprecation("Using custom manifest #{plugin_manifest_path} is deprecated. Please use a `knife rehash` autogenerated manifest instead.")
55
+ Knife::SubcommandLoader::CustomManifestLoader.new(chef_config_dir, plugin_manifest)
56
+ else
57
+ Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir)
58
+ end
59
+ end
60
+
61
+ def self.plugin_manifest?
62
+ plugin_manifest_path && File.exist?(plugin_manifest_path)
63
+ end
64
+
65
+ def self.autogenerated_manifest?
66
+ plugin_manifest? && plugin_manifest.key?(HashedCommandLoader::KEY)
67
+ end
68
+
69
+ def self.custom_manifest?
70
+ plugin_manifest? && plugin_manifest.key?("plugins")
71
+ end
72
+
73
+ def self.plugin_manifest
74
+ Chef::JSONCompat.from_json(File.read(plugin_manifest_path))
75
+ end
76
+
77
+ def self.plugin_manifest_path
78
+ Chef::Util::PathHelper.home('.chef', 'plugin_manifest.json')
79
+ end
80
+
81
+ def initialize(chef_config_dir, env = nil)
32
82
  @chef_config_dir = chef_config_dir
33
- @forced_activate = {}
34
83
 
35
84
  # Deprecated and un-used instance variable.
36
85
  @env = env
37
86
  unless env.nil?
38
- Chef::Log.deprecation("The env argument to Chef::Knife::SubcommandLoader is deprecated. If you are using env to inject/mock HOME, consider mocking Chef::Util::PathHelper.home instead.")
87
+ Chef.log_deprecation("The env argument to Chef::Knife::SubcommandLoader is deprecated. If you are using env to inject/mock HOME, consider mocking Chef::Util::PathHelper.home instead.")
39
88
  end
40
89
  end
41
90
 
42
91
  # Load all the sub-commands
43
92
  def load_commands
93
+ return true if @loaded
44
94
  subcommand_files.each { |subcommand| Kernel.load subcommand }
45
- true
95
+ @loaded = true
46
96
  end
47
97
 
48
- # Returns an Array of paths to knife commands located in chef_config_dir/plugins/knife/
49
- # and ~/.chef/plugins/knife/
50
- def site_subcommands
51
- user_specific_files = []
52
-
53
- if chef_config_dir
54
- user_specific_files.concat Dir.glob(File.expand_path("plugins/knife/*.rb", Chef::Util::PathHelper.escape_glob(chef_config_dir)))
55
- end
56
-
57
- # finally search ~/.chef/plugins/knife/*.rb
58
- Chef::Util::PathHelper.home('.chef', 'plugins', 'knife') do |p|
59
- user_specific_files.concat Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(p), '*.rb'))
60
- end
98
+ def force_load
99
+ @loaded=false
100
+ load_commands
101
+ end
61
102
 
62
- user_specific_files
103
+ def load_command(_command_args)
104
+ load_commands
63
105
  end
64
106
 
65
- # Returns a Hash of paths to knife commands built-in to chef, or installed via gem.
66
- # If rubygems is not installed, falls back to globbing the knife directory.
67
- # The Hash is of the form {"relative/path" => "/absolute/path"}
68
- #--
69
- # Note: the "right" way to load the plugins is to require the relative path, i.e.,
70
- # require 'chef/knife/command'
71
- # but we're getting frustrated by bugs at every turn, and it's slow besides. So
72
- # subcommand loader has been modified to load the plugins by using Kernel.load
73
- # with the absolute path.
74
- def gem_and_builtin_subcommands
75
- if have_plugin_manifest?
76
- find_subcommands_via_manifest
107
+ def list_commands(pref_cat = nil)
108
+ load_commands
109
+ if pref_cat && Chef::Knife.subcommands_by_category.key?(pref_cat)
110
+ { pref_cat => Chef::Knife.subcommands_by_category[pref_cat] }
77
111
  else
78
- # search all gems for chef/knife/*.rb
79
- require 'rubygems'
80
- find_subcommands_via_rubygems
112
+ Chef::Knife.subcommands_by_category
81
113
  end
82
- rescue LoadError
83
- find_subcommands_via_dirglob
84
114
  end
85
115
 
86
- def subcommand_files
87
- @subcommand_files ||= (gem_and_builtin_subcommands.values + site_subcommands).flatten.uniq
116
+ def command_class_from(args)
117
+ cmd_words = positional_arguments(args)
118
+ load_command(cmd_words)
119
+ result = Chef::Knife.subcommands[find_longest_key(Chef::Knife.subcommands,
120
+ cmd_words, '_')]
121
+ result || Chef::Knife.subcommands[args.first.gsub('-', '_')]
88
122
  end
89
123
 
90
- # If the user has created a ~/.chef/plugin_manifest.json file, we'll use
91
- # that instead of inspecting the on-system gems to find the plugins. The
92
- # file format is expected to look like:
93
- #
94
- # { "plugins": {
95
- # "knife-ec2": {
96
- # "paths": [
97
- # "/home/alice/.rubymanagerthing/gems/knife-ec2-x.y.z/lib/chef/knife/ec2_server_create.rb",
98
- # "/home/alice/.rubymanagerthing/gems/knife-ec2-x.y.z/lib/chef/knife/ec2_server_delete.rb"
99
- # ]
100
- # }
101
- # }
102
- # }
103
- #
104
- # Extraneous content in this file is ignored. This intentional so that we
105
- # can adapt the file format for potential behavior changes to knife in
106
- # the future.
107
- def find_subcommands_via_manifest
108
- # Format of subcommand_files is "relative_path" (something you can
109
- # Kernel.require()) => full_path. The relative path isn't used
110
- # currently, so we just map full_path => full_path.
111
- subcommand_files = {}
112
- plugin_manifest["plugins"].each do |plugin_name, plugin_manifest|
113
- plugin_manifest["paths"].each do |cmd_path|
114
- subcommand_files[cmd_path] = cmd_path
115
- end
116
- end
117
- subcommand_files.merge(find_subcommands_via_dirglob)
124
+ def guess_category(args)
125
+ category_words = positional_arguments(args)
126
+ category_words.map! { |w| w.split('-') }.flatten!
127
+ find_longest_key(Chef::Knife.subcommands_by_category,
128
+ category_words, ' ')
118
129
  end
119
130
 
131
+
132
+ #
133
+ # This is shared between the custom_manifest_loader and the gem_glob_loader
134
+ #
120
135
  def find_subcommands_via_dirglob
121
136
  # The "require paths" of the core knife subcommands bundled with chef
122
137
  files = Dir[File.join(Chef::Util::PathHelper.escape_glob(File.expand_path('../../../knife', __FILE__)), '*.rb')]
@@ -128,95 +143,65 @@ class Chef
128
143
  subcommand_files
129
144
  end
130
145
 
131
- def find_subcommands_via_rubygems
132
- files = find_files_latest_gems 'chef/knife/*.rb'
133
- subcommand_files = {}
134
- files.each do |file|
135
- rel_path = file[/(#{Regexp.escape File.join('chef', 'knife', '')}.*)\.rb/, 1]
136
-
137
- # When not installed as a gem (ChefDK/appbundler in particular), AND
138
- # a different version of Chef is installed via gems, `files` will
139
- # include some files from the 'other' Chef install. If this contains
140
- # a knife command that doesn't exist in this version of Chef, we will
141
- # get a LoadError later when we try to require it.
142
- next if from_different_chef_version?(file)
143
-
144
- subcommand_files[rel_path] = file
145
- end
146
-
147
- subcommand_files.merge(find_subcommands_via_dirglob)
148
- end
149
-
150
- def have_plugin_manifest?
151
- plugin_manifest_path && File.exist?(plugin_manifest_path)
152
- end
153
-
154
- def plugin_manifest
155
- Chef::JSONCompat.from_json(File.read(plugin_manifest_path))
156
- end
157
-
158
- def plugin_manifest_path
159
- Chef::Util::PathHelper.home('.chef', 'plugin_manifest.json')
146
+ #
147
+ # Subclassses should define this themselves. Eventually, this will raise a
148
+ # NotImplemented error, but for now, we mimic the behavior the user was likely
149
+ # to get in the past.
150
+ #
151
+ def subcommand_files
152
+ Chef.log_deprecation "Using Chef::Knife::SubcommandLoader directly is deprecated.
153
+ Please use Chef::Knife::SubcommandLoader.for_config(chef_config_dir, env)"
154
+ @subcommand_files ||= if Chef::Knife::SubcommandLoader.plugin_manifest?
155
+ Chef::Knife::SubcommandLoader::CustomManifestLoader.new(chef_config_dir, env).subcommand_files
156
+ else
157
+ Chef::Knife::SubcommandLoader::GemGlobLoader.new(chef_config_dir, env).subcommand_files
158
+ end
160
159
  end
161
160
 
162
- private
163
-
164
- def find_files_latest_gems(glob, check_load_path=true)
165
- files = []
166
-
167
- if check_load_path
168
- files = $LOAD_PATH.map { |load_path|
169
- Dir["#{File.expand_path glob, Chef::Util::PathHelper.escape_glob(load_path)}#{Gem.suffix_pattern}"]
170
- }.flatten.select { |file| File.file? file.untaint }
171
- end
172
-
173
- gem_files = latest_gem_specs.map do |spec|
174
- # Gem::Specification#matches_for_glob wasn't added until RubyGems 1.8
175
- if spec.respond_to? :matches_for_glob
176
- spec.matches_for_glob("#{glob}#{Gem.suffix_pattern}")
161
+ #
162
+ # Utility function for finding an element in a hash given an array
163
+ # of words and a separator. We find the the longest key in the
164
+ # hash composed of the given words joined by the separator.
165
+ #
166
+ def find_longest_key(hash, words, sep = '_')
167
+ match = nil
168
+ until match || words.empty?
169
+ candidate = words.join(sep)
170
+ if hash.key?(candidate)
171
+ match = candidate
177
172
  else
178
- check_spec_for_glob(spec, glob)
173
+ words.pop
179
174
  end
180
- end.flatten
181
-
182
- files.concat gem_files
183
- files.uniq! if check_load_path
184
-
185
- return files
186
- end
187
-
188
- def latest_gem_specs
189
- @latest_gem_specs ||= if Gem::Specification.respond_to? :latest_specs
190
- Gem::Specification.latest_specs(true) # find prerelease gems
191
- else
192
- Gem.source_index.latest_specs(true)
193
175
  end
176
+ match
194
177
  end
195
178
 
196
- def check_spec_for_glob(spec, glob)
197
- dirs = if spec.require_paths.size > 1 then
198
- "{#{spec.require_paths.join(',')}}"
199
- else
200
- spec.require_paths.first
201
- end
202
-
203
- glob = File.join(Chef::Util::PathHelper.escape_glob(spec.full_gem_path, dirs), glob)
204
-
205
- Dir[glob].map { |f| f.untaint }
179
+ #
180
+ # The positional arguments from the argument list provided by the
181
+ # users. Used to search for subcommands and categories.
182
+ #
183
+ # @return [Array<String>]
184
+ #
185
+ def positional_arguments(args)
186
+ args.select { |arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }
206
187
  end
207
188
 
208
- def from_different_chef_version?(path)
209
- matches_any_chef_gem?(path) && !matches_this_chef_gem?(path)
210
- end
189
+ # Returns an Array of paths to knife commands located in
190
+ # chef_config_dir/plugins/knife/ and ~/.chef/plugins/knife/
191
+ def site_subcommands
192
+ user_specific_files = []
211
193
 
212
- def matches_any_chef_gem?(path)
213
- path =~ MATCHES_CHEF_GEM
214
- end
194
+ if chef_config_dir
195
+ user_specific_files.concat Dir.glob(File.expand_path("plugins/knife/*.rb", Chef::Util::PathHelper.escape_glob(chef_config_dir)))
196
+ end
215
197
 
216
- def matches_this_chef_gem?(path)
217
- path =~ MATCHES_THIS_CHEF_GEM
218
- end
198
+ # finally search ~/.chef/plugins/knife/*.rb
199
+ Chef::Util::PathHelper.home('.chef', 'plugins', 'knife') do |p|
200
+ user_specific_files.concat Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(p), '*.rb'))
201
+ end
219
202
 
203
+ user_specific_files
204
+ end
220
205
  end
221
206
  end
222
207
  end