knife-role-replace 0.0.1 → 0.0.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.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require 'knife-role-replace/version'
5
+
6
+ Gem::Specification.new do |g|
7
+ g.authors = ["mvam75"]
8
+ g.email = ["foo@example.org"]
9
+ g.description = %q{Replaces roles on a node when given search criteria}
10
+ g.summary = %q{Replaces roles on a node when given search criteria. Use 'knife role replace' To search and replace roles on a node.}
11
+ g.homepage = 'https://github.com/mvam75/knife-role-replace'
12
+
13
+ g.files = `git ls-files`.split($\)
14
+ g.executables = g.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ g.name = "knife-role-replace"
16
+ g.require_paths = ["lib"]
17
+ g.version = Knife::TP::VERSION
18
+ end
@@ -0,0 +1,64 @@
1
+ require 'chef/knife'
2
+
3
+ module TP
4
+ class RoleReplace < Chef::Knife
5
+
6
+ banner "knife role replace -s SEARCH_ROLE -o ROLE_TO_REPLACE -n NEW_ROLE"
7
+
8
+ deps do
9
+ require 'chef/node'
10
+ end
11
+
12
+ option :search_role,
13
+ :short => '-s ROLE',
14
+ :long => '--search_role ROLE'
15
+
16
+ option :old_role,
17
+ :short => '-o ROLE',
18
+ :long => '--old_role ROLE'
19
+
20
+ option :new_role,
21
+ :short => '-n ROLE',
22
+ :long => '--new_role ROLE'
23
+
24
+ def search(attribute = "*", value = "*", include_node_data = false)
25
+ response = include_node_data ? {} : []
26
+ Chef::Search::Query.new.search(:node, attribute+":"+value) do |n|
27
+ if include_node_data then
28
+ response[n.name] = n unless n.nil?
29
+ else
30
+ response << n.name unless n.nil?
31
+ end
32
+ end
33
+ response
34
+ end
35
+
36
+ def run
37
+ s = search('role', "#{config[:search_role]}*")
38
+ s.map do |host|
39
+ @node = Chef::Node.load(host)
40
+
41
+ list = @node.run_list
42
+ ui.msg("Here is the current set of roles on this host: #{list}")
43
+
44
+ @new_list = list.map do |ele|
45
+ old_role_name = config[:old_role]
46
+
47
+ if ele == "role[#{config[:old_role]}]"
48
+ "role[#{config[:new_role]}]"
49
+ else
50
+ "#{ele}"
51
+ end
52
+ end
53
+
54
+ ui.msg("Fixing up the run_list!\n")
55
+ ui.msg("Here is the modified set of roles: #{@new_list}")
56
+
57
+ @node.run_list(@new_list)
58
+ @node.save
59
+ ui.msg("Node run_list has been saved on #{host}.") unless !$?.success?
60
+ end
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module TP
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-role-replace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,12 @@ email:
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
- files: []
20
+ files:
21
+ - Gemfile
22
+ - knife-role-replace.gemspec
23
+ - lib/chef/knife/replace.rb
24
+ - lib/knife-role-replace.rb
25
+ - lib/knife-role-replace/version.rb
21
26
  homepage: https://github.com/mvam75/knife-role-replace
22
27
  licenses: []
23
28
  post_install_message: