forked from apache/deltacloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
691 lines (605 loc) · 19.9 KB
/
server.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# Copyright (C) 2009, 2010 Red Hat, Inc.
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
require 'sinatra'
require 'deltacloud'
require 'drivers'
require 'json'
require 'sinatra/respond_to'
require 'sinatra/static_assets'
require 'sinatra/rabbit'
require 'sinatra/lazy_auth'
require 'erb'
require 'haml'
require 'open3'
require 'lib/deltacloud/helpers/blob_stream'
require 'sinatra/rack_driver_select'
set :version, '0.1.0'
use RackDriverSelect
configure do
set :raise_errors => false
set :show_exceptions, false
set :views, File.dirname(__FILE__) + '/views'
set :public, File.dirname(__FILE__) + '/public'
end
configure :development do
# So we can just use puts for logging
$stdout.sync = true
$stderr.sync = true
end
# You could use $API_HOST environment variable to change your hostname to
# whatever you want (eg. if you running API behind NAT)
HOSTNAME=ENV['API_HOST'] ? ENV['API_HOST'] : nil
error Deltacloud::Validation::Failure do
report_error(400, "validation_failure")
end
error Deltacloud::BackendCapability::Failure do
report_error(405, "backend_capability_failure")
end
error Deltacloud::AuthException do
report_error(403, "auth_exception")
end
error Deltacloud::BackendError do
report_error(500, "backend_error")
end
error Sinatra::Rabbit::UnsupportedCollectionException do
report_error(404, "not_found")
end
Sinatra::Application.register Sinatra::RespondTo
# Redirect to /api
get '/' do redirect url_for('/api'), 301; end
get '/api/drivers\/?' do
respond_to do |format|
format.xml { haml :"api/drivers" }
format.html { haml :"api/drivers" }
end
end
get '/api\/?' do
if params[:force_auth]
return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
end
respond_to do |format|
format.xml { haml :"api/show" }
format.json do
{ :api => {
:version => settings.version,
:driver => driver_symbol,
:links => entry_points.collect { |l| { :rel => l[0], :href => l[1]} }
}
}.to_json
end
format.html { haml :"api/show" }
end
end
# Rabbit DSL
collection :realms do
description <<END
Within a cloud provider a realm represents a boundary containing resources.
The exact definition of a realm is left to the cloud provider.
In some cases, a realm may represent different datacenters, different continents,
or different pools of resources within a single datacenter.
A cloud provider may insist that resources must all exist within a single realm in
order to cooperate. For instance, storage volumes may only be allowed to be mounted to
instances within the same realm.
END
operation :index do
description <<END
Operation will list all available realms. Realms can be filtered using
the "architecture" parameter.
END
with_capability :realms
param :id, :string
param :architecture, :string, :optional, [ 'i386', 'x86_64' ]
control { filter_all(:realms) }
end
#FIXME: It always shows whole list
operation :show do
description 'Show an realm identified by "id" parameter.'
with_capability :realm
param :id, :string, :required
control { show(:realm) }
end
end
collection :images do
description <<END
An image is a platonic form of a machine. Images are not directly executable,
but are a template for creating actual instances of machines."
END
operation :index do
description <<END
The images collection will return a set of all images
available to the current use. Images can be filtered using the
"owner_id" and "architecture" parameters.
END
with_capability :images
param :id, :string
param :architecture, :string, :optional
control { filter_all(:images) }
end
operation :show do
description 'Show an image identified by "id" parameter.'
with_capability :image
param :id, :string, :required
control { show(:image) }
end
end
collection :instance_states do
description "The possible states of an instance, and how to traverse between them "
operation :index do
control do
@machine = driver.instance_state_machine
respond_to do |format|
format.xml { haml :'instance_states/show', :layout => false }
format.json do
out = []
@machine.states.each do |state|
transitions = state.transitions.collect do |t|
t.automatically? ? {:to => t.destination, :auto => 'true'} : {:to => t.destination, :action => t.action}
end
out << { :name => state, :transitions => transitions }
end
out.to_json
end
format.html { haml :'instance_states/show'}
format.gv { erb :"instance_states/show" }
format.png do
# Trick respond_to into looking up the right template for the
# graphviz file
format_backup = format
format(:gv)
gv = erb(:"instance_states/show")
format(format_backup)
png = ''
cmd = 'dot -Kdot -Gpad="0.2,0.2" -Gsize="5.0,8.0" -Gdpi="180" -Tpng'
Open3.popen3( cmd ) do |stdin, stdout, stderr|
stdin.write( gv )
stdin.close()
png = stdout.read
end
content_type 'image/png'
png
end
end
end
end
end
get "/api/instances/new" do
@instance = Instance.new( { :id=>params[:id], :image_id=>params[:image_id] } )
@image = driver.image( credentials, :id => params[:image_id] )
@hardware_profiles = driver.hardware_profiles(credentials, :architecture => @image.architecture )
@realms = driver.realms(credentials)
if driver_has_feature?(:register_to_load_balancer)
@load_balancers = driver.load_balancers(credentials)
end
respond_to do |format|
format.html { haml :"instances/new" }
end
end
get '/api/load_balancers/new' do
@realms = driver.realms(credentials)
@instances = driver.instances(credentials) if driver_has_feature?(:register_instance, :load_balancers)
respond_to do |format|
format.html { haml :"load_balancers/new" }
end
end
collection :load_balancers do
description "Load balancers"
operation :index do
description "List of all active load balancers"
control do
filter_all :load_balancers
end
end
operation :show do
description "Show details about given load balancer"
param :id, :string, :required
control { show :load_balancer }
end
operation :create do
description "Create a new load balancer"
param :name, :string, :required
param :realm_id, :string, :required
param :listener_protocol, :string, :required, ['HTTP', 'TCP']
param :listener_balancer_port, :string, :required
param :listener_instance_port, :string, :required
control do
@load_balancer = driver.create_load_balancer(credentials, params)
respond_to do |format|
format.xml { haml :"load_balancers/show" }
format.html { haml :"load_balancers/show" }
end
end
end
operation :register, :method => :post, :member => true do
description "Add instance to loadbalancer"
param :id, :string, :required
param :instance_id, :string, :required
control do
driver.lb_register_instance(credentials, params)
redirect(load_balancer_url(params[:id]))
end
end
operation :unregister, :method => :post, :member => true do
description "Remove instance from loadbalancer"
param :id, :string, :required
param :instance_id, :string, :required
control do
driver.lb_unregister_instance(credentials, params)
redirect(load_balancer_url(params[:id]))
end
end
operation :destroy do
description "Destroy given load balancer"
param :id, :string, :required
control do
driver.destroy_load_balancer(credentials, params[:id])
redirect(load_balancers_url)
end
end
end
collection :instances do
description <<END
An instance is a concrete machine realized from an image.
The images collection may be obtained by following the link from the primary entry-point."
END
operation :index do
description "List all instances."
with_capability :instances
param :id, :string, :optional
param :state, :string, :optional
control { filter_all(:instances) }
end
operation :show do
description 'Show an instance identified by "id" parameter.'
with_capability :instance
param :id, :string, :required
control { show(:instance) }
end
operation :create do
description "Create a new instance."
with_capability :create_instance
param :image_id, :string, :required
param :realm_id, :string, :optional
param :hwp_id, :string, :optional
control do
@image = driver.image(credentials, :id => params[:image_id])
instance = driver.create_instance(credentials, @image.id, params)
respond_to do |format|
format.xml do
response.status = 201 # Created
response['Location'] = instance_url(instance.id)
@instance = instance
haml :"instances/show"
end
format.html do
redirect instance_url(instance.id) if instance and instance.id
redirect instances_url
end
end
end
end
operation :reboot, :method => :post, :member => true do
description "Reboot a running instance."
with_capability :reboot_instance
param :id, :string, :required
control { instance_action(:reboot) }
end
operation :start, :method => :post, :member => true do
description "Start an instance."
with_capability :start_instance
param :id, :string, :required
control { instance_action(:start) }
end
operation :stop, :method => :post, :member => true do
description "Stop a running instance."
with_capability :stop_instance
param :id, :string, :required
control { instance_action(:stop) }
end
operation :destroy do
description "Destroy an instance."
with_capability :destroy_instance
param :id, :string, :required
control { instance_action(:destroy) }
end
end
collection :hardware_profiles do
description <<END
A hardware profile represents a configuration of resources upon which a
machine may be deployed. It defines aspects such as local disk storage,
available RAM, and architecture. Each provider is free to define as many
(or as few) hardware profiles as desired.
END
operation :index do
description "List of available hardware profiles."
with_capability :hardware_profiles
param :id, :string
param :architecture, :string, :optional, [ 'i386', 'x86_64' ]
control do
@profiles = driver.hardware_profiles(credentials, params)
respond_to do |format|
format.xml { haml :'hardware_profiles/index' }
format.html { haml :'hardware_profiles/index' }
format.json { convert_to_json(:hardware_profile, @profiles) }
end
end
end
operation :show do
description "Show specific hardware profile."
with_capability :hardware_profile
param :id, :string, :required
control do
@profile = driver.hardware_profile(credentials, params[:id])
if @profile
respond_to do |format|
format.xml { haml :'hardware_profiles/show', :layout => false }
format.html { haml :'hardware_profiles/show' }
format.json { convert_to_json(:hardware_profile, @profile) }
end
else
report_error(404, 'not_found')
end
end
end
end
get '/api/storage_snapshots/new' do
respond_to do |format|
format.html { haml :"storage_snapshots/new" }
end
end
collection :storage_snapshots do
description "Storage snapshots description here"
operation :index do
description "List of storage snapshots."
with_capability :storage_snapshots
param :id, :string
control { filter_all(:storage_snapshots) }
end
operation :show do
description "Show storage snapshot."
with_capability :storage_snapshot
param :id, :string, :required
control { show(:storage_snapshot) }
end
operation :create do
description "Create a new snapshot from volume"
with_capability :create_storage_snapshot
param :volume_id, :string, :required
control do
@storage_snapshot = driver.create_storage_snapshot(credentials, params)
show(:storage_snapshot)
end
end
operation :destroy do
description "Delete storage snapshot"
with_capability :destroy_storage_snapshot
param :id, :string, :required
control do
driver.create_storage_snapshot(credentials, params)
redirect(storage_snapshot_url(params[:id]))
end
end
end
get '/api/storage_volumes/new' do
respond_to do |format|
format.html { haml :"storage_volumes/new" }
end
end
get '/api/storage_volumes/attach' do
respond_to do |format|
@instances = driver.instances(credentials)
format.html { haml :"storage_volumes/attach" }
end
end
collection :storage_volumes do
description "Storage volumes description here"
operation :index do
description "List of storage volumes."
with_capability :storage_volumes
param :id, :string
control { filter_all(:storage_volumes) }
end
operation :show do
description "Show storage volume."
with_capability :storage_volume
param :id, :string, :required
control { show(:storage_volume) }
end
operation :create do
description "Create a new storage volume"
with_capability :create_storage_volume
param :snapshot_id, :string, :optional
param :capacity, :string, :optional
param :realm_id, :string, :optional
control do
@storage_volume = driver.create_storage_volume(credentials, params)
respond_to do |format|
format.html { haml :"storage_volumes/show" }
format.xml { haml :"storage_volumes/show" }
end
end
end
operation :attach, :method => :post, :member => true do
description "Attach storage volume to instance"
with_capability :attach_storage_volume
param :id, :string, :required
param :instance_id,:string, :required
param :device, :string, :required
control do
driver.attach_storage_volume(credentials, params)
redirect(storage_volume_url(params[:id]))
end
end
operation :detach, :method => :post, :member => true do
description "Detach storage volume to instance"
with_capability :detach_storage_volume
param :id, :string, :required
control do
volume = driver.storage_volume(credentials, :id => params[:id])
driver.detach_storage_volume(credentials, :id => volume.id, :instance_id => volume.instance_id,
:device => volume.device)
redirect(storage_volume_url(params[:id]))
end
end
operation :destroy do
description "Destroy storage volume"
with_capability :destroy_storage_volume
param :id, :string, :optional
control do
driver.destroy_storage_volume(credentials, params)
redirect(storage_volumes_url)
end
end
end
get '/api/keys/new' do
respond_to do |format|
format.html { haml :"keys/new" }
end
end
collection :keys do
description "Instance authentication credentials."
operation :index do
description "List all available credentials which could be used for instance authentication."
with_capability :keys
control do
filter_all :keys
end
end
operation :show do
description "Show details about given instance credential."
with_capability :key
param :id, :string, :required
control { show :key }
end
operation :create do
description "Create a new instance credential if backend supports this."
with_capability :create_key
param :name, :string, :required
control do
@key = driver.create_key(credentials, { :key_name => params[:name] })
respond_to do |format|
format.html { haml :"keys/show" }
format.xml { haml :"keys/show", :ugly => true }
end
end
end
operation :destroy do
description "Destroy given instance credential if backend supports this."
with_capability :destroy_key
param :id, :string, :required
control do
driver.destroy_key(credentials, { :key_name => params[:id]})
redirect(keys_url)
end
end
end
#get html form for creating a new blob
get '/api/buckets/:bucket/new_blob' do
@bucket_id = params[:bucket]
respond_to do |format|
format.html {haml :"blobs/new"}
end
end
#create a new blob
post '/api/buckets/:bucket' do
bucket_id = params[:bucket]
blob_id = params['blob_id']
blob_data = params['blob_data']
@blob = driver.create_blob(credentials, bucket_id, blob_id, blob_data )
respond_to do |format|
format.html { haml :"blobs/show"}
format.xml { haml :"blobs/show" }
end
end
#delete a blob
delete '/api/buckets/:bucket/:blob' do
bucket_id = params[:bucket]
blob_id = params[:blob]
driver.delete_blob(credentials, bucket_id, blob_id)
redirect(bucket_url(bucket_id))
end
#Get a particular blob's particulars (not actual blob data)
get '/api/buckets/:bucket/:blob' do
@blob = driver.blob(credentials, { :id => params[:blob], 'bucket' => params[:bucket]})
if @blob
respond_to do |format|
format.html { haml :"blobs/show" }
format.xml { haml :"blobs/show" }
format.json { convert_to_json(blobs, @blob) }
end
else
report_error(404, 'not_found')
end
end
#get the content of a particular blob
get '/api/buckets/:bucket/:blob/content' do
@blob = driver.blob(credentials, { :id => params[:blob], 'bucket' => params[:bucket]})
params['content_length'] = @blob.content_length
params['content_type'] = @blob.content_type
params['content_disposition'] = "attachment; filename=#{@blob.id}"
BlobStream.call(env, credentials, params)
end
#Get html form for creating a new bucket
get '/api/buckets/new' do
respond_to do |format|
format.html { haml :"buckets/new" }
end
end
collection :buckets do
description "Cloud Storage buckets - aka buckets|directories|folders"
operation :index do
description "List buckets associated with this account"
with_capability :buckets
param :id, :string
param :name, :string
param :size, :string
control { filter_all(:buckets) }
end
operation :show do
description "Show bucket"
with_capability :bucket
param :id, :string
control { show(:bucket) }
end
operation :create do
description "Create a new bucket (POST /api/buckets)"
with_capability :create_bucket
param :name, :string, :required
control do
@bucket = driver.create_bucket(credentials, params[:name], params)
respond_to do |format|
format.xml do
response.status = 201 # Created
response['Location'] = bucket_url(@bucket.id)
haml :"buckets/show"
end
format.html do
redirect bucket_url(@bucket.id) if @bucket and @bucket.id
redirect buckets_url
end
end
end
end
operation :destroy do
description "Delete a bucket by name - bucket must be empty"
with_capability :delete_bucket
param :id, :string, :required
control do
driver.delete_bucket(credentials, params[:id], params)
redirect(buckets_url)
end
end
end