knife-role-replace 0.0.3 → 0.0.4
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/lib/chef/knife/replace.rb +87 -36
- data/lib/knife-role-replace/version.rb +1 -1
- metadata +2 -2
data/lib/chef/knife/replace.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
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
|
-
|
43
|
+
node = Chef::Node.load(host)
|
40
44
|
|
41
|
-
list =
|
42
|
-
ui.msg("Here is the current
|
45
|
+
list = node.run_list
|
46
|
+
ui.msg("Here is the current run_list: #{list}")
|
43
47
|
|
44
48
|
begin
|
45
49
|
|
46
|
-
|
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
|
61
|
+
ui.msg("Here is the modified run_list: #{new_list}")
|
58
62
|
|
59
|
-
|
60
|
-
|
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
|
|
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.
|
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-
|
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:
|