-
Notifications
You must be signed in to change notification settings - Fork 4
/
set_attribute.rb
56 lines (48 loc) · 1.27 KB
/
set_attribute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class SetAttribute < Chef::Knife
deps do
require 'chef/api_client'
require 'chef'
require 'chef/node'
require 'chef/role'
end
banner "knife set_attribute node|role [NODENAME OR ROLENAME] [ATTRIBUTE] [VALUE]"
def run
unless name_args.length == 4
show_usage
exit 1
else
@name = @name_args[1]
attribute_arg = @name_args[2]
new_value = @name_args[3]
if @name_args[0] == 'node'
type = 'node'
elsif @name_args[0] == 'role'
type = 'role'
end
end
if type == 'node'
@obj = Chef::Node.load(@name)
elsif type == 'role'
@obj = Chef::Role.load(@name)
end
slices = attribute_arg.split('.')
last = slices.pop
parts = slices
if type == 'node'
hash = parts.inject(@obj.normal) { |h,attr| !h.has_key?(attr) ? (h[attr]={}; h[attr]) : h[attr]}
elsif type == 'role'
hash = parts.inject(@obj.override_attributes) { |h,attr| !h.has_key?(attr) ? (h[attr]={}; h[attr]) : h[attr]}
end
if new_value.start_with?('[') and new_value.end_with?(']')
prev = eval(new_value)
new_arr = []
prev.each do |x|
new_arr << eval(x)
end
hash[last] = new_arr
else
hash[last] = new_value
end
@obj.save
end
end