bke_chef-rundeck 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ # Changelog 2.2.0
2
+ ## Release Summary
3
+ ### Deprecation Warning!
4
+ If you have been relying on chef-rundeck squashing node environment, role and run list data into the 'tags' field, be aware that this is going to change. Rundeck has supported custom resource attributes in node filters for quite some time now. If you rely on the current behavior, please consider pinning against chef-rundeck 2.x.
5
+ * Better data validation on chef-rundeck.json.
6
+ * Expanded functional test coverage.
7
+ * Better documentation.
8
+
9
+ # Changelog 2.1.0
10
+ ## Release Summary
11
+ * As some of the others have mentioned partial search is the way to go. We have seen 80%-90% improvement in performance. In addition we have introduced caching via a tmp file, this allows large chef node data not to remain in memory and slow down responses. Sinatra also will now run in env 'production' by default.
12
+
13
+ ### New CLI params:
14
+
15
+ * --env (production/development) (default: production)
16
+ * --partial-search (true/false) - Only supported when using Chef Server 11 (default: false)
17
+ * --timeout (cache timeout in seconds) (default: 30)
18
+
19
+ # Changelog - 2.0.0
20
+ ## Release Summary
21
+ * Added support for search defined project resource definitions
22
+ * Added support for Rundeck 2.0 and custom attributes
23
+ * Added support for Windows Systems in Rundeck node configuration (via overthere-winrm)
24
+ * Refactor xml node generation
25
+ # Changelog - 1.0.1
26
+ ## Release Summary
27
+ * Fixes to allow us to test Chef 11 and Chef 10 in Travis, based on the Chefspec way.
28
+ # Changelog - 1.0.0
29
+ ## Release Summary
30
+ * chef-rundeck is under new management (GitHub org name is 'oswaldlabs'). We use it at work in production and we are serious about maintaining it. If you have suggestions or code that you would like to contribute, particularly if it improves our test coverage, please send us a pull request or open a GitHub issue.
31
+ * A basic functioning test suite that builds under Travis-CI and Ruby 1.9.2/1.9.3, connects to a Hosted Chef organization and generates a simple Rundeck resource model. ("rake spec" to test it for yourself!)
32
+ * chef-rundeck should now drop a pidfile at startup and remove it during normal shutdown.
33
+ * Chef node tags now appear as tags in the generated Rundeck resource model. (Thanks, giorgio-v!)
34
+
35
+ ## Detailed Changes
36
+ * [Tags from node](https://github.com/oswaldlabs/chef-rundeck/commit/d83373be4b903595d4db30d8c41a0a3bad340994) (giorgio-v)
37
+ * [Initial attempt at Travis-ification.](https://github.com/oswaldlabs/chef-rundeck/commit/f64bdfce1dd12368f8c2364dd29b8d8acc63c606) (Steven Wagner)
38
+ * [Slight refactoring for Ruby 1.9/RSpec + Travis ...](https://github.com/oswaldlabs/chef-rundeck/commit/89b1e22ebd7b611aa1ce8f28d1364a24cbddb814) (Steven Wagner)
39
+ * [Added jeweler dependency to make Travis happy.](https://github.com/oswaldlabs/chef-rundeck/commit/4cad7308687b4c2964474421b7f0028123bffad6) (Steven Wagner)
40
+ * [New fancy markdown README, ditch old busted RDoc README.](https://github.com/oswaldlabs/chef-rundeck/commit/df015dbd7771c331e76898c30e90102868793f28) (Steven Wagner)
41
+ * [Sorry, Travis, adding the development gems back in.](https://github.com/oswaldlabs/chef-rundeck/commit/659f413872c5923cf534aa964da92e9c2b0bda5b) (Steven Wagner)
42
+ * [Added new Authors](https://github.com/oswaldlabs/chef-rundeck/commit/de29b9c2c516a1bae1521a702b45233383a30231) (Brian Scott)
43
+ * [Update Bundle](https://github.com/oswaldlabs/chef-rundeck/commit/2f83030f879c47a638037f24de74f6b5e9a270a1) (Brian Scott)
44
+ * [Removed task check deps](https://github.com/oswaldlabs/chef-rundeck/commit/26c1599dc2fe0c3865e80ca55abffee50e648033) (Brian Scott)
45
+ * [Attempt at adding specs](https://github.com/oswaldlabs/chef-rundeck/commit/3d108b773ad0c34423060e508d275087693c0cb9) (Brian Scott)
46
+ * [Adding client.rb and client key for a test Chef org.](https://github.com/oswaldlabs/chef-rundeck/commit/27aae944b4cc8cc58ef6fbb34dbcba9eb375d2d1) (Steven Wagner)
47
+ * [Simple, stupid script for use in setting up a Travis worker.](https://github.com/oswaldlabs/chef-rundeck/commit/58b02ff7eb0378e7c20f412a85537625018a3131) (Steven Wagner)
48
+ * [Added Chef client configuration install script.](https://github.com/oswaldlabs/chef-rundeck/commit/5d9ede2bb5c2736b10ba5401636a100dac9e9416) (Steven Wagner)
49
+ * [Script didn't get executed at the proper time. Let's try after_install -> before_script ...](https://github.com/oswaldlabs/chef-rundeck/commit/d27eed173abc94469d83117999a31469af7500f2) (Steven Wagner)
50
+ * [rdoc -> markdown for README, new email.](https://github.com/oswaldlabs/chef-rundeck/commit/7d1b27844c9e8a2f74e456a1524cfabebc4d4f8b) (Steven Wagner)
51
+ * [Paths now relative to root of repo.](https://github.com/oswaldlabs/chef-rundeck/commit/92cf6ae73dd84ef0393dbf45a24ff9f2a5f3078c) (Steven Wagner)
52
+ * [We can sudo!](https://github.com/oswaldlabs/chef-rundeck/commit/74533fd4f2d0264ccd488a93ed9a746dc0d517c1) (Steven Wagner)
53
+ * [Ensuring that :node_name is set.](https://github.com/oswaldlabs/chef-rundeck/commit/de1e858d431492cc1a965a511ecd8f7c2a70b5ba) (Steven Wagner)
54
+ * [Debug output](https://github.com/oswaldlabs/chef-rundeck/commit/e1eac806cba0207415d41179ddab18b3f22ae5f3) (Steven Wagner)
55
+ * [Modifying stuff to work with Travis.](https://github.com/oswaldlabs/chef-rundeck/commit/9dbba97c7bd4d68f3b1413387217125fc7881f00) (Steven Wagner)
56
+ * [configure -> config_file, oops](https://github.com/oswaldlabs/chef-rundeck/commit/ecc076674236fbd806b8f4c749dcf80fea6834ae) (Steven Wagner)
57
+ * [Fixed failed spec in spec_helper](https://github.com/oswaldlabs/chef-rundeck/commit/5940e4fbeadc2fa486ce84d518fb538e7592b5c5) (Brian Scott)
58
+ * [Spec test](https://github.com/oswaldlabs/chef-rundeck/commit/9a5fc66deb0513889de53a372b73031c99d5a6b8) (Brian Scott)
59
+ * [Added default web UI value (Hosted Chef) to pass tests.](https://github.com/oswaldlabs/chef-rundeck/commit/6c49cd1b143283bb4637f66bcbcd0feec21e4575) (Steven Wagner)
60
+ * [Adding some chef-rundeck-specific config values.](https://github.com/oswaldlabs/chef-rundeck/commit/6d0ae6e81fd890dad9b5388a559dfd6bbcb2d56f) (Steven Wagner)
61
+ * [Drop a pidfile at startup. Should nuke it if the app stops. Appends port number for multi-tenant deployments.](https://github.com/oswaldlabs/chef-rundeck/commit/af63dcf91260d0f2c3f4882d14f71f0cffb4f838) (Steven Wagner)
62
+ * [Merge pull request #21 from leftathome/manage_pidfile](https://github.com/oswaldlabs/chef-rundeck/commit/98f30ddf318f61a70c4a824c2896da96c7435473) (Brian Scott)
63
+ * [default node_name to fqdn. fixes #11](https://github.com/oswaldlabs/chef-rundeck/commit/efd6e1aec07b749dd7b288141dcf349bad49ced1) (Joseph Anthony Pasquale Holsten)
64
+ * [Load config in test](https://github.com/oswaldlabs/chef-rundeck/commit/83c2d9b6558f02877b5a86b1ff8419b3d3b118a5) (Joseph Anthony Pasquale Holsten)
65
+ * [Set ChefRundeck.username in test](https://github.com/oswaldlabs/chef-rundeck/commit/3e7e7632a1e39b95124221ba0fd9d7e69373dc5f) (Joseph Anthony Pasquale Holsten)
66
+ * [Set web_ui_url in test](https://github.com/oswaldlabs/chef-rundeck/commit/e1a24b232522dfda24d52b1701152fd3958c0ca5) (Joseph Anthony Pasquale Holsten)
67
+ * [Merge pull request #18 from simplymeasured/default-to-fqdn-for-node-name](https://github.com/oswaldlabs/chef-rundeck/commit/ecf4833ad067f983db24910485bfff0519dddc5c) (Brian Scott)
68
+ * [Added Nokogiri development dependency. Used for XML validation in testing.](https://github.com/oswaldlabs/chef-rundeck/commit/91dfff2dcaa431582155f8eab0217bcd1b49854d) (Steven Wagner)
69
+ * [Added simple test to validate XML output.](https://github.com/oswaldlabs/chef-rundeck/commit/eec5573c89dac33e1aea45f2f41319aa9218d4b5) (Steven Wagner)
70
+ * [Bump chef to 11.6.0.](https://github.com/oswaldlabs/chef-rundeck/commit/7c0146c72d46c033b20917a92eb9cee7214603eb) (Steven Wagner)
71
+ * [Merge pull request #17 from giorgio-v/chef-tags](https://github.com/oswaldlabs/chef-rundeck/commit/c1333410a4b9ea70c30574727d11557da5d71d7b) (Brian Scott)
72
+ * [Merge pull request #22 from leftathome/travis_fixes](https://github.com/oswaldlabs/chef-rundeck/commit/12a8c79ecfa25c012d98a6202c87369c1ca0dc55) (Brian Scott)
73
+ * [Use Chef REST API](https://github.com/oswaldlabs/chef-rundeck/commit/d122c213680b8c3ce3afa85a316d7849e72c5c48) (Joseph Martin)
74
+ * [Merge pull request #23 from hugespoon/rest-api](https://github.com/oswaldlabs/chef-rundeck/commit/aa4530cf765afdf01d108562a78961fc90dbe19d) (Brian Scott)
75
+ * [Fix spec target so that it works from command-line.](https://github.com/oswaldlabs/chef-rundeck/commit/aee31730f6f2520eda2ec6265a7c8bf0bd251879) (Steven Wagner)
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bke_chef-rundeck"
8
+ s.version = "2.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Jacob", "Brian Scott", "Steven Wagner", "Peter Crossley"]
12
+ s.date = "2011-11-07"
13
+ s.description = "Provides a resource endpoint for RunDeck from a Chef Server"
14
+ s.email = "brainscott@gmail.com"
15
+ s.executables = ["chef-rundeck"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.md"
19
+ ]
20
+ s.license = 'Apache License 2.0 (Apache-2.0)'
21
+ s.files = [
22
+ ".document",
23
+ "LICENSE",
24
+ "NOTICE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "bin/chef-rundeck",
28
+ "lib/chef-rundeck.rb",
29
+ "lib/partial_search.rb",
30
+ "spec/chef-rundeck_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/oswaldlabs/chef-rundeck"
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "Integrates Chef with RunDeck"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<sinatra>, [">= 0"])
44
+ s.add_runtime_dependency(%q<chef>, [">= 0"])
45
+ s.add_runtime_dependency(%q<mixlib-cli>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
47
+ s.add_development_dependency(%q<yard>, [">= 0"])
48
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
49
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<sinatra>, [">= 0"])
52
+ s.add_dependency(%q<chef>, [">= 0"])
53
+ s.add_dependency(%q<mixlib-cli>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_dependency(%q<yard>, [">= 0"])
56
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
57
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<sinatra>, [">= 0"])
61
+ s.add_dependency(%q<chef>, [">= 0"])
62
+ s.add_dependency(%q<mixlib-cli>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
64
+ s.add_dependency(%q<yard>, [">= 0"])
65
+ s.add_dependency(%q<rack-test>, [">= 0"])
66
+ s.add_dependency(%q<nokogiri>, [">= 0"])
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org/"
2
+
3
+ gem 'jeweler'
4
+ gem 'chef', '~> 10'
5
+ gemspec :path => '..'
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org/"
2
+
3
+ gem 'jeweler'
4
+ gem 'chef', '~> 11'
5
+ gemspec :path => '..'
@@ -0,0 +1,327 @@
1
+ #
2
+ # Copyright 2010, Opscode, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'sinatra/base'
18
+ require 'chef'
19
+ require 'chef/node'
20
+ require 'chef/mixin/xml_escape'
21
+ require 'chef/role'
22
+ require 'chef/environment'
23
+ require 'chef/data_bag'
24
+ require 'chef/data_bag_item'
25
+ require 'partial_search'
26
+
27
+ REQUIRED_ATTRS = [ :kernel, :fqdn, :platform, :platform_version ]
28
+
29
+ class MissingAttribute < StandardError
30
+ attr_reader :name
31
+ def initialize(name)
32
+ @name = name
33
+ end
34
+ end
35
+
36
+ class ChefRundeck < Sinatra::Base
37
+
38
+ include Chef::Mixin::XMLEscape
39
+
40
+ class << self
41
+ attr_accessor :config_file
42
+ attr_accessor :username
43
+ attr_accessor :web_ui_url
44
+ attr_accessor :api_url
45
+ attr_accessor :client_key
46
+ attr_accessor :project_config
47
+ attr_accessor :cache_timeout
48
+ attr_accessor :partial_search
49
+
50
+ def configure
51
+ Chef::Config.from_file(ChefRundeck.config_file)
52
+ Chef::Log.level = Chef::Config[:log_level]
53
+
54
+ unless ChefRundeck.api_url
55
+ ChefRundeck.api_url = Chef::Config[:chef_server_url]
56
+ end
57
+
58
+ unless ChefRundeck.client_key
59
+ ChefRundeck.client_key = Chef::Config[:client_key]
60
+ end
61
+
62
+
63
+ if (File.exists?(ChefRundeck.project_config)) then
64
+ Chef::Log.info("Using JSON project file #{ChefRundeck.project_config}")
65
+ projects = File.open(ChefRundeck.project_config, "r") { |f| JSON.parse(f.read) }
66
+ projects.keys.each do | project |
67
+ get "/#{project}" do
68
+ content_type 'text/xml'
69
+ Chef::Log.info("Loading nodes for /#{project}")
70
+ # TODO: Validate project data before rendering the document?
71
+ send_file build_project project, projects[project]['pattern'], (projects[project]['username'].nil? ? ChefRundeck.username : projects[project]['username']), (projects[project]['hostname'].nil? ? "fqdn" : projects[project]['hostname']), projects[project]['attributes']
72
+ end
73
+ cache_file = "#{Dir.tmpdir}/chef-rundeck-#{project}.xml"
74
+ at_exit { File.delete(cache_file) if File.exist?(cache_file) }
75
+ end
76
+ end
77
+
78
+ get '/' do
79
+ content_type 'text/xml'
80
+ Chef::Log.info("Loading all nodes for /")
81
+ send_file build_project
82
+ end
83
+
84
+ cache_file = "#{Dir.tmpdir}/chef-rundeck-default.xml"
85
+ at_exit { File.delete(cache_file) if File.exist?(cache_file) }
86
+ end
87
+ end
88
+
89
+ def build_project (project="default", pattern="*:*", username=ChefRundeck.username, hostname="fqdn", custom_attributes=nil)
90
+ response = nil
91
+ begin
92
+
93
+ # file is too new use it again
94
+ if (File.exists?("#{Dir.tmpdir}/chef-rundeck-#{project}.xml") && (Time.now - File.atime("#{Dir.tmpdir}/chef-rundeck-#{project}.xml") < ChefRundeck.cache_timeout)) then
95
+ return "#{Dir.tmpdir}/chef-rundeck-#{project}.xml"
96
+ end
97
+
98
+ results = []
99
+ if ChefRundeck.partial_search then
100
+ keys = { 'name' => ['name'],
101
+ 'kernel_machine' => [ 'kernel', 'machine' ],
102
+ 'kernel_os' => [ 'kernel', 'os' ],
103
+ 'fqdn' => [ 'fqdn' ],
104
+ 'run_list' => [ 'run_list' ],
105
+ 'roles' => [ 'roles' ],
106
+ 'recipes' => [ 'recipes' ],
107
+ 'chef_environment' => [ 'chef_environment' ],
108
+ 'platform' => [ 'platform'],
109
+ 'platform_version' => [ 'platform_version' ],
110
+ 'tags' => [ 'tags' ],
111
+ 'hostname' => [hostname]
112
+ }
113
+ if !custom_attributes.nil? then
114
+ custom_attributes.each do |attr|
115
+ attr_name = attr.gsub('.', '_')
116
+ attr_value = attr.split('.')
117
+ keys[attr_name] = attr_value
118
+ end
119
+ end
120
+ # do search
121
+ Chef::Log.info("partial search started (project: '#{project}')")
122
+ results = partial_search(:node,pattern, :keys => keys)
123
+ Chef::Log.info("partial search finshed (project: '#{project}', count: #{results.length})")
124
+ else
125
+ q = Chef::Search::Query.new
126
+ Chef::Log.info("search started (project: '#{project}')")
127
+ results = q.search("node",pattern)[0]
128
+ Chef::Log.info("search finshed (project: '#{project}', count: #{results.length})")
129
+ results = convert_results(results, hostname, custom_attributes)
130
+ end
131
+
132
+ response = File.open("#{Dir.tmpdir}/chef-rundeck-#{project}.xml", 'w')
133
+ response.write '<?xml version="1.0" encoding="UTF-8"?>'
134
+ response.write '<!DOCTYPE project PUBLIC "-//DTO Labs Inc.//DTD Resources Document 1.0//EN" "project.dtd">'
135
+ response.write '<project>'
136
+
137
+ Chef::Log.info("building nodes (project: '#{project}')")
138
+ failed = 0
139
+ results.each do |node|
140
+ begin
141
+ # validate the node
142
+ begin
143
+ node_is_valid? node
144
+ rescue ArgumentError => ae
145
+ Chef::Log.warn("invalid node element: #{ae}")
146
+ failed = failed + 1
147
+ next
148
+ end
149
+
150
+ #write the node to the project
151
+ response.write build_node(node, username, hostname, custom_attributes)
152
+ rescue Exception => e
153
+ Chef::Log.error("=== could not generate xml for #{node}: #{e.message}")
154
+ Chef::Log.debug(e.backtrace.join('\n'))
155
+ end
156
+ end
157
+ Chef::Log.info("nodes complete (project: '#{project}', total: #{results.length - failed}, failed: #{failed})")
158
+
159
+ response.write "</project>"
160
+ Chef::Log.debug(response)
161
+ ensure
162
+ response.close unless response == nil
163
+ end
164
+ return response.path
165
+ end
166
+ end
167
+
168
+ def build_node (node, username, hostname, custom_attributes)
169
+ #--
170
+ # Certain features in Rundeck require the osFamily value to be set to 'unix' to work appropriately. - SRK
171
+ #++
172
+ data = ''
173
+ os_family = node['kernel_os'] =~ /winnt|windows/i ? 'winnt' : 'unix'
174
+ nodeexec = node['kernel_os'] =~ /winnt|windows/i ? "node-executor=\"overthere-winrm\"" : ''
175
+ data << <<-EOH
176
+ <node name="#{xml_escape(node['fqdn'])}" #{nodeexec}
177
+ type="Node"
178
+ description="#{xml_escape(node['name'])}"
179
+ osArch="#{xml_escape(node['kernel_machine'])}"
180
+ osFamily="#{xml_escape(os_family)}"
181
+ osName="#{xml_escape(node['platform'])}"
182
+ osVersion="#{xml_escape(node['platform_version'])}"
183
+ roles="#{xml_escape(node['roles'].join(','))}"
184
+ recipes="#{xml_escape(node['recipes'].join(','))}"
185
+ tags="#{xml_escape([ node['roles'], node['recipes'], node['chef_environment'], node['tags']].flatten.join(","))}"
186
+ environment="#{xml_escape(node['chef_environment'])}"
187
+ username="#{xml_escape(username)}"
188
+ hostname="#{xml_escape(node['hostname'])}"
189
+ editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node['name'])}/edit" #{custom_attributes.nil? ? '/': ''}>
190
+ EOH
191
+ if !custom_attributes.nil? then
192
+ custom_attributes.each do |attr|
193
+ attr_name = attr
194
+ attr_value = node[attr.gsub('.','_')]
195
+ data << <<-EOH
196
+ <attribute name="#{attr_name}"><![CDATA[#{attr_value}]]></attribute>
197
+ EOH
198
+ end
199
+ data << "</node>"
200
+ end
201
+
202
+ return data
203
+ end
204
+
205
+ def get_custom_attr (obj, params)
206
+ value = obj
207
+ Chef::Log.debug("loading custom attributes for node: #{obj['name']} with #{params}")
208
+ params.each do |p|
209
+ value = value[p.to_sym]
210
+ if value.nil? then
211
+ break
212
+ end
213
+ end
214
+ return value.nil? ? "" : value.to_s
215
+ end
216
+
217
+ # Convert results to be compatiable with Chef 11 format
218
+ def convert_results(results, hostname, custom_attributes)
219
+ new_results = []
220
+ results.each do |node|
221
+ n = {}
222
+ n['name'] = node.name
223
+ n['chef_environment'] = node.chef_environment
224
+ n['run_list'] = node.run_list
225
+ n['recipes'] = !node.run_list.nil? ? node.run_list.recipes : nil
226
+ n['roles'] = !node.run_list.nil? ? node.run_list.roles : nil
227
+ n['fqdn'] = node['fqdn']
228
+ n['hostname'] = get_custom_attr(node, hostname.split('.'))
229
+ n['kernel_machine'] = !node['kernel'].nil? ? node['kernel']['machine'] : nil
230
+ n['kernel_os'] = !node['kernel'].nil? ? node['kernel']['os'] : nil
231
+ n['platform'] = node['platform']
232
+ n['platform_version'] = node['platform_version']
233
+ n['tags'] = node['tags']
234
+
235
+ if !custom_attributes.nil? then
236
+ custom_attributes.each do |attr|
237
+ ps_name = attr.gsub('.','_')
238
+ n[ps_name] = get_custom_attr(node, attr.split('.'))
239
+ end
240
+ end
241
+ new_results << n
242
+ end
243
+ return new_results
244
+ end
245
+
246
+
247
+ # Helper def to validate the node
248
+ def node_is_valid?(node)
249
+ raise ArgumentError, "#{node} missing 'name'" if !node['name']
250
+ raise ArgumentError, "#{node} missing 'chef_environment'" if !node['chef_environment']
251
+ raise ArgumentError, "#{node} missing 'run_list'" if !node['run_list']
252
+ raise ArgumentError, "#{node} missing 'recipes'" if !node['recipes']
253
+ raise ArgumentError, "#{node} missing 'roles'" if !node['roles']
254
+ raise ArgumentError, "#{node} missing 'fqdn'" if !node['fqdn']
255
+ raise ArgumentError, "#{node} missing 'hostname'" if !node['hostname']
256
+ raise ArgumentError, "#{node} missing 'kernel.machine'" if !node['kernel_machine']
257
+ raise ArgumentError, "#{node} missing 'kernel.os'" if !node['kernel_os']
258
+ raise ArgumentError, "#{node} missing 'platform'" if !node['platform']
259
+ raise ArgumentError, "#{node} missing 'platform_version'" if !node['platform_version']
260
+ end
261
+
262
+
263
+ # partial_search(type, query, options, &block)
264
+ #
265
+ # Searches for nodes, roles, etc. and returns the results. This method may
266
+ # perform more than one search request, if there are a large number of results.
267
+ #
268
+ # ==== Parameters
269
+ # * +type+: index type (:role, :node, :client, :environment, data bag name)
270
+ # * +query+: SOLR query. "*:*", "role:blah", "not role:blah", etc. Defaults to '*:*'
271
+ # * +options+: hash with options:
272
+ # ** +:start+: First row to return (:start => 50, :rows => 100 means "return the
273
+ # 50th through 150th result")
274
+ # ** +:rows+: Number of rows to return. Defaults to 1000.
275
+ # ** +:sort+: a SOLR sort specification. Defaults to 'X_CHEF_id_CHEF_X asc'.
276
+ # ** +:keys+: partial search keys. If this is not specified, the search will
277
+ # not be partial.
278
+ #
279
+ # ==== Returns
280
+ #
281
+ # This method returns an array of search results. Partial search results will
282
+ # be JSON hashes with the structure specified in the +keys+ option. Other
283
+ # results include +Chef::Node+, +Chef::Role+, +Chef::Client+, +Chef::Environment+,
284
+ # +Chef::DataBag+ and +Chef::DataBagItem+ objects, depending on the search type.
285
+ #
286
+ # If a block is specified, the block will be called with each result instead of
287
+ # returning an array. This method will not block if it returns
288
+ #
289
+ # If start or row is specified, and no block is given, the result will be a
290
+ # triple containing the list, the start and total:
291
+ #
292
+ # [ [ row1, row2, ... ], start, total ]
293
+ #
294
+ # ==== Example
295
+ #
296
+ # partial_search(:node, 'role:webserver',
297
+ # keys: {
298
+ # name: [ 'name' ],
299
+ # ip: [ 'amazon', 'ip', 'public' ]
300
+ # }
301
+ # ).each do |node|
302
+ # puts "#{node[:name]}: #{node[:ip]}"
303
+ # end
304
+ #
305
+ def partial_search(type, query='*:*', *args, &block)
306
+ # Support both the old (positional args) and new (hash args) styles of calling
307
+ if args.length == 1 && args[0].is_a?(Hash)
308
+ args_hash = args[0]
309
+ else
310
+ args_hash = {}
311
+ args_hash[:sort] = args[0] if args.length >= 1
312
+ args_hash[:start] = args[1] if args.length >= 2
313
+ args_hash[:rows] = args[2] if args.length >= 3
314
+ end
315
+ # If you pass a block, or have the start or rows arguments, do raw result parsing
316
+ if Kernel.block_given? || args_hash[:start] || args_hash[:rows]
317
+ PartialSearch.new.search(type, query, args_hash, &block)
318
+
319
+ # Otherwise, do the iteration for the end user
320
+ else
321
+ results = Array.new
322
+ PartialSearch.new.search(type, query, args_hash) do |o|
323
+ results << o
324
+ end
325
+ results
326
+ end
327
+ end