Class: Vagrant::Node::ClientController

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-node/clientcontroller.rb

Class Method Summary collapse

Class Method Details

.authorized?(token, challenge) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/vagrant-node/clientcontroller.rb', line 44

def self.authorized?(token,challenge)
 ensure_environment			 
 
 @pw.authorized?(token,challenge)			 
end

.backup_log(vmname = nil) ⇒ Object

BACKUP LOG METHOD ###############

FIXME No está controlado el que el parámetro sea nil



750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/vagrant-node/clientcontroller.rb', line 750

def self.backup_log(vmname=nil)
	ensure_environment				
	
	#begin
	
		@db.get_backup_log_entries(vmname)
		
	# rescue => e
		# puts e.message									
	# end
	
end

.box_add(box, url, user = "guest", pass = "--no-pass") ⇒ Object

BOX ADD METHOD #######################



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
# File 'lib/vagrant-node/clientcontroller.rb', line 94

def self.box_add(box,url,user="guest",pass="--no-pass")
	command_block = Proc.new {
               				#ensure_environment
               				
               				boxes = []			
               
               				#TODO
               				
               				# Get the provider if one was set
               				provider = nil
               #				provider = options[:provider].to_sym if options[:provider]
               
               				begin
               					
               					#uri = "\\\\155.54.190.227\\boxtmp\\boxes\\debian_squeeze_32.box"
               #					
               #					if uri=~ /^\\\\(.*?)\\(.*?)\\(.*?)$/						
               #						puts "EL HOST ES #{$1}"
               #						puts "EL Share ES #{$2}"
               #						puts "EL PATH ES #{$3}"
               #						host = $1
               #						share = $2
               #						path = $3
               #						
               #						Getting and checking box file						
               #						boxname=File.basename(path.gsub('\\',File::SEPARATOR))
               #						
               #            raise 'Box file format not supported' if File.extname(boxname)!=".box"
               #
               #						samba = nil
               #						begin						
               #						samba = Sambal::Client.new(  :host     =>  host,
               #																				:share    =>  share,
               #																				:user     =>  user,
               #																				:password =>  pass)
               #					
               #						
               #						
               #						Get the tmp file name					
               #						temp_path = @env.tmp_path.join("box" + Time.now.to_i.to_s)
               #				
               #					
               #						response = nil
               #						
               #						smbclient //155.54.190.227/boxtmp --no-pass -W WORKGROUP -U guest -p 445
               #						smbclient //155.54.190.227/boxtmp -D boxes -c "get debian_squeeze_321.box" -N
               #						
               #						command="smbclient //#{host}/#{share} -D #{dirlocation} -c \"get #{boxname}\" -U #{user} --no-pass"
               #						
               #
               #						FIXME encontrar si existe algún tipo de notificación por
               #						interrupciónde la descarga
               #						FIXME a little hack beacuse in version 0.1.2 of sambal there is 
               #						a timeout that close the download after 10 seconds 
               #						def samba.ask(cmd)							
               #							@i.printf("#{cmd}\n")
               #							response = @o.expect(/^smb:.*\\>/)[0]				
               #						end
               #						
               #						response = samba.get(path, temp_path.to_s)
               #						FIXME DELETE
               #						pp response.inspect						
               #						
               #						raise response.message if !response.success?
               #						
               #						if response.success?								
               #								File download succesfully
               #								added_box = nil
               #								begin									
               #									provider=nil
               #									force = true
               #									added_box = @env.boxes.add(temp_path,box,nil,force)									
               #									boxes << {:name=>box,:provider=>added_box.provider.to_s}
               #								rescue Vagrant::Errors::BoxUpgradeRequired									
               #									Upgrade the box
               #									env.boxes.upgrade(box)
               #			
               #									Try adding it again
               #									retry
               #								rescue Exception => e									
               #									boxes = nil
               #								end
               #													
               #						end
               #						
               #						rescue Exception => e
               #							puts "EXCEPCION de descarga" if response
               #							puts "EXCEPCION de conexion" if !response
               #							puts e.message
               #							boxes=nil
               #						end
               #						
               #						
               #						Closing connection
               #						samba.close if samba
               #						
               #						
               #						Cleaning
               #						if temp_path && File.exist?(temp_path)
               #            	File.unlink(temp_path)
               #          	end
               # 
               #          	          		 
               #					else
               
                         
                         
                           boxes <<{:name=>box}
               #						FIXME Ver qué poner en los parámetros de la llamada
               						provider=nil
               						force = true # Always overwrite box if exists
               						insecure = true #Don't validate SSL certs
               						#Calling original box add action
               						@env.action_runner.run(Vagrant::Action.action_box_add, {
                           :box_name     => box,
                           :box_provider => provider,
                           :box_url      => url,
                           :box_force    => force,
                           :box_download_insecure => insecure,
                         	})
               
               #					end
               
               
		     
		     

                                   
           boxes					     
		     
	rescue =>e
			puts e.message
	end

	}
     method("execute_queued").call(&command_block);
	
end

.box_delete(box, provider) ⇒ Object

BOX DELETE METHOD #####################



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vagrant-node/clientcontroller.rb', line 74

def self.box_delete(box,provider)

	ensure_environment
	
	boxes = []			

  box = @env.boxes.find(box,provider.to_sym)
  
  if (box)			  	
  	boxes << box.name			  
  	box.destroy!			  				  	
  end 
					
	boxes
	
end

.config_showObject

CONFIG SHOW METHOD #####################



767
768
769
770
771
# File 'lib/vagrant-node/clientcontroller.rb', line 767

def self.config_show
    ensure_environment			    
    
    file = @env.root_path+"Vagrantfile"
end

.config_upload(cfile) ⇒ Object

CONFIG UPLOAD METHOD #####################



776
777
778
779
780
781
782
783
784
# File 'lib/vagrant-node/clientcontroller.rb', line 776

def self.config_upload(cfile)
    ensure_environment
                        
    f = File.new(@env.root_path+"Vagrantfile", "w")
    f.write(cfile)
    f.close
    
    true
end

.execute_queuedObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-node/clientcontroller.rb', line 18

def self.execute_queued			
  ensure_environment
  #Generating a random identifier for process
  rpid=rand(1000000)
  
  pid = fork do
    begin
     @db.create_queued_process(rpid)			     
     res = yield			     
     @db.set_queued_process_result(rpid,res.to_json)		     
     
    rescue Exception => e
      @db.set_queued_process_error(rpid,e)			      
    end
  end
  rpid
end

.listboxesObject

BOX LIST METHOD #######################



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant-node/clientcontroller.rb', line 53

def self.listboxes
  

			ensure_environment
			
			boxes = @env.boxes.all.sort				
			
			fboxes = Array.new				
			boxes.each do |name, provider|					
				fboxes << {"name" => name,"provider" => provider}
			end
			
			
			fboxes		

	
end

.operation_queued(id) ⇒ Object

Raises:



787
788
789
790
791
792
793
794
# File 'lib/vagrant-node/clientcontroller.rb', line 787

def self.operation_queued(id)          
    ensure_environment
    result = @db.get_queued_process_result(id)          
    
    raise RestException.new(404,"The operation ID: #{id} not found") if result.empty?
    
    result.first 
end

.operation_queued_lastObject



796
797
798
799
800
801
# File 'lib/vagrant-node/clientcontroller.rb', line 796

def self.operation_queued_last          
    ensure_environment
    
    @db.get_queued_last         
     
end

.password_change(new_password) ⇒ Object

NODE PASSWORD CHANGE ###################



739
740
741
742
743
744
# File 'lib/vagrant-node/clientcontroller.rb', line 739

def self.password_change(new_password)
  ensure_environment
  
  @db.node_password_set(new_password,true)
  true
end

.send_challengeObject



36
37
38
39
40
41
42
# File 'lib/vagrant-node/clientcontroller.rb', line 36

def self.send_challenge			  
  challenge = SecureRandom.urlsafe_base64(nil, false)
  cookie = SecureRandom.urlsafe_base64(nil, false)			  
  
  
  {:cookie => cookie, :challenge => challenge}
end

.vm_add(config, rename) ⇒ Object

VIRTUAL MACHINE ADD METHOD ###############



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
# File 'lib/vagrant-node/clientcontroller.rb', line 281

def self.vm_add(config,rename)
  ensure_environment    
  begin  
    path="/tmp/conf."+Time.now.to_i.to_s          
    f = File.new(path, "w")
    f.write(config)
    f.close          
            
             
    cm = ConfigManager.new(@env.root_path+"Vagrantfile")
    cmtmp = ConfigManager.new(path)
    
           
    current_vms = @env.machine_names
    
    cmtmp.get_vm_names.each do |key|             
     
      if current_vms.include?(key)  
        raise RestException.new(406,"There is a remote VM with the same name: \"#{key}\"") if rename=="false"             
        cmtmp.rename_vm(key,"#{key}_1")            
      end    
    end
    
    cm.insert_vms_sexp(cmtmp.extract_vms_sexp)
      
 rescue => e   
   raise e 
 ensure
   File.delete(path)
 end      
  
  
end

.vm_confirmed_destroy(vmname) ⇒ Object

VIRTUAL MACHINE DESTROY METHOD ###############



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/vagrant-node/clientcontroller.rb', line 337

def self.vm_confirmed_destroy(vmname)
  ensure_environment
  
  machine_names = []
  
  
  #begin        
  
    get_vms(vmname.to_sym).each do |machine|                       
      machine_names << machine.name
      machine.action(:destroy, :force_confirm_destroy => true)            
    end
                    
    machine_names
    
  #rescue => e          
#         return nil
  #end
  
end

.vm_delete(vmname, remove = false) ⇒ Object

VIRTUAL MACHINE DELETE METHOD ###############



320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/vagrant-node/clientcontroller.rb', line 320

def self.vm_delete(vmname,remove=false)
  ensure_environment
  
  cm = ConfigManager.new(@env.root_path+"Vagrantfile")         
  
  self.vm_confirmed_destroy(vmname) if remove!=false
  
  cm.delete_vm(vmname) 
  
  true        
  
end

.vm_halt(vmname, force) ⇒ Object

VIRTUAL MACHINE HALT METHOD #################



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/vagrant-node/clientcontroller.rb', line 361

def self.vm_halt(vmname,force)
  command_block = Proc.new {
			#ensure_environment				
		
			machine_names = []

			#begin				
				
				get_vms(vmname).each do |machine|						
					machine.action(:halt, :force_halt => force)
					machine_names << {"vmname" => machine.name.to_s,
														"status" => machine.state.short_description}
				end
						
				machine_names
		}
		
		method("execute_queued").call(&command_block);
	
	#rescue => e					
#					return nil
	#end
	
end

.vm_provision(vmname) ⇒ Object

VIRTUAL MACHINE PROVISION METHOD ##################



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/vagrant-node/clientcontroller.rb', line 635

def self.vm_provision(vmname)
	#ensure_environment
  command_block = Proc.new {
			machine_names = []

      # Por ahora no dejo que el vmname esté vacío para realizar la operación sobre todas las vm
			raise RestException.new(404,"Virtual Machine not specified") if (vmname==nil ||vmname.empty?)
			#begin
			
				#Provisioning								
				get_vms(vmname).each do |machine|										
					machine_names << machine.name	
					machine.action(:provision)					
				end
				           
				
			
				machine_names
	}
	
     method("execute_queued").call(&command_block);	
		
	
end

.vm_resume(vmname) ⇒ Object

VIRTUAL MACHINE RESUME METHOD ##################



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/vagrant-node/clientcontroller.rb', line 441

def self.vm_resume(vmname)
  command_block = Proc.new {
	#ensure_environment

	machine_names = []			
		#Launching machines
					
		get_vms(vmname).each do |machine|
			machine.action(:resume)
			machine_names << {"vmname" => machine.name.to_s,
												"status" => machine.state.short_description}					
		end
		           
		
	
		machine_names
	}
	
	method("execute_queued").call(&command_block);
	
	
end

.vm_snapshot_delete(vmname, snapshot_id) ⇒ Object

VIRTUAL MACHINE SNAPSHOT DELETE METHOD #############



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
# File 'lib/vagrant-node/clientcontroller.rb', line 551

def self.vm_snapshot_delete(vmname,snapshot_id)
  ensure_environment        
  
  #begin
    
    
    raise RestException.new(404,"Virtual Machine not specified") if (vmname==nil ||vmname.empty?)
    
     
         
    get_vms(vmname).each do |machine|
      env = 
      {             
        :machine        => machine,
        :machine_action => SnapshotAction::DELETE,
        :snapshot_id  => snapshot_id,               
      }
#             
#             
      res = @env.action_runner.run(SnapshotAction,env)
#             
      return res[:delete_snapshot]
#             
    end   
#           
  # rescue => e
    # puts e.message
# #         return nil        
  # end
  
end

.vm_snapshot_restore(vmname, snapshot_id) ⇒ Object

VIRTUAL MACHINE SNAPSHOT RESTORE METHOD #############



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
# File 'lib/vagrant-node/clientcontroller.rb', line 588

def self.vm_snapshot_restore(vmname,snapshot_id)
  command_block = Proc.new {
	  #ensure_environment				
	
	  raise RestException.new(404,"Virtual Machine not specified") if (vmname==nil ||vmname.empty?)
	  
	  restore_results = {}
		
						
		get_vms(vmname).each do |machine|				  
		
		  
			prev_state=machine.state.id
			#First, ensure that the machine is in a proper state
			#to restore the snapshot (save, poweroff)
			machine.action(:suspend) if prev_state==:running
			
			#Now the machine is ready for restoration
			env = 
			{							
     			:machine        => machine,
     			:machine_action => SnapshotAction::RESTORE,
     			:snapshot_id 	=> snapshot_id        			   			
			}
			
			
			res = @env.action_runner.run(SnapshotAction,env)
			
			#Now restore the vm to the previous state if running
			machine.action(:up) if prev_state==:running
			
			restore_results[machine.name.to_sym]=res[:restore_result]						
		end		
		
		restore_results
		
		
  }
 method("execute_queued").call(&command_block);
	
end

.vm_snapshot_take(vmname, name, desc = " ") ⇒ Object

VIRTUAL MACHINE SNAPSHOT TAKE METHOD #############



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
# File 'lib/vagrant-node/clientcontroller.rb', line 502

def self.vm_snapshot_take(vmname,name,desc=" ")
  command_block = Proc.new {
	#ensure_environment				
	
		
		raise RestException.new(404,"Virtual Machine not specified") if (vmname==nil ||vmname.empty?)
		
		
		machine = get_vms(vmname)
		raise RestException.new(404,"Virtual Machine not found") if (machine.empty?)
		
        env = 
        {              
           :machine        => machine.first,
           :machine_action => SnapshotAction::TAKE,
           :snapshot_name  => name,
           :snapshot_desc  => desc             
         }
           
         res = @env.action_runner.run(SnapshotAction,env)
           
         res[:last_snapshot]            
         
        		
		# get_vms(vmname).each do |machine|
		 # env = 
		 # {							
     			# :machine        => machine,
     			# :machine_action => SnapshotAction::TAKE,
     			# :snapshot_name 	=> name,
     			# :snapshot_desc  => desc        			
			# }
#  						
			# res = @env.action_runner.run(SnapshotAction,env)
# 						
			# return res[:last_snapshot]						
# 						
		# end
		
		
	}		
		
	method("execute_queued").call(&command_block);
	
end

.vm_snapshot_take_file(vmname) ⇒ Object

VIRTUAL MACHINE BACKUP TAKE METHOD #############



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/vagrant-node/clientcontroller.rb', line 693

def self.vm_snapshot_take_file(vmname)
	ensure_environment
	
	current_machine = nil
	t = Time.now.strftime "%Y-%m-%d %H:%M:%S"
	begin
	  
		machines=get_vms(vmname)
		
		return [404,"Virtual Machine not found"] if machines.empty?
							
		machines.each do |machine|						
			
			current_machine = machine.name.to_s						
			
			env = 
			{							
				:machine        => machine,
				:machine_action => SnapshotAction::BACKUP,
				:path						=> @env.data_dir
			}
			
			@db.add_backup_log_entry(t,current_machine,BACKUP_IN_PROGRESS)
		
			res = @env.action_runner.run(SnapshotAction,env)
			
			if res[:bak_filename] == SnapshotAction::ERROR
				@db.update_backup_log_entry(t,current_machine,BACKUP_ERROR)
				return [500,"Internal Error"] if res[:bak_filename] == SnapshotAction::ERROR
			else					
				@db.update_backup_log_entry(t,current_machine,BACKUP_SUCCESS)
				return [200,res[:bak_filename]]
			end
			
		end	
				
	rescue => e					
		@db.update_backup_log_entry(t,current_machine,BACKUP_ERROR)
		return [500,"Internal Error"]				
	end
	
end

.vm_snapshots(vmname = nil) ⇒ Object

VIRTUAL MACHINE SNAPSHOT LIST METHOD #############



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
# File 'lib/vagrant-node/clientcontroller.rb', line 467

def self.vm_snapshots(vmname=nil)
	ensure_environment				
	
	#begin
	
		snapshots = {}
							
		get_vms(vmname).each do |machine|
			
			env = 
			{							
     			:machine        => machine,
     			:machine_action => SnapshotAction::LIST
			}
			
			
			res = @env.action_runner.run(SnapshotAction,env)
			
			snapshots[machine.name.to_sym]=res[:snapshots_list]
			
		end		
	
	  
		snapshots
				
	# rescue => e
		# puts e.message
# #					return nil				
	# end
	
end

.vm_ssh_config(vmname) ⇒ Object

VIRTUAL MACHINE SSHCONFIG## ###############



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/vagrant-node/clientcontroller.rb', line 667

def self.vm_ssh_config(vmname)
	ensure_environment
	
	
	#Ensure vmname exists and it is not empty
	return nil if vmname.empty?
		
	
	#begin
		info = Array.new
		get_vms(vmname).each do |machine|												
			info << machine.ssh_info						
		end
		
		info[0]
		
	# rescue => e
		# puts e.message
#					return nil
	#end	

end

.vm_status(vmname = nil) ⇒ Object

VIRTUAL MACHINE STATUS METHOD ###############



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/vagrant-node/clientcontroller.rb', line 389

def self.vm_status(vmname=nil)
	ensure_environment				
	
	
		
	status = Array.new
						
	get_vms(vmname).each do |machine|
			
		status << {"name" => machine.name.to_s,
					"status" => machine.state.short_description,
					"provider" => machine.provider_name}
	end		


	status
				
	
	
end

.vm_suspend(vmname) ⇒ Object

VIRTUAL MACHINE SUSPEND METHOD ##################



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/vagrant-node/clientcontroller.rb', line 413

def self.vm_suspend(vmname)
  command_block = Proc.new {
	  #ensure_environment

	  machine_names = []

	
	
		
		#Suspendiing machines								
		get_vms(vmname).each do |machine|
			machine.action(:suspend)
			machine_names << {"vmname" => machine.name.to_s,
													"status" => machine.state.short_description}
		end           
		
	
	  machine_names
	  
	}
		
	method("execute_queued").call(&command_block);
	
end

.vm_up(vmname) ⇒ Object

VIRTUAL MACHINE UP METHOD ##################



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
# File 'lib/vagrant-node/clientcontroller.rb', line 238

def self.vm_up(vmname)
  command_block = Proc.new {
	#ensure_environment
		
	machine_names = []
	
	#begin
      
		options = {}
		options[:parallel] = true
	
		#Launching machines
		@env.batch(options[:parallel]) do |batch|			
			get_vms(vmname).each do |machine|
				batch.action(machine, :up, options)							
				machine_names << {"vmname" => machine.name.to_s,
													"status" => "running"}												
			end
		end           
		
		
		machine_names
		}
		
		method("execute_queued").call(&command_block);
		
	# rescue Vagrant::Errors::BoxNotFound => e
	  # puts "BOX NOT FOUND"
	# rescue => e
	  # pp e
	  # puts e.message
	  # raise RestException.new(404,e.message)								 
	# end
	
end