forked from chef-boneyard/resource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchef.rb
More file actions
389 lines (323 loc) · 8.94 KB
/
Copy pathchef.rb
File metadata and controls
389 lines (323 loc) · 8.94 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
resource :path do
resource :path_source do
end
resource :content_source do
end
resource :content do
end
type :json_value, Union[Array[json_value], Hash[Symbol => json_value], String, Boolean, NilClass]
end
resource :machine do
resource :entry do
property :path, Path, identity: true
property :mode, Integer do
end
property :owner, User
property :group, Group
def load
stat = File.stat(path)
mode stat.mode
owner stat.uid
group stat.gid
end
# - create (actual !exists)
# - change[columns] (all columns if actual exists)
# - change[]
def update_resource
handle_create { File.write(path, content) }
handle_update :mode { File.chmod(mode, path) }
end
handle_update :owner, :group do
end
dry_run_test :mode do
end
handle_update :mode do
end
end
def self.open(path)
stat = File.stat(path)
case stat.type
when :file
file.open(path)
when :dir
directory.open(path)
when :symlink
symlink.open(path)
else
super(path)
end
end
end
resource :directory, entry do
property :children, IndexedSet[entry] do
def load
replace(File.entries(path).map { |child_path| entry.open(child_path) })
end
def update_resource
each_added { |added| added.update_resource }
each_modified { |modified| modified.update_resource }
each_removed { |removed| removed.delete_resource }
end
end
end
resource :file, entry do
property :content, String do
def load
IO.read(parent_struct.path)
end
end
def update_resource
converge :content do
IO.write(path, content)
end
super
end
end
resource :symlink, entry do
property :to, entry do
def load
entry.open(File.linktarget(path))
end
end
def update_resource
converge :to do
File.symlink(path, to.path)
end
super
end
end
def root_directory
directory '/'
end
property :users, IndexedSet[User, :uid] do
def by_name(name)
each { |user| user.name == name }.first
end
end
property :groups, IndexedSet[Group, :gid] do
def by_name(name)
each { |group| group.name == name }.first
end
end
end
resource :formats do
resource :json do
property :content, Content, identity: true
property :value, json_value
def load
value JSON.parse(content.read)
end
def update_resource
if_updated :value { content.write(value) }
end
end
end
#
# The Chef API. Lets you manipulate all objects Chef knows about (get, put, post and delete)
#
# @example
#
resource :chef do
#
# The data source. Supports any PathSource, include a ChefServerSource and a ChefRepositorySource
#
# Depending on the data source, you may be dropped into an org-specific directory or
# @example Default source (driven by config)
# chef.organization('blah') do
# organization 'blah'
# end
# @example Chef API source
# chef 'http://api.opscode.com', username: 'jkeiser', private_key: '/etc/x.pem' do
# organization 'blah' # -> chef_api_source.open('organizations/blah') -> http_source.open('organizations/blah')
# end
# @example Chef repository source
# chef '/home/jkeiser/chef_repo' do
# node 'blah' # -> chef_repo_source.open('organizations/blah') -> file_system_source.open('organizations/blah.json')
# end
# @example Custom source
# chef etcd_source('http://127.0.0.1:3030') do
# node 'blah' # -> etcd_source.open('nodes/blah')
# end
property :source, PathSource[JSONValue], identity: true do
def connection
end
coerce
# This is where we do the magic of converting user input to
def self.coerce(parent, *args)
if value.nil?
if args.empty?
value
elsif is_valid?(parent, value)
value
elsif JSONValue.is_valid?(parent, value)
elsif !value.nil?
uri = UriType.coerce(parent, value)
case uri.scheme
when 'file'
ChefRepositorySource.open(uri, *args, **named_args)
when 'http', 'https'
ChefServerSource.open(uri, *args, **named_args)
else
# TODO config. Something funny going on here: can we even access the instanced config?
end
end
end
end
HTTP.get("https://#{aws.machine('web1').public_ip_address}")
aws region: 'us-east-1' do
def connection
AWS.connect(...)
end
resource :vpc do
property :vpc_id, Integer
end
vpc 'vpc2324397' do
subnets
end
m = Machine.open('blah')
m.instance_eval do
vpc 'vpc-120394812'
end
m.update_resource
machine 'blah' do
property :vpc, VPC
property :billing_address do
property :street, String
property :city, String
end
property :name, String do
def current_resource
end
end
property :ip_address, String
property :name,
def current_resource
location = chef.node('blah').propertys['chef_provisioning']['location']
parent.connection.instances[location['instance_id']]
end
end
image 'my_image' do
end
security_group 'sec' do
end
end
us_east = aws(region: 'us-east-1')
us_west = aws(region: 'us-west-1')
gce = google_compute_engine
us_east.instance_eval do
definition :small_machine, aws.machine do
ami 'ami23847234'
instance_type 'x-small'
end
end
data_centers = [ us_east, us_west, gce ]
data_centers.each do
.small_machine 'blah' do
recipe 'apache'
end
resource :old_driver do
property :driver_url, String, identity: true
resource :machine, chef_provisioning.machine do
end
end
resource :aws do
property :aws_url, Uri, identity: true, default:
property :profile_name, String, identity: true, default:
property :region, identity: true, required: false
property :account_id, String, identity: true
resource :machine, chef_provisioning.machine do
end
# Defining an actual value on an property
class ChefAPI
class User < StructResource
property :chef_server, Uri, identity: true, default: proc { Config.chef_server_url }
property :chef_client_name, String, identity: true, default: proc { Config.node_name }
property :chef_client_key, String, identity: true, default: proc { Config.client_key }
property :username, String, identity: true
property :name, String
property :public_key, RSA::PublicKey
def load
chef_rest = REST.new(chef_server, chef_client_name, chef_client_key)
value = JSON.parse(chef_rest.get("users/#{name}"))
name json['name']
username json['username']
public_key json['public_key']
end
recipe_for_create do
end
recipe_for_update do
end
recipe do
converge do
chef_rest = REST.new(chef_server, chef_client_name, chef_client_key)
json = JSON.to_json {
username: username,
name: name,
public_key: public_key.to_s
}
chef_rest.put("users/#{name}", json)
end
end
end
end
resource :chef
property :chef_server_url, Uri, identity: true, default: proc { Config.chef_server_url }
property :chef_client_name, String, identity: true, default: proc { Config.node_name }
property :chef_client_key, String, identity: true, default: proc { Config.client_key }
def chef_rest
@chef_rest ||= REST.new(chef_server_url, chef_client_name, chef_client_key)
end
resource :user do
property :username, String, identity: true
property :name, String
property :public_key, RSA::PublicKey
def load
json = parent.chef_rest.get("users/#{name}")
value = JSON.parse(json)
name json['name']
username json['username']
public_key json['public_key']
end
def update_resource
converge do
json = JSON.to_json {
username: username,
name: name,
public_key: public_key.to_s
}
parent.chef_rest.put("users/#{name}", json)
end
end
end
end
resource :organization do
property :name, String, identity: true
property :description, String
def current_resource
@current_resource ||= begin
REST.get("organizations/#{name}")
rescue HTTPException
@current_resource = nil
raise unless $!.code == 404
end
end
def update_resource
converge :name, :description do
if exists?
REST.put("organizations/#{name}", to_h)
else
REST.post("organizations", to_h)
end
end
end
property :members, Hash[String => User] do
def current_resource
REST.get("data/#{data_bag.name}/users").inject({}) do |hash,name|
result[name] = user(name) # create the user resource
end
end
end
property :nodes, Array[Node] do
end
end
end