Class: Chef::Environment
Constant Summary
collapse
- DEFAULT =
"default"
- COMBINED_COOKBOOK_CONSTRAINT =
/(.+)(?:[\s]+)((?:#{Chef::VersionConstraint::OPS.join('|')})(?:[\s]+).+)$/
Instance Attribute Summary
#source_file
Class Method Summary
collapse
Instance Method Summary
collapse
#class_from_file, #from_file
#lazy, #set_or_return, #validate
Constructor Details
#initialize(chef_server_rest: nil) ⇒ Environment
Returns a new instance of Environment.
39
40
41
42
43
44
45
46
|
# File 'lib/chef/environment.rb', line 39
def initialize(chef_server_rest: nil)
@name = ""
@description = ""
@default_attributes = Mash.new
@override_attributes = Mash.new
@cookbook_versions = Hash.new
@chef_server_rest = chef_server_rest
end
|
Class Method Details
.chef_server_rest ⇒ Object
52
53
54
|
# File 'lib/chef/environment.rb', line 52
def self.chef_server_rest
Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
|
.from_hash(o) ⇒ Object
217
218
219
220
221
222
223
224
225
|
# File 'lib/chef/environment.rb', line 217
def self.from_hash(o)
environment = new
environment.name(o["name"])
environment.description(o["description"])
environment.cookbook_versions(o["cookbook_versions"])
environment.default_attributes(o["default_attributes"])
environment.override_attributes(o["override_attributes"])
environment
end
|
.list(inflate = false) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/chef/environment.rb', line 227
def self.list(inflate = false)
if inflate
response = Hash.new
Chef::Search::Query.new.search(:environment) do |e|
response[e.name] = e unless e.nil?
end
response
else
chef_server_rest.get("environments")
end
end
|
.load(name) ⇒ Object
239
240
241
242
243
244
245
|
# File 'lib/chef/environment.rb', line 239
def self.load(name)
if Chef::Config[:solo_legacy_mode]
load_from_file(name)
else
from_hash(chef_server_rest.get("environments/#{name}"))
end
end
|
.load_filtered_recipe_list(environment) ⇒ Object
288
289
290
|
# File 'lib/chef/environment.rb', line 288
def self.load_filtered_recipe_list(environment)
chef_server_rest.get("environments/#{environment}/recipes")
end
|
.load_from_file(name) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/chef/environment.rb', line 247
def self.load_from_file(name)
unless File.directory?(Chef::Config[:environment_path])
raise Chef::Exceptions::InvalidEnvironmentPath, "Environment path '#{Chef::Config[:environment_path]}' is invalid"
end
js_file = File.join(Chef::Config[:environment_path], "#{name}.json")
rb_file = File.join(Chef::Config[:environment_path], "#{name}.rb")
if File.exists?(js_file)
hash = Chef::JSONCompat.parse(IO.read(js_file))
from_hash(hash)
elsif File.exists?(rb_file)
environment = Chef::Environment.new
environment.name(name)
environment.from_file(rb_file)
environment
else
raise Chef::Exceptions::EnvironmentNotFound, "Environment '#{name}' could not be loaded from disk"
end
end
|
.validate_cookbook_version(version) ⇒ Object
304
305
306
307
308
309
310
311
312
313
314
|
# File 'lib/chef/environment.rb', line 304
def self.validate_cookbook_version(version)
if Chef::Config[:solo_legacy_mode]
raise Chef::Exceptions::IllegalVersionConstraint,
"Environment cookbook version constraints not allowed in chef-solo"
else
Chef::VersionConstraint.new version
true
end
rescue ArgumentError
false
end
|
.validate_cookbook_versions(cv) ⇒ Object
296
297
298
299
300
301
302
|
# File 'lib/chef/environment.rb', line 296
def self.validate_cookbook_versions(cv)
return false unless cv.kind_of?(Hash)
cv.each_value do |version|
return false unless Chef::Environment.validate_cookbook_version(version)
end
true
end
|
Instance Method Details
#add_cookbook_constraint_error(index, cookbook_constraint_spec) ⇒ Object
199
200
201
202
|
# File 'lib/chef/environment.rb', line 199
def add_cookbook_constraint_error(index, cookbook_constraint_spec)
invalid_fields[:cookbook_version] ||= {}
invalid_fields[:cookbook_version][index] = "#{cookbook_constraint_spec} is not a valid cookbook constraint"
end
|
#chef_server_rest ⇒ Object
48
49
50
|
# File 'lib/chef/environment.rb', line 48
def chef_server_rest
@chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
|
#cookbook(cookbook, version) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/chef/environment.rb', line 109
def cookbook(cookbook, version)
validate({
:version => version,
}, {
:version => {
:callbacks => { "should be a valid version requirement" => lambda { |v| Chef::Environment.validate_cookbook_version(v) } },
},
})
@cookbook_versions[cookbook] = version
end
|
#cookbook_versions(arg = nil) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/chef/environment.rb', line 96
def cookbook_versions(arg = nil)
set_or_return(
:cookbook_versions,
arg,
{
:kind_of => Hash,
:callbacks => {
"should be a valid set of cookbook version requirements" => lambda { |cv| Chef::Environment.validate_cookbook_versions(cv) },
},
}
)
end
|
#create ⇒ Object
283
284
285
286
|
# File 'lib/chef/environment.rb', line 283
def create
chef_server_rest.post("environments", self)
self
end
|
#default_attributes(arg = nil) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/chef/environment.rb', line 72
def default_attributes(arg = nil)
set_or_return(
:default_attributes,
arg,
:kind_of => Hash
)
end
|
#default_attributes=(attrs) ⇒ Object
80
81
82
|
# File 'lib/chef/environment.rb', line 80
def default_attributes=(attrs)
default_attributes(attrs)
end
|
#description(arg = nil) ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/chef/environment.rb', line 64
def description(arg = nil)
set_or_return(
:description,
arg,
:kind_of => String
)
end
|
#destroy ⇒ Object
269
270
271
|
# File 'lib/chef/environment.rb', line 269
def destroy
chef_server_rest.delete("environments/#{@name}")
end
|
#invalid_fields ⇒ Object
204
205
206
|
# File 'lib/chef/environment.rb', line 204
def invalid_fields
@invalid_fields ||= {}
end
|
#name(arg = nil) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/chef/environment.rb', line 56
def name(arg = nil)
set_or_return(
:name,
arg,
{ :regex => /^[\-[:alnum:]_]+$/, :kind_of => String }
)
end
|
#override_attributes(arg = nil) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/chef/environment.rb', line 84
def override_attributes(arg = nil)
set_or_return(
:override_attributes,
arg,
:kind_of => Hash
)
end
|
#override_attributes=(attrs) ⇒ Object
92
93
94
|
# File 'lib/chef/environment.rb', line 92
def override_attributes=(attrs)
override_attributes(attrs)
end
|
#save ⇒ Object
273
274
275
276
277
278
279
280
281
|
# File 'lib/chef/environment.rb', line 273
def save
begin
chef_server_rest.put("environments/#{@name}", self)
rescue Net::HTTPServerException => e
raise e unless e.response.code == "404"
chef_server_rest.post("environments", self)
end
self
end
|
#to_hash ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/chef/environment.rb', line 120
def to_hash
result = {
"name" => @name,
"description" => @description,
"cookbook_versions" => @cookbook_versions,
"json_class" => self.class.name,
"chef_type" => "environment",
"default_attributes" => @default_attributes,
"override_attributes" => @override_attributes,
}
result
end
|
#to_json(*a) ⇒ Object
133
134
135
|
# File 'lib/chef/environment.rb', line 133
def to_json(*a)
Chef::JSONCompat.to_json(to_hash, *a)
end
|
#to_s ⇒ Object
292
293
294
|
# File 'lib/chef/environment.rb', line 292
def to_s
@name
end
|
#update_attributes_from_params(params) ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/chef/environment.rb', line 145
def update_attributes_from_params(params)
unless params[:default_attributes].nil? || params[:default_attributes].size == 0
default_attributes(Chef::JSONCompat.from_json(params[:default_attributes]))
end
unless params[:override_attributes].nil? || params[:override_attributes].size == 0
override_attributes(Chef::JSONCompat.from_json(params[:override_attributes]))
end
end
|
#update_cookbook_constraint_from_param(index, cookbook_constraint_spec) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/chef/environment.rb', line 184
def update_cookbook_constraint_from_param(index, cookbook_constraint_spec)
valid = true
md = cookbook_constraint_spec.match(COMBINED_COOKBOOK_CONSTRAINT)
if md.nil? || md[2].nil?
valid = false
add_cookbook_constraint_error(index, cookbook_constraint_spec)
elsif self.class.validate_cookbook_version(md[2])
cookbook_versions[md[1]] = md[2]
else
valid = false
add_cookbook_constraint_error(index, cookbook_constraint_spec)
end
valid
end
|
#update_from!(o) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/chef/environment.rb', line 137
def update_from!(o)
description(o.description)
cookbook_versions(o.cookbook_versions)
default_attributes(o.default_attributes)
override_attributes(o.override_attributes)
self
end
|
#update_from_params(params) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/chef/environment.rb', line 154
def update_from_params(params)
bkup_cb_versions = cookbook_versions
cookbook_versions(Hash.new)
valid = true
begin
name(params[:name])
rescue Chef::Exceptions::ValidationFailed => e
invalid_fields[:name] = e.message
valid = false
end
description(params[:description])
unless params[:cookbook_version].nil?
params[:cookbook_version].each do |index, cookbook_constraint_spec|
unless cookbook_constraint_spec.nil? || cookbook_constraint_spec.size == 0
valid &&= update_cookbook_constraint_from_param(index, cookbook_constraint_spec)
end
end
end
update_attributes_from_params(params)
valid = validate_required_attrs_present && valid
cookbook_versions(bkup_cb_versions) unless valid valid
end
|
#validate_required_attrs_present ⇒ Object
208
209
210
211
212
213
214
215
|
# File 'lib/chef/environment.rb', line 208
def validate_required_attrs_present
if name.nil? || name.size == 0
invalid_fields[:name] ||= "name cannot be empty"
false
else
true
end
end
|