Initial Chef repository

This commit is contained in:
Greg Karékinian
2015-07-21 19:45:23 +02:00
parent 7e5401fc71
commit ee4079fa85
1151 changed files with 185163 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
partial_search Cookbook CHANGELOG
=================================
This file is used to list changes made in each version of the partial_search cookbook.
v1.0.8 (2014-02-25)
-------------------
- [COOK-4260] Update compatibility in README.md
v1.0.6
------
- **Hotfix** - Revert client-side caching bug
v1.0.4
------
### New Feature
- **[COOK-2584](https://tickets.opscode.com/browse/COOK-2584)** - Add client-side result cache
v1.0.2
------
### Bug
- [COOK-3164]: `partial_search` should use
`Chef::Config[:chef_server_url]` instead of `search_url`
v1.0.0
------
- Initial release

View File

@@ -0,0 +1,82 @@
Partial Search Cookbook
=======================
[Partial Search](http://docs.opscode.com/essentials_search.html#partial-search)
is a search API available on Chef Server. (see Notes below for version compatibility)
It can be used to reduce the network bandwidth and the memory used by
chef-client to process search results.
This cookbook provides an experimental interface to the partial search
API by providing a `partial_search` method that can be used instead of
the `search` method in your recipes.
Since Chef Client 11.10.0 the partial_search capability has been built-in
so it does not require this cookbook.
The `partial_search` method allows you to retrieve just the attributes
of interest. For example, you can execute a search to return just the
name and IP addresses of the nodes in your infrastructure rather than
receiving an array of complete node objects and post-processing them.
Install
-------
Upload this cookbook and include it in the dependencies of any
cookbook where you would like to use `partial_search`.
Usage
-----
When you call `partial_search`, you need to specify the key paths of the
attributes you want returned. Key paths are specified as an array
of strings. Each key path is mapped to a short name of your
choosing. Consider the following example:
```ruby
partial_search(:node, 'role:web',
:keys => { 'name' => [ 'name' ],
'ip' => [ 'ipaddress' ],
'kernel_version' => [ 'kernel', 'version' ]
}
).each do |result|
puts result['name']
puts result['ip']
puts result['kernel_version']
end
```
In the example above, two attributes will be extracted (on the
server) from the nodes that match the search query. The result will
be a simple hash with keys 'name' and 'ip'.
Notes
-----
* We would like your feedback on this feature and the interface
provided by this cookbook. Please send comments to the chef-dev
mailing list.
* The partial search API is available in the Open Source Chef Server since 11.0.4
* The partial search API is available in Enterprise Chef Server since 1.2.2
License & Authors
-----------------
- Author:: Adam Jacob (<adam@opscode.com>)
- Author:: John Keiser (<jkeiser@opscode.com>)
```text
Copyright:: 2012-2013, Opscode, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```

View File

@@ -0,0 +1,147 @@
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/config'
require 'uri'
require 'chef/rest'
# These are needed so that JSON can inflate search results
require 'chef/node'
require 'chef/role'
require 'chef/environment'
require 'chef/data_bag'
require 'chef/data_bag_item'
class Chef
class PartialSearch
attr_accessor :rest
def initialize(url=nil)
@rest = ::Chef::REST.new(url || ::Chef::Config[:chef_server_url])
end
# Search Solr for objects of a given type, for a given query. If you give
# it a block, it will handle the paging for you dynamically.
def search(type, query='*:*', args={}, &block)
raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))
sort = args.include?(:sort) ? args[:sort] : 'X_CHEF_id_CHEF_X asc'
start = args.include?(:start) ? args[:start] : 0
rows = args.include?(:rows) ? args[:rows] : 1000
query_string = "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}"
if args[:keys]
response = @rest.post_rest(query_string, args[:keys])
response_rows = response['rows'].map { |row| row['data'] }
else
response = @rest.get_rest(query_string)
response_rows = response['rows']
end
if block
response_rows.each { |o| block.call(o) unless o.nil?}
unless (response["start"] + response_rows.length) >= response["total"]
nstart = response["start"] + rows
args_hash = {
:keys => args[:keys],
:sort => sort,
:start => nstart,
:rows => rows
}
search(type, query, args_hash, &block)
end
true
else
[ response_rows, response["start"], response["total"] ]
end
end
def list_indexes
response = @rest.get_rest("search")
end
private
def escape(s)
s && URI.escape(s.to_s)
end
end
end
# partial_search(type, query, options, &block)
#
# Searches for nodes, roles, etc. and returns the results. This method may
# perform more than one search request, if there are a large number of results.
#
# ==== Parameters
# * +type+: index type (:role, :node, :client, :environment, data bag name)
# * +query+: SOLR query. "*:*", "role:blah", "not role:blah", etc. Defaults to '*:*'
# * +options+: hash with options:
# ** +:start+: First row to return (:start => 50, :rows => 100 means "return the
# 50th through 150th result")
# ** +:rows+: Number of rows to return. Defaults to 1000.
# ** +:sort+: a SOLR sort specification. Defaults to 'X_CHEF_id_CHEF_X asc'.
# ** +:keys+: partial search keys. If this is not specified, the search will
# not be partial.
#
# ==== Returns
#
# This method returns an array of search results. Partial search results will
# be JSON hashes with the structure specified in the +keys+ option. Other
# results include +Chef::Node+, +Chef::Role+, +Chef::Client+, +Chef::Environment+,
# +Chef::DataBag+ and +Chef::DataBagItem+ objects, depending on the search type.
#
# If a block is specified, the block will be called with each result instead of
# returning an array. This method will not block if it returns
#
# If start or row is specified, and no block is given, the result will be a
# triple containing the list, the start and total:
#
# [ [ row1, row2, ... ], start, total ]
#
# ==== Example
#
# partial_search(:node, 'role:webserver',
# keys: {
# name: [ 'name' ],
# ip: [ 'amazon', 'ip', 'public' ]
# }
# ).each do |node|
# puts "#{node[:name]}: #{node[:ip]}"
# end
#
def partial_search(type, query='*:*', *args, &block)
# Support both the old (positional args) and new (hash args) styles of calling
if args.length == 1 && args[0].is_a?(Hash)
args_hash = args[0]
else
args_hash = {}
args_hash[:sort] = args[0] if args.length >= 1
args_hash[:start] = args[1] if args.length >= 2
args_hash[:rows] = args[2] if args.length >= 3
end
# If you pass a block, or have the start or rows arguments, do raw result parsing
if Kernel.block_given? || args_hash[:start] || args_hash[:rows]
Chef::PartialSearch.new.search(type, query, args_hash, &block)
# Otherwise, do the iteration for the end user
else
results = Array.new
Chef::PartialSearch.new.search(type, query, args_hash) do |o|
results << o
end
results
end
end

