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<SaveNeighbor> 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<SaveNeighbor>() ;
54 clientInVM = "/home/mpi/InGuest" ;
56 vm_user_passwd = "mpi" ;
63 public void setDeployFault( boolean _bool )
69 public ArrayList<SaveNeighbor> getSaveNeighbors() { return save_neighbors ; }
71 @SuppressWarnings("unchecked")
72 public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
76 save_neighbors = (ArrayList<SaveNeighbor>) _sn.clone() ;
80 public void addSaveNeighbor( SaveNeighbor _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()
390 deployFault = false ;
392 // Initial deployment
393 File file = new File( working_directory + "/" + directory + "/initial.hpcvm" ) ;
402 file = new File( working_directory + "/" + directory + "/first.hpcvm" ) ;
409 // Redeployment after a fault
411 file = new File( working_directory + "/" + directory + "/fault.hpcvm" ) ;
423 if( ! deploy && ! first && ! deployFault )
428 // Deployment of the VM
432 System.out.print( "Saving the initial VMX file ... " ) ;
434 String[] command = new String[]{ "/bin/cp",
435 working_directory + "/" + directory + "/" + vmx_name,
436 working_directory + "/" + directory + "/" + vmx_name_initial } ;
439 Process p = Runtime.getRuntime().exec( command ) ;
442 if( p.exitValue() == 0 )
444 System.out.println( "Successfully saved initial the VMX file." ) ;
446 System.err.println( "Unsuccessful save of the initial VMX file!" ) ;
447 printProcessError( p ) ;
450 } catch( IOException e ) {
451 System.err.println( "Error during initial VMX file save: " ) ;
452 e.printStackTrace() ;
454 } catch( InterruptedException e ) {
455 e.printStackTrace() ;
459 System.out.print( "Rewritting WMX file ... " ) ;
462 FileReader fin = null ;
464 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_initial ) );
465 } catch( FileNotFoundException e ) {
466 System.err.println( "Unable to open the initial VMX file!" ) ;
467 e.printStackTrace() ;
471 BufferedReader bin = new BufferedReader( fin ) ;
473 FileWriter fout = null ;
476 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
477 } catch( IOException e ) {
478 System.err.println( "Unable to open the VMX file!" ) ;
479 e.printStackTrace() ;
483 // Keeping only some information
484 // -- this force VmWare to generate new identifiers and MAC address
485 while( line != null )
488 line = bin.readLine() ;
490 if( ! line.contains( "ethernet0.addressType" )
491 && ! line.contains( "uuid.location " )
492 && ! line.contains( "uuid.bios " )
493 && ! line.contains( "ethernet0.generatedAddress " )
494 && ! line.contains( "ethernet0.generatedAddressOffset " )
497 fout.write( line + "\n" ) ;
499 } catch( IOException e ) {}
504 } catch (IOException e) {
505 System.err.println( "Unable to close the initial VMX file!" ) ;
506 e.printStackTrace() ;
509 // Adding the copy information
512 fout.write( "msg.autoAnswer = \"TRUE\"\n" ) ;
513 fout.write( "answer.msg.checkpoint.cpufeaturecheck.fail = \"_Yes\"\n" ) ;
514 fout.write( "checkpoint.vmState = \"\"\n" ) ;
515 fout.write( "answer.msg.uuid.altered = \"I _copied it\"\n" ) ;
516 } catch( IOException e ) {
517 System.err.println( "Unable to add infortion to VMX file!" ) ;
518 e.printStackTrace() ;
524 } catch( IOException e ) {
525 System.err.println( "Unable to close the VMX file!" ) ;
526 e.printStackTrace() ;
530 System.out.println( "Successful rewrite of the VMX file." ) ;
532 // Removing the initial mark
533 System.out.print( "Removing the deployment mark ... " ) ;
535 command = new String[]{ "/bin/rm",
536 working_directory + "/" + directory + "/initial.hpcvm" } ;
539 Process p = Runtime.getRuntime().exec( command ) ;
542 if( p.exitValue() == 0 )
544 System.out.println( "Successful deletion of the deployment mark." ) ;
546 System.err.println( "Unsuccessful deletion of the deployment mark!" ) ;
547 printProcessError( p ) ;
550 } catch( IOException e ) {
551 System.err.println( "Error during deletion of the deployment mark: " ) ;
552 e.printStackTrace() ;
554 } catch( InterruptedException e ) {
555 e.printStackTrace() ;
561 // Writing the first start mark
563 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
564 fw.write( "first!\n" ) ;
569 } catch( IOException e1 ) {
570 e1.printStackTrace() ;
571 System.err.println( "Unable to mark the first run of the VM!" ) ;
576 // First start of the VM
580 System.out.print( "Saving the first start VMX file ... " ) ;
582 String[] command = new String[]{ "/bin/cp",
583 working_directory + "/" + directory + "/" + vmx_name,
584 working_directory + "/" + directory + "/" + vmx_name_first } ;
587 Process p = Runtime.getRuntime().exec( command ) ;
590 if( p.exitValue() == 0 )
592 System.out.println( "Successfully saved the first start VMX file." ) ;
594 System.err.println( "Unsuccessful save of the first start VMX file!" ) ;
595 printProcessError( p ) ;
598 } catch( IOException e ) {
599 System.err.println( "Error during first start VMX file save: " ) ;
600 e.printStackTrace() ;
602 } catch( InterruptedException e ) {
603 e.printStackTrace() ;
607 System.out.print( "Rewritting WMX file ... " ) ;
610 FileReader fin = null ;
612 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
613 } catch( FileNotFoundException e ) {
614 System.err.println( "Unable to open the first start VMX file!" ) ;
615 e.printStackTrace() ;
619 BufferedReader bin = new BufferedReader( fin ) ;
621 FileWriter fout = null ;
624 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
625 } catch( IOException e ) {
626 System.err.println( "Unable to open the VMX file!" ) ;
627 e.printStackTrace() ;
631 // Keeping all information
632 // -- except copy information
633 while( line != null )
636 line = bin.readLine() ;
638 if( ! line.contains( "answer.msg.uuid.altered " ) )
640 fout.write( line + "\n" ) ;
642 } catch( IOException e ) {}
647 } catch (IOException e) {
648 System.err.println( "Unable to close the first start VMX file!" ) ;
649 e.printStackTrace() ;
654 } catch( IOException e ) {
655 System.err.println( "Unable to close the VMX file!" ) ;
656 e.printStackTrace() ;
660 System.out.println( "Successful rewrite of the VMX file." ) ;
662 // Removing the initial mark
663 System.out.print( "Removing the first start mark ... " ) ;
665 command = new String[]{ "/bin/rm",
666 working_directory + "/" + directory + "/first.hpcvm" } ;
669 Process p = Runtime.getRuntime().exec( command ) ;
672 if( p.exitValue() == 0 )
674 System.out.println( "Successful deletion of the first start mark." ) ;
676 System.err.println( "Unsuccessful deletion of the first start mark!" ) ;
677 printProcessError( p ) ;
680 } catch( IOException e ) {
681 System.err.println( "Error during deletion of the first start mark: " ) ;
682 e.printStackTrace() ;
684 } catch( InterruptedException e ) {
685 e.printStackTrace() ;
695 // Redeployment after a fault of a VM
699 System.out.print( "Saving the original VMX file ... " ) ;
701 String[] command = new String[]{ "/bin/cp",
702 working_directory + "/" + directory + "/" + vmx_name,
703 working_directory + "/" + directory + "/" + vmx_name_first } ;
706 Process p = Runtime.getRuntime().exec( command ) ;
709 if( p.exitValue() == 0 )
711 System.out.println( "Successfully saved the original VMX file." ) ;
713 System.err.println( "Unsuccessful save of the original VMX file!" ) ;
714 printProcessError( p ) ;
717 } catch( IOException e ) {
718 System.err.println( "Error during original VMX file save: " ) ;
719 e.printStackTrace() ;
721 } catch( InterruptedException e ) {
722 e.printStackTrace() ;
726 System.out.print( "Rewritting WMX file ... " ) ;
729 FileReader fin = null ;
731 fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
732 } catch( FileNotFoundException e ) {
733 System.err.println( "Unable to open the original VMX file!" ) ;
734 e.printStackTrace() ;
738 BufferedReader bin = new BufferedReader( fin ) ;
740 FileWriter fout = null ;
743 fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
744 } catch( IOException e ) {
745 System.err.println( "Unable to open the VMX file!" ) ;
746 e.printStackTrace() ;
750 // Keeping all information
751 while( line != null )
754 line = bin.readLine() ;
756 if( ! line.contains( "answer.msg.uuid.altered " ) )
758 fout.write( line + "\n" ) ;
760 } catch( IOException e ) {}
765 } catch (IOException e) {
766 System.err.println( "Unable to close the original VMX file!" ) ;
767 e.printStackTrace() ;
770 // Writing information to indicate that the VM has been moved
772 fout.write( "answer.msg.uuid.altered = \"I _moved it\"\n" ) ;
773 } catch (IOException e1) {
774 System.err.println( "Unable to indicate that the VM has been moved in the VMX file!" ) ;
775 e1.printStackTrace() ;
780 } catch( IOException e ) {
781 System.err.println( "Unable to close the VMX file!" ) ;
782 e.printStackTrace() ;
786 System.out.println( "Successful rewrite of the VMX file." ) ;
788 // Removing lock files
789 System.out.print( "Removing lock files ... " ) ;
791 command = new String[]{ "/bin/rm", "-rf",
792 working_directory + "/" + directory
793 + "/" + vmx_name + ".lck" } ;
796 Process p = Runtime.getRuntime().exec( command ) ;
799 if( p.exitValue() == 0 )
801 System.out.println( "Successfully deleted lock files." ) ;
803 System.err.println( "Unsuccessful deletion of lock files!" ) ;
804 // printProcessError( p.getErrorStream() ) ;
805 printProcessError( p ) ;
809 } catch( IOException e ) {
810 System.err.println( "Error during lock files deletion: " ) ;
811 e.printStackTrace() ;
813 } catch( InterruptedException e ) {
814 e.printStackTrace() ;
818 // Removing the initial mark
819 System.out.print( "Removing the fault mark ... " ) ;
821 command = new String[]{ "/bin/rm",
822 working_directory + "/" + directory + "/fault.hpcvm" } ;
825 Process p = Runtime.getRuntime().exec( command ) ;
828 if( p.exitValue() == 0 )
830 System.out.println( "Successful deletion of the fault mark." ) ;
832 System.err.println( "Unsuccessful deletion of the fault mark!" ) ;
833 printProcessError( p ) ;
836 } catch( IOException e ) {
837 System.err.println( "Error during deletion of the fault mark: " ) ;
838 e.printStackTrace() ;
840 } catch( InterruptedException e ) {
841 e.printStackTrace() ;
845 deployFault = false ;
847 // Writing the first start mark
849 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
850 fw.write( "first!" ) ;
856 } catch( IOException e1 ) {
857 e1.printStackTrace() ;
858 System.err.println( "Unable to mark the first run of the VM!" ) ;
866 private void printProcessError( Process _p )
870 System.err.println( "Error: " + _p.exitValue() ) ;
871 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
874 while( (line = br.readLine()) != null )
876 System.err.println( line ) ;
878 } catch( IOException e ) {
879 e.printStackTrace() ;
887 /** La programmation est un art, respectons ceux qui la pratiquent !! **/