vagrant-node 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,9 @@ require 'json'
4
4
  require 'rack'
5
5
  require 'vagrant-node/clientcontroller'
6
6
  require 'vagrant-node/apidesc'
7
+ require 'vagrant-node/exceptions'
7
8
 
9
+ require 'cgi'
8
10
 
9
11
  require 'pp'
10
12
  module Vagrant
@@ -12,10 +14,18 @@ module Vagrant
12
14
  module ServerAPI
13
15
 
14
16
  class API < Sinatra::Base
15
- include RestRoutes
16
- before do
17
- content_type :json
18
- end
17
+ include RestRoutes
18
+ COOKIE_TOKEN_ID = "TOKEN"
19
+ COOKIE_EXPIRATION_TIME = 5
20
+ def initialize
21
+ super
22
+ @session_table=Hash.new
23
+ end
24
+ #use Rack::Session::Pool, :expire_after => 2592000
25
+ #enable :sessions
26
+ # before do
27
+ # content_type :json
28
+ # end
19
29
 
20
30
 
21
31
  #UPLOAD A FILE
@@ -26,10 +36,47 @@ module Vagrant
26
36
  # redirect '/'
27
37
  #end
28
38
 
29
-
30
- # before '/admin/*' do
31
- # authenticate!
32
- # end
39
+ #before '^.*(^login).*' do
40
+ before %r{^(?!#{RouteManager.login_route}$)} do
41
+ content_type :json
42
+ #FIXME REMOVE
43
+ #puts "ENTRO BEFORE"
44
+ #pp request.env
45
+ token = ""
46
+ cookie = ""
47
+
48
+ #pp "TOKEN DESCAPADO = #{CGI::unescape(request.env['HTTP_CONTENT_MD5'])}"
49
+
50
+
51
+ cookie = request.cookies[COOKIE_TOKEN_ID]
52
+ token = CGI::unescape(request.env['HTTP_CONTENT_MD5']) if request.env['HTTP_CONTENT_MD5']!=nil
53
+
54
+ #FIXME REMOVE
55
+ # pp "COOKIES = #{cookie}"
56
+ # pp "TOKEN = #{token}"
57
+ #
58
+ # pp "TIENE COOKIE " if (@session_table.has_key?(cookie))
59
+ # pp "CONTIENE TOKEN " if (challenged?(token))
60
+
61
+ if (!@session_table.has_key?(cookie)||
62
+ !challenged?(token))
63
+ redirect to(RouteManager.login_route)
64
+
65
+ else
66
+ #raise RestException.new(401,"Not authorized") if !authorized?(cookie,token)
67
+ halt 401, "Not authorized\n" if !authorized?(cookie,token)
68
+ end
69
+ clear_session_table
70
+ end
71
+
72
+ after %r{^(?!#{RouteManager.login_route}$)} do
73
+
74
+ cookie=request.cookies[COOKIE_TOKEN_ID]
75
+
76
+ @session_table.delete(cookie) if cookie!=nil
77
+ #FIXME REMOVE
78
+ #pp "-------------------------------------------"
79
+ end
33
80
 
34
81
  ######### FIXME DELETE #####################
35
82
  get '/' do
@@ -37,122 +84,256 @@ module Vagrant
37
84
  end
38
85
 
39
86
  ###############################################
40
-
87
+ get RouteManager.login_route do
88
+ res = ClientController.send_challenge
89
+ #FIXME REMOVE
90
+ #puts "LOGIN"
91
+
92
+
93
+ #@session_table[res[:cookie]]={:expiration => Time::now.to_i,:challenge=>res[:challenge]}
94
+ #pp res[:cookie]
95
+ @session_table[res[:cookie]]={:expiration => Time::now.to_i+COOKIE_EXPIRATION_TIME,:challenge=>res[:challenge]}
96
+ #pp @session_table.inspect
97
+
98
+ response.set_cookie(COOKIE_TOKEN_ID, res[:cookie])
99
+ headers "Content_MD5" => res[:challenge]
100
+ status 200
101
+
102
+ #handle_response_result(ClientController.send_challenge)
103
+ end
104
+
41
105
  get RouteManager.box_list_route do
42
- ClientController.listboxes.to_json
106
+ #ClientController.listboxes.to_json
107
+ execute(:listboxes)
43
108
  end
44
109
 
45
- delete RouteManager.box_delete_route do
46
- handle_response_result(ClientController.box_delete(params[:box],params[:provider]))
110
+ delete RouteManager.box_delete_route do
111
+ #handle_response_result(ClientController.box_delete("params[:box]",params[:provider]))
112
+ execute(:box_delete,true,params[:box],params[:provider])
47
113
  end
48
114
 
49
- post RouteManager.box_add_route do
50
- handle_response_result(ClientController.box_add(params[:box],params[:url]))
115
+ post RouteManager.box_add_route do
116
+ execute_async(:box_add,params[:box],params[:url])
51
117
  end
52
118
 
53
119
 
54
120
 
55
121
  get RouteManager.vm_status_all_route do
56
- handle_response_result(ClientController.vm_status(nil))
122
+ #handle_response_result(ClientController.vm_status(nil))
123
+ execute(:vm_status)
57
124
  end
58
125
 
59
126
 
60
127
 
61
128
  get RouteManager.vm_status_route do
62
- handle_response_result(ClientController.vm_status(params[:vm]))
129
+ #handle_response_result(ClientController.vm_status(params[:vm]))
130
+ execute(:vm_status,true,params[:vm])
63
131
  end
64
132
 
65
133
  #accept :vmname as paramter. This parameter
66
134
  #could be empty
67
- post RouteManager.vm_up_route do
68
- handle_response_result(ClientController.vm_up(params[:vmname]))
135
+ post RouteManager.vm_up_route do
136
+ execute_async(:vm_up,params[:vmname])
69
137
  end
70
138
 
71
139
  #accept :vmname and :force as paramters
72
140
  post RouteManager.vm_halt_route do
73
- handle_response_result(ClientController.vm_halt(params[:vmname],params[:force]))
141
+ #handle_response_result(ClientController.vm_halt(params[:vmname],params[:force]))
142
+ execute_async(:vm_halt,params[:vmname],params[:force])
74
143
  end
75
144
 
76
145
  #accept :vmname as paramter. This parameter
77
146
  #could be empty
78
147
  post RouteManager.vm_destroy_route do
79
- handle_response_result(ClientController.vm_confirmed_destroy(params[:vmname]))
148
+ #handle_response_result(ClientController.vm_confirmed_destroy(params[:vmname]))
149
+ execute(:vm_confirmed_destroy,true,params[:vmname])
80
150
  end
151
+
152
+ put RouteManager.vm_add_route do
153
+ execute(:vm_add,false,params[:file],params[:rename])
154
+ end
155
+
156
+ delete RouteManager.vm_delete_route do
157
+ execute(:vm_delete,false,params[:vm],params[:remove])
158
+ end
81
159
 
82
160
  #accept :vmname as paramter. This parameter
83
161
  #could be empty
84
- post RouteManager.vm_suspend_route do
85
- handle_response_result(ClientController.vm_suspend(params[:vmname]))
162
+ post RouteManager.vm_suspend_route do
163
+ #handle_response_result(ClientController.vm_suspend(params[:vmname]))
164
+ execute_async(:vm_suspend,params[:vmname])
86
165
  end
87
166
 
88
167
  #accept :vmname as paramter. This parameter
89
168
  #could be empty
90
169
  post RouteManager.vm_resume_route do
91
- handle_response_result(ClientController.vm_resume(params[:vmname]))
170
+ #handle_response_result(ClientController.vm_resume(params[:vmname]))
171
+ execute_async(:vm_resume,params[:vmname])
92
172
  end
93
173
 
94
174
  post RouteManager.vm_provision_route do
95
- handle_response_result(ClientController.vm_provision(params[:vmname]))
175
+ #handle_response_result(ClientController.vm_provision(params[:vmname]))
176
+ execute_async(:vm_provision,params[:vmname])
96
177
  end
97
178
 
98
179
 
99
180
 
100
181
  get RouteManager.vm_sshconfig_route do
101
- handle_response_result(ClientController.vm_ssh_config(params[:vm]))
182
+ #handle_response_result(ClientController.vm_ssh_config(params[:vm]))
183
+ execute(:vm_ssh_config,true,params[:vm])
102
184
  end
103
185
 
104
186
  get RouteManager.snapshots_all_route do
105
- handle_response_result(ClientController.vm_snapshots(nil))
187
+ #handle_response_result(ClientController.vm_snapshots(nil))
188
+ execute(:vm_snapshots,true)
106
189
  end
107
190
 
108
191
 
109
192
 
110
193
  get RouteManager.vm_snapshots_route do
111
- handle_response_result(ClientController.vm_snapshots(params[:vm]))
194
+ #handle_response_result(ClientController.vm_snapshots(params[:vm]))
195
+ execute(:vm_snapshots,true,params[:vm])
112
196
  end
113
197
 
114
198
  get RouteManager.vm_snapshot_take_route do
115
- result=ClientController.vm_snapshot_take_file(params[:vmname])
199
+ #result=ClientController.vm_snapshot_take_file(params[:vmname])
200
+ result=execute(:vm_snapshot_take_file,false,params[:vmname])
116
201
 
117
202
 
118
203
  send_file "#{result[1]}", :filename => result[1],
119
- :type => 'Application/octet-stream' if result[0]==200 && params[:download]="true"
204
+ :type => 'Application/octet-stream' if result[0]==200 && params[:download]=="true"
120
205
 
121
206
  status result[0]
122
207
 
123
208
  end
124
209
 
125
210
 
126
- post RouteManager.vm_snapshot_take_route do
127
- handle_response_result(ClientController.vm_snapshot_take(params[:vmname],params[:name],params[:desc]))
211
+ post RouteManager.vm_snapshot_take_route do
212
+ #handle_response_result(ClientController.vm_snapshot_take(params[:vmname],params[:name],params[:desc]))
213
+ execute_async(:vm_snapshot_take,params[:vmname],params[:name],params[:desc])
128
214
  end
129
215
 
130
216
 
131
217
  post RouteManager.vm_snapshot_restore_route do
132
- handle_response_result(ClientController.vm_snapshot_restore(params[:vmname],params[:snapid]))
218
+ #handle_response_result(ClientController.vm_snapshot_restore(params[:vmname],params[:snapid]))
219
+ execute_async(:vm_snapshot_restore,params[:vmname],params[:snapid])
133
220
  end
134
221
 
222
+ delete RouteManager.vm_snapshot_delete_route do
223
+ execute(:vm_snapshot_delete,false,params[:vm],params[:snapid])
224
+ end
225
+
135
226
 
136
227
  get RouteManager.vm_backup_log_route do
137
- handle_response_result(ClientController.backup_log(params[:vm]))
228
+ #handle_response_result(ClientController.backup_log(params[:vm]))
229
+ execute(:backup_log,true,params[:vm])
138
230
  end
139
231
 
140
232
 
141
233
 
142
234
  get RouteManager.node_backup_log_route do
143
- handle_response_result(ClientController.backup_log(nil))
235
+ #handle_response_result(ClientController.backup_log(nil))
236
+ execute(:backup_log,true)
237
+ end
238
+
239
+
240
+ get RouteManager.config_show_route do
241
+ result= execute(:config_show,false)
242
+ #result=ClientController.config_show
243
+
244
+ send_file(result, :disposition => 'attachment', :filename => result)
245
+
246
+ end
247
+
248
+ post RouteManager.node_password_change_route do
249
+ execute(:password_change,false,params[:password])
144
250
  end
145
251
 
252
+ post RouteManager.config_upload_route do
253
+ execute(:config_upload,false,params[:file])
254
+ end
255
+
256
+ get RouteManager.node_queue_route do
257
+ execute(:operation_queued,true,params[:id])
258
+ end
259
+
260
+ get RouteManager.node_queue_last_route do
261
+ execute(:operation_queued_last,true)
262
+ end
263
+
264
+
146
265
 
147
266
  private
148
- def handle_response_result(result)
149
- if (!result)
150
- status 500
151
- elsif (result.empty?)
152
- status 404
153
- end
154
- result.to_json
155
- end
267
+ def clear_session_table
268
+ @session_table.delete_if {|key,value| Time::now.to_i >= value[:expiration]}
269
+ end
270
+
271
+ #FIXME Factorizar estos dos métodos
272
+ def execute_async(method,*params)
273
+ begin
274
+ if params.empty?
275
+ result=ClientController.send method.to_sym
276
+ else
277
+ result=ClientController.send method.to_sym,*params
278
+ end
279
+
280
+
281
+ #pp result
282
+ status 202
283
+ body "Location: #{result}"
284
+ # body "Location: #{result}".to_json
285
+
286
+ rescue => e
287
+ #FIXME DELETE PUTS
288
+ #puts "EN EXCEPCION"
289
+ puts e.class
290
+ puts e.message
291
+
292
+
293
+ exception = ((e.class==RestException)? e:ExceptionMutator.new(e))
294
+
295
+ halt exception.code,exception.message
296
+ end
297
+
298
+ end
299
+
300
+ #def execute(method,params = {} ,to_json = true)
301
+ def execute(method,to_json = true,*params)
302
+ begin
303
+ if params.empty?
304
+ result=ClientController.send method.to_sym
305
+ else
306
+ result=ClientController.send method.to_sym,*params
307
+ end
308
+ #puts "TERMINADA LA OPERACION"
309
+ #puts "A JSON " if to_json
310
+ #puts "resultado #{result}"
311
+ return result.to_json if to_json
312
+ result
313
+
314
+ rescue => e
315
+ #puts "EN EXCEPCION"
316
+ puts e.class
317
+ puts e.message
318
+
319
+
320
+ exception = ((e.class==RestException)? e:ExceptionMutator.new(e))
321
+
322
+ halt exception.code,exception.message
323
+ end
324
+
325
+ end
326
+
327
+
328
+
329
+ def authorized?(id,token)
330
+ @session_table[id][:challenge]
331
+ ClientController.authorized?(token,@session_table[id][:challenge]) if @session_table.has_key?(id)
332
+ end
333
+
334
+ def challenged?(token)
335
+ (token!=nil && token.size==32)
336
+ end
156
337
 
157
338
  end
158
339
  end
@@ -36,6 +36,10 @@ module RestRoutes
36
36
  VM_DESTROY_ROUTE
37
37
  end
38
38
 
39
+ def self.vm_add_route
40
+ VM_ADD_ROUTE
41
+ end
42
+
39
43
  def self.vm_status_route
40
44
  VM_STATUS_ROUTE
41
45
  end
@@ -64,6 +68,14 @@ module RestRoutes
64
68
  VM_SNAPSHOT_TAKE_ROUTE
65
69
  end
66
70
 
71
+ def self.vm_snapshot_delete_route
72
+ VM_SNAPSHOT_DELETE_ROUTE
73
+ end
74
+
75
+ def self.vm_delete_route
76
+ VM_DELETE_ROUTE
77
+ end
78
+
67
79
  def self.vm_snapshot_restore_route
68
80
  VM_SNAPSHOT_RESTORE_ROUTE
69
81
  end
@@ -72,11 +84,42 @@ module RestRoutes
72
84
  NODE_BACKUP_LOG_ROUTE
73
85
  end
74
86
 
87
+ def self.node_password_change_route
88
+ NODE_PASSWORD_CHANGE
89
+ end
90
+
91
+
92
+
75
93
  def self.vm_backup_log_route
76
94
  VM_BACKUP_LOG_ROUTE
77
95
  end
78
96
 
79
97
 
98
+ def self.config_show_route
99
+ NODE_CONFIG_SHOW_ROUTE
100
+ end
101
+
102
+ def self.config_upload_route
103
+ NODE_CONFIG_UPLOAD_ROUTE
104
+ end
105
+
106
+
107
+ def self.login_route
108
+ LOGIN_ROUTE
109
+ end
110
+
111
+ def self.node_queue_route
112
+ NODE_QUEUE_ROUTE
113
+ end
114
+
115
+ def self.node_queue_last_route
116
+ NODE_QUEUE_LAST_ROUTE
117
+ end
118
+
119
+
120
+
121
+
122
+
80
123
  def self.box_list_url(host,port)
81
124
  "http://#{host}:#{port}#{box_list_route}"
82
125
  end
@@ -95,8 +138,20 @@ module RestRoutes
95
138
  url
96
139
 
97
140
  end
98
-
99
141
 
142
+ def self.node_queue_url(host,port,id)
143
+ url=String.new(node_queue_route)
144
+ url[":id"] = id
145
+ url="http://#{host}:#{port}#{url}"
146
+ end
147
+
148
+ def self.node_queue_last_url(host,port)
149
+ "http://#{host}:#{port}#{node_queue_last_route}"
150
+ end
151
+
152
+ def self.login_url(host,port)
153
+ "http://#{host}:#{port}#{login_route}"
154
+ end
100
155
 
101
156
  def self.vm_up_url(host,port)
102
157
  "http://#{host}:#{port}#{vm_up_route}"
@@ -110,6 +165,10 @@ module RestRoutes
110
165
  "http://#{host}:#{port}#{vm_destroy_route}"
111
166
  end
112
167
 
168
+ def self.vm_add_url(host,port)
169
+ "http://#{host}:#{port}#{vm_add_route}"
170
+ end
171
+
113
172
  def self.vm_suspend_url(host,port)
114
173
  "http://#{host}:#{port}#{vm_suspend_route}"
115
174
  end
@@ -168,6 +227,16 @@ module RestRoutes
168
227
 
169
228
  end
170
229
 
230
+ def self.vm_snapshot_delete_url(host,port,vmname,snapid)
231
+ url=String.new(vm_snapshot_delete_route)
232
+ url[":vm"]=vmname
233
+ url[":snapid"]=snapid
234
+ url="http://#{host}:#{port}#{url}"
235
+
236
+ url
237
+
238
+ end
239
+
171
240
  def self.vm_snapshot_restore_url(host,port,vmname)
172
241
  url=String.new(vm_snapshot_restore_route)
173
242
  url[":vm"]=vmname
@@ -177,6 +246,17 @@ module RestRoutes
177
246
 
178
247
  end
179
248
 
249
+ def self.vm_delete_url(host,port,vmname)
250
+ url=String.new(vm_delete_route)
251
+ url[":vm"]=vmname
252
+ url="http://#{host}:#{port}#{url}"
253
+
254
+ url
255
+ end
256
+
257
+
258
+
259
+
180
260
  def self.backup_log_url(host,port,vmname=nil)
181
261
  url="http://#{host}:#{port}#{node_backup_log_route}"
182
262
 
@@ -190,6 +270,17 @@ module RestRoutes
190
270
 
191
271
  end
192
272
 
273
+ def self.config_show_url(host,port)
274
+ "http://#{host}:#{port}#{config_show_route}"
275
+ end
276
+
277
+ def self.node_password_change_url(host,port)
278
+ "http://#{host}:#{port}#{node_password_change_route}"
279
+ end
280
+
281
+ def self.config_upload_url(host,port)
282
+ "http://#{host}:#{port}#{config_upload_route}"
283
+ end
193
284
 
194
285
 
195
286
  private
@@ -200,6 +291,8 @@ module RestRoutes
200
291
  VM_UP_ROUTE = "/api/vm/up"
201
292
  VM_HALT_ROUTE = "/api/vm/halt"
202
293
  VM_DESTROY_ROUTE = "/api/vm/destroy"
294
+ VM_DELETE_ROUTE = "/api/vm/:vm/delete"
295
+ VM_ADD_ROUTE = "/api/vm/add"
203
296
  VM_SUSPEND_ROUTE = "/api/vm/suspend"
204
297
  VM_RESUME_ROUTE = "/api/vm/resume"
205
298
  VM_PROVISION_ROUTE = "/api/vm/provision"
@@ -210,11 +303,22 @@ module RestRoutes
210
303
  SNAPSHOTS_ALL_ROUTE = "/api/vm/snapshots"
211
304
  VM_SNAPSHOTS_ROUTE = "/api/vm/:vm/snapshots"
212
305
  VM_SNAPSHOT_TAKE_ROUTE = "/api/vm/:vm/take"
306
+ VM_SNAPSHOT_DELETE_ROUTE = "/api/vm/:vm/:snapid/delete"
213
307
  VM_SNAPSHOT_RESTORE_ROUTE = "/api/vm/:vm/restore"
214
308
 
215
309
  VM_BACKUP_LOG_ROUTE = "/api/vm/:vm/backuplog"
216
310
  NODE_BACKUP_LOG_ROUTE = "/api/backuplog"
217
311
 
312
+ NODE_CONFIG_SHOW_ROUTE = "/api/config/show"
313
+ NODE_CONFIG_UPLOAD_ROUTE = "/api/config/upload"
314
+
315
+ NODE_PASSWORD_CHANGE = "/api/password/change"
316
+
317
+ NODE_QUEUE_ROUTE = "/api/queue/:id"
318
+ NODE_QUEUE_LAST_ROUTE = "/api/queuelast"
319
+
320
+ LOGIN_ROUTE = "/api/login"
321
+
218
322
  end
219
323
 
220
324
  end