View File

@@ -0,0 +1,29 @@
{
"name": "partial_search",
"version": "1.0.8",
"description": "Provides experimental interface to partial search API in Opscode Hosted Chef",
"long_description": "Partial Search Cookbook\n=======================\n[Partial Search](http://docs.opscode.com/essentials_search.html#partial-search)\nis a search API available on Chef Server. (see Notes below for version compatibility) \nIt can be used to reduce the network bandwidth and the memory used by\nchef-client to process search results.\n\nThis cookbook provides an experimental interface to the partial search\nAPI by providing a `partial_search` method that can be used instead of\nthe `search` method in your recipes.\n\nSince Chef Client 11.10.0 the partial_search capability has been built-in\nso it does not require this cookbook.\n\nThe `partial_search` method allows you to retrieve just the attributes\nof interest. For example, you can execute a search to return just the\nname and IP addresses of the nodes in your infrastructure rather than\nreceiving an array of complete node objects and post-processing them.\n\n\nInstall\n-------\nUpload this cookbook and include it in the dependencies of any\ncookbook where you would like to use `partial_search`.\n\n\nUsage\n-----\nWhen you call `partial_search`, you need to specify the key paths of the\nattributes you want returned. Key paths are specified as an array\nof strings. Each key path is mapped to a short name of your\nchoosing. Consider the following example:\n\n```ruby\npartial_search(:node, 'role:web',\n :keys => { 'name' => [ 'name' ],\n 'ip' => [ 'ipaddress' ],\n 'kernel_version' => [ 'kernel', 'version' ]\n }\n).each do |result|\n puts result['name']\n puts result['ip']\n puts result['kernel_version']\nend\n```\n\nIn the example above, two attributes will be extracted (on the\nserver) from the nodes that match the search query. The result will\nbe a simple hash with keys 'name' and 'ip'.\n\n\nNotes\n-----\n* We would like your feedback on this feature and the interface\n provided by this cookbook. Please send comments to the chef-dev\n mailing list.\n\n* The partial search API is available in the Open Source Chef Server since 11.0.4\n\n* The partial search API is available in Enterprise Chef Server since 1.2.2\n\n\nLicense & Authors\n-----------------\n- Author:: Adam Jacob (<adam@opscode.com>)\n- Author:: John Keiser (<jkeiser@opscode.com>)\n\n```text\nCopyright:: 2012-2013, Opscode, Inc.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n",
"maintainer": "Opscode, Inc.",
"maintainer_email": "cookbooks@opscode.com",
"license": "Apache 2.0",
"platforms": {
},
"dependencies": {
},
"recommendations": {
},
"suggestions": {
},
"conflicting": {
},
"providing": {
},
"replacing": {
},
"attributes": {
},
"groupings": {
},
"recipes": {
}
}

View File

@@ -0,0 +1,7 @@
name "partial_search"
maintainer "Opscode, Inc."
maintainer_email "cookbooks@opscode.com"
license "Apache 2.0"
description "Provides experimental interface to partial search API in Opscode Hosted Chef"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "1.0.8"

View File

@@ -0,0 +1 @@
#