3 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.util.ArrayList;
12 public class VirtualMachine
16 private String initial_archive_name ;
17 private String directory ;
18 private String vmx_name ;
19 private String vmx_name_first ;
20 private String vmx_name_initial ;
21 private String save_current ;
22 private String save_last ;
24 private int no_save_last ;
25 private Status status ;
26 private int computation_id ;
27 private String working_directory ;
28 private ArrayList<ServicesClient> save_neighbors ;
29 private String clientInVM ;
30 private String vm_user ;
31 private String vm_user_passwd ;
32 private boolean deploy ;
33 private boolean first ;
34 private boolean deployFault ;
36 public VirtualMachine()
40 initial_archive_name = "VmTest.tgz" ;
41 directory = "VmTest" ;
42 vmx_name = "VmTest.vmx" ;
43 vmx_name_first = "VmTest.vmx.first" ;
44 vmx_name_initial = "VmTest.vmx.initial" ;
45 save_current = "VmTest.tgz" ;
46 save_last = "VmTest.tgz" ;
47 working_directory = "/localhome/vmware" ;
50 status = new Status() ;
51 status.setStatus( "stopped" ) ;
53 save_neighbors = new ArrayList<ServicesClient>() ;
54 clientInVM = "/home/mpi/InGuest" ;
56 vm_user_passwd = "mpi" ;
63 public void setDeployFault( boolean _bool )
69 public ArrayList<ServicesClient> getSaveNeighbors() { return save_neighbors ; }
71 @SuppressWarnings("unchecked")
72 public void setSaveNeighbors( ArrayList<ServicesClient> _sn )
76 save_neighbors = (ArrayList<ServicesClient>) _sn.clone() ;
80 public void addSaveNeighbor( ServicesClient _sn )
84 save_neighbors.add( _sn ) ;
89 public String getVmUser()
95 public void setVmUser( String _user )
101 public String getVmUserPasswd()
103 return vm_user_passwd ;
107 public void setVmUserPasswd( String _user_passwd )
109 vm_user_passwd = _user_passwd ;
113 public String getClientInVM()
119 public void setClientInVM( String _civ )
125 public String getStatus()
127 return status.getStatus() ;
131 public void setStatus( String _status )
133 status.setStatus( _status ) ;
137 public String getWorkingDirectory()
139 return working_directory ;
143 public void setWorkingDirectory( String _wd )
145 working_directory = _wd ;
148 public String getName() {
153 public void setName(String name) {
157 public String getIp() {
161 public void setIp(String ip) {
165 public String getInitial_archive_name() {
166 return initial_archive_name;
169 public void setInitial_archive_name(String initial_archive_name) {
170 this.initial_archive_name = initial_archive_name;
173 public String getDirectory() {
177 public void setDirectory(String directory) {
178 this.directory = directory;
181 public String getVmx_name() {
185 public void setVmx_name(String vmx_name) {
186 this.vmx_name = vmx_name;
189 // public String getVmx_name_normal() {
190 // return vmx_name_normal;
193 // public void setVmx_name_normal(String vmx_name_normal) {
194 // this.vmx_name_normal = vmx_name_normal;
197 // public String getVmx_name_crash() {
198 // return vmx_name_crash;
201 // public void setVmx_name_crash(String vmx_name_crash) {
202 // this.vmx_name_crash = vmx_name_crash;
205 public String getSave_current() {
209 public void setSave_current(String save_current) {
210 this.save_current = save_current;
213 public String getSave_last() {
217 public void setSave_last(String save_last) {
218 System.out.println( "Save name: " + save_last ) ;
219 this.save_last = save_last;
222 public int getNo_save()
227 public void setNo_save( int no_save )
229 this.no_save = no_save ;
232 public int getNo_save_last()
234 return no_save_last ;
237 public void setNo_save_last( int no_save_last )
239 this.no_save_last = no_save_last ;
242 public int getComputationId() { return computation_id ; }
245 public void setComputationId( int _id )
247 computation_id = _id ;
251 public int deployLastSave()
253 System.out.print( "Removing current VM ... " ) ;
255 String[] command = new String[] { "/bin/rm", "-rf",
256 working_directory + "/" + directory } ;
259 pr = Runtime.getRuntime().exec( command ) ;
260 // synchronized( pr ){
262 } catch( IOException e ) {
263 System.err.println( "Error while removing current VM!" ) ;
264 e.printStackTrace() ;
266 } catch( InterruptedException e ) {
267 e.printStackTrace() ;
271 if( pr.exitValue() == 0 )
273 System.out.println( "Successful deletion of current VM." ) ;
275 System.err.println( "Unssuccessful deletion of current VM!" ) ;
276 printProcessError( pr ) ;
279 System.out.print( "Deploying the last save ... " ) ;
281 command = new String[] { "/bin/tar", "-xzf",
282 working_directory + "/" + save_last,
283 "-C", working_directory } ;
286 pr = Runtime.getRuntime().exec( command ) ;
287 // synchronized( pr ){
289 } catch( IOException e ) {
290 System.err.println( "Error while deploying the last secure save!" ) ;
291 e.printStackTrace() ;
293 } catch( InterruptedException e ) {
294 e.printStackTrace() ;
299 if( pr.exitValue() == 0 )
301 System.out.println( "Successful extraction of the save archive." ) ;
304 System.err.println( "unSuccessful extraction of the save archive!" ) ;
305 printProcessError( pr ) ;
312 public int deployInitialVM()
314 System.out.print( "Removing current VM ... " ) ;
316 String[] command = new String[] { "/bin/rm", "-rf",
317 working_directory + "/" + directory } ;
320 pr = Runtime.getRuntime().exec( command ) ;
321 // synchronized( pr ){
323 } catch( IOException e ) {
324 System.err.println( "Error while removing current VM!" ) ;
325 e.printStackTrace() ;
327 } catch( InterruptedException e ) {
328 e.printStackTrace() ;
332 if( pr.exitValue() == 0 )
334 System.out.println( "Successful deletion of current VM." ) ;
336 System.err.println( "Unsuccessful deletion of current VM!" ) ;
337 printProcessError( pr ) ;
340 System.out.print( "Deploying the initial archive ... " ) ;
342 command = new String[] { "/bin/tar", "-xzf",
343 working_directory + "/" + save_last,
344 "-C", working_directory } ;
347 pr = Runtime.getRuntime().exec( command ) ;
348 // synchronized( pr ){
350 } catch( IOException e ) {
351 System.err.println( "Error while deploying the initial archive!" ) ;
352 e.printStackTrace() ;
354 } catch( InterruptedException e ) {
355 e.printStackTrace() ;
360 if( pr.exitValue() == 0 )
362 System.out.println( "Successful extraction of the initial archive." ) ;
365 System.err.println( "Unsuccessful extraction of the initial archive!" ) ;
366 printProcessError( pr ) ;
371 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/initial.hpcvm" ) ) ;
372 fw.write( "initial!" ) ;
377 } catch( IOException e1 ) {
378 e1.printStackTrace() ;
379 System.err.println( "Unable to mark the initial deployment!" ) ;
386 public int checkVmx()
388 // Initial deployment
389 File file = new File( working_directory + "/" + directory + "/initial.hpcvm" ) ;
398 file = new File( working_directory + "/" + directory + "/first.hpcvm" ) ;
405 // Redeployment after a fault
407 file = new File( working_directory + "/" + directory + "/fault.hpcvm" ) ;
418 // Deployment of the VM
422 System.out.print( "Saving the initial VMX file ... " ) ;
424 String[] command = new String[]{ "/bin/cp",
425 working_directory + "/" + directory + "/" + vmx_name,
426 working_directory + "/" + directory + "/" + vmx_name_initial } ;
429 Process p = Runtime.getRuntime().exec( command ) ;
432 if( p.exitValue() == 0 )
434 System.out.println( "Successfully saved initial the VMX file." ) ;
436 System.err.println( "Unsuccessful save of the initial VMX file!" ) ;
437 printProcessError( p ) ;
440 } catch( IOException e ) {
441 System.err.println( "Error during initial VMX file save: " ) ;
442 e.printStackTrace() ;
444 } catch( InterruptedException e ) {
445 e.printStackTrace() ;
449 System.out.print( "Rewritting WMX file ... " ) ;
452 FileReader fin = null ;
454 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_initial ) );
455 } catch( FileNotFoundException e ) {
456 System.err.println( "Unable to open the initial VMX file!" ) ;
457 e.printStackTrace() ;
461 BufferedReader bin = new BufferedReader( fin ) ;
463 FileWriter fout = null ;
466 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
467 } catch( IOException e ) {
468 System.err.println( "Unable to open the VMX file!" ) ;
469 e.printStackTrace() ;
473 // Keeping only some information
474 // -- this force VmWare to generate new identifiers and MAC address
475 while( line != null )
478 line = bin.readLine() ;
480 if( ! line.contains( "ethernet0.addressType" )
481 && ! line.contains( "uuid.location " )
482 && ! line.contains( "uuid.bios " )
483 && ! line.contains( "ethernet0.generatedAddress " )
484 && ! line.contains( "ethernet0.generatedAddressOffset " )
487 fout.write( line + "\n" ) ;
489 } catch( IOException e ) {}
494 } catch (IOException e) {
495 System.err.println( "Unable to close the initial VMX file!" ) ;
496 e.printStackTrace() ;
499 // Adding the copy information
502 fout.write( "msg.autoAnswer = \"TRUE\"\n" ) ;
503 fout.write( "answer.msg.checkpoint.cpufeaturecheck.fail = \"_Yes\"\n" ) ;
504 fout.write( "checkpoint.vmState = \"\"\n" ) ;
505 fout.write( "answer.msg.uuid.altered = \"I _copied it\"\n" ) ;
506 } catch( IOException e ) {
507 System.err.println( "Unable to add infortion to VMX file!" ) ;
508 e.printStackTrace() ;
514 } catch( IOException e ) {
515 System.err.println( "Unable to close the VMX file!" ) ;
516 e.printStackTrace() ;
520 System.out.println( "Successful rewrite of the VMX file." ) ;
522 // Removing the initial mark
523 System.out.print( "Removing the deployment mark ... " ) ;
525 command = new String[]{ "/bin/rm",
526 working_directory + "/" + directory + "/initial.hpcvm" } ;
529 Process p = Runtime.getRuntime().exec( command ) ;
532 if( p.exitValue() == 0 )
534 System.out.println( "Successfully deletion of the deployment mark." ) ;
536 System.err.println( "Unsuccessful deletion of the deployment mark!" ) ;
537 printProcessError( p ) ;
540 } catch( IOException e ) {
541 System.err.println( "Error during deletion of the deployment mark: " ) ;
542 e.printStackTrace() ;
544 } catch( InterruptedException e ) {
545 e.printStackTrace() ;
551 // Writing the first start mark
553 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
554 fw.write( "first!\n" ) ;
559 } catch( IOException e1 ) {
560 e1.printStackTrace() ;
561 System.err.println( "Unable to mark the first run of the VM!" ) ;
566 // First start of the VM
570 System.out.print( "Saving the first start VMX file ... " ) ;
572 String[] command = new String[]{ "/bin/cp",
573 working_directory + "/" + directory + "/" + vmx_name,
574 working_directory + "/" + directory + "/" + vmx_name_first } ;
577 Process p = Runtime.getRuntime().exec( command ) ;
580 if( p.exitValue() == 0 )
582 System.out.println( "Successfully saved the first start VMX file." ) ;
584 System.err.println( "Unsuccessful save of the first start VMX file!" ) ;
585 printProcessError( p ) ;
588 } catch( IOException e ) {
589 System.err.println( "Error during first start VMX file save: " ) ;
590 e.printStackTrace() ;
592 } catch( InterruptedException e ) {
593 e.printStackTrace() ;
597 System.out.print( "Rewritting WMX file ... " ) ;
600 FileReader fin = null ;
602 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
603 } catch( FileNotFoundException e ) {
604 System.err.println( "Unable to open the first start VMX file!" ) ;
605 e.printStackTrace() ;
609 BufferedReader bin = new BufferedReader( fin ) ;
611 FileWriter fout = null ;
614 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
615 } catch( IOException e ) {
616 System.err.println( "Unable to open the VMX file!" ) ;
617 e.printStackTrace() ;
621 // Keeping all information
622 // -- except copy information
623 while( line != null )
626 line = bin.readLine() ;
628 if( ! line.contains( "answer.msg.uuid.altered " ) )
630 fout.write( line + "\n" ) ;
632 } catch( IOException e ) {}
637 } catch (IOException e) {
638 System.err.println( "Unable to close the first start VMX file!" ) ;
639 e.printStackTrace() ;
644 } catch( IOException e ) {
645 System.err.println( "Unable to close the VMX file!" ) ;
646 e.printStackTrace() ;
650 System.out.println( "Successful rewrite of the VMX file." ) ;
652 // Removing the initial mark
653 System.out.print( "Removing the first start mark ... " ) ;
655 command = new String[]{ "/bin/rm",
656 working_directory + "/" + directory + "/first.hpcvm" } ;
659 Process p = Runtime.getRuntime().exec( command ) ;
662 if( p.exitValue() == 0 )
664 System.out.println( "Successfully deletion of the first start mark." ) ;
666 System.err.println( "Unsuccessful deletion of the first start mark!" ) ;
667 printProcessError( p ) ;
670 } catch( IOException e ) {
671 System.err.println( "Error during deletion of the first start mark: " ) ;
672 e.printStackTrace() ;
674 } catch( InterruptedException e ) {
675 e.printStackTrace() ;
685 // Redeployment after a fault of a VM
689 System.out.print( "Saving the original VMX file ... " ) ;
691 String[] command = new String[]{ "/bin/cp",
692 working_directory + "/" + directory + "/" + vmx_name,
693 working_directory + "/" + directory + "/" + vmx_name_first } ;
696 Process p = Runtime.getRuntime().exec( command ) ;
699 if( p.exitValue() == 0 )
701 System.out.println( "Successfully saved the original VMX file." ) ;
703 System.err.println( "Unsuccessful save of the original VMX file!" ) ;
704 printProcessError( p ) ;
707 } catch( IOException e ) {
708 System.err.println( "Error during original VMX file save: " ) ;
709 e.printStackTrace() ;
711 } catch( InterruptedException e ) {
712 e.printStackTrace() ;
716 System.out.print( "Rewritting WMX file ... " ) ;
719 FileReader fin = null ;
721 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
722 } catch( FileNotFoundException e ) {
723 System.err.println( "Unable to open the original VMX file!" ) ;
724 e.printStackTrace() ;
728 BufferedReader bin = new BufferedReader( fin ) ;
730 FileWriter fout = null ;
733 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
734 } catch( IOException e ) {
735 System.err.println( "Unable to open the VMX file!" ) ;
736 e.printStackTrace() ;
740 // Keeping all information
741 while( line != null )
744 line = bin.readLine() ;
746 if( ! line.contains( "answer.msg.uuid.altered " ) )
748 fout.write( line + "\n" ) ;
750 } catch( IOException e ) {}
755 } catch (IOException e) {
756 System.err.println( "Unable to close the original VMX file!" ) ;
757 e.printStackTrace() ;
760 // Writing information to indicate that the VM has been moved
762 fout.write( "answer.msg.uuid.altered = \"I _moved it\"\n" ) ;
763 } catch (IOException e1) {
764 System.err.println( "Unable to indicate that the VM has been moved in the VMX file!" ) ;
765 e1.printStackTrace() ;
770 } catch( IOException e ) {
771 System.err.println( "Unable to close the VMX file!" ) ;
772 e.printStackTrace() ;
776 System.out.println( "Successful rewrite of the VMX file." ) ;
778 // Removing lock files
779 System.out.print( "Removing lock files ... " ) ;
781 command = new String[]{ "/bin/rm", "-rf",
782 working_directory + "/" + directory
783 + "/" + vmx_name + ".lck" } ;
786 Process p = Runtime.getRuntime().exec( command ) ;
789 if( p.exitValue() == 0 )
791 System.out.println( "Successfully deleted lock files." ) ;
793 System.err.println( "Unsuccessful deletion of lock files!" ) ;
794 // printProcessError( p.getErrorStream() ) ;
795 printProcessError( p ) ;
799 } catch( IOException e ) {
800 System.err.println( "Error during lock files deletion: " ) ;
801 e.printStackTrace() ;
803 } catch( InterruptedException e ) {
804 e.printStackTrace() ;
808 // Removing the initial mark
809 System.out.print( "Removing the fault mark ... " ) ;
811 command = new String[]{ "/bin/rm",
812 working_directory + "/" + directory + "/fault.hpcvm" } ;
815 Process p = Runtime.getRuntime().exec( command ) ;
818 if( p.exitValue() == 0 )
820 System.out.println( "Successfully deletion of the fault mark." ) ;
822 System.err.println( "Unsuccessful deletion of the fault mark!" ) ;
823 printProcessError( p ) ;
826 } catch( IOException e ) {
827 System.err.println( "Error during deletion of the fault mark: " ) ;
828 e.printStackTrace() ;
830 } catch( InterruptedException e ) {
831 e.printStackTrace() ;
835 deployFault = false ;
837 // Writing the first start mark
839 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
840 fw.write( "first!" ) ;
846 } catch( IOException e1 ) {
847 e1.printStackTrace() ;
848 System.err.println( "Unable to mark the first run of the VM!" ) ;
856 private void printProcessError( Process _p )
860 System.err.println( "Error: " + _p.exitValue() ) ;
861 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
864 while( (line = br.readLine()) != null )
866 System.err.println( line ) ;
868 } catch( IOException e ) {
869 e.printStackTrace() ;
877 /** La programmation est un art, respectons ceux qui la pratiquent !! **/