knife-role-replace 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,49 +1,53 @@
1
1
  require 'chef/knife'
2
2
 
3
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
4
 
8
- deps do
9
- require 'chef/node'
5
+ def self.search(attribute = "*", value = "*", include_node_data = false)
6
+ response = include_node_data ? {} : []
7
+ Chef::Search::Query.new.search(:node, attribute+":"+value) do |n|
8
+ if include_node_data then
9
+ response[n.name] = n unless n.nil?
10
+ else
11
+ response << n.name unless n.nil?
12
+ end
13
+ end
14
+ response
10
15
  end
11
16
 
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
17
+ class RoleReplace < Chef::Knife
18
+
19
+ banner "knife role replace -s search_role -o role_to_replace -n new_role "
20
+
21
+ deps do
22
+ require 'chef/node'
34
23
  end
35
24
 
25
+ option :search_role,
26
+ :short => '-s ROLE',
27
+ :long => '--search_role ROLE'
28
+
29
+ option :old_role,
30
+ :short => '-o ROLE',
31
+ :long => '--old_role ROLE'
32
+
33
+ option :new_role,
34
+ :short => '-n ROLE',
35
+ :long => '--new_role ROLE'
36
+
36
37
  def run
37
- s = search('role', "#{config[:search_role]}*")
38
+ search_role = config[:search_role]
39
+ #search_role = @name_args
40
+
41
+ s = TP::search('role', "#{search_role}*")
38
42
  s.map do |host|
39
- @node = Chef::Node.load(host)
43
+ node = Chef::Node.load(host)
40
44
 
41
- list = @node.run_list
42
- ui.msg("Here is the current set of roles on this host: #{list}")
45
+ list = node.run_list
46
+ ui.msg("Here is the current run_list: #{list}")
43
47
 
44
48
  begin
45
49
 
46
- @new_list = list.map do |ele|
50
+ new_list = list.map do |ele|
47
51
  if rest.get_rest("/roles/#{config[:new_role]}")
48
52
  if ele == "role[#{config[:old_role]}]"
49
53
  "role[#{config[:new_role]}]"
@@ -54,10 +58,10 @@ module TP
54
58
  end
55
59
 
56
60
  ui.msg("Fixing up the run_list!\n")
57
- ui.msg("Here is the modified set of roles: #{@new_list}")
61
+ ui.msg("Here is the modified run_list: #{new_list}")
58
62
 
59
- @node.run_list(@new_list)
60
- @node.save
63
+ node.run_list(new_list)
64
+ node.save
61
65
 
62
66
  if $?.success?
63
67
  ui.msg("Node run_list has been saved on #{host}.")
@@ -67,8 +71,55 @@ module TP
67
71
 
68
72
  rescue Net::HTTPServerException
69
73
  ui.fatal("Role: '#{config[:new_role]}' was not found on the server. Did you upload it?")
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ class RoleAdd < Chef::Knife
81
+
82
+ banner "knife role add -s search_role -n new_role -p position "
83
+
84
+ deps do
85
+ require 'chef/node'
86
+ end
87
+
88
+ option :search_role,
89
+ :short => '-s ROLE',
90
+ :long => '--search_role ROLE'
91
+
92
+ option :position,
93
+ :short => '-p position',
94
+ :long => '--position position'
95
+
96
+ option :new_role,
97
+ :short => '-n new_role',
98
+ :long => '--new_role role'
99
+
100
+ def run
101
+ #search_role = @name_args
102
+ search_role = config[:search_role]
103
+
104
+ s = TP::search('role', "#{search_role}*")
105
+ s.map do |host|
106
+ node = Chef::Node.load(host)
107
+
108
+ list = node.run_list
109
+ ui.msg("Here is your current run_list: #{list}")
110
+
111
+ begin
112
+ if rest.get_rest("/roles/#{config[:new_role]}")
113
+ new_list = list.to_a.insert("#{config[:position]}".to_i, "role[#{config[:new_role]}]")
114
+ end
115
+
116
+ ui.msg("Here is your new run_list: #{new_list}")
117
+ node.run_list(new_list)
118
+ node.save
119
+
120
+ rescue Net::HTTPServerException
121
+ ui.fatal("Role: '#{config[:new_role]}' was not found on the server. Did you upload it?")
70
122
  end
71
-
72
123
  end
73
124
  end
74
125
 
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module TP
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-25 00:00:00.000000000 Z
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Replaces roles on a node when given search criteria
15
15
  email: