chefsearch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0178526293118a8c9f6966fc8355c6c6e6ef1450
4
+ data.tar.gz: 274df200166fc311ce9c491bb252ae7c26343359
5
+ SHA512:
6
+ metadata.gz: b728aeb966497635900b2138de32c93ac13e01d7e711765f12f222f2eceda532c537d63a2e972a2423845420f550f81f492e803479c0d77f72491ff7036f49da
7
+ data.tar.gz: b03c37a1e6bbbefb69bf75e3135289d03edf07a8ed85464e8313e0eebfd56a31a8e3f4928fe2ccef44a6ff5eaae115e6060b32f379e419c2d37f97bc20cd6606
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.5
6
+ - 2.2.0
7
+ branches:
8
+ only:
9
+ - master
10
+ before_install:
11
+ - gem install bundler
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ChefSearch gem CHANGELOG
2
+ ======================
3
+ This file is used to list changes made in each version of the ChefSearch gem.
4
+
5
+ v0.0.1 (2015-02-20)
6
+ -------------------
7
+ - Initial gem publish
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chefsearch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Grant Ridder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # ChefSearch
2
+
3
+ [![Build Status](https://travis-ci.org/shortdudey123/chefsearch.svg?branch=master)](https://travis-ci.org/shortdudey123/chefsearch)
4
+ [![Gem Version](http://img.shields.io/gem/v/chefsearch.svg)](https://rubygems.org/gems/chefsearch)
5
+ [![Coverage Status](https://img.shields.io/coveralls/shortdudey123/chefsearch/master.svg)](https://coveralls.io/r/shortdudey123/chefsearch?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/shortdudey123/chefsearch/badges/gpa.svg)](https://codeclimate.com/github/shortdudey123/chefsearch)
7
+ [![Dependency Status](https://img.shields.io/gemnasium/shortdudey123/chefsearch.svg)](https://gemnasium.com/shortdudey123/chefsearch)
8
+
9
+
10
+ Search a Chef server via CLI or API given a role, name, or Chef environment
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'chefsearch'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install chefsearch
27
+
28
+ ## Usage
29
+
30
+ ### CLI
31
+
32
+ ```
33
+ chefsearch --help
34
+ Usage: ChefSearch [options]
35
+
36
+ Options:
37
+ -n, --name=<s> Node name
38
+ -r, --role=<s> Chef role
39
+ -e, --environment=<s> Chef environment
40
+ -c, --config=<s> Chef knife config filepath
41
+ -v, --version Print version and exit
42
+ -h, --help Show this message
43
+ ```
44
+
45
+ ### API
46
+
47
+ Print out all nodes from Chef server
48
+
49
+ ```ruby
50
+ require `chefsearch`
51
+ c = ChefSearch::Search.new
52
+ puts c.search
53
+ ```
54
+
55
+ Print out nodes from Chef server using the given knife config and meeting the search criteria
56
+
57
+ ```ruby
58
+ require `chefsearch`
59
+ c = ChefSearch::Search.new(config)
60
+ puts c.search(name: name,
61
+ role: role,
62
+ chef_environment: environment
63
+ )
64
+ ```
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it ( https://github.com/shortdudey123/chefsearch/fork )
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ desc 'Run all linters on the codebase'
8
+ task :linters do
9
+ Rake::Task['rubocop'].invoke
10
+ end
11
+
12
+ desc 'rubocop compliancy checks'
13
+ RuboCop::RakeTask.new(:rubocop) do |t|
14
+ t.patterns = %w{ bin/console exe/chefsearch lib/**/*.rb lib/*.rb spec/*.rb }
15
+ end
16
+
17
+ task default: [:linters, :spec]
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'chefsearch'
5
+ require 'pry'
6
+
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install --path .bundle
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chefsearch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chefsearch"
8
+ spec.version = ChefSearch::VERSION
9
+ spec.authors = ["Grant Ridder"]
10
+ spec.email = ["shortdudey123@gmail.com"]
11
+
12
+ spec.summary = %q{Quickly search a Chef server}
13
+ spec.description = %q{Search a Chef server via CLI or API given a role, name, or Chef environment}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.bindir = 'exe'
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '~> 2.0'
24
+
25
+ spec.add_dependency 'chef'
26
+ spec.add_dependency 'trollop', '~> 2'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.8'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'rubocop'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'coveralls'
34
+ end
data/exe/chefsearch ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'chefsearch'
6
+ require 'chefsearch/cli'
7
+
8
+ ChefSearch::CLI.new(ARGV).execute!
data/lib/chefsearch.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'logger'
2
+
3
+ require 'chefsearch/version'
4
+ require 'chefsearch/search'
5
+ require 'chefsearch/errors'
6
+
7
+ ###
8
+ #
9
+ # ChefSearch searches a Chef server via CLI or API
10
+ # given a role, name, or Chef environment
11
+ module ChefSearch
12
+ def self.logger
13
+ @logger ||= Logger.new(STDOUT).tap do |l|
14
+ l.level = Logger::INFO
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ require 'trollop'
2
+
3
+ module ChefSearch
4
+ ###
5
+ # CLI execution
6
+ #
7
+ class CLI
8
+ attr_reader :opts
9
+
10
+ # setup CLI options
11
+ def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR,
12
+ kernel = Kernel)
13
+ @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout,
14
+ stderr, kernel
15
+ end
16
+
17
+ # Run the CLI command
18
+ def execute!
19
+ parse_options
20
+
21
+ c = ChefSearch::Search.new(@opts.config)
22
+ puts c.search(name: @opts.name,
23
+ role: @opts.role,
24
+ chef_environment: @opts.environment
25
+ )
26
+ end
27
+
28
+ private
29
+
30
+ def parse_options
31
+ @opts = Trollop.options(@argv) do
32
+ banner 'Usage: ChefSearch [options]'
33
+ version(ChefSearch::VERSION)
34
+
35
+ banner ''
36
+ banner 'Options:'
37
+ opt :name, 'Node name', type: :string
38
+ opt :role, 'Chef role', type: :string
39
+ opt :environment, 'Chef environment', type: :string
40
+ opt :config, 'Chef knife config filepath', type: :string
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module ChefSearch
2
+ class FileNotFoundError < StandardError; end
3
+ end
@@ -0,0 +1,91 @@
1
+ require 'chef'
2
+
3
+ module ChefSearch
4
+ ###
5
+ #
6
+ # Implements the searching
7
+ #
8
+ class Search
9
+ FILTER_RESULT = { name: ['name'] }
10
+
11
+ def initialize(config = nil)
12
+ @config_filename = config
13
+ @chef_config = load_config
14
+ end
15
+
16
+ def search(opts = {})
17
+ name = opts.fetch(:name, '*')
18
+ role = opts.fetch(:role, '*')
19
+ chef_environment = opts.fetch(:chef_environment, '*')
20
+
21
+ query = build_query(name, role, chef_environment)
22
+ ChefSearch.logger.debug { query }
23
+
24
+ results = Chef::Search::Query.new.search(:node,
25
+ query,
26
+ filter_result: FILTER_RESULT
27
+ )
28
+ parse_results(results)
29
+ end
30
+
31
+ private
32
+
33
+ def build_query(name, role, chef_environment)
34
+ name = '*' if name.nil?
35
+ role = '*' if role.nil?
36
+ chef_environment = '*' if chef_environment.nil?
37
+
38
+ "name:#{name} AND "\
39
+ "role:#{role} AND "\
40
+ "chef_environment:#{chef_environment}"
41
+ end
42
+
43
+ def load_config
44
+ # try default places since no name was passed
45
+ if @config_filename.nil?
46
+ filepath = find_config_file
47
+ # name was passed, use it
48
+ else
49
+ f_given = File.expand_path(@config_filename)
50
+ fail(FileNotFoundError, "#{f_given} does not exist!") unless
51
+ File.exist?(f_given)
52
+ filepath = f_given
53
+ end
54
+
55
+ ChefSearch.logger.debug { filepath }
56
+
57
+ Chef::Config.from_file(filepath)
58
+ end
59
+
60
+ def find_config_file
61
+ f_local = File.join(File.dirname(__FILE__), '.chef', 'knife.rb')
62
+ f_home = File.join(Etc.getpwuid.dir, '.chef', 'knife.rb')
63
+
64
+ if File.exist?(f_local)
65
+ filepath = f_local
66
+ elsif File.exist?(f_home)
67
+ filepath = f_home
68
+ else
69
+ abort('Unable to file a Chef config. Please speficy its path.')
70
+ end
71
+
72
+ filepath
73
+ end
74
+
75
+ def parse_results(results)
76
+ results_ret = []
77
+
78
+ return results_ret if results[0].empty?
79
+
80
+ results[0].each do |r|
81
+ results_ret.push(r['data']['name'])
82
+ end
83
+
84
+ results_ret.sort!
85
+
86
+ ChefSearch.logger.debug { results_ret }
87
+
88
+ results_ret
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,7 @@
1
+ ###
2
+ #
3
+ # ChefSearch searches a Chef server via CLI or API
4
+ # given a role, name, or Chef environment
5
+ module ChefSearch
6
+ VERSION = '0.0.1'
7
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chefsearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Grant Ridder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: trollop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Search a Chef server via CLI or API given a role, name, or Chef environment
126
+ email:
127
+ - shortdudey123@gmail.com
128
+ executables:
129
+ - chefsearch
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".coveralls.yml"
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".travis.yml"
137
+ - CHANGELOG.md
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - bin/console
144
+ - bin/setup
145
+ - chefsearch.gemspec
146
+ - exe/chefsearch
147
+ - lib/chefsearch.rb
148
+ - lib/chefsearch/cli.rb
149
+ - lib/chefsearch/errors.rb
150
+ - lib/chefsearch/search.rb
151
+ - lib/chefsearch/version.rb
152
+ homepage: ''
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - "~>"
163
+ - !ruby/object:Gem::Version
164
+ version: '2.0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.2.2
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Quickly search a Chef server
176
+ test_files: []