ohai 6.24.2 → 7.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/lib/ohai.rb +1 -1
  2. data/lib/ohai/application.rb +5 -12
  3. data/lib/ohai/common/dmi.rb +126 -0
  4. data/lib/ohai/config.rb +9 -9
  5. data/lib/ohai/{plugins/darwin/hostname.rb → dsl.rb} +7 -7
  6. data/lib/ohai/dsl/plugin.rb +186 -0
  7. data/lib/ohai/dsl/plugin/versionvi.rb +62 -0
  8. data/lib/ohai/dsl/plugin/versionvii.rb +106 -0
  9. data/lib/ohai/exception.rb +11 -2
  10. data/lib/ohai/hints.rb +48 -0
  11. data/lib/ohai/loader.rb +205 -0
  12. data/lib/ohai/mixin/command.rb +14 -18
  13. data/lib/ohai/mixin/constant_helper.rb +53 -0
  14. data/lib/ohai/mixin/gce_metadata.rb +0 -2
  15. data/lib/ohai/{plugins/freebsd/uptime.rb → mixin/network_constants.rb} +15 -13
  16. data/lib/ohai/mixin/os.rb +59 -0
  17. data/lib/ohai/mixin/seconds_to_human.rb +48 -0
  18. data/lib/ohai/plugins/aix/cpu.rb +26 -22
  19. data/lib/ohai/plugins/aix/filesystem.rb +47 -48
  20. data/lib/ohai/plugins/aix/kernel.rb +11 -7
  21. data/lib/ohai/plugins/aix/memory.rb +8 -4
  22. data/lib/ohai/plugins/aix/network.rb +73 -82
  23. data/lib/ohai/plugins/aix/platform.rb +9 -6
  24. data/lib/ohai/plugins/aix/uptime.rb +14 -12
  25. data/lib/ohai/plugins/azure.rb +33 -11
  26. data/lib/ohai/plugins/c.rb +103 -78
  27. data/lib/ohai/plugins/chef.rb +20 -8
  28. data/lib/ohai/plugins/cloud.rb +234 -230
  29. data/lib/ohai/plugins/command.rb +6 -2
  30. data/lib/ohai/plugins/darwin/cpu.rb +10 -4
  31. data/lib/ohai/plugins/darwin/filesystem.rb +32 -32
  32. data/lib/ohai/plugins/darwin/network.rb +157 -154
  33. data/lib/ohai/plugins/darwin/platform.rb +18 -16
  34. data/lib/ohai/plugins/darwin/system_profiler.rb +49 -46
  35. data/lib/ohai/plugins/dmi.rb +102 -102
  36. data/lib/ohai/plugins/ec2.rb +30 -28
  37. data/lib/ohai/plugins/erlang.rb +19 -16
  38. data/lib/ohai/plugins/eucalyptus.rb +37 -36
  39. data/lib/ohai/plugins/freebsd/cpu.rb +34 -29
  40. data/lib/ohai/plugins/freebsd/filesystem.rb +32 -32
  41. data/lib/ohai/plugins/freebsd/memory.rb +35 -25
  42. data/lib/ohai/plugins/freebsd/network.rb +94 -89
  43. data/lib/ohai/plugins/freebsd/platform.rb +10 -4
  44. data/lib/ohai/plugins/freebsd/virtualization.rb +62 -61
  45. data/lib/ohai/plugins/gce.rb +30 -36
  46. data/lib/ohai/plugins/groovy.rb +14 -11
  47. data/lib/ohai/plugins/hostname.rb +132 -10
  48. data/lib/ohai/plugins/ip_scopes.rb +31 -16
  49. data/lib/ohai/plugins/java.rb +45 -21
  50. data/lib/ohai/plugins/kernel.rb +186 -19
  51. data/lib/ohai/plugins/keys.rb +6 -2
  52. data/lib/ohai/plugins/languages.rb +6 -2
  53. data/lib/ohai/plugins/linode.rb +38 -30
  54. data/lib/ohai/plugins/linux/block_device.rb +19 -15
  55. data/lib/ohai/plugins/linux/cpu.rb +42 -38
  56. data/lib/ohai/plugins/linux/filesystem.rb +70 -130
  57. data/lib/ohai/plugins/linux/lsb.rb +35 -35
  58. data/lib/ohai/plugins/linux/memory.rb +65 -61
  59. data/lib/ohai/plugins/linux/network.rb +343 -362
  60. data/lib/ohai/plugins/linux/platform.rb +90 -88
  61. data/lib/ohai/plugins/linux/virtualization.rb +95 -90
  62. data/lib/ohai/plugins/lua.rb +14 -10
  63. data/lib/ohai/plugins/mono.rb +17 -13
  64. data/lib/ohai/plugins/netbsd/cpu.rb +25 -23
  65. data/lib/ohai/plugins/netbsd/filesystem.rb +32 -32
  66. data/lib/ohai/plugins/netbsd/memory.rb +59 -59
  67. data/lib/ohai/plugins/netbsd/network.rb +92 -88
  68. data/lib/ohai/plugins/netbsd/platform.rb +10 -3
  69. data/lib/ohai/plugins/netbsd/virtualization.rb +41 -38
  70. data/lib/ohai/plugins/network.rb +131 -130
  71. data/lib/ohai/plugins/network_listeners.rb +26 -22
  72. data/lib/ohai/plugins/nodejs.rb +14 -10
  73. data/lib/ohai/plugins/ohai.rb +11 -6
  74. data/lib/ohai/plugins/ohai_time.rb +6 -2
  75. data/lib/ohai/plugins/openbsd/cpu.rb +20 -15
  76. data/lib/ohai/plugins/openbsd/filesystem.rb +32 -32
  77. data/lib/ohai/plugins/openbsd/memory.rb +59 -59
  78. data/lib/ohai/plugins/openbsd/network.rb +92 -88
  79. data/lib/ohai/plugins/openbsd/platform.rb +10 -4
  80. data/lib/ohai/plugins/openbsd/virtualization.rb +41 -39
  81. data/lib/ohai/plugins/openstack.rb +23 -21
  82. data/lib/ohai/plugins/os.rb +8 -32
  83. data/lib/ohai/plugins/passwd.rb +27 -22
  84. data/lib/ohai/plugins/perl.rb +21 -16
  85. data/lib/ohai/plugins/php.rb +15 -12
  86. data/lib/ohai/plugins/platform.rb +9 -9
  87. data/lib/ohai/plugins/{linux/kernel.rb → ps.rb} +13 -12
  88. data/lib/ohai/plugins/python.rb +16 -12
  89. data/lib/ohai/plugins/rackspace.rb +82 -77
  90. data/lib/ohai/plugins/root_group.rb +13 -9
  91. data/lib/ohai/plugins/ruby.rb +50 -48
  92. data/lib/ohai/plugins/sigar/cpu.rb +21 -17
  93. data/lib/ohai/plugins/sigar/filesystem.rb +26 -23
  94. data/lib/ohai/plugins/sigar/memory.rb +17 -14
  95. data/lib/ohai/plugins/sigar/network.rb +133 -67
  96. data/lib/ohai/plugins/sigar/network_route.rb +33 -28
  97. data/lib/ohai/plugins/sigar/platform.rb +10 -6
  98. data/lib/ohai/plugins/solaris2/cpu.rb +41 -33
  99. data/lib/ohai/plugins/solaris2/dmi.rb +146 -146
  100. data/lib/ohai/plugins/solaris2/filesystem.rb +74 -77
  101. data/lib/ohai/plugins/{aix/hostname.rb → solaris2/memory.rb} +7 -4
  102. data/lib/ohai/plugins/solaris2/network.rb +91 -86
  103. data/lib/ohai/plugins/solaris2/platform.rb +40 -36
  104. data/lib/ohai/plugins/solaris2/virtualization.rb +54 -59
  105. data/lib/ohai/plugins/solaris2/zpools.rb +38 -38
  106. data/lib/ohai/plugins/ssh_host_key.rb +49 -45
  107. data/lib/ohai/plugins/uptime.rb +87 -25
  108. data/lib/ohai/plugins/virtualization.rb +59 -55
  109. data/lib/ohai/plugins/windows/cpu.rb +44 -40
  110. data/lib/ohai/plugins/windows/filesystem.rb +28 -22
  111. data/lib/ohai/plugins/windows/network.rb +88 -79
  112. data/lib/ohai/plugins/windows/platform.rb +10 -3
  113. data/lib/ohai/provides_map.rb +184 -0
  114. data/lib/ohai/runner.rb +109 -0
  115. data/lib/ohai/system.rb +103 -184
  116. data/lib/ohai/version.rb +4 -3
  117. data/spec/data/plugins/___lib64___libc.so.6.output +135 -0
  118. data/spec/data/plugins/___lib___libc.so.6.output +135 -0
  119. data/spec/data/plugins/cc.output +135 -0
  120. data/spec/data/plugins/cl.output +135 -0
  121. data/spec/data/plugins/devenv.com.output +135 -0
  122. data/spec/data/plugins/env.output +71 -0
  123. data/spec/data/plugins/erl.output +144 -0
  124. data/spec/data/plugins/gcc.output +135 -0
  125. data/spec/data/plugins/groovy.output +159 -0
  126. data/spec/data/plugins/java.output +143 -0
  127. data/spec/data/plugins/lua.output +143 -0
  128. data/spec/data/plugins/messages.rb +8 -0
  129. data/spec/data/plugins/node.output +143 -0
  130. data/spec/data/plugins/perl.output +143 -0
  131. data/spec/data/plugins/php.output +143 -0
  132. data/spec/data/plugins/python.output +143 -0
  133. data/spec/data/plugins/uname.output +71 -0
  134. data/spec/data/plugins/v6message.rb +2 -0
  135. data/spec/data/plugins/v7message.rb +7 -0
  136. data/spec/data/plugins/what.output +135 -0
  137. data/spec/data/plugins/xlc.output +135 -0
  138. data/spec/spec_helper.rb +52 -18
  139. data/spec/support/integration_helper.rb +44 -0
  140. data/spec/unit/dsl/plugin_spec.rb +298 -0
  141. data/spec/unit/hints_spec.rb +65 -0
  142. data/spec/unit/loader_spec.rb +226 -0
  143. data/spec/unit/mixin/ec2_metadata_spec.rb +9 -9
  144. data/spec/unit/plugins/aix/cpu_spec.rb +17 -18
  145. data/spec/unit/plugins/aix/filesystem_spec.rb +20 -18
  146. data/spec/unit/plugins/aix/hostname_spec.rb +8 -4
  147. data/spec/unit/plugins/aix/kernel_spec.rb +13 -12
  148. data/spec/unit/plugins/aix/network_spec.rb +66 -45
  149. data/spec/unit/plugins/aix/platform_spec.rb +11 -10
  150. data/spec/unit/plugins/aix/uptime_spec.rb +7 -8
  151. data/spec/unit/plugins/azure_spec.rb +20 -21
  152. data/spec/unit/plugins/c_spec.rb +88 -87
  153. data/spec/unit/plugins/chef_spec.rb +5 -6
  154. data/spec/unit/plugins/cloud_spec.rb +81 -82
  155. data/spec/unit/plugins/darwin/cpu_spec.rb +8 -9
  156. data/spec/unit/plugins/darwin/hostname_spec.rb +23 -11
  157. data/spec/unit/plugins/darwin/kernel_spec.rb +39 -16
  158. data/spec/unit/plugins/darwin/network_spec.rb +375 -378
  159. data/spec/unit/plugins/darwin/platform_spec.rb +23 -50
  160. data/spec/unit/plugins/darwin/system_profiler_spec.rb +7 -8
  161. data/spec/unit/plugins/dmi_spec.rb +7 -12
  162. data/spec/unit/plugins/ec2_spec.rb +70 -59
  163. data/spec/unit/plugins/erlang_spec.rb +18 -20
  164. data/spec/unit/plugins/eucalyptus_spec.rb +37 -33
  165. data/spec/unit/plugins/fail_spec.rb +276 -23
  166. data/spec/unit/plugins/freebsd/hostname_spec.rb +11 -9
  167. data/spec/unit/plugins/freebsd/kernel_spec.rb +8 -8
  168. data/spec/unit/plugins/freebsd/platform_spec.rb +9 -10
  169. data/spec/unit/plugins/freebsd/virtualization_spec.rb +32 -32
  170. data/spec/unit/plugins/gce_spec.rb +19 -25
  171. data/spec/unit/plugins/groovy_spec.rb +10 -16
  172. data/spec/unit/plugins/hostname_spec.rb +45 -20
  173. data/spec/unit/plugins/ip_scopes_spec.rb +97 -0
  174. data/spec/unit/plugins/java_spec.rb +145 -99
  175. data/spec/unit/plugins/kernel_spec.rb +51 -15
  176. data/spec/unit/plugins/linode_spec.rb +31 -29
  177. data/spec/unit/plugins/linux/cpu_spec.rb +42 -43
  178. data/spec/unit/plugins/linux/filesystem_spec.rb +143 -324
  179. data/spec/unit/plugins/linux/hostname_spec.rb +16 -23
  180. data/spec/unit/plugins/linux/kernel_spec.rb +22 -8
  181. data/spec/unit/plugins/linux/lsb_spec.rb +54 -56
  182. data/spec/unit/plugins/linux/network_spec.rb +196 -238
  183. data/spec/unit/plugins/linux/platform_spec.rb +186 -205
  184. data/spec/unit/plugins/linux/uptime_spec.rb +13 -25
  185. data/spec/unit/plugins/linux/virtualization_spec.rb +149 -129
  186. data/spec/unit/plugins/lua_spec.rb +12 -15
  187. data/spec/unit/plugins/mono_spec.rb +10 -15
  188. data/spec/unit/plugins/netbsd/hostname_spec.rb +17 -7
  189. data/spec/unit/plugins/netbsd/kernel_spec.rb +8 -8
  190. data/spec/unit/plugins/netbsd/platform_spec.rb +8 -10
  191. data/spec/unit/plugins/network_spec.rb +358 -235
  192. data/spec/unit/plugins/nodejs_spec.rb +13 -15
  193. data/spec/unit/plugins/ohai_spec.rb +3 -4
  194. data/spec/unit/plugins/ohai_time_spec.rb +8 -9
  195. data/spec/unit/plugins/openbsd/hostname_spec.rb +21 -11
  196. data/spec/unit/plugins/openbsd/kernel_spec.rb +8 -8
  197. data/spec/unit/plugins/openbsd/platform_spec.rb +6 -12
  198. data/spec/unit/plugins/openstack_spec.rb +214 -0
  199. data/spec/unit/plugins/os_spec.rb +13 -16
  200. data/spec/unit/plugins/passwd_spec.rb +12 -13
  201. data/spec/unit/plugins/perl_spec.rb +20 -44
  202. data/spec/unit/plugins/php_spec.rb +20 -15
  203. data/spec/unit/plugins/platform_spec.rb +24 -29
  204. data/spec/unit/plugins/python_spec.rb +13 -17
  205. data/spec/unit/plugins/rackspace_spec.rb +90 -57
  206. data/spec/unit/plugins/root_group_spec.rb +17 -19
  207. data/spec/unit/plugins/ruby_spec.rb +15 -16
  208. data/spec/unit/plugins/sigar/network_route_spec.rb +12 -13
  209. data/spec/unit/plugins/solaris2/cpu_spec.rb +82 -0
  210. data/spec/unit/plugins/solaris2/hostname_spec.rb +12 -13
  211. data/spec/unit/plugins/solaris2/kernel_spec.rb +11 -15
  212. data/{lib/ohai/plugins/openbsd/uptime.rb → spec/unit/plugins/solaris2/memory_spec.rb} +10 -12
  213. data/spec/unit/plugins/solaris2/network_spec.rb +44 -20
  214. data/spec/unit/plugins/solaris2/platform_spec.rb +13 -22
  215. data/spec/unit/plugins/solaris2/virtualization_spec.rb +37 -45
  216. data/spec/unit/plugins/ssh_host_keys_spec.rb +11 -15
  217. data/spec/unit/provides_map_spec.rb +208 -0
  218. data/spec/unit/runner_spec.rb +488 -0
  219. data/spec/unit/system_spec.rb +639 -104
  220. metadata +151 -87
  221. checksums.yaml +0 -7
  222. data/lib/ohai/mixin/from_file.rb +0 -36
  223. data/lib/ohai/plugins/aix/ps.rb +0 -23
  224. data/lib/ohai/plugins/darwin/kernel.rb +0 -37
  225. data/lib/ohai/plugins/darwin/ps.rb +0 -23
  226. data/lib/ohai/plugins/darwin/uptime.rb +0 -32
  227. data/lib/ohai/plugins/dmi_common.rb +0 -121
  228. data/lib/ohai/plugins/freebsd/hostname.rb +0 -22
  229. data/lib/ohai/plugins/freebsd/kernel.rb +0 -37
  230. data/lib/ohai/plugins/freebsd/ps.rb +0 -24
  231. data/lib/ohai/plugins/hpux/cpu.rb +0 -19
  232. data/lib/ohai/plugins/hpux/filesystem.rb +0 -19
  233. data/lib/ohai/plugins/hpux/hostname.rb +0 -19
  234. data/lib/ohai/plugins/hpux/memory.rb +0 -19
  235. data/lib/ohai/plugins/hpux/network.rb +0 -19
  236. data/lib/ohai/plugins/hpux/platform.rb +0 -19
  237. data/lib/ohai/plugins/hpux/ps.rb +0 -23
  238. data/lib/ohai/plugins/hpux/uptime.rb +0 -19
  239. data/lib/ohai/plugins/linux/hostname.rb +0 -40
  240. data/lib/ohai/plugins/linux/ps.rb +0 -23
  241. data/lib/ohai/plugins/linux/uptime.rb +0 -28
  242. data/lib/ohai/plugins/netbsd/hostname.rb +0 -22
  243. data/lib/ohai/plugins/netbsd/kernel.rb +0 -35
  244. data/lib/ohai/plugins/netbsd/ps.rb +0 -24
  245. data/lib/ohai/plugins/netbsd/uptime.rb +0 -31
  246. data/lib/ohai/plugins/openbsd/hostname.rb +0 -22
  247. data/lib/ohai/plugins/openbsd/kernel.rb +0 -35
  248. data/lib/ohai/plugins/openbsd/ps.rb +0 -24
  249. data/lib/ohai/plugins/sigar/hostname.rb +0 -28
  250. data/lib/ohai/plugins/sigar/uptime.rb +0 -27
  251. data/lib/ohai/plugins/solaris2/hostname.rb +0 -34
  252. data/lib/ohai/plugins/solaris2/kernel.rb +0 -40
  253. data/lib/ohai/plugins/solaris2/ps.rb +0 -23
  254. data/lib/ohai/plugins/solaris2/uptime.rb +0 -36
  255. data/lib/ohai/plugins/windows/hostname.rb +0 -33
  256. data/lib/ohai/plugins/windows/kernel.rb +0 -58
  257. data/lib/ohai/plugins/windows/kernel_devices.rb +0 -39
  258. data/lib/ohai/plugins/windows/uptime.rb +0 -23
  259. data/spec/unit/mixin/from_file_spec.rb +0 -53
@@ -21,7 +21,14 @@
21
21
  # Windows_NT kernel, so we herby decree that "windows" will refer to all
22
22
  # platforms built upon the Windows_NT kernel and have access to win32 or win64
23
23
  # subsystems.
24
- platform os
25
- platform_version kernel['release']
26
- platform_family "windows"
24
+
25
+ Ohai.plugin(:Platform) do
26
+ provides "platform", "platform_version", "platform_family"
27
+
28
+ collect_data(:windows) do
29
+ platform os
30
+ platform_version kernel['release']
31
+ platform_family "windows"
32
+ end
33
+ end
27
34
 
@@ -0,0 +1,184 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Author:: Daniel DeLeo (<dan@opscode.com>)
4
+ # Copyright:: Copyright (c) 2008, 2013 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'ohai/mash'
21
+ require 'ohai/exception'
22
+ require 'ohai/mixin/os'
23
+ require 'ohai/dsl'
24
+
25
+ module Ohai
26
+ class ProvidesMap
27
+
28
+ attr_reader :map
29
+
30
+ def initialize
31
+ @map = Mash.new
32
+ end
33
+
34
+ def set_providers_for(plugin, provided_attributes)
35
+ unless plugin.kind_of?(Ohai::DSL::Plugin)
36
+ raise ArgumentError, "set_providers_for only accepts Ohai Plugin classes (got: #{plugin})"
37
+ end
38
+
39
+ provided_attributes.each do |attribute|
40
+ attrs = @map
41
+ parts = normalize_and_validate(attribute)
42
+ parts.each do |part|
43
+ attrs[part] ||= Mash.new
44
+ attrs = attrs[part]
45
+ end
46
+ attrs[:_plugins] ||= []
47
+ attrs[:_plugins] << plugin
48
+ end
49
+ end
50
+
51
+ # gather plugins providing exactly the attributes listed
52
+ def find_providers_for(attributes)
53
+ plugins = []
54
+ attributes.each do |attribute|
55
+ attrs = select_subtree(@map, attribute)
56
+ raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless attrs
57
+ raise Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: \'#{attribute}\'" unless attrs[:_plugins]
58
+ plugins += attrs[:_plugins]
59
+ end
60
+ plugins.uniq
61
+ end
62
+
63
+ # This function is used to fetch the plugins for the attributes specified
64
+ # in the CLI options to Ohai.
65
+ # It first attempts to find the plugins for the attributes
66
+ # or the sub attributes given.
67
+ # If it can't find any, it looks for plugins that might
68
+ # provide the parents of a given attribute and returns the
69
+ # first parent found.
70
+ def deep_find_providers_for(attributes)
71
+ plugins = []
72
+ attributes.each do |attribute|
73
+ attrs = select_subtree(@map, attribute)
74
+
75
+ unless attrs
76
+ attrs = select_closest_subtree(@map, attribute)
77
+
78
+ unless attrs
79
+ raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'"
80
+ end
81
+ end
82
+
83
+ collect_plugins_in(attrs, plugins)
84
+ end
85
+
86
+ plugins.uniq
87
+ end
88
+
89
+ # This function is used to fetch the plugins from
90
+ # 'depends "languages"' statements in plugins.
91
+ # It gathers plugins providing each of the attributes listed, or the
92
+ # plugins providing the closest parent attribute
93
+ def find_closest_providers_for(attributes)
94
+ plugins = []
95
+ attributes.each do |attribute|
96
+ parts = normalize_and_validate(attribute)
97
+ raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless @map[parts[0]]
98
+ attrs = select_closest_subtree(@map, attribute)
99
+ raise Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: \'#{attribute}\'" unless attrs
100
+ plugins += attrs[:_plugins]
101
+ end
102
+ plugins.uniq
103
+ end
104
+
105
+ def all_plugins(attribute_filter=nil)
106
+ if attribute_filter.nil?
107
+ collected = []
108
+ collect_plugins_in(map, collected).uniq
109
+ else
110
+ deep_find_providers_for(Array(attribute_filter))
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def normalize_and_validate(attribute)
117
+ raise Ohai::Exceptions::AttributeSyntaxError, "Attribute contains duplicate '/' characters: #{attribute}" if attribute.match(/\/\/+/)
118
+ raise Ohai::Exceptions::AttributeSyntaxError, "Attribute contains a trailing '/': #{attribute}" if attribute.match(/\/$/)
119
+
120
+ parts = attribute.split('/')
121
+ parts.shift if parts.length != 0 && parts[0].length == 0 # attribute begins with a '/'
122
+ parts
123
+ end
124
+
125
+ def select_subtree(provides_map, attribute)
126
+ subtree = provides_map
127
+ parts = normalize_and_validate(attribute)
128
+ parts.each do |part|
129
+ return nil unless subtree[part]
130
+ subtree = subtree[part]
131
+ end
132
+ subtree
133
+ end
134
+
135
+ def select_closest_subtree(provides_map, attribute)
136
+ attr, *rest = normalize_and_validate(attribute)
137
+
138
+ # return nil if the top-level part of the attribute is not a
139
+ # top-level key in the provides_map (can't search any lower, and
140
+ # no information to return from this level of the search)
141
+ return nil unless provides_map[attr]
142
+
143
+ # attr is a key in the provides_map, search for the sub
144
+ # attribute under attr (if attribute = attr/sub1/sub2 then we
145
+ # search provides_map[attr] for sub1/sub2)
146
+ unless rest.empty?
147
+ subtree = select_closest_subtree(provides_map[attr], rest.join('/'))
148
+ end
149
+
150
+ if subtree.nil?
151
+ # no subtree of provides_map[attr] either 1) has a
152
+ # subattribute, 2) has a plugin providing a subattribute.
153
+ unless provides_map[attr][:_plugins]
154
+ # no providers for this attribute, this subtree won't do.
155
+ return nil # no providers for this attribute
156
+ else
157
+ # there are providers for this attribute, return its subtree
158
+ # to indicate this is the closest subtree
159
+ return provides_map[attr]
160
+ end
161
+ end
162
+
163
+ # we've already found a closest subtree or we've search all
164
+ # parent attributes of the original attribute and found no
165
+ # providers (subtree is nil in this case)
166
+ subtree
167
+ end
168
+
169
+ # Takes a section of the map, recursively searches for a `_plugins` key
170
+ # to find all the plugins in that section of the map. If given the whole
171
+ # map, it will find all of the plugins that have at least one provided
172
+ # attribute.
173
+ def collect_plugins_in(provides_map, collected)
174
+ provides_map.keys.each do |plugin|
175
+ if plugin.eql?("_plugins")
176
+ collected.concat(provides_map[plugin])
177
+ else
178
+ collect_plugins_in(provides_map[plugin], collected)
179
+ end
180
+ end
181
+ collected
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,109 @@
1
+ #
2
+ # Author:: Claire McQuin (<claire@opscode.com>)
3
+ # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
7
+ # may not use this file except in compliance with the License. You may
8
+ # 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 expressed or
15
+ # implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License
18
+ #
19
+
20
+ require 'ohai/dsl'
21
+
22
+ module Ohai
23
+ class Runner
24
+
25
+ # safe_run: set to true if this runner will run plugins in
26
+ # safe-mode. default false.
27
+ def initialize(controller, safe_run = false)
28
+ @provides_map = controller.provides_map
29
+ @safe_run = safe_run
30
+ end
31
+
32
+ # Runs plugins and any un-run dependencies.
33
+ # If force is set to true, then this plugin and its dependencies
34
+ # will be run even if they have been run before.
35
+ def run_plugin(plugin)
36
+ unless plugin.kind_of?(Ohai::DSL::Plugin)
37
+ raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin #{plugin} (must be an Ohai::DSL::Plugin or subclass)"
38
+ end
39
+
40
+ if Ohai::Config[:disabled_plugins].include?(plugin.name)
41
+ Ohai::Log.debug("Skipping disabled plugin #{plugin.name}")
42
+ return false
43
+ end
44
+
45
+ begin
46
+ case plugin.version
47
+ when :version7
48
+ run_v7_plugin(plugin)
49
+ when :version6
50
+ run_v6_plugin(plugin)
51
+ else
52
+ raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin version #{plugin.version} for plugin #{plugin}"
53
+ end
54
+ rescue Ohai::Exceptions::Error
55
+ raise
56
+ rescue Exception,Errno::ENOENT => e
57
+ Ohai::Log.debug("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
58
+ end
59
+ end
60
+
61
+ def run_v6_plugin(plugin)
62
+ return true if plugin.has_run?
63
+
64
+ @safe_run ? plugin.safe_run : plugin.run
65
+ end
66
+
67
+ def run_v7_plugin(plugin)
68
+ visited = [ plugin ]
69
+ while !visited.empty?
70
+ next_plugin = visited.pop
71
+
72
+ next if next_plugin.has_run?
73
+
74
+ if visited.include?(next_plugin)
75
+ raise Ohai::Exceptions::DependencyCycle, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, plugin).join(", ") }"
76
+ end
77
+
78
+ dependency_providers = fetch_plugins(next_plugin.dependencies)
79
+
80
+ # Remove the already ran plugins from dependencies if force is not set
81
+ # Also remove the plugin that we are about to run from dependencies as well.
82
+ dependency_providers.delete_if { |dep_plugin|
83
+ dep_plugin.has_run? || dep_plugin.eql?(next_plugin)
84
+ }
85
+
86
+ if dependency_providers.empty?
87
+ @safe_run ? next_plugin.safe_run : next_plugin.run
88
+ else
89
+ visited << next_plugin << dependency_providers.first
90
+ end
91
+ end
92
+ end
93
+
94
+ def fetch_plugins(attributes)
95
+ @provides_map.find_closest_providers_for(attributes)
96
+ end
97
+
98
+ # Given a list of plugins and the first plugin in the cycle,
99
+ # returns the list of plugin source files responsible for the
100
+ # cycle. Does not include plugins that aren't a part of the cycle
101
+ def get_cycle(plugins, cycle_start)
102
+ cycle = plugins.drop_while { |plugin| !plugin.eql?(cycle_start) }
103
+ names = []
104
+ cycle.each { |plugin| names << plugin.name }
105
+ names
106
+ end
107
+
108
+ end
109
+ end
data/lib/ohai/system.rb CHANGED
@@ -16,225 +16,158 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- require 'ohai/mash'
19
+ require 'ohai/loader'
20
20
  require 'ohai/log'
