vagrant-node 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in vagrant-node.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (C) Cátedra SAES-UMU 2013 <fjsanpedro@gmail.com>
2
+
3
+ VAGRANT-NODE is free software: you can redistribute it and/or modify it
4
+ under the terms of the GNU Lesser General Public License as published
5
+ by the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ VAGRANT-NODE is distributed in the hope that it will be useful, but
9
+ WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
+ See the GNU Lesser General Public License for more details.
12
+
13
+ You should have received a copy of the GNU Lesser General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,4 @@
1
+ vagrant-node
2
+ ============
3
+
4
+ Vagrant-node plugin for Vagrant Framework
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,21 @@
1
+ require "vagrant-node/version"
2
+
3
+ module Vagrant
4
+ module Node
5
+ class Plugin < Vagrant.plugin("2")
6
+
7
+ name "node"
8
+ description <<-DESC
9
+ ESTE PLUGIN ES EL QUE LANZA EL SERVIDOR
10
+ DESC
11
+
12
+
13
+ command ('nodeserver') do
14
+ require_relative "vagrant-node/nodeservercommand"
15
+ Command
16
+ end
17
+
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,285 @@
1
+ require 'pp'
2
+ require "rexml/document"
3
+ #require 'rubygems'
4
+ require 'zip/zip'
5
+
6
+
7
+ module Vagrant
8
+ module Node
9
+
10
+ class SnapshotAction
11
+ LIST = :list
12
+ TAKE = :take
13
+ RESTORE = :restore
14
+ BACKUP = :backup
15
+ ERROR = :error
16
+
17
+ def initialize(app, env)
18
+ @app = app
19
+ end
20
+
21
+ def call(env)
22
+
23
+ #FIXME generate a result code error to this case in order
24
+ #to provide more info to the client
25
+ return @app.call(env) if env[:machine].state.id==:not_created
26
+
27
+ current_driver=env[:machine].provider.driver
28
+
29
+ begin
30
+ current_driver.check_snapshot_stuff
31
+ rescue Exception => e
32
+ case env[:machine].provider_name.to_s
33
+ when "virtualbox" then
34
+ attach_vbox_methods(current_driver)
35
+ end
36
+
37
+ end
38
+
39
+ case env[:machine_action]
40
+ when LIST then
41
+ env[:snapshots_list]=current_driver.list
42
+ when TAKE then
43
+ env[:last_snapshot]=current_driver.take_snapshot(env[:snapshot_name],env[:snapshot_desc])
44
+ when RESTORE then
45
+
46
+ env[:restore_result]=current_driver.restore_snapshot(env[:snapshot_id])
47
+ when BACKUP then
48
+ env[:bak_filename]=current_driver.backup_vm(env[:machine].name.to_s,env[:machine].data_dir)
49
+ end
50
+
51
+ @app.call(env)
52
+
53
+ end
54
+
55
+ private
56
+
57
+ def attach_vbox_methods(driver)
58
+
59
+ def driver.check_snapshot_stuff
60
+ return true
61
+ end
62
+
63
+ #FIXME Si no se hace con --pause y la máquina esta running
64
+ #se produce un error y deja a la máquina en estado 'gurumeditating'
65
+ def driver.take_snapshot(name,description=" ")
66
+
67
+ begin
68
+ #Execute the take command
69
+ if (description)
70
+ execute("snapshot",self.uuid,"take",name,"--description",description,"--pause")
71
+ else
72
+ execute("snapshot",self.uuid,"take",name,"--pause")
73
+ end
74
+
75
+ snapshot = {}
76
+ #Getting the uuid of the latest snapshot
77
+ execute("snapshot",self.uuid,"list").split("\n").each do |line|
78
+
79
+ if line =~ /Name:\s(.*?)\s\(UUID:\s(.*?)\)\s\*$/
80
+ snapshot[:name] = $1
81
+ snapshot[:id] = $2
82
+ end
83
+
84
+ end
85
+
86
+ #return snapshot
87
+ snapshot
88
+
89
+
90
+ rescue => e
91
+ puts e.message
92
+ return nil
93
+ end
94
+
95
+
96
+ end #driver.take_snapshot
97
+
98
+
99
+ def driver.backup_vm(vmname,path)
100
+
101
+ zipfilename=nil
102
+ begin
103
+ #Execute the export command
104
+
105
+
106
+ time = Time.now
107
+ basename = "Backup.#{vmname}.#{time.year}.#{time.month}.#{time.day}.#{time.hour}.#{time.min}.#{time.sec}"
108
+ zipfilename = path.to_s + '/'+ basename + '.zip'
109
+
110
+ vmdir = nil
111
+ execute("showvminfo",self.uuid,"--machinereadable").split("\n").each do |line|
112
+ vmdir = File.dirname($1) if line =~ /^CfgFile="(.*?)"$/
113
+ end
114
+
115
+
116
+ Zip::ZipFile.open(zipfilename, Zip::ZipFile::CREATE) do |zipfile|
117
+ Dir[File.join(vmdir, '**', '**')].each do |file|
118
+ zipfile.add(file.sub(vmdir+"/", ''), file)
119
+ end
120
+ end
121
+
122
+ zipfilename
123
+
124
+ rescue => e
125
+ File.delete(zipfilename) if File.exists?(zipfilename)
126
+ return ERROR
127
+ end
128
+
129
+
130
+ end #driver.backup_vm
131
+
132
+
133
+
134
+ # def driver.export_vm(path)
135
+ #
136
+ # begin
137
+ #Execute the export command
138
+ # basename = "Backup.#{Time.now.to_i.to_s}"
139
+ # filename = path + basename
140
+ # execute("export",self.uuid,"--output",filename.to_s+".ovf")
141
+ # zipfilename = filename.to_s + '.zip'
142
+ #
143
+ # files_to_zip = []
144
+ # Dir.chdir(path)
145
+ # Dir.glob(basename+"*") do |file|
146
+ # files_to_zip << file
147
+ # end
148
+ #
149
+ # Zip::ZipFile.open(zipfilename, Zip::ZipFile::CREATE) do |zipfile|
150
+ # files_to_zip.each do |file|
151
+ # zipfile.add(file,file)
152
+ # end
153
+ # end
154
+ #
155
+ #
156
+ # Cleaning files
157
+ # files_to_zip.each do |file|
158
+ # File.delete(file)
159
+ # end
160
+ # return zipfilename
161
+ #
162
+ # rescue => e
163
+ # puts e.message
164
+ # return ERROR
165
+ # end
166
+
167
+
168
+ #end #driver.export_vm
169
+
170
+
171
+ def driver.list
172
+ config = nil
173
+ current_snapshot = nil
174
+
175
+ execute("showvminfo",self.uuid,"--machinereadable").split("\n").each do |line|
176
+ config = $1 if line =~ /^CfgFile="(.*?)"$/
177
+
178
+ end
179
+
180
+ snapshots = []
181
+
182
+ doc = REXML::Document.new File.new(config)
183
+ machine = doc.elements["VirtualBox/Machine"]
184
+
185
+ @current_snapshot = machine.attributes["currentSnapshot"]
186
+
187
+ snapshots = search_snapshot(machine) if @current_snapshot
188
+
189
+
190
+ snapshots
191
+
192
+
193
+ end #driver.list
194
+
195
+ def driver.search_snapshot(path)
196
+
197
+ snapshots = []
198
+ if path
199
+ path.elements.each("Snapshot") { |element|
200
+ snapshot = {}
201
+
202
+ snapshot[:uuid] = element.attributes["uuid"]
203
+ snapshot[:name] = element.attributes["name"]
204
+ snapshot[:timestamp] = element.attributes["timeStamp"]
205
+
206
+ if element.elements["Description"]
207
+ snapshot[:description] = element.elements["Description"].text
208
+ else
209
+ snapshot[:description] = " "
210
+ end
211
+
212
+ if (snapshot[:uuid]==@current_snapshot)
213
+
214
+ snapshot[:current_state] = true
215
+ else
216
+ snapshot[:current_state] = false
217
+ end
218
+
219
+ snapshot[:snapshots] = search_snapshot(element.elements["Snapshots"])
220
+
221
+ snapshots << snapshot
222
+ }
223
+ end #driver.search_snapshot(path)
224
+
225
+ #return snapshot array
226
+ snapshots
227
+
228
+ end
229
+
230
+ #snapipd could be the snapshot uuid or its name
231
+ def driver.restore_snapshot(snapid)
232
+
233
+ snapshots = []
234
+
235
+ begin
236
+
237
+ result = execute("snapshot",self.uuid,"restore",snapid)
238
+
239
+ if result =~ /^Restoring snapshot\s(.*?)$/
240
+ snapshots << $1
241
+ end
242
+
243
+ #return snapshot array
244
+ snapshots
245
+
246
+ rescue Vagrant::Errors::VBoxManageError => e
247
+ return snapshots if e.message=~ /Could not find a snapshot named/
248
+ end
249
+ end
250
+
251
+ end # attach_vbox_methods(driver)
252
+
253
+
254
+ end
255
+
256
+ end
257
+ end
258
+
259
+ # def driver.list
260
+ # puts "LISTING SNAPSHOT"
261
+ # begin
262
+ # snapshots = []
263
+ # puts "UUID #{self.uuid}"
264
+ # execute("snapshot",self.uuid,"list").split("\n").each do |line|
265
+ # snapshot = {}
266
+ #
267
+ # if line =~ /Name:\s(.*?)\s\(UUID:\s(.*?)\)$/
268
+ # snapshot[:name] = $1
269
+ # snapshot[:id] = $2
270
+ # snapshot[:current_state] = false
271
+ # elsif line =~ /Name:\s(.*?)\s\(UUID:\s(.*?)\)\s\*$/
272
+ # snapshot[:name] = $1
273
+ # snapshot[:id] = $2
274
+ # snapshot[:current_state] = true
275
+ # end
276
+ # snapshots.push(snapshot)
277
+ # end
278
+ # rescue Exception => e
279
+ # puts e.message
280
+ # end
281
+ #
282
+ # puts snapshots
283
+ # return snapshots
284
+ # end
285
+
@@ -0,0 +1,160 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'json'
4
+ require 'rack'
5
+ require 'vagrant-node/clientcontroller'
6
+ require 'vagrant-node/apidesc'
7
+
8
+
9
+ require 'pp'
10
+ module Vagrant
11
+ module Node
12
+ module ServerAPI
13
+
14
+ class API < Sinatra::Base
15
+ include RestRoutes
16
+ before do
17
+ content_type :json
18
+ end
19
+
20
+
21
+ #UPLOAD A FILE
22
+ # post '/' do
23
+ # tempfile = params['file'][:tempfile]
24
+ # filename = params['file'][:filename]
25
+ # File.copy(tempfile.path, "./files/#{filename}")
26
+ # redirect '/'
27
+ #end
28
+
29
+
30
+ # before '/admin/*' do
31
+ # authenticate!
32
+ # end
33
+
34
+ ######### FIXME DELETE #####################
35
+ get '/' do
36
+ "Hello World"
37
+ end
38
+
39
+ ###############################################
40
+
41
+ get RouteManager.box_list_route do
42
+ ClientController.listboxes.to_json
43
+ end
44
+
45
+ delete RouteManager.box_delete_route do
46
+ handle_response_result(ClientController.box_delete(params[:box],params[:provider]))
47
+ end
48
+
49
+ post RouteManager.box_add_route do
50
+ handle_response_result(ClientController.box_add(params[:box],params[:url]))
51
+ end
52
+
53
+
54
+
55
+ get RouteManager.vm_status_all_route do
56
+ handle_response_result(ClientController.vm_status(nil))
57
+ end
58
+
59
+
60
+
61
+ get RouteManager.vm_status_route do
62
+ handle_response_result(ClientController.vm_status(params[:vm]))
63
+ end
64
+
65
+ #accept :vmname as paramter. This parameter
66
+ #could be empty
67
+ post RouteManager.vm_up_route do
68
+ handle_response_result(ClientController.vm_up(params[:vmname]))
69
+ end
70
+
71
+ #accept :vmname and :force as paramters
72
+ post RouteManager.vm_halt_route do
73
+ handle_response_result(ClientController.vm_halt(params[:vmname],params[:force]))
74
+ end
75
+
76
+ #accept :vmname as paramter. This parameter
77
+ #could be empty
78
+ post RouteManager.vm_destroy_route do
79
+ handle_response_result(ClientController.vm_confirmed_destroy(params[:vmname]))
80
+ end
81
+
82
+ #accept :vmname as paramter. This parameter
83
+ #could be empty
84
+ post RouteManager.vm_suspend_route do
85
+ handle_response_result(ClientController.vm_suspend(params[:vmname]))
86
+ end
87
+
88
+ #accept :vmname as paramter. This parameter
89
+ #could be empty
90
+ post RouteManager.vm_resume_route do
91
+ handle_response_result(ClientController.vm_resume(params[:vmname]))
92
+ end
93
+
94
+ post RouteManager.vm_provision_route do
95
+ handle_response_result(ClientController.vm_provision(params[:vmname]))
96
+ end
97
+
98
+
99
+
100
+ get RouteManager.vm_sshconfig_route do
101
+ handle_response_result(ClientController.vm_ssh_config(params[:vm]))
102
+ end
103
+
104
+ get RouteManager.snapshots_all_route do
105
+ handle_response_result(ClientController.vm_snapshots(nil))
106
+ end
107
+
108
+
109
+
110
+ get RouteManager.vm_snapshots_route do
111
+ handle_response_result(ClientController.vm_snapshots(params[:vm]))
112
+ end
113
+
114
+ get RouteManager.vm_snapshot_take_route do
115
+ result=ClientController.vm_snapshot_take_file(params[:vmname])
116
+
117
+
118
+ send_file "#{result[1]}", :filename => result[1],
119
+ :type => 'Application/octet-stream' if result[0]==200 && params[:download]="true"
120
+
121
+ status result[0]
122
+
123
+ end
124
+
125
+
126
+ post RouteManager.vm_snapshot_take_route do
127
+ handle_response_result(ClientController.vm_snapshot_take(params[:vmname],params[:name],params[:desc]))
128
+ end
129
+
130
+
131
+ post RouteManager.vm_snapshot_restore_route do
132
+ handle_response_result(ClientController.vm_snapshot_restore(params[:vmname],params[:snapid]))
133
+ end
134
+
135
+
136
+ get RouteManager.vm_backup_log_route do
137
+ handle_response_result(ClientController.backup_log(params[:vm]))
138
+ end
139
+
140
+
141
+
142
+ get RouteManager.node_backup_log_route do
143
+ handle_response_result(ClientController.backup_log(nil))
144
+ end
145
+
146
+
147
+ 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
156
+
157
+ end
158
+ end
159
+ end
160
+ end