knife-tagbatch 0.1.0
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.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +1 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/knife-tagbatch.gemspec +19 -0
- data/lib/chef/knife/tagbatch.rb +95 -0
- data/lib/knife-tagbatch/version.rb +5 -0
- data/spec/chef/knife/tagbatch_spec.rb +8 -0
- data/spec/spec_helper.rb +2 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b45ebbcc76cffffe8e71e6506442bd0b904cbea2
|
4
|
+
data.tar.gz: 64b35eda0fd8513169ee0b7fa32d0f60b72417a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 95dce0967f599d5d680af7e6eabce2850369da6047fa3a9d8d3639f87df2c216df6440c5cb0a54a5609709fe1bd818f7a9999f6b4e170def2946230d905f51d1
|
7
|
+
data.tar.gz: e83dbbeb662b44aa6e1f5dd589005c18f26eb1608438c4ffecf038dcab1d8078149d106a2961a0403b36218671b299b82651a6aa518d80366c477e00b5f84892
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
GNU General Public License (GPL>=3)
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Knife::Tagbatch
|
2
|
+
|
3
|
+
Tagbatch is a plugin for the Chef Knife tool (https://docs.chef.io/knife.html). It adds search functionality to knife tag and allows the execution of multiple tag additions or removals in one statement. There is a flag for logging and dry-run execution.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Gem installation:
|
8
|
+
`gem install knife-tagbatch`
|
9
|
+
|
10
|
+
Manual installation:
|
11
|
+
Clone https://github.com/nkaravias/knife-tagbatch.git
|
12
|
+
Create ~/.chef/knife/plugins
|
13
|
+
Copy lib/chef/knife/tagbatch.rb to plugins
|
14
|
+
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
knife tag batch <operation> <tag string> <knife search regexp> | -l | -d
|
19
|
+
|
20
|
+
Allowed operations are add | remove for creating and removing Chef tags
|
21
|
+
|
22
|
+
e.g Add the 'webserver' tag to all nodes that have the web-server role on environment x.
|
23
|
+
knife tag add webserver 'role:web-server AND chef_environment=x'
|
24
|
+
|
25
|
+
Optional arguments:
|
26
|
+
|
27
|
+
-l (Logging): Knife will write the output of the tag operation per node on tagBatch.log in the current working directory
|
28
|
+
-d (dry run): Knife will execute the operation but will not save the results (tag creation / removal) on the node
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/nkaravias/knife-tagbatch/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'knife-tagbatch/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "knife-tagbatch"
|
7
|
+
gem.version = CustomChef::KnifeTagBatch::VERSION
|
8
|
+
gem.authors = ["Nikolas Karavias"]
|
9
|
+
gem.email = ["nikolas.karavias@oracle.com"]
|
10
|
+
gem.licenses = ["GPL-3"]
|
11
|
+
gem.summary = %q{Add search functionality to the knife tag command}
|
12
|
+
gem.description = gem.summary
|
13
|
+
gem.homepage = "https://github.com/nkaravias/knife-tagbatch"
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.add_runtime_dependency "chef"
|
19
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "knife-tagbatch/version"
|
2
|
+
|
3
|
+
module CustomChef
|
4
|
+
module KnifeTagBatch
|
5
|
+
class TagBatch < Chef::Knife
|
6
|
+
deps do
|
7
|
+
require 'chef/node'
|
8
|
+
require 'chef/search/query'
|
9
|
+
end
|
10
|
+
# Don't save the changes to the node object
|
11
|
+
option :dry_run,
|
12
|
+
:short => "-d",
|
13
|
+
:long => "--dry-run",
|
14
|
+
:description => "Don't save the node changes",
|
15
|
+
:boolean => true | false,
|
16
|
+
:default => false
|
17
|
+
|
18
|
+
# Log tagging log to ./batch-tag.log
|
19
|
+
option :batch_log,
|
20
|
+
:short => "-l",
|
21
|
+
:long => "--batch-log",
|
22
|
+
:description => "Write batch results to $CWD/tagBatch.log",
|
23
|
+
:boolean => true | false,
|
24
|
+
:default => false
|
25
|
+
|
26
|
+
banner "knife tag batch add|remove TAG SEARCH_QUERY"
|
27
|
+
|
28
|
+
def run
|
29
|
+
@command = validate_arg(@name_args[0], 'command', /^add$|^remove$/)
|
30
|
+
@tag = validate_arg(@name_args[1],'tag',/[\w-]*/)
|
31
|
+
@search_query = validate_arg(@name_args[2], 'search', /[\w]*:[\w*-]*(\sAND\s|\sOR\s|\sNOT\s)*/)
|
32
|
+
@query_nodes = Chef::Search::Query.new.search('node', @search_query)
|
33
|
+
ui.msg "Search returned #{@query_nodes.last} node(s)"
|
34
|
+
if @query_nodes.last > 0
|
35
|
+
ui.confirm("Execute command \"#{@command}\" with tag #{@tag} on #{@query_nodes.last} node(s)?", append_instructions=true)
|
36
|
+
elsif @query_nodes.last == 0
|
37
|
+
ui.warn('No nodes returned from search')
|
38
|
+
else
|
39
|
+
ui.fatal('Invalid return from search')
|
40
|
+
end
|
41
|
+
|
42
|
+
@query_nodes[0].each do |node|
|
43
|
+
self.send("#{@command}_tag",node,@tag)
|
44
|
+
end
|
45
|
+
ui.msg('Operation completed')
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_tag(node, tag)
|
49
|
+
if node.tags.include?(tag)
|
50
|
+
log = "#{Time.now.getutc}:#{node.name} already has tag \"#{@tag}\""
|
51
|
+
else
|
52
|
+
node.tags << tag
|
53
|
+
unless config[:dry_run]
|
54
|
+
node.save
|
55
|
+
end
|
56
|
+
log = "#{Time.now.getutc}:Created tag \"#{@tag}\" on #{node.name}"
|
57
|
+
end
|
58
|
+
if config[:batch_log]
|
59
|
+
log_node(log)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def remove_tag(node, tag)
|
64
|
+
if node.tags.include?(tag)
|
65
|
+
node.tags.delete(tag)
|
66
|
+
unless config[:dry_run]
|
67
|
+
node.save
|
68
|
+
end
|
69
|
+
log = "#{Time.now.getutc}:Removed tag \"#{@tag}\" on #{node.name}"
|
70
|
+
else
|
71
|
+
log = "#{Time.now.getutc}:The tag \"#{@tag}\" does not exist on #{node.name}"
|
72
|
+
end
|
73
|
+
if config[:batch_log]
|
74
|
+
log_node(log)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def log_node(str)
|
79
|
+
log_filename = 'tagBatch.log'
|
80
|
+
File.open(log_filename, 'a') do |file|
|
81
|
+
file.puts str
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_arg(arg, type, pattern)
|
86
|
+
unless arg =~ pattern
|
87
|
+
show_usage
|
88
|
+
ui.fatal "Argument #{type}:#{arg} is not valid"
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
arg
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-tagbatch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikolas Karavias
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-04 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
|
+
description: Add search functionality to the knife tag command
|
28
|
+
email:
|
29
|
+
- nikolas.karavias@oracle.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- knife-tagbatch.gemspec
|
40
|
+
- lib/chef/knife/tagbatch.rb
|
41
|
+
- lib/knife-tagbatch/version.rb
|
42
|
+
- spec/chef/knife/tagbatch_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
homepage: https://github.com/nkaravias/knife-tagbatch
|
45
|
+
licenses:
|
46
|
+
- GPL-3
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.4.6
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Add search functionality to the knife tag command
|
68
|
+
test_files:
|
69
|
+
- spec/chef/knife/tagbatch_spec.rb
|
70
|
+
- spec/spec_helper.rb
|