Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction and modification of save mechanisms.
[hpcvm.git] / src / and / hpcvm / VirtualMachine.java
1 package and.hpcvm ;
2
3 import java.io.BufferedReader;
4 import java.io.File;
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;
11
12 public class VirtualMachine 
13 {       
14         private String name ;
15         private String ip ;
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 ;
23         private int no_save ;
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 ;
35         
36         public VirtualMachine()
37         {
38                 name = "VmTest" ;
39                 ip = "127.0.0.1" ;
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" ;
48                 no_save = 0 ;
49                 no_save_last = 0 ;
50                 status = new Status() ;
51                 status.setStatus( "stopped" ) ;
52                 computation_id  = -1 ;
53                 save_neighbors = new ArrayList<SaveNeighbor>() ;
54                 clientInVM = "/home/mpi/InGuest" ;
55                 vm_user = "mpi" ;
56                 vm_user_passwd = "mpi" ;
57                 first = false ;
58                 deploy = false ;
59                 deployFault = false ;
60         }
61         
62         
63         public void setDeployFault( boolean _bool )
64         {
65                 deployFault = _bool ;
66         }
67         
68         
69         public ArrayList<SaveNeighbor> getSaveNeighbors() { return save_neighbors ; }
70         
71         @SuppressWarnings("unchecked")
72         public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
73         {
74                 if( _sn != null ) 
75                 {
76                         save_neighbors = (ArrayList<SaveNeighbor>) _sn.clone() ;
77                 }
78         }
79         
80         public void addSaveNeighbor( SaveNeighbor _sn )
81         {
82                 if( _sn != null )
83                 {
84                         save_neighbors.add( _sn ) ;
85                 }
86         }
87         
88         
89         public String getVmUser()
90         {
91                 return vm_user ;
92         }
93         
94         
95         public void setVmUser( String _user )
96         {
97                 vm_user = _user ;
98         }
99         
100         
101         public String getVmUserPasswd()
102         {
103                 return vm_user_passwd ;
104         }
105         
106         
107         public void setVmUserPasswd( String _user_passwd )
108         {
109                 vm_user_passwd = _user_passwd ;
110         }
111         
112         
113         public String getClientInVM()
114         {
115                 return clientInVM ;
116         }
117         
118         
119         public void setClientInVM( String _civ )
120         {
121                 clientInVM = _civ ;
122         }
123         
124         
125         public String getStatus()
126         {
127                 return status.getStatus() ;
128         }
129         
130         
131         public void setStatus( String _status )
132         {
133                 status.setStatus( _status ) ;
134         }
135         
136         
137         public String getWorkingDirectory()
138         {
139                 return working_directory ;
140         }
141         
142         
143         public void setWorkingDirectory( String _wd )
144         {
145                 working_directory = _wd ;
146         }
147
148         public String getName() {
149                 return name;
150         }
151
152         
153         public void setName(String name) {
154                 this.name = name;
155         }
156
157         public String getIp() {
158                 return ip;
159         }
160
161         public void setIp(String ip) {
162                 this.ip = ip;
163         }
164
165         public String getInitial_archive_name() {
166                 return initial_archive_name;
167         }
168
169         public void setInitial_archive_name(String initial_archive_name) {
170                 this.initial_archive_name = initial_archive_name;
171         }
172
173         public String getDirectory() {
174                 return directory;
175         }
176
177         public void setDirectory(String directory) {
178                 this.directory = directory;
179         }
180
181         public String getVmx_name() {
182                 return vmx_name;
183         }
184
185         public void setVmx_name(String vmx_name) {
186                 this.vmx_name = vmx_name;
187         }
188
189 //      public String getVmx_name_normal() {
190 //              return vmx_name_normal;
191 //      }
192
193 //      public void setVmx_name_normal(String vmx_name_normal) {
194 //              this.vmx_name_normal = vmx_name_normal;
195 //      }
196
197 //      public String getVmx_name_crash() {
198 //              return vmx_name_crash;
199 //      }
200
201 //      public void setVmx_name_crash(String vmx_name_crash) {
202 //              this.vmx_name_crash = vmx_name_crash;
203 //      }
204
205         public String getSave_current() {
206                 return save_current;
207         }
208
209         public void setSave_current(String save_current) {
210                 this.save_current = save_current;
211         }
212
213         public String getSave_last() {
214                 return save_last;
215         }
216
217         public void setSave_last(String save_last) {
218                 System.out.println( "Save name: " + save_last ) ;
219                 this.save_last = save_last;
220         }
221
222         public int getNo_save() 
223         {
224                 return no_save ;
225         }
226
227         public void setNo_save( int no_save ) 
228         {
229                 this.no_save = no_save ;
230         }
231
232         public int getNo_save_last() 
233         {
234                 return no_save_last ;
235         }
236
237         public void setNo_save_last( int no_save_last ) 
238         {
239                 this.no_save_last = no_save_last ;
240         }
241         
242         public int getComputationId() { return computation_id ; }
243         
244         
245         public void setComputationId( int _id )
246         {
247                 computation_id = _id ;
248         }
249         
250         
251         public int deployLastSave()
252         {
253                 System.out.print( "Removing current VM ... " ) ;
254                 
255                 String[] command = new String[] { "/bin/rm", "-rf", 
256                                 working_directory + "/" + directory } ;
257                 Process pr = null ;
258                 try {
259                         pr = Runtime.getRuntime().exec( command ) ;
260 //                      synchronized( pr ){
261                         pr.waitFor() ; //;}
262                 } catch( IOException e ) {
263                         System.err.println( "Error while removing current VM!" ) ;
264                         e.printStackTrace() ;
265                         return 1 ;
266                 } catch( InterruptedException e ) {
267                         e.printStackTrace() ;
268                         return 1 ;
269                 }
270                 
271                 if( pr.exitValue() == 0 )
272                 {
273                         System.out.println( "Successful deletion of current VM." ) ;
274                 } else {
275                         System.err.println( "Unssuccessful deletion of current VM!" ) ;
276                         printProcessError( pr ) ;
277                 }
278                 
279                 System.out.print( "Deploying the last save ... " ) ;
280                 
281                 command = new String[] { "/bin/tar", "-xzf", 
282                                 working_directory + "/" + save_last,
283                                 "-C", working_directory } ;
284                 pr = null ;
285                 try {
286                         pr = Runtime.getRuntime().exec( command ) ;
287 //                      synchronized( pr ){
288                         pr.waitFor() ; //;}
289                 } catch( IOException e ) {
290                         System.err.println( "Error while deploying the last secure save!" ) ;
291                         e.printStackTrace() ;
292                         return 1 ;
293                 } catch( InterruptedException e ) {
294                         e.printStackTrace() ;
295                         return 1 ;
296                 }
297                         
298                 
299                 if( pr.exitValue() == 0 )
300                 {
301                         System.out.println( "Successful extraction of the save archive." ) ;
302                         return 0 ;
303                 } else {
304                         System.err.println( "unSuccessful extraction of the save archive!" ) ;
305                         printProcessError( pr ) ;
306                 }
307                 
308                 return 1 ;
309         }
310         
311         
312         public int deployInitialVM()
313         {
314                 System.out.print( "Removing current VM ... " ) ;
315                 
316                 String[] command = new String[] { "/bin/rm", "-rf", 
317                                 working_directory + "/" + directory } ;
318                 Process pr = null ;
319                 try {
320                         pr = Runtime.getRuntime().exec( command ) ;
321 //                      synchronized( pr ){
322                         pr.waitFor() ; //;}
323                 } catch( IOException e ) {
324                         System.err.println( "Error while removing current VM!" ) ;
325                         e.printStackTrace() ;
326                         return 1 ;
327                 } catch( InterruptedException e ) {
328                         e.printStackTrace() ;
329                         return 1 ;
330                 }
331                 
332                 if( pr.exitValue() == 0 )
333                 {
334                         System.out.println( "Successful deletion of current VM." ) ;
335                 } else {
336                         System.err.println( "Unsuccessful deletion of current VM!" ) ;
337                         printProcessError( pr ) ;
338                 }
339                 
340                 System.out.print( "Deploying the initial archive ... " ) ;
341                 
342                 command = new String[] { "/bin/tar", "-xzf", 
343                                 working_directory + "/" + save_last,
344                                 "-C", working_directory } ;
345                 pr = null ;
346                 try {
347                         pr = Runtime.getRuntime().exec( command ) ;
348 //                      synchronized( pr ){
349                         pr.waitFor() ; //;}
350                 } catch( IOException e ) {
351                         System.err.println( "Error while deploying the initial archive!" ) ;
352                         e.printStackTrace() ;
353                         return 1 ;
354                 } catch( InterruptedException e ) {
355                         e.printStackTrace() ;
356                         return 1 ;
357                 }
358                         
359                 
360                 if( pr.exitValue() == 0 )
361                 {
362                         System.out.println( "Successful extraction of the initial archive." ) ;
363                         deploy = true ;
364                 } else {
365                         System.err.println( "Unsuccessful extraction of the initial archive!" ) ;
366                         printProcessError( pr ) ;
367                         return 1 ;
368                 }
369                 
370                 try {
371                         FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/initial.hpcvm" ) ) ;
372                         fw.write( "initial!" ) ;
373                         fw.flush() ;
374                         fw.close() ;
375                         fw = null ;
376                         return 0 ;
377                 } catch( IOException e1 ) {
378                         e1.printStackTrace() ;
379                         System.err.println( "Unable to mark the initial deployment!" ) ;
380                 }
381                 
382                 return 1 ;
383         }
384
385
386         public int checkVmx() 
387         {
388                 deploy = false ;
389                 first = false ;
390                 deployFault = false ;
391                 
392                 // Initial deployment
393                 File file = new File( working_directory + "/" + directory + "/initial.hpcvm" ) ;
394                 
395                 if( file.exists() )
396                 {
397                         deploy = true ;
398                 }
399                 
400                 // First execution
401                 file = null ;
402                 file = new File( working_directory + "/" + directory + "/first.hpcvm" ) ;
403                 
404                 if( file.exists() )
405                 {
406                         first = true ;
407                 }
408                 
409                 // Redeployment after a fault
410                 file = null ;
411                 file = new File( working_directory + "/" + directory + "/fault.hpcvm" ) ;
412                 
413                 if( file.exists() )
414                 {
415                         deploy = false ;
416                         first = false ;
417                         deployFault = true ;
418                 }
419                 
420                 file = null ;
421                 
422                 // If nothing to do
423                 if( ! deploy && ! first && ! deployFault )
424                 {
425                         return 0 ;
426                 }
427                 
428                 // Deployment of the VM
429                 
430                 if( deploy )
431                 {
432                         System.out.print( "Saving the initial VMX file ... " ) ;
433                         
434                         String[] command = new String[]{ "/bin/cp", 
435                                         working_directory + "/" + directory + "/" + vmx_name,
436                                         working_directory + "/" + directory + "/" + vmx_name_initial } ;
437                         
438                         try {
439                                 Process p = Runtime.getRuntime().exec( command ) ;
440                                 p.waitFor() ;
441                 
442                                 if( p.exitValue() == 0 )
443                                 {
444                                         System.out.println( "Successfully saved initial the VMX file." ) ;
445                                 } else {
446                                         System.err.println( "Unsuccessful save of the initial VMX file!" ) ;
447                                         printProcessError( p ) ;
448                                         return 1 ;
449                                 }
450                         } catch( IOException e ) {
451                                 System.err.println( "Error during initial VMX file save: " ) ;
452                                 e.printStackTrace() ;
453                                 return 1 ;
454                         } catch( InterruptedException e ) {
455                                 e.printStackTrace() ;
456                                 return 1 ;
457                         }
458                         
459                         System.out.print( "Rewritting WMX file ... " ) ;
460                         
461                         
462                         FileReader fin = null ;
463                         try {
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() ;
468                                 return 1 ;
469                         }
470                                 
471                         BufferedReader bin = new BufferedReader( fin ) ;
472                         String line = "" ;
473                         FileWriter fout = null ;
474                         
475                         try {
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() ;
480                                 return 1 ;
481                         }
482                                 
483                         // Keeping only some information
484                         // -- this force VmWare to generate new identifiers and MAC address
485                         while( line != null )
486                         {               
487                                 try {
488                                         line = bin.readLine() ;
489                                         if( line != null )
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 " ) 
495                                           )
496                                         {
497                                                 fout.write( line + "\n" ) ;
498                                         }
499                                 } catch( IOException e ) {}
500                         }
501                                 
502                         try {
503                                 bin.close() ;
504                         } catch (IOException e) {
505                                 System.err.println( "Unable to close the initial VMX file!" ) ;
506                                 e.printStackTrace() ;
507                         }
508                                 
509                         // Adding the copy information
510                         try
511                         {
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() ;
519                                 return 1 ;
520                         }
521                                 
522                         try {
523                                 fout.close() ;
524                         } catch( IOException e ) {
525                                 System.err.println( "Unable to close the VMX file!" ) ;
526                                 e.printStackTrace() ;
527                                 return 1 ;
528                         }
529                         
530                         System.out.println( "Successful rewrite of the VMX file." ) ;
531                         
532                         // Removing the initial mark
533                         System.out.print( "Removing the deployment mark ... " ) ;
534                         
535                         command = new String[]{ "/bin/rm", 
536                                         working_directory + "/" + directory + "/initial.hpcvm" } ;
537                         
538                         try {
539                                 Process p = Runtime.getRuntime().exec( command ) ;
540                                 p.waitFor() ;
541                 
542                                 if( p.exitValue() == 0 )
543                                 {
544                                         System.out.println( "Successful deletion of the deployment mark." ) ;
545                                 } else {
546                                         System.err.println( "Unsuccessful deletion of the deployment mark!" ) ;
547                                         printProcessError( p ) ;
548                                         return 1 ;
549                                 }
550                         } catch( IOException e ) {
551                                 System.err.println( "Error during deletion of the deployment mark: " ) ;
552                                 e.printStackTrace() ;
553                                 return 1 ;
554                         } catch( InterruptedException e ) {
555                                 e.printStackTrace() ;
556                                 return 1 ;
557                         }
558                         
559                         deploy = false ;
560                         
561                         // Writing the first start mark
562                         try {
563                                 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
564                                 fw.write( "first!\n" ) ;
565                                 fw.flush() ;
566                                 fw.close() ;
567                                 fw = null ;
568                                 return 0 ;
569                         } catch( IOException e1 ) {
570                                 e1.printStackTrace() ;
571                                 System.err.println( "Unable to mark the first run of the VM!" ) ;
572                         }       
573                 }
574                 
575                 
576                 // First start of the VM
577                 
578                 if( first )
579                 {
580                         System.out.print( "Saving the first start VMX file ... " ) ;
581                         
582                         String[] command = new String[]{ "/bin/cp", 
583                                         working_directory + "/" + directory + "/" + vmx_name,
584                                         working_directory + "/" + directory + "/" + vmx_name_first } ;
585                         
586                         try {
587                                 Process p = Runtime.getRuntime().exec( command ) ;
588                                 p.waitFor() ;
589                 
590                                 if( p.exitValue() == 0 )
591                                 {
592                                         System.out.println( "Successfully saved the first start VMX file." ) ;
593                                 } else {
594                                         System.err.println( "Unsuccessful save of the first start VMX file!" ) ;
595                                         printProcessError( p ) ;
596                                         return 1 ;
597                                 }
598                         } catch( IOException e ) {
599                                 System.err.println( "Error during first start VMX file save: " ) ;
600                                 e.printStackTrace() ;
601                                 return 1 ;
602                         } catch( InterruptedException e ) {
603                                 e.printStackTrace() ;
604                                 return 1 ;
605                         }
606                         
607                         System.out.print( "Rewritting WMX file ... " ) ;
608                         
609                         
610                         FileReader fin = null ;
611                         try {
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() ;
616                                 return 1 ;
617                         }
618                                 
619                         BufferedReader bin = new BufferedReader( fin ) ;
620                         String line = "" ;
621                         FileWriter fout = null ;
622                         
623                         try {
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() ;
628                                 return 1 ;
629                         }
630                                 
631                         // Keeping all information
632                         // -- except copy information
633                         while( line != null )
634                         {               
635                                 try {
636                                         line = bin.readLine() ;
637                                         if( line != null )
638                                         if( ! line.contains( "answer.msg.uuid.altered " ) )
639                                         {
640                                                 fout.write( line + "\n" ) ;
641                                         }
642                                 } catch( IOException e ) {}
643                         }
644                                 
645                         try {
646                                 bin.close() ;
647                         } catch (IOException e) {
648                                 System.err.println( "Unable to close the first start VMX file!" ) ;
649                                 e.printStackTrace() ;
650                         }
651                                 
652                         try {
653                                 fout.close() ;
654                         } catch( IOException e ) {
655                                 System.err.println( "Unable to close the VMX file!" ) ;
656                                 e.printStackTrace() ;
657                                 return 1 ;
658                         }
659                         
660                         System.out.println( "Successful rewrite of the VMX file." ) ;
661                         
662                         // Removing the initial mark
663                         System.out.print( "Removing the first start mark ... " ) ;
664                         
665                         command = new String[]{ "/bin/rm", 
666                                         working_directory + "/" + directory + "/first.hpcvm" } ;
667                         
668                         try {
669                                 Process p = Runtime.getRuntime().exec( command ) ;
670                                 p.waitFor() ;
671                 
672                                 if( p.exitValue() == 0 )
673                                 {
674                                         System.out.println( "Successful deletion of the first start mark." ) ;
675                                 } else {
676                                         System.err.println( "Unsuccessful deletion of the first start mark!" ) ;
677                                         printProcessError( p ) ;
678                                         return 1 ;
679                                 }
680                         } catch( IOException e ) {
681                                 System.err.println( "Error during deletion of the first start mark: " ) ;
682                                 e.printStackTrace() ;
683                                 return 1 ;
684                         } catch( InterruptedException e ) {
685                                 e.printStackTrace() ;
686                                 return 1 ;
687                         }
688                         
689                         first = false ;
690
691                         return 0 ;
692                 }
693                 
694                 
695                 // Redeployment after a fault of a VM
696                 
697                 if( deployFault )
698                 {
699                         System.out.print( "Saving the original VMX file ... " ) ;
700                         
701                         String[] command = new String[]{ "/bin/cp", 
702                                         working_directory + "/" + directory + "/" + vmx_name,
703                                         working_directory + "/" + directory + "/" + vmx_name_first } ;
704                         
705                         try {
706                                 Process p = Runtime.getRuntime().exec( command ) ;
707                                 p.waitFor() ;
708                 
709                                 if( p.exitValue() == 0 )
710                                 {
711                                         System.out.println( "Successfully saved the original VMX file." ) ;
712                                 } else {
713                                         System.err.println( "Unsuccessful save of the original VMX file!" ) ;
714                                         printProcessError( p ) ;
715                                         return 1 ;
716                                 }
717                         } catch( IOException e ) {
718                                 System.err.println( "Error during original VMX file save: " ) ;
719                                 e.printStackTrace() ;
720                                 return 1 ;
721                         } catch( InterruptedException e ) {
722                                 e.printStackTrace() ;
723                                 return 1 ;
724                         }
725                         
726                         System.out.print( "Rewritting WMX file ... " ) ;
727                         
728                         
729                         FileReader fin = null ;
730                         try {
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() ;
735                                 return 1 ;
736                         }
737                                 
738                         BufferedReader bin = new BufferedReader( fin ) ;
739                         String line = "" ;
740                         FileWriter fout = null ;
741                         
742                         try {
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() ;
747                                 return 1 ;
748                         }
749                                 
750                         // Keeping all information
751                         while( line != null )
752                         {               
753                                 try {
754                                         line = bin.readLine() ;
755                                         if( line != null )
756                                         if( ! line.contains( "answer.msg.uuid.altered " ) )
757                                         {
758                                                 fout.write( line + "\n" ) ;
759                                         }
760                                 } catch( IOException e ) {}
761                         }
762                                 
763                         try {
764                                 bin.close() ;
765                         } catch (IOException e) {
766                                 System.err.println( "Unable to close the original VMX file!" ) ;
767                                 e.printStackTrace() ;
768                         }
769                         
770                         // Writing information to indicate that the VM has been moved
771                         try {
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() ;
776                         }
777                                 
778                         try {
779                                 fout.close() ;
780                         } catch( IOException e ) {
781                                 System.err.println( "Unable to close the VMX file!" ) ;
782                                 e.printStackTrace() ;
783                                 return 1 ;
784                         }
785                         
786                         System.out.println( "Successful rewrite of the VMX file." ) ;
787                         
788                         // Removing lock files
789                         System.out.print( "Removing lock files ... " ) ;
790                 
791                         command = new String[]{ "/bin/rm", "-rf", 
792                                 working_directory + "/" + directory 
793                                 + "/" + vmx_name + ".lck" } ;
794                         
795                         try {
796                                 Process p = Runtime.getRuntime().exec( command ) ;
797                                 p.waitFor() ;
798                 
799                                 if( p.exitValue() == 0 )
800                                 {
801                                         System.out.println( "Successfully deleted lock files." ) ;
802                                 } else {
803                                         System.err.println( "Unsuccessful deletion of lock files!" ) ;
804 //                                      printProcessError( p.getErrorStream() ) ;
805                                         printProcessError( p ) ;
806                                         
807                                         return 1 ;
808                                 }
809                         } catch( IOException e ) {
810                                 System.err.println( "Error during lock files deletion: " ) ;
811                                 e.printStackTrace() ;
812                                 return 1 ;
813                         } catch( InterruptedException e ) {
814                                 e.printStackTrace() ;
815                                 return 1 ;
816                         }
817                         
818                         // Removing the initial mark
819                         System.out.print( "Removing the fault mark ... " ) ;
820                         
821                         command = new String[]{ "/bin/rm", 
822                                         working_directory + "/" + directory + "/fault.hpcvm" } ;
823                         
824                         try {
825                                 Process p = Runtime.getRuntime().exec( command ) ;
826                                 p.waitFor() ;
827                 
828                                 if( p.exitValue() == 0 )
829                                 {
830                                         System.out.println( "Successful deletion of the fault mark." ) ;
831                                 } else {
832                                         System.err.println( "Unsuccessful deletion of the fault mark!" ) ;
833                                         printProcessError( p ) ;
834                                         return 1 ;
835                                 }
836                         } catch( IOException e ) {
837                                 System.err.println( "Error during deletion of the fault mark: " ) ;
838                                 e.printStackTrace() ;
839                                 return 1 ;
840                         } catch( InterruptedException e ) {
841                                 e.printStackTrace() ;
842                                 return 1 ;
843                         }
844                         
845                         deployFault = false ;
846                         
847                         // Writing the first start mark
848                         try {
849                                 FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
850                                 fw.write( "first!" ) ;
851                                 fw.flush() ;
852                                 fw.close() ;
853                                 fw = null ;
854                                 first = true ;
855                                 return 0 ;
856                         } catch( IOException e1 ) {
857                                 e1.printStackTrace() ;
858                                 System.err.println( "Unable to mark the first run of the VM!" ) ;
859                         }       
860                 }               
861                 
862                 return 1 ;
863         }
864         
865         
866         private void printProcessError( Process _p )
867         {
868                 if( _p != null )
869                 {
870                         System.err.println( "Error: " + _p.exitValue() ) ;
871                         BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
872                         String line ;
873                         try {
874                                 while( (line = br.readLine()) != null )
875                                 {
876                                         System.err.println( line ) ;
877                                 }
878                         } catch( IOException e ) {
879                                 e.printStackTrace() ;
880                         }
881                 }
882         }
883
884         
885 }
886
887 /** La programmation est un art, respectons ceux qui la pratiquent !! **/