21
- require 'ohai/mixin/from_file'
21
+ require 'ohai/mash'
22
+ require 'ohai/runner'
23
+ require 'ohai/dsl'
22
24
  require 'ohai/mixin/command'
25
+ require 'ohai/mixin/os'
23
26
  require 'ohai/mixin/string'
27
+ require 'ohai/mixin/constant_helper'
28
+ require 'ohai/provides_map'
29
+ require 'ohai/hints'
24
30
  require 'mixlib/shellout'
25
31
 
26
32
  require 'yajl'
27
33
 
28
34
  module Ohai
29
35
  class System
30
- attr_accessor :data, :seen_plugins, :hints
36
+ include Ohai::Mixin::ConstantHelper
31
37
 
32
- include Ohai::Mixin::FromFile
33
- include Ohai::Mixin::Command
38
+ attr_accessor :data
39
+ attr_reader :provides_map
40
+ attr_reader :v6_dependency_solver
34
41
 
35
42
  def initialize
36
- @data = Mash.new
37
- @seen_plugins = Hash.new
38
- @providers = Mash.new
39
43
  @plugin_path = ""
40
- @hints = Hash.new
41
- end
42
-
43
- def [](key)
44
- @data[key]
44
+ reset_system
45
45
  end
46
46
 
47
- def []=(key, value)
48
- @data[key] = value
49
- end
47
+ def reset_system
48
+ @data = Mash.new
49
+ @provides_map = ProvidesMap.new
50
50
 
