vagrant-nodemaster 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/Rakefile +1 -0
  4. data/lib/vagrant-nodemaster.rb +26 -0
  5. data/lib/vagrant-nodemaster/apidesc.rb +220 -0
  6. data/lib/vagrant-nodemaster/node/nodeadd.rb +33 -0
  7. data/lib/vagrant-nodemaster/node/nodedbmanager.rb +176 -0
  8. data/lib/vagrant-nodemaster/node/nodelist.rb +34 -0
  9. data/lib/vagrant-nodemaster/node/noderemove.rb +43 -0
  10. data/lib/vagrant-nodemaster/node/nodestatus.rb +73 -0
  11. data/lib/vagrant-nodemaster/node/nodeupdate.rb +43 -0
  12. data/lib/vagrant-nodemaster/nodecommand.rb +102 -0
  13. data/lib/vagrant-nodemaster/remote/remotebackupcommand.rb +90 -0
  14. data/lib/vagrant-nodemaster/remote/remotebackuplog.rb +62 -0
  15. data/lib/vagrant-nodemaster/remote/remotebackuptake.rb +50 -0
  16. data/lib/vagrant-nodemaster/remote/remoteboxadd.rb +35 -0
  17. data/lib/vagrant-nodemaster/remote/remoteboxcommand.rb +98 -0
  18. data/lib/vagrant-nodemaster/remote/remoteboxdelete.rb +40 -0
  19. data/lib/vagrant-nodemaster/remote/remoteboxlist.rb +32 -0
  20. data/lib/vagrant-nodemaster/remote/remotedestroy.rb +66 -0
  21. data/lib/vagrant-nodemaster/remote/remotehalt.rb +48 -0
  22. data/lib/vagrant-nodemaster/remote/remoteprovision.rb +43 -0
  23. data/lib/vagrant-nodemaster/remote/remoteresume.rb +32 -0
  24. data/lib/vagrant-nodemaster/remote/remotesnapshotcommand.rb +93 -0
  25. data/lib/vagrant-nodemaster/remote/remotesnapshotlist.rb +60 -0
  26. data/lib/vagrant-nodemaster/remote/remotesnapshotrestore.rb +34 -0
  27. data/lib/vagrant-nodemaster/remote/remotesnapshottake.rb +31 -0
  28. data/lib/vagrant-nodemaster/remote/remotessh.rb +55 -0
  29. data/lib/vagrant-nodemaster/remote/remotesuspend.rb +32 -0
  30. data/lib/vagrant-nodemaster/remote/remoteup.rb +43 -0
  31. data/lib/vagrant-nodemaster/remote/remotevmstatus.rb +43 -0
  32. data/lib/vagrant-nodemaster/remotecommand.rb +155 -0
  33. data/lib/vagrant-nodemaster/requestcontroller.rb +344 -0
  34. data/lib/vagrant-nodemaster/version.rb +5 -0
  35. data/vagrant-nodemaster.gemspec +29 -0
  36. metadata +157 -0
@@ -0,0 +1,344 @@
1
+ require 'rest_client'
2
+
3
+ require 'pp'
4
+ require 'vagrant-nodemaster/apidesc'
5
+ require 'vagrant-nodemaster/node/nodedbmanager'
6
+
7
+ module Vagrant
8
+ module NodeMaster
9
+ class RequestController
10
+ include RestRoutes
11
+
12
+ def self.get_remote_boxes(host)
13
+ client=get_host_parameters(host)
14
+
15
+ response = RestClient.get RouteManager.box_list_url(client[:address],client[:port])
16
+ JSON.parse(response.to_str)
17
+ end
18
+
19
+ def self.box_delete(host,box,provider)
20
+ client=get_host_parameters(host)
21
+
22
+ response = RestClient.delete RouteManager.box_delete_url(client[:address],client[:port],box,provider)
23
+
24
+ JSON.parse(response.to_str)
25
+
26
+ end
27
+
28
+ def self.box_add(host,box,url)
29
+ client=get_host_parameters(host)
30
+
31
+ resource = RestClient::Resource.new(
32
+ RouteManager.box_add_url(client[:address],client[:port]),
33
+ :timeout => -1
34
+ )
35
+
36
+ response = resource.post :box => box,:url => url
37
+
38
+ JSON.parse(response.to_str)
39
+
40
+ end
41
+
42
+ def self.vm_up(host,vmname)
43
+ client=get_host_parameters(host)
44
+ #Debido a que puede durar bastante el levantar
45
+ #las máquinas, establezco que la petición no expire
46
+ resource = RestClient::Resource.new(
47
+ RouteManager.vm_up_url(client[:address],client[:port]),
48
+ :timeout => -1
49
+ )
50
+
51
+ response = resource.post :vmname => vmname
52
+
53
+ JSON.parse(response.to_str)
54
+
55
+ end
56
+
57
+ def self.vm_halt(host,vmname,force)
58
+ client=get_host_parameters(host)
59
+ #Debido a que puede durar bastante apagar
60
+ #las máquinas, establezco que la petición no expire
61
+ resource = RestClient::Resource.new(
62
+ RouteManager.vm_halt_url(client[:address],client[:port]),
63
+ :timeout => -1
64
+ )
65
+
66
+ response = resource.post :vmname => vmname,:force=>force
67
+
68
+ JSON.parse(response.to_str)
69
+
70
+ end
71
+
72
+ def self.vm_destroy (host,vmname)
73
+ client=get_host_parameters(host)
74
+ resource = RestClient::Resource.new(
75
+ RouteManager.vm_destroy_url(client[:address],client[:port]),
76
+ :timeout => -1
77
+ )
78
+
79
+ response = resource.post :vmname => vmname
80
+
81
+ JSON.parse(response.to_str)
82
+
83
+ end
84
+
85
+
86
+
87
+ def self.vm_status(host,vmname)
88
+ client=get_host_parameters(host)
89
+
90
+ get_vm_status(client[:address],client[:port],vmname)
91
+
92
+ # response = RestClient.get RouteManager.vm_status_url(client[:address],client[:port],vmname)
93
+ #
94
+ # return JSON.parse(response.to_str)
95
+
96
+ end
97
+
98
+ def self.vm_ssh_config(host,vmname)
99
+ client=get_host_parameters(host)
100
+
101
+ response = RestClient.get RouteManager.vm_sshconfig_url(client[:address],client[:port],vmname)
102
+
103
+ result=JSON.parse(response.to_str, {:symbolize_names => true})
104
+ #Change the target machine
105
+ result[:host]=client[:address]
106
+
107
+ result
108
+ end
109
+
110
+
111
+ def self.vm_suspend(host,vmname)
112
+ client=get_host_parameters(host)
113
+ #Debido a que puede durar bastante el levantar
114
+ #las máquinas, establezco que la petición no expire
115
+ resource = RestClient::Resource.new(
116
+ RouteManager.vm_suspend_url(client[:address],client[:port]),
117
+ :timeout => -1
118
+ )
119
+
120
+ response = resource.post :vmname => vmname
121
+
122
+ JSON.parse(response.to_str)
123
+
124
+ end
125
+
126
+ def self.vm_resume(host,vmname)
127
+ client=get_host_parameters(host)
128
+ #Debido a que puede durar bastante el levantar
129
+ #las máquinas, establezco que la petición no expire
130
+ resource = RestClient::Resource.new(
131
+ RouteManager.vm_resume_url(client[:address],client[:port]),
132
+ :timeout => -1
133
+ )
134
+
135
+ response = resource.post :vmname => vmname
136
+
137
+ JSON.parse(response.to_str)
138
+
139
+ end
140
+
141
+
142
+ def self.vm_provision(host,vmname)
143
+ client=get_host_parameters(host)
144
+ #Debido a que puede durar bastante el levantar
145
+ #las máquinas, establezco que la petición no expire
146
+ resource = RestClient::Resource.new(
147
+ RouteManager.vm_provision_url(client[:address],client[:port]),
148
+ :timeout => -1
149
+ )
150
+
151
+ response = resource.post :vmname => vmname
152
+
153
+ JSON.parse(response.to_str)
154
+
155
+ end
156
+
157
+ def self.get_remote_snapshots(host,vmname)
158
+ client=get_host_parameters(host)
159
+
160
+ url=RouteManager.snapshot_list_url(client[:address],client[:port],vmname)
161
+
162
+ response = RestClient.get url
163
+
164
+
165
+ JSON.parse(response.to_str,{:symbolize_names => true})
166
+
167
+ end
168
+
169
+ def self.vm_snapshot_take(host,vmname,sname,sdesc)
170
+ client=get_host_parameters(host)
171
+ resource = RestClient::Resource.new(
172
+ RouteManager.vm_snapshot_take_url(client[:address],client[:port],vmname),
173
+ :timeout => -1
174
+ )
175
+ response = resource.post :vmname => vmname,:name => sname,:desc => sdesc
176
+
177
+ JSON.parse(response.to_str,{:symbolize_names => true})
178
+
179
+ end
180
+
181
+ def self.vm_snapshot_restore(host,vmname,snapid)
182
+ client=get_host_parameters(host)
183
+ resource = RestClient::Resource.new(
184
+ RouteManager.vm_snapshot_restore_url(client[:address],client[:port],vmname),
185
+ :timeout => -1
186
+ )
187
+ response = resource.post :vmname => vmname,:snapid => snapid
188
+ JSON.parse(response.to_str,{:symbolize_names => true})
189
+ end
190
+
191
+ def self.node_backup_take(ui,target_dir,host=nil,vmname=nil)
192
+
193
+ current_client=nil
194
+ current_file = nil
195
+ download_backup = false
196
+ begin
197
+
198
+ raise "Directory \"#{target_dir}\" does not exists" if target_dir && !File.directory?(target_dir)
199
+
200
+ download_backup = true if target_dir
201
+
202
+ clients = []
203
+ if (host)
204
+ clients << get_host_parameters(host)
205
+ else
206
+ dbmanager=DB::NodeDBManager.new
207
+ clients = dbmanager.get_nodes
208
+ end
209
+
210
+ clients.each do |client|
211
+ current_client=client
212
+ #Fist get remote virtual machines
213
+ vms=get_vm_status(client[:address],client[:port],vmname)
214
+
215
+ vms.each do |vm|
216
+
217
+ th = nil
218
+ if (ui)
219
+ th = Thread.new(ui) do |ui|
220
+ draw_progress(ui,download_backup,client[:name],vm["name"])
221
+ end
222
+ end
223
+
224
+
225
+
226
+ resource = RestClient::Resource.new(
227
+ RouteManager.vm_snapshot_take_url(client[:address],client[:port],vm["name"]),
228
+ :download => download_backup,
229
+ :timeout => -1
230
+ )
231
+
232
+ response = resource.get({:params => {'download' => download_backup}})
233
+
234
+ th.kill if th
235
+
236
+ if response.code==200
237
+ if download_backup && response.headers[:content_type]=="Application/octet-stream"
238
+
239
+ time = Time.now
240
+
241
+ basename = "Backup.#{client[:name]}.#{vm["name"]}.#{time.year}.#{time.month}.#{time.day}.#{time.hour}.#{time.min}.#{time.sec}"
242
+ current_file = "#{target_dir}/#{basename}.zip"
243
+ File.open(current_file, "w") do |f|
244
+ f.write(response.body)
245
+ end
246
+
247
+ current_file=""
248
+ #FIXME DELETE If we want to use the filename of the attachment
249
+ # if response.headers[:content_disposition] =~ /^attachment; filename=\"(.*?)\"$/
250
+ # current_file = "#{target_dir}/#{$1}"
251
+ # File.open(current_file, "w") do |f|
252
+ # f.write(response.body)
253
+ # end
254
+ # current_file=""
255
+ # end
256
+ end
257
+
258
+ ui.success("OK") if ui
259
+
260
+ end
261
+
262
+ end
263
+
264
+ end
265
+
266
+ rescue RestClient::ResourceNotFound => e
267
+ ui.error("Remote Client \"#{current_client[:name]}\": Box \"#{vmname}\" could not be found") if ui
268
+ rescue Exception => e
269
+ ui.error("ERROR: "+e.message) if ui
270
+ #Checking that the tmp file is removed
271
+ File.delete(current_file) if current_file && File.exists?(current_file)
272
+ end
273
+
274
+ end
275
+
276
+ def self.node_backup_log(ui,node,vmname=nil)
277
+ clients = []
278
+
279
+
280
+ raise "Error finding the node" if node==nil
281
+
282
+ client = get_host_parameters(node)
283
+
284
+ resource = RestClient::Resource.new(
285
+ RouteManager.backup_log_url(client[:address],client[:port],vmname),
286
+ :timeout => -1
287
+ )
288
+
289
+
290
+ response = resource.get :vmname => vmname
291
+
292
+ JSON.parse(response.to_str)
293
+
294
+ end
295
+
296
+
297
+ private
298
+
299
+ def self.get_host_parameters(host)
300
+
301
+ dbmanager=DB::NodeDBManager.new
302
+
303
+ return dbmanager.get_node(host)
304
+
305
+ end
306
+
307
+ def self.get_vm_status(address,port,vmname)
308
+ response = RestClient.get RouteManager.vm_status_url(address,port,vmname)
309
+ JSON.parse(response.to_str)
310
+ end
311
+
312
+ def self.draw_progress(ui,download,node,vmname)
313
+ return if !ui
314
+ ui.info("Downloading ",:new_line => false) if download
315
+ ui.info("Waiting for ",:new_line => false) if !download
316
+
317
+ ui.info("backup of Virtual Machine \'#{vmname}\' from Node \'#{node}\' . . . ",:new_line => false)
318
+
319
+ val=0
320
+ while true
321
+ sleep(0.1)
322
+
323
+ case val
324
+ when 0 then
325
+ ui.info("|\b",:new_line => false)
326
+ when 1 then
327
+ ui.info("/\b",:new_line => false)
328
+ when 2 then
329
+ ui.info("-\b",:new_line => false)
330
+ when 3 then
331
+ ui.info("\\\b",:new_line => false)
332
+ end
333
+ val=val+1
334
+
335
+ val=0 if val==4
336
+ end
337
+ end
338
+
339
+
340
+ end
341
+ end
342
+ end
343
+
344
+
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module NodeMaster
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-nodemaster/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-nodemaster"
8
+ spec.version = Vagrant::NodeMaster::VERSION
9
+ spec.authors = ["Francisco Javier Lopez de San Pedro"]
10
+ spec.email = ["fjsanpedro@gmail.com"]
11
+ spec.description = "Cátedra SAES-UMU"
12
+ spec.summary = "Escribir summary"
13
+ spec.homepage = "http://www.catedrasaes.org"
14
+ spec.license = "GNU"
15
+
16
+
17
+ spec.rubyforge_project = "vagrant-nodemaster"
18
+
19
+ spec.add_dependency "rest-client"
20
+ spec.add_dependency "sqlite3"
21
+
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-nodemaster
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Francisco Javier Lopez de San Pedro
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-06-25 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sqlite3
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 9
57
+ segments:
58
+ - 1
59
+ - 3
60
+ version: "1.3"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
77
+ description: "C\xC3\xA1tedra SAES-UMU"
78
+ email:
79
+ - fjsanpedro@gmail.com
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - Rakefile
90
+ - lib/vagrant-nodemaster.rb
91
+ - lib/vagrant-nodemaster/apidesc.rb
92
+ - lib/vagrant-nodemaster/node/nodeadd.rb
93
+ - lib/vagrant-nodemaster/node/nodedbmanager.rb
94
+ - lib/vagrant-nodemaster/node/nodelist.rb
95
+ - lib/vagrant-nodemaster/node/noderemove.rb
96
+ - lib/vagrant-nodemaster/node/nodestatus.rb
97
+ - lib/vagrant-nodemaster/node/nodeupdate.rb
98
+ - lib/vagrant-nodemaster/nodecommand.rb
99
+ - lib/vagrant-nodemaster/remote/remotebackupcommand.rb
100
+ - lib/vagrant-nodemaster/remote/remotebackuplog.rb
101
+ - lib/vagrant-nodemaster/remote/remotebackuptake.rb
102
+ - lib/vagrant-nodemaster/remote/remoteboxadd.rb
103
+ - lib/vagrant-nodemaster/remote/remoteboxcommand.rb
104
+ - lib/vagrant-nodemaster/remote/remoteboxdelete.rb
105
+ - lib/vagrant-nodemaster/remote/remoteboxlist.rb
106
+ - lib/vagrant-nodemaster/remote/remotedestroy.rb
107
+ - lib/vagrant-nodemaster/remote/remotehalt.rb
108
+ - lib/vagrant-nodemaster/remote/remoteprovision.rb
109
+ - lib/vagrant-nodemaster/remote/remoteresume.rb
110
+ - lib/vagrant-nodemaster/remote/remotesnapshotcommand.rb
111
+ - lib/vagrant-nodemaster/remote/remotesnapshotlist.rb
112
+ - lib/vagrant-nodemaster/remote/remotesnapshotrestore.rb
113
+ - lib/vagrant-nodemaster/remote/remotesnapshottake.rb
114
+ - lib/vagrant-nodemaster/remote/remotessh.rb
115
+ - lib/vagrant-nodemaster/remote/remotesuspend.rb
116
+ - lib/vagrant-nodemaster/remote/remoteup.rb
117
+ - lib/vagrant-nodemaster/remote/remotevmstatus.rb
118
+ - lib/vagrant-nodemaster/remotecommand.rb
119
+ - lib/vagrant-nodemaster/requestcontroller.rb
120
+ - lib/vagrant-nodemaster/version.rb
121
+ - vagrant-nodemaster.gemspec
122
+ homepage: http://www.catedrasaes.org
123
+ licenses:
124
+ - GNU
125
+ post_install_message:
126
+ rdoc_options: []
127
+
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: 3
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirements: []
149
+
150
+ rubyforge_project: vagrant-nodemaster
151
+ rubygems_version: 1.8.15
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Escribir summary
155
+ test_files: []
156
+
157
+ has_rdoc: