Class: Chef::Role
Instance Attribute Summary
#source_file
Class Method Summary
collapse
Instance Method Summary
collapse
#lazy, #set_or_return, #validate
#class_from_file, #from_file
Constructor Details
#initialize(chef_server_rest: nil) ⇒ Role
Create a new Chef::Role object.
37
38
39
40
41
42
43
44
|
# File 'lib/chef/role.rb', line 37
def initialize(chef_server_rest: nil)
@name = ""
@description = ""
@default_attributes = Mash.new
@override_attributes = Mash.new
@env_run_lists = { "_default" => Chef::RunList.new }
@chef_server_rest = chef_server_rest
end
|
Class Method Details
.chef_server_rest ⇒ Object
.from_disk(name) ⇒ Object
Load a role from disk - prefers to load the JSON, but will happily load the raw rb files as well. Can search within directories in the role_path.
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/chef/role.rb', line 247
def self.from_disk(name)
paths = Array(Chef::Config[:role_path])
paths.each do |path|
roles_files = Dir.glob(File.join(Chef::Util::PathHelper.escape_glob_dir(path), "**", "**"))
js_files = roles_files.select { |file| file.match(%r{/#{name}\.json$}) }
rb_files = roles_files.select { |file| file.match(%r{/#{name}\.rb$}) }
if js_files.count > 1 || rb_files.count > 1
raise Chef::Exceptions::DuplicateRole, "Multiple roles of same type found named #{name}"
end
js_path, rb_path = js_files.first, rb_files.first
if js_path && File.exist?(js_path)
hsh = Chef::JSONCompat.parse(IO.read(js_path))
return from_hash(hsh)
elsif rb_path && File.exist?(rb_path)
role = Chef::Role.new
role.name(name)
role.from_file(rb_path)
return role
end
end
raise Chef::Exceptions::RoleNotFound, "Role '#{name}' could not be loaded from disk"
end
|
.from_hash(o) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/chef/role.rb', line 170
def self.from_hash(o)
role = new
role.name(o["name"])
role.description(o["description"])
role.default_attributes(o["default_attributes"])
role.override_attributes(o["override_attributes"])
env_run_list_hash = { "_default" => (o.key?("run_list") ? o["run_list"] : o["recipes"]) }
if o["env_run_lists"]
env_run_list_hash.merge!(o["env_run_lists"])
end
role.env_run_lists(env_run_list_hash)
role
end
|
.list(inflate = false) ⇒ Object
Get the list of all roles from the API.
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/chef/role.rb', line 192
def self.list(inflate = false)
if inflate
response = {}
Chef::Search::Query.new.search(:role) do |n|
response[n.name] = n unless n.nil?
end
response
else
chef_server_rest.get("roles")
end
end
|
.load(name) ⇒ Object
Load a role by name from the API
205
206
207
|
# File 'lib/chef/role.rb', line 205
def self.load(name)
from_hash(chef_server_rest.get("roles/#{name}"))
end
|
Instance Method Details
#active_run_list_for(environment) ⇒ Object
88
89
90
|
# File 'lib/chef/role.rb', line 88
def active_run_list_for(environment)
@env_run_lists.key?(environment) ? environment : "_default"
end
|
#chef_server_rest ⇒ Object
46
47
48
|
# File 'lib/chef/role.rb', line 46
def chef_server_rest
@chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
|
#create ⇒ Object
Create the role via the REST API
235
236
237
238
|
# File 'lib/chef/role.rb', line 235
def create
chef_server_rest.post("roles", self)
self
end
|
#default_attributes(arg = nil) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/chef/role.rb', line 117
def default_attributes(arg = nil)
set_or_return(
:default_attributes,
arg,
kind_of: Hash
)
end
|
#description(arg = nil) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/chef/role.rb', line 62
def description(arg = nil)
set_or_return(
:description,
arg,
kind_of: String
)
end
|
#destroy ⇒ Object
Remove this role via the REST API
218
219
220
|
# File 'lib/chef/role.rb', line 218
def destroy
chef_server_rest.delete("roles/#{@name}")
end
|
#env_run_lists(env_run_lists = nil) ⇒ Object
Also known as:
env_run_list
Per environment run lists
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/chef/role.rb', line 93
def env_run_lists(env_run_lists = nil)
unless env_run_lists.nil?
unless env_run_lists.key?("_default")
msg = "_default key is required in env_run_lists.\n"
msg << "(env_run_lists: #{env_run_lists.inspect})"
raise Chef::Exceptions::InvalidEnvironmentRunListSpecification, msg
end
@env_run_lists.clear
env_run_lists.each { |k, v| @env_run_lists[k] = Chef::RunList.new(*Array(v)) }
end
@env_run_lists
end
|
#env_run_lists_add(env_run_lists = nil) ⇒ Object
Also known as:
env_run_list_add
108
109
110
111
112
113
|
# File 'lib/chef/role.rb', line 108
def env_run_lists_add(env_run_lists = nil)
unless env_run_lists.nil?
env_run_lists.each { |k, v| @env_run_lists[k] = Chef::RunList.new(*Array(v)) }
end
@env_run_lists
end
|
#environment(env_name) ⇒ Object
209
210
211
|
# File 'lib/chef/role.rb', line 209
def environment(env_name)
chef_server_rest.get("roles/#{@name}/environments/#{env_name}")
end
|
#environments ⇒ Object
213
214
215
|
# File 'lib/chef/role.rb', line 213
def environments
chef_server_rest.get("roles/#{@name}/environments")
end
|
#name(arg = nil) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/chef/role.rb', line 54
def name(arg = nil)
set_or_return(
:name,
arg,
regex: /^[\-[:alnum:]_]+$/
)
end
|
#override_attributes(arg = nil) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/chef/role.rb', line 125
def override_attributes(arg = nil)
set_or_return(
:override_attributes,
arg,
kind_of: Hash
)
end
|
#run_list(*args) ⇒ Object
Also known as:
recipes
70
71
72
73
74
75
|
# File 'lib/chef/role.rb', line 70
def run_list(*args)
if args.length > 0
@env_run_lists["_default"].reset!(args)
end
@env_run_lists["_default"]
end
|
#run_list_for(environment) ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/chef/role.rb', line 80
def run_list_for(environment)
if env_run_lists[environment].nil?
env_run_lists["_default"]
else
env_run_lists[environment]
end
end
|
#save ⇒ Object
Save this role via the REST API
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/chef/role.rb', line 223
def save
begin
chef_server_rest.put("roles/#{@name}", self)
rescue Net::HTTPClientException => e
raise e unless e.response.code == "404"
chef_server_rest.post("roles", self)
end
self
end
|
#to_h ⇒ Object
Also known as:
to_hash
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/chef/role.rb', line 133
def to_h
env_run_lists_without_default = @env_run_lists.dup
env_run_lists_without_default.delete("_default")
{
"name" => @name,
"description" => @description,
"json_class" => self.class.name,
"default_attributes" => @default_attributes,
"override_attributes" => @override_attributes,
"chef_type" => "role",
"run_list" => run_list.run_list.map(&:to_s),
"env_run_lists" => env_run_lists_without_default.inject({}) do |accumulator, (k, v)|
accumulator[k] = v.map(&:to_s)
accumulator
end,
}
end
|
#to_json(*a) ⇒ Object
Serialize this object as a hash
#to_s ⇒ Object
241
242
243
|
# File 'lib/chef/role.rb', line 241
def to_s
"role[#{@name}]"
end
|
#update_from!(o) ⇒ Object
161
162
163
164
165
166
167
168
|
# File 'lib/chef/role.rb', line 161
def update_from!(o)
description(o.description)
recipes(o.recipes) if defined?(o.recipes)
default_attributes(o.default_attributes)
override_attributes(o.override_attributes)
env_run_lists(o.env_run_lists) unless o.env_run_lists.nil?
self
end
|