51
- def each(&block)
52
- @data.each do |key, value|
53
- block.call(key, value)
54
- end
55
- end
51
+ @v6_dependency_solver = Hash.new
56
52
 
57
- def attribute?(name)
58
- @data.has_key?(name)
59
- end
53
+ @loader = Ohai::Loader.new(self)
54
+ @runner = Ohai::Runner.new(self, true)
60
55
 
61
- def set(name, *value)
62
- set_attribute(name, *value)
63
- end
56
+ Ohai::Hints.refresh_hints()
64
57
 
65
- def from(cmd)
66
- status, stdout, stderr = run_command(:command => cmd)
67
- return "" if stdout.nil? || stdout.empty?
68
- stdout.strip
58
+ # Remove the previously defined plugins
59
+ recursive_remove_constants(Ohai::NamedPlugin)
69
60
  end
70
61
 
71
- def provides(*paths)
72
- paths.each do |path|
73
- parts = path.split('/')
74
- h = @providers
75
- unless parts.length == 0
76
- parts.shift if parts[0].length == 0
77
- parts.each do |part|
78
- h[part] ||= Mash.new
79
- h = h[part]
80
- end
81
- end
82
- h[:_providers] ||= []
83
- h[:_providers] << @plugin_path
84
- end
62
+ def [](key)
63
+ @data[key]
85
64
  end
86
65
 
87
- # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
88
- def from_with_regex(cmd, *regex_list)
89
- regex_list.flatten.each do |regex|
90
- status, stdout, stderr = run_command(:command => cmd)
91
- return "" if stdout.nil? || stdout.empty?
92
- stdout.chomp!.strip
93
- md = stdout.match(regex)
94
- return md[1]
95
- end
96
- end
97
-
98
- def set_attribute(name, *values)
99
- @data[name] = Array18(*values)
100
- @data[name]
101
- end
66
+ def all_plugins(attribute_filter=nil)
67
+ # Reset the system when all_plugins is called since this function
68
+ # can be run multiple times in order to pick up any changes in the
69
+ # config or plugins with Chef.
70
+ reset_system
102
71
 
103
- def get_attribute(name)
104
- @data[name]
72
+ load_plugins
73
+ run_plugins(true, attribute_filter)
105
74
  end
106
-
107
- def hint?(name)
108
- @json_parser ||= Yajl::Parser.new
109
-
110
- return @hints[name] if @hints[name]
111
-
112
- Ohai::Config[:hints_path].each do |path|
113
- filename = File.join(path, "#{name}.json")
114
- if File.exist?(filename)
115
- begin
116
- hash = @json_parser.parse(File.read(filename))
117
- @hints[name] = hash || Hash.new # hint should exist because the file did, even if it didn't contain anything
118
- rescue Yajl::ParseError => e
119
- Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
120
- end
121
- end
122
- end
123
75
 
