knife-api 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +1 -0
- data/knife-api.gemspec +22 -0
- data/lib/knife/api.rb +63 -0
- data/lib/knife/api/version.rb +7 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 158d503433c25d5538023b080a366051098ad155
|
4
|
+
data.tar.gz: 4065edc3773299e2834ada16861d430d9c6250ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42f944444499e3dfe47f0f1d939343b59c706ebb91778537c3e93077437708b90e97b524cbef25230c8646ccb48bf3fee9fc85a6edc5573d76a7746db0a853f8
|
7
|
+
data.tar.gz: 2b646c315f0a6f055d2c45f49a8f3407eabe6c9dd808b6cf37626cb98fc2cca7a6b4b26874ead5ce474645b7cc9c94eafef31910887b8283e4e7ebd59a90c994
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Erik Hollensbe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Knife-API
|
2
|
+
|
3
|
+
Decided to change the name of this gem from knife-dsl to knife-api because the gem exposes methods from knife, the
|
4
|
+
workhorse command line tool from the chef system and makes the methods available to ruby code. A library that lets
|
5
|
+
you drive a command line tool programmatically offers an API and is not a DSL.
|
6
|
+
|
7
|
+
A small library that lets you drive Chef's `knife` programmatically
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'knife-api'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install knife-api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
The main feature of this library is the ability to drive knife as a method call
|
26
|
+
as opposed to an individual program. This has a number of benefits:
|
27
|
+
|
28
|
+
* It's a lot faster
|
29
|
+
* You can capture output
|
30
|
+
* You're relying on code that has a contract to maintain compatible with Chef
|
31
|
+
* Use knife plugins all you want without breaking your workflow
|
32
|
+
* Optionally, Chef becomes constrained by a gemfile (as do the plugins you use)
|
33
|
+
and that constraint remains consistent.
|
34
|
+
|
35
|
+
It provides two calls, `knife` and `knife_capture` that are injected into the
|
36
|
+
top-level namespace automatically. Additionally, if you are using `rake`, it
|
37
|
+
will detect this and make it available to rake's DSL as well.
|
38
|
+
|
39
|
+
If you wish to use this code elsewhere, just `include Chef::Knife::API` into
|
40
|
+
your classes/modules.
|
41
|
+
|
42
|
+
Both commands take two argument-passing styles. Both use an array of strings to
|
43
|
+
represent the ARGV passed to knife, but there is additionally a shorthand to
|
44
|
+
make the actual subcommand stand out: if you supply a symbol or string with
|
45
|
+
underscore-delimited subcommand names, it will automatically convert this for
|
46
|
+
you. This allows you to visually distinguish a command from its arguments.
|
47
|
+
|
48
|
+
knife-api allows the use of alternative Chef Configs via the `CHEF_CONFIG`
|
49
|
+
environment variable.
|
50
|
+
|
51
|
+
## Example
|
52
|
+
|
53
|
+
Here's a `Rakefile` which lists nodes in one task, and in another, converts the
|
54
|
+
node metadata supplied by `knife node show $x` to something suitable to display
|
55
|
+
with `pp`. The practical applications of both tasks are pretty specious, but
|
56
|
+
they're small and display the functionality.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'knife/api'
|
60
|
+
require 'pp'
|
61
|
+
require 'json'
|
62
|
+
|
63
|
+
task :list_nodes do
|
64
|
+
status = knife :node_list
|
65
|
+
fail if status > 0
|
66
|
+
end
|
67
|
+
|
68
|
+
task :show_node, :node_name do |task_name, args|
|
69
|
+
stdout, stderr, status = knife_capture :node_show, [args[:node_name], '-F', 'j']
|
70
|
+
fail if status > 0
|
71
|
+
pp JSON.load(stdout)
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/knife-api.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'knife/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "knife-api"
|
8
|
+
gem.version = Chef::Knife::API::VERSION
|
9
|
+
gem.authors = ["Erik Hollensbe", "Andy Glick"]
|
10
|
+
gem.email = ["andyglick@gmailcom"]
|
11
|
+
gem.description = %q{A small library that lets you drive Chef's 'knife' programmatically}
|
12
|
+
gem.summary = %q{A small library that lets you drive Chef's 'knife' programmatically}
|
13
|
+
gem.homepage = "https://github.com/andyglick/knife-api"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'chef', '>= 10.0', '< 12.0.0'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
data/lib/knife/api.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'chef/application/knife'
|
2
|
+
require 'knife/api/version'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module Chef::Knife::API
|
6
|
+
module Chef::Knife::API::Support
|
7
|
+
def self.run_knife(command, args)
|
8
|
+
unless command.kind_of?(Array)
|
9
|
+
command = command.to_s.split(/[\s_]+/)
|
10
|
+
end
|
11
|
+
|
12
|
+
command += args
|
13
|
+
|
14
|
+
if ENV["CHEF_CONFIG"]
|
15
|
+
command += ['-c', ENV["CHEF_CONFIG"]]
|
16
|
+
end
|
17
|
+
|
18
|
+
opts = Chef::Application::Knife.new.options
|
19
|
+
Chef::Knife.run(command, opts)
|
20
|
+
return 0
|
21
|
+
rescue SystemExit => e
|
22
|
+
return e.status
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def knife(command, args=[])
|
27
|
+
Chef::Knife::API::Support.run_knife(command, args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def knife_capture(command, args=[], input=nil)
|
31
|
+
null = Gem.win_platform? ? File.open('NUL:', 'r') : File.open('/dev/null', 'r')
|
32
|
+
|
33
|
+
warn = $VERBOSE
|
34
|
+
$VERBOSE = nil
|
35
|
+
stderr, stdout, stdin = STDERR, STDOUT, STDIN
|
36
|
+
|
37
|
+
Object.const_set("STDERR", StringIO.new('', 'r+'))
|
38
|
+
Object.const_set("STDOUT", StringIO.new('', 'r+'))
|
39
|
+
Object.const_set("STDIN", input ? StringIO.new(input, 'r') : null)
|
40
|
+
$VERBOSE = warn
|
41
|
+
|
42
|
+
status = Chef::Knife::API::Support.run_knife(command, args)
|
43
|
+
return STDOUT.string, STDERR.string, status
|
44
|
+
ensure
|
45
|
+
warn = $VERBOSE
|
46
|
+
$VERBOSE = nil
|
47
|
+
Object.const_set("STDERR", stderr)
|
48
|
+
Object.const_set("STDOUT", stdout)
|
49
|
+
Object.const_set("STDIN", stdin)
|
50
|
+
$VERBOSE = warn
|
51
|
+
null.close
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class << eval("self", TOPLEVEL_BINDING)
|
56
|
+
include Chef::Knife::API
|
57
|
+
end
|
58
|
+
|
59
|
+
if defined? Rake::API
|
60
|
+
module Rake::API
|
61
|
+
include Chef::Knife::API
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Erik Hollensbe
|
8
|
+
- Andy Glick
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: chef
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '10.0'
|
21
|
+
- - "<"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 12.0.0
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '10.0'
|
31
|
+
- - "<"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 12.0.0
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
type: :development
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
description: A small library that lets you drive Chef's 'knife' programmatically
|
49
|
+
email:
|
50
|
+
- andyglick@gmailcom
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- ".gitignore"
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- knife-api.gemspec
|
61
|
+
- lib/knife/api.rb
|
62
|
+
- lib/knife/api/version.rb
|
63
|
+
homepage: https://github.com/andyglick/knife-api
|
64
|
+
licenses: []
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.2.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: A small library that lets you drive Chef's 'knife' programmatically
|
86
|
+
test_files: []
|