124
- @hints[name]
76
+ def load_plugins
77
+ @loader.load_all
125
78
  end
126
-
127
79
 
128
- def all_plugins
129
- require_plugin('os')
130
-
131
- Ohai::Config[:plugin_path].each do |path|
132
- [
133
- Dir[File.join(path, '*')],
134
- Dir[File.join(path, @data[:os], '**', '*')]
135
- ].flatten.each do |file|
136
- file_regex = Regexp.new("#{File.expand_path(path)}#{File::SEPARATOR}(.+).rb$")
137
- md = file_regex.match(file)
138
- if md
139
- plugin_name = md[1].gsub(File::SEPARATOR, "::")
140
- require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
141
- end
142
- end
80
+ def run_plugins(safe = false, attribute_filter = nil)
81
+ # First run all the version 6 plugins
82
+ @v6_dependency_solver.values.each do |v6plugin|
83
+ @runner.run_plugin(v6plugin)
143
84
  end
144
- unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
145
- # Catch any errant children who need to be reaped
146
- begin
147
- true while Process.wait(-1, Process::WNOHANG)
148
- rescue Errno::ECHILD
149
- end
150
- end
151
- true
152
- end
153
85
 
154
- def collect_providers(providers)
155
- refreshments = []
156
- if providers.is_a?(Mash)
157
- providers.keys.each do |provider|
158
- if provider.eql?("_providers")
159
- refreshments << providers[provider]
160
- else
161
- refreshments << collect_providers(providers[provider])
162
- end
163
- end
164
- else
165
- refreshments << providers
86
+ # Then run all the version 7 plugins
87
+ begin
88
+ @provides_map.all_plugins(attribute_filter).each { |plugin|
89
+ @runner.run_plugin(plugin)
90
+ }
91
+ rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e
92
+ Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
93
+ raise
166
94
  end
167
- refreshments.flatten.uniq
168
95
  end
169
96
 
170
- def refresh_plugins(path = '/')
171
- parts = path.split('/')
172
- if parts.length == 0
173
- h = @providers
97
+ def pathify_v6_plugin(plugin_name)
98
+ path_components = plugin_name.split("::")
99
+ File.join(path_components) + ".rb"
100
+ end
101
+
102
+ #
103
+ # Below APIs are from V6.
104
+ # Make sure that you are not breaking backwards compatibility
105
+ # if you are changing any of the APIs below.
106
+ #
107
+ def require_plugin(plugin_ref, force=false)
108
+ plugins = [ ]
109
+ # This method is only callable by version 6 plugins.
110
+ # First we check if there exists a v6 plugin that fulfills the dependency.
111
+ if @v6_dependency_solver.has_key? pathify_v6_plugin(plugin_ref)
112
+ # Note that: partial_path looks like Plugin::Name
113
+ # keys for @v6_dependency_solver are in form 'plugin/name.rb'
114
+ plugins << @v6_dependency_solver[pathify_v6_plugin(plugin_ref)]
174
115
  else
175
- parts.shift if parts[0].length == 0
176
- h = @providers
177
- parts.each do |part|
178
- break unless h.has_key?(part)
179
- h = h[part]
116
+ # While looking up V7 plugins we need to convert the plugin_ref to an attribute.
117
+ attribute = plugin_ref.gsub("::", "/")
118
+ begin
119
+ plugins = @provides_map.find_providers_for([attribute])
120
+ rescue Ohai::Exceptions::AttributeNotFound
121
+ Ohai::Log.debug("Can not find any v7 plugin that provides #{attribute}")
122
+ plugins = [ ]
180
123
  end
181
124
  end
182
125
 
183
- refreshments = collect_providers(h)
184
- Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
185
-
186
- # remove the hints cache
187
- @hints = Hash.new
188
-
189
- refreshments.each do |r|
190
- @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
191
- end
192
- refreshments.each do |r|
193
- require_plugin(r) unless @seen_plugins.has_key?(r)
126
+ if plugins.empty?
127
+ raise Ohai::Exceptions::DependencyNotFound, "Can not find a plugin for dependency #{plugin_ref}"
128
+ else
129
+ plugins.each do |plugin|
130
+ begin
131
+ @runner.run_plugin(plugin)
132
+ rescue SystemExit, Interrupt
133
+ raise
134
+ rescue Ohai::Exceptions::DependencyCycle, Ohai::Exceptions::AttributeNotFound => e
135
+ Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
136
+ raise
137
+ rescue Exception,Errno::ENOENT => e
138
+ Ohai::Log.debug("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
139
+ end
140
+ end
194
141
  end
195
142
  end
196
143
 
197
- def require_plugin(plugin_name, force=false)
198
- unless force
199
- return true if @seen_plugins[plugin_name]
200
- end
201
-
202
- if Ohai::Config[:disabled_plugins].include?(plugin_name)
203
- Ohai::Log.debug("Skipping disabled plugin #{plugin_name}")
204
- return false
205
- end
206
-
207
- @plugin_path = plugin_name
208
-
209
- filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
210
-
211
- Ohai::Config[:plugin_path].each do |path|
212
- check_path = File.expand_path(File.join(path, filename))
213
- begin
214
- @seen_plugins[plugin_name] = true
215
- Ohai::Log.debug("Loading plugin #{plugin_name}")
216
- from_file(check_path)
217
- return true
218
- rescue Errno::ENOENT => e
219
- Ohai::Log.debug("No #{plugin_name} at #{check_path}")
220
- rescue SystemExit, Interrupt
221
- raise
222
- rescue Exception,Errno::ENOENT => e
223
- Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
224
- end
144
+ # Re-runs plugins that provide the attributes specified by
145
+ # +attribute_filter+. If +attribute_filter+ is not given, re-runs all
146
+ # plugins.
147
+ #
148
+ # Note that dependencies will not be re-run, so you must specify all of the
149
+ # attributes you want refreshed in the +attribute_filter+
150
+ #
151
+ # This method takes a naive approach to v6 plugins: it simply re-runs all
152
+ # of them whenever called.
153
+ def refresh_plugins(attribute_filter=nil)
154
+ Ohai::Hints.refresh_hints()
155
+ @provides_map.all_plugins(attribute_filter).each do |plugin|
156
+ plugin.reset!
225
157
  end
158
+ run_plugins(true, attribute_filter)
226
159
  end
227
160
 
228
- # Sneaky! Lets us stub out require_plugin when testing plugins, but still
229
- # call the real require_plugin to kick the whole thing off.
230
- alias :_require_plugin :require_plugin
231
-
161
+ #
232
162
  # Serialize this object as a hash
163
+ #
233
164
  def to_json
234
165
  Yajl::Encoder.new.encode(@data)
235
166
  end
236
167
 
168
+ #
237
169
  # Pretty Print this object as JSON
170
+ #
238
171
  def json_pretty_print(item=nil)
239
172
  Yajl::Encoder.new(:pretty => true).encode(item || @data)
240
173
  end
@@ -258,19 +191,5 @@ module Ohai
258
191
  raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!"
259
192
  end
260
193
  end
261
-
262
- def method_missing(name, *args)
263
- return get_attribute(name) if args.length == 0
264
-
265
- set_attribute(name, *args)
266
- end
267
-
268
- private
269
-
270
- def Array18(*args)
271
- return nil if args.empty?
272
- return args.first if args.length == 1
273
- return *args
274
- end
275
194
  end
276
195
  end