System.err.println( "Problem with semaphore acquiring!" ) ;
e2.printStackTrace() ;
}
+
+ // Checking first start
+ if( machine.checkVmx() == 1 )
+ {
+ return 1 ;
+ }
boolean ret = true ;
int retry = 0 ;
e1.printStackTrace() ;
}
- if( ! isRestartedSave )
+// if( ! isRestartedSave )
+// {
+// /** Restoring the original vmx file (necessary after a crash) **/
+// command = new String[]{ "/bin/cp",
+// working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_normal(),
+// working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
+//
+// try {
+// Process p = Runtime.getRuntime().exec( command ) ;
+// p.waitFor() ;
+//
+// if( p.exitValue() == 0 )
+// {
+// System.out.println( "Successfully replaced the VMX file." ) ;
+// sema.release() ;
+// emergencyStop = false ;
+// return 0 ;
+// } else {
+// System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
+//// printProcessError( p.getErrorStream() ) ;
+// printProcessError( p ) ;
+// sema.release() ;
+// return 1 ;
+// }
+// } catch( IOException e ) {
+// System.err.println( "Error during VMX file replacement: " ) ;
+// e.printStackTrace() ;
+// sema.release() ;
+// return 1 ;
+// } catch( InterruptedException e ) {
+// e.printStackTrace() ;
+// sema.release() ;
+// return 1 ;
+// }
+// } else {
+ if( machine.checkVmx() == 0 )
{
- /** Restoring the original vmx file (necessary after a crash) **/
- command = new String[]{ "/bin/cp",
- working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_normal(),
- working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
-
- try {
- Process p = Runtime.getRuntime().exec( command ) ;
- p.waitFor() ;
-
- if( p.exitValue() == 0 )
- {
- System.out.println( "Successfully replaced the VMX file." ) ;
- sema.release() ;
- emergencyStop = false ;
- return 0 ;
- } else {
- System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
-// printProcessError( p.getErrorStream() ) ;
- printProcessError( p ) ;
- sema.release() ;
- return 1 ;
- }
- } catch( IOException e ) {
- System.err.println( "Error during VMX file replacement: " ) ;
- e.printStackTrace() ;
- sema.release() ;
- return 1 ;
- } catch( InterruptedException e ) {
- e.printStackTrace() ;
- sema.release() ;
- return 1 ;
- }
- } else {
sema.release() ;
emergencyStop = false ;
return 0 ;
if( isRestartedSave )
{
// Using the specific vmx file
- System.out.print( "Changing VMX file after crash ... " ) ;
-
- String[] command = new String[]{ "/bin/cp",
- working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_crash(),
- working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
+ machine.setDeployFault( true ) ;
+ // Writing the restarted save mark
try {
- Process p = Runtime.getRuntime().exec( command ) ;
- p.waitFor() ;
-
- if( p.exitValue() == 0 )
- {
- System.out.println( "Successfully replaced the VMX file." ) ;
- } else {
- System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
-// printProcessError( p.getErrorStream() ) ;
- printProcessError( p ) ;
-
- return 1 ;
- }
- } catch( IOException e ) {
- System.err.println( "Error during VMX file replacement: " ) ;
- e.printStackTrace() ;
- } catch( InterruptedException e ) {
- e.printStackTrace() ;
- }
-
- // Removing lock files
- System.out.print( "Removing lock files ... " ) ;
-
- command = new String[]{ "/bin/rm", "-rf",
- working_directory + "/" + machine.getDirectory()
- + "/" + machine.getVmx_name() + ".lck" } ;
+ FileWriter fw = new FileWriter( new File( working_directory + "/" + machine.getDirectory() + "/fault.hpcvm" ) ) ;
+ fw.write( "fault!" ) ;
+ fw.flush() ;
+ fw.close() ;
+ fw = null ;
+ } catch( IOException e1 ) {
+ e1.printStackTrace() ;
+ System.err.println( "Unable to mark the fault!" ) ;
+ }
- try {
- Process p = Runtime.getRuntime().exec( command ) ;
- p.waitFor() ;
-
- if( p.exitValue() == 0 )
- {
- System.out.println( "Successfully deleted lock files." ) ;
- } else {
- System.err.println( "Unsuccessful deletion of lock files!" ) ;
-// printProcessError( p.getErrorStream() ) ;
- printProcessError( p ) ;
-
- return 1 ;
- }
- } catch( IOException e ) {
- System.err.println( "Error during lock files deletion: " ) ;
- e.printStackTrace() ;
- } catch( InterruptedException e ) {
- e.printStackTrace() ;
+ if( machine.checkVmx() == 1 )
+ {
+ return 1 ;
}
+
+// System.out.print( "Changing VMX file after crash ... " ) ;
+//
+// String[] command = new String[]{ "/bin/cp",
+// working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_crash(),
+// working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
+//
+// try {
+// Process p = Runtime.getRuntime().exec( command ) ;
+// p.waitFor() ;
+//
+// if( p.exitValue() == 0 )
+// {
+// System.out.println( "Successfully replaced the VMX file." ) ;
+// } else {
+// System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
+//// printProcessError( p.getErrorStream() ) ;
+// printProcessError( p ) ;
+//
+// return 1 ;
+// }
+// } catch( IOException e ) {
+// System.err.println( "Error during VMX file replacement: " ) ;
+// e.printStackTrace() ;
+// } catch( InterruptedException e ) {
+// e.printStackTrace() ;
+// }
/** Retrieving VM assigned IP **/
String vmIP = null ;
{
if( sendSaveOkVM() == 0 )
{
+ date_last_save = System.currentTimeMillis() ;
+
return 0 ;
}
} else {
// if( ! error )
// {
if( sendSaveOkVM() == 1 ) { error = true ; }
+
+
// }
// /** Deletion of the tar archive **/
return 1 ;
}
- boolean ok = true ;
-
ArrayList<ServicesClient> sn = machine.getSaveNeighbors() ;
for( int i = 0 ; i < sn.size() ; i++ )
{
- System.out.print( "Sending save to " + sn.get( i ) + " ... " ) ;
+ String name ;
+ String wd ;
+ String snIP ;
+
try {
- command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
- sn.get( i ).getIPHost() + ":"
- + sn.get( i ).getWorkingDirectory() } ;
- } catch( RemoteException e1 ) {
- System.err.println( "Unable to retrieve save neighbor information!" ) ;
- e1.printStackTrace() ;
- ok = false ;
+ name = sn.get( i ).getName() ;
+ wd = sn.get( i ).getWorkingDirectory() ;
+ snIP = sn.get( i ).getIPHost() ;
+ } catch (RemoteException e2) {
+ System.err.println( "Unable to retrieve information on one save neighbor!" ) ;
+ e2.printStackTrace() ;
+ continue ;
}
+
+ System.out.print( "Sending save to " + name + " ... " ) ;
+
+ command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
+ snIP + ":" + wd } ;
- if( ok )
try {
procSave = Runtime.getRuntime().exec( command ) ;
procSave.waitFor() ;
/* Starting the VM save */
saveVM() ;
+
+ date_last_save = System.currentTimeMillis() ;
} else {
sendSaveOkVM() ;
}
@Override
public void saveOk()
{
- String save_name = machine.getName() + "_" + machine.getComputationId() +
- "_last.tar.gz" ;
+ String save_name = machine.getName() + "_last_" + machine.getComputationId() +
+ ".tar.gz" ;
String save_new = machine.getName() + "_new_"
+ machine.getComputationId() + ".tar.gz" ;
}
}
+ // Informing the server
+ int ret = 1 ;
+ try {
+ ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
+ } catch( RemoteException e ) {
+ System.err.println( "Unable to inform the server about the new save name!" ) ;
+ e.printStackTrace() ;
+ }
+
+ if( ret == 0 )
+ {
+ System.out.println( "Successfully informing the server about the new save name." ) ;
+ } else {
+ System.err.println( "Problem on the server while informing it about the new save name!" ) ;
+ }
+
// Ok here
lastSaveOk = true ;
}
@Override
- public void replaceSavingNeighbor( ServicesClient _old, ServicesClient _new )
+ public void replaceSavingNeighbor( String _old, ServicesClient _new )
{
System.out.print( "Replacing a save neihgbor ... " ) ;
if( _old != null && _new != null )
for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
{
try {
- if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPVM() ) )
+ if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old ) )
{
machine.getSaveNeighbors().set( i, _new ) ;
System.out.println( "Save neighbor successfully changed." ) ;
}
+ public Integer deployVM( String _name, String _archive, String _directory )
+ {
+ if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
+ {
+ File file = new File( working_directory + "/" + _archive ) ;
+ if( ! file.exists() )
+ {
+ System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
+ file = null ;
+ return 2 ;
+ } else if( file.isDirectory() ) {
+ System.err.println( _archive + " is a directory!" ) ;
+ file = null ;
+ return 1 ;
+ }
+
+ file = null ;
+
+ machine.setName( _name ) ;
+ machine.setInitial_archive_name( _archive ) ;
+ machine.setDirectory( _directory ) ;
+
+ if( machine.deployInitialVM() == 1 )
+ {
+ System.err.println( "Unable to deploy the initial VM archive!" ) ;
+ } else {
+ return 0 ;
+ }
+ }
+
+ return 1 ;
+ }
+
+
private class SaveProcess
{
boolean status ;
package and.hpcvm ;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
{
if( _sn != null )
{
- save_neighbor.set( 0, _sn ) ;
+ if( save_neighbor.size() == 0 )
+ {
+ save_neighbor.add( _sn ) ;
+ } else {
+ save_neighbor.set( 0, _sn ) ;
+ }
- System.out.println( "My save neighbor is " + _sn ) ;
+// System.out.println( "My save neighbor is " + _sn ) ;
try {
client.getStub().setSavingNeighbor( _sn ) ;
private ConnectedMonitor monitor ;
private DiscCount counter ;
private ArrayList<IPAssociation> vmIPs ;
+ private String working_directory ;
protected Server() throws RemoteException
computingClients = new ArrayList<Server.ComputingClient>() ;
monitor = null ;
+ working_directory = "/localhome/vmware" ;
+
exportObject() ;
vmIPs = new ArrayList<IPAssociation>() ;
cl.getStatus().equalsIgnoreCase( "saving" ) )
{
ComputingClient cc = cl.getComputingClient() ;
- ServicesClient dead = cc.getClient().getStub() ;
+// ServicesClient dead = cc.getClient().getStub() ;
String ipDead = cc.getClient().getIP() ;
boolean ok = false ;
for( int k = 0 ; k < computingClients.size() ; k++ )
{
try {
- computingClients.get( i ).getClient().getStub().
- replaceSavingNeighbor( dead, clients.get( i ).getStub() ) ;
+ computingClients.get( k ).getClient().getStub().
+ replaceSavingNeighbor( ipDead, clients.get( i ).getStub() ) ;
} catch( RemoteException e ) {
System.err.println( "Unable to inform " + computingClients.get( k ).getClient().getName() +
" of the replacement of a save neighbor!" ) ;
}
}
-
System.out.println( "Dead client successfully replaced." ) ;
- // restart vms
+
break ;
} else {
System.err.println( "Dead client not replaced!!" ) ;
while( counter.getNb() != 0 )
{
try {
- System.out.println( "### WAITING counter ###" ) ;
- counter.wait() ; // !!!!! synchro
+ counter.wait() ;
} catch( InterruptedException e ) {
e.printStackTrace() ;
}
return 0 ;
}
-
-
+
+
+ public Integer changeSaveName( String _ip, String _saveName )
+ {
+ if( _ip != null && _ip.length() > 0 && _saveName != null && _saveName.length() > 0 )
+ {
+ for( int i = 0 ; i < computingClients.size() ; i ++ )
+ {
+ if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
+ {
+ computingClients.get( i ).setLastSave( _saveName ) ;
+ System.out.println( "Save name successfully change for " + _ip ) ;
+ return 0 ;
+ }
+ }
+ }
+
+ return 1 ;
+ }
@Override
public ArrayList<ServicesClient> startApplication( int _nb )
@Override
- public String getAssociatedIP( String _ip ) throws RemoteException
+ public String getAssociatedIP( String _ip )
{
String ret = null ;
return ret ;
}
+
+ public Integer deployVM( String _name, String _archive, String _directory )
+ {
+ int pb = -1 ;
+ int nb = 0 ;
+
+ if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0
+ && _directory != null && _directory.length() > 0 )
+ {
+ System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
+
+ File file = new File( working_directory + "/" + _archive ) ;
+ if( ! file.exists() )
+ {
+ System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
+ file = null ;
+ return 1 ;
+ } else if( file.isDirectory() ) {
+ System.err.println( _archive + " is a directory!" ) ;
+ file = null ;
+ return 1 ;
+ }
+
+ file = null ;
+
+ // TODO do a better deployment !!
+ int ret ;
+ boolean error ;
+
+ pb = 0 ;
+
+ for( int i = 0 ; i < clients.size() ; i++ )
+ {
+ ret = 1 ;
+ error = false ;
+ try {
+ ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
+ } catch( RemoteException e ) {
+ System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
+ e.printStackTrace() ;
+ }
+
+ // The client does not have the archive, we have to send it.
+ if( ret == 2 )
+ {
+ System.out.print( "Sending VM archive to " + clients.get( i ).getName() + " ... " ) ;
+
+ String wd = "" ;
+ String snIP = "" ;
+ error = false ;
+
+ try {
+ wd = clients.get( i ).getStub().getWorkingDirectory() ;
+ snIP = clients.get( i ).getStub().getIPHost() ;
+ } catch (RemoteException e2) {
+ System.err.println( "Unable to retrieve information on " + clients.get( i ).getName() + "!" ) ;
+ e2.printStackTrace() ;
+ error = true ;
+ pb++ ;
+ }
+
+ String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
+ snIP + ":" + wd } ;
+
+ if( ! error )
+ try {
+ Process proc = Runtime.getRuntime().exec( command ) ;
+ proc.waitFor() ;
+
+ if( proc.exitValue() == 0 )
+ {
+ System.out.println( "Initial VM archive successfully sent." ) ;
+ } else {
+ System.err.println( "Initial VM archive not sent!" ) ;
+// printProcessError( p.getErrorStream() ) ;
+ System.err.println( "Error: " + proc.exitValue() ) ;
+ BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
+
+ String l ;
+ try {
+ while( (l = b.readLine()) != null )
+ {
+ System.err.println( l ) ;
+ }
+ } catch( IOException e ) {
+ e.printStackTrace() ;
+ }
+
+ error = true ;
+ pb++ ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during initial VM archive send command: " ) ;
+ e.printStackTrace() ;
+ error = true ;
+ pb++ ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ error = true ;
+ pb++ ;
+ }
+
+
+ if( error )
+ {
+ continue ;
+ }
+
+ // Second try ...
+ ret = 1 ;
+ try {
+ ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
+ } catch( RemoteException e ) {
+ System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
+ e.printStackTrace() ;
+ pb++ ;
+ }
+ }
+
+ if( ret == 0 )
+ {
+ System.out.println( "Initial VM archive successfully deployed on " + clients.get( i ).getName() + "." ) ;
+ nb++ ;
+ }
+ }
+ }
+
+ if( pb > 0 )
+ {
+ if( pb == 1 )
+ System.err.println( "** " + pb + " machine is not deployed!" ) ;
+ if( pb > 1 )
+ System.err.println( "** " + pb + " machine(s) are not deployed!" ) ;
+ }
+
+ return nb ;
+ }
+
+
+ public String getWorkingDirectory()
+ {
+ return working_directory ;
+ }
+
}
/** La programmation est un art, respectons ceux qui la pratiquent !! **/
package and.hpcvm ;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
private String initial_archive_name ;
private String directory ;
private String vmx_name ;
- private String vmx_name_normal ;
- private String vmx_name_crash ;
+ private String vmx_name_first ;
+ private String vmx_name_initial ;
private String save_current ;
private String save_last ;
private int no_save ;
private String clientInVM ;
private String vm_user ;
private String vm_user_passwd ;
+ private boolean deploy ;
+ private boolean first ;
+ private boolean deployFault ;
public VirtualMachine()
{
initial_archive_name = "VmTest.tgz" ;
directory = "VmTest" ;
vmx_name = "VmTest.vmx" ;
- vmx_name_normal = "VmTest.vmx.normal" ;
- vmx_name_crash = "VmTest.vmx.crash" ;
+ vmx_name_first = "VmTest.vmx.first" ;
+ vmx_name_initial = "VmTest.vmx.initial" ;
save_current = "VmTest.tgz" ;
save_last = "VmTest.tgz" ;
working_directory = "/localhome/vmware" ;
clientInVM = "/home/mpi/InGuest" ;
vm_user = "mpi" ;
vm_user_passwd = "mpi" ;
+ first = false ;
+ deploy = false ;
+ deployFault = false ;
+ }
+
+
+ public void setDeployFault( boolean _bool )
+ {
+ deployFault = _bool ;
}
this.vmx_name = vmx_name;
}
- public String getVmx_name_normal() {
- return vmx_name_normal;
- }
+// public String getVmx_name_normal() {
+// return vmx_name_normal;
+// }
- public void setVmx_name_normal(String vmx_name_normal) {
- this.vmx_name_normal = vmx_name_normal;
- }
+// public void setVmx_name_normal(String vmx_name_normal) {
+// this.vmx_name_normal = vmx_name_normal;
+// }
- public String getVmx_name_crash() {
- return vmx_name_crash;
- }
+// public String getVmx_name_crash() {
+// return vmx_name_crash;
+// }
- public void setVmx_name_crash(String vmx_name_crash) {
- this.vmx_name_crash = vmx_name_crash;
- }
+// public void setVmx_name_crash(String vmx_name_crash) {
+// this.vmx_name_crash = vmx_name_crash;
+// }
public String getSave_current() {
return save_current;
{
computation_id = _id ;
}
-
+
public int deployLastSave()
{
{
System.out.println( "Successful deletion of current VM." ) ;
} else {
- System.err.println( "Error: " + pr.exitValue() ) ;
- BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ;
-
- String l ;
- try {
- while( (l = b.readLine()) != null )
- {
- System.err.println( l ) ;
- }
- } catch( IOException e ) {
- e.printStackTrace() ;
- }
+ System.err.println( "Unssuccessful deletion of current VM!" ) ;
+ printProcessError( pr ) ;
}
System.out.print( "Deploying the last save ... " ) ;
System.out.println( "Successful extraction of the save archive." ) ;
return 0 ;
} else {
- System.err.println( "Error: " + pr.exitValue() ) ;
- BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ;
+ System.err.println( "unSuccessful extraction of the save archive!" ) ;
+ printProcessError( pr ) ;
+ }
+
+ return 1 ;
+ }
+
+
+ public int deployInitialVM()
+ {
+ System.out.print( "Removing current VM ... " ) ;
+
+ String[] command = new String[] { "/bin/rm", "-rf",
+ working_directory + "/" + directory } ;
+ Process pr = null ;
+ try {
+ pr = Runtime.getRuntime().exec( command ) ;
+// synchronized( pr ){
+ pr.waitFor() ; //;}
+ } catch( IOException e ) {
+ System.err.println( "Error while removing current VM!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ if( pr.exitValue() == 0 )
+ {
+ System.out.println( "Successful deletion of current VM." ) ;
+ } else {
+ System.err.println( "Unsuccessful deletion of current VM!" ) ;
+ printProcessError( pr ) ;
+ }
+
+ System.out.print( "Deploying the initial archive ... " ) ;
+
+ command = new String[] { "/bin/tar", "-xzf",
+ working_directory + "/" + save_last,
+ "-C", working_directory } ;
+ pr = null ;
+ try {
+ pr = Runtime.getRuntime().exec( command ) ;
+// synchronized( pr ){
+ pr.waitFor() ; //;}
+ } catch( IOException e ) {
+ System.err.println( "Error while deploying the initial archive!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+
+ if( pr.exitValue() == 0 )
+ {
+ System.out.println( "Successful extraction of the initial archive." ) ;
+ deploy = true ;
+ } else {
+ System.err.println( "Unsuccessful extraction of the initial archive!" ) ;
+ printProcessError( pr ) ;
+ return 1 ;
+ }
+
+ try {
+ FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/initial.hpcvm" ) ) ;
+ fw.write( "initial!" ) ;
+ fw.flush() ;
+ fw.close() ;
+ fw = null ;
+ return 0 ;
+ } catch( IOException e1 ) {
+ e1.printStackTrace() ;
+ System.err.println( "Unable to mark the initial deployment!" ) ;
+ }
+
+ return 1 ;
+ }
+
+
+ public int checkVmx()
+ {
+ // Initial deployment
+ File file = new File( working_directory + "/" + directory + "/initial.hpcvm" ) ;
+
+ if( file.exists() )
+ {
+ deploy = true ;
+ }
+
+ // First execution
+ file = null ;
+ file = new File( working_directory + "/" + directory + "/first.hpcvm" ) ;
+
+ if( file.exists() )
+ {
+ first = true ;
+ }
+
+ // Redeployment after a fault
+ file = null ;
+ file = new File( working_directory + "/" + directory + "/fault.hpcvm" ) ;
+
+ if( file.exists() )
+ {
+ deploy = false ;
+ first = false ;
+ deployFault = true ;
+ }
+
+ file = null ;
+
+ // Deployment of the VM
+
+ if( deploy )
+ {
+ System.out.print( "Saving the initial VMX file ... " ) ;
+
+ String[] command = new String[]{ "/bin/cp",
+ working_directory + "/" + directory + "/" + vmx_name,
+ working_directory + "/" + directory + "/" + vmx_name_initial } ;
- String l ;
try {
- while( (l = b.readLine()) != null )
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
{
- System.err.println( l ) ;
+ System.out.println( "Successfully saved initial the VMX file." ) ;
+ } else {
+ System.err.println( "Unsuccessful save of the initial VMX file!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
}
} catch( IOException e ) {
- e.printStackTrace() ;
+ System.err.println( "Error during initial VMX file save: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.print( "Rewritting WMX file ... " ) ;
+
+
+ FileReader fin = null ;
+ try {
+ fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_initial ) );
+ } catch( FileNotFoundException e ) {
+ System.err.println( "Unable to open the initial VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
}
+
+ BufferedReader bin = new BufferedReader( fin ) ;
+ String line = "" ;
+ FileWriter fout = null ;
+
+ try {
+ fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to open the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ // Keeping only some information
+ // -- this force VmWare to generate new identifiers and MAC address
+ while( line != null )
+ {
+ try {
+ line = bin.readLine() ;
+ if( line != null )
+ if( ! line.contains( "ethernet0.addressType" )
+ && ! line.contains( "uuid.location " )
+ && ! line.contains( "uuid.bios " )
+ && ! line.contains( "ethernet0.generatedAddress " )
+ && ! line.contains( "ethernet0.generatedAddressOffset " )
+ )
+ {
+ fout.write( line + "\n" ) ;
+ }
+ } catch( IOException e ) {}
+ }
+
+ try {
+ bin.close() ;
+ } catch (IOException e) {
+ System.err.println( "Unable to close the initial VMX file!" ) ;
+ e.printStackTrace() ;
+ }
+
+ // Adding the copy information
+ try
+ {
+ fout.write( "msg.autoAnswer = \"TRUE\"\n" ) ;
+ fout.write( "answer.msg.checkpoint.cpufeaturecheck.fail = \"_Yes\"\n" ) ;
+ fout.write( "checkpoint.vmState = \"\"\n" ) ;
+ fout.write( "answer.msg.uuid.altered = \"I _copied it\"\n" ) ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to add infortion to VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ try {
+ fout.close() ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to close the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.println( "Successful rewrite of the VMX file." ) ;
+
+ // Removing the initial mark
+ System.out.print( "Removing the deployment mark ... " ) ;
+
+ command = new String[]{ "/bin/rm",
+ working_directory + "/" + directory + "/initial.hpcvm" } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully deletion of the deployment mark." ) ;
+ } else {
+ System.err.println( "Unsuccessful deletion of the deployment mark!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during deletion of the deployment mark: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ deploy = false ;
+
+ // Writing the first start mark
+ try {
+ FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
+ fw.write( "first!\n" ) ;
+ fw.flush() ;
+ fw.close() ;
+ fw = null ;
+ return 0 ;
+ } catch( IOException e1 ) {
+ e1.printStackTrace() ;
+ System.err.println( "Unable to mark the first run of the VM!" ) ;
+ }
+ }
+
+
+ // First start of the VM
+
+ if( first )
+ {
+ System.out.print( "Saving the first start VMX file ... " ) ;
+
+ String[] command = new String[]{ "/bin/cp",
+ working_directory + "/" + directory + "/" + vmx_name,
+ working_directory + "/" + directory + "/" + vmx_name_first } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully saved the first start VMX file." ) ;
+ } else {
+ System.err.println( "Unsuccessful save of the first start VMX file!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during first start VMX file save: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.print( "Rewritting WMX file ... " ) ;
+
+
+ FileReader fin = null ;
+ try {
+ fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
+ } catch( FileNotFoundException e ) {
+ System.err.println( "Unable to open the first start VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ BufferedReader bin = new BufferedReader( fin ) ;
+ String line = "" ;
+ FileWriter fout = null ;
+
+ try {
+ fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to open the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ // Keeping all information
+ // -- except copy information
+ while( line != null )
+ {
+ try {
+ line = bin.readLine() ;
+ if( line != null )
+ if( ! line.contains( "answer.msg.uuid.altered " ) )
+ {
+ fout.write( line + "\n" ) ;
+ }
+ } catch( IOException e ) {}
+ }
+
+ try {
+ bin.close() ;
+ } catch (IOException e) {
+ System.err.println( "Unable to close the first start VMX file!" ) ;
+ e.printStackTrace() ;
+ }
+
+ try {
+ fout.close() ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to close the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.println( "Successful rewrite of the VMX file." ) ;
+
+ // Removing the initial mark
+ System.out.print( "Removing the first start mark ... " ) ;
+
+ command = new String[]{ "/bin/rm",
+ working_directory + "/" + directory + "/first.hpcvm" } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully deletion of the first start mark." ) ;
+ } else {
+ System.err.println( "Unsuccessful deletion of the first start mark!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during deletion of the first start mark: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ first = false ;
+
+ return 0 ;
}
+
+ // Redeployment after a fault of a VM
+
+ if( deployFault )
+ {
+ System.out.print( "Saving the original VMX file ... " ) ;
+
+ String[] command = new String[]{ "/bin/cp",
+ working_directory + "/" + directory + "/" + vmx_name,
+ working_directory + "/" + directory + "/" + vmx_name_first } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully saved the original VMX file." ) ;
+ } else {
+ System.err.println( "Unsuccessful save of the original VMX file!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during original VMX file save: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.print( "Rewritting WMX file ... " ) ;
+
+
+ FileReader fin = null ;
+ try {
+ fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) );
+ } catch( FileNotFoundException e ) {
+ System.err.println( "Unable to open the original VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ BufferedReader bin = new BufferedReader( fin ) ;
+ String line = "" ;
+ FileWriter fout = null ;
+
+ try {
+ fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to open the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ // Keeping all information
+ while( line != null )
+ {
+ try {
+ line = bin.readLine() ;
+ if( line != null )
+ if( ! line.contains( "answer.msg.uuid.altered " ) )
+ {
+ fout.write( line + "\n" ) ;
+ }
+ } catch( IOException e ) {}
+ }
+
+ try {
+ bin.close() ;
+ } catch (IOException e) {
+ System.err.println( "Unable to close the original VMX file!" ) ;
+ e.printStackTrace() ;
+ }
+
+ // Writing information to indicate that the VM has been moved
+ try {
+ fout.write( "answer.msg.uuid.altered = \"I _moved it\"\n" ) ;
+ } catch (IOException e1) {
+ System.err.println( "Unable to indicate that the VM has been moved in the VMX file!" ) ;
+ e1.printStackTrace() ;
+ }
+
+ try {
+ fout.close() ;
+ } catch( IOException e ) {
+ System.err.println( "Unable to close the VMX file!" ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ System.out.println( "Successful rewrite of the VMX file." ) ;
+
+ // Removing lock files
+ System.out.print( "Removing lock files ... " ) ;
+
+ command = new String[]{ "/bin/rm", "-rf",
+ working_directory + "/" + directory
+ + "/" + vmx_name + ".lck" } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully deleted lock files." ) ;
+ } else {
+ System.err.println( "Unsuccessful deletion of lock files!" ) ;
+// printProcessError( p.getErrorStream() ) ;
+ printProcessError( p ) ;
+
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during lock files deletion: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ // Removing the initial mark
+ System.out.print( "Removing the fault mark ... " ) ;
+
+ command = new String[]{ "/bin/rm",
+ working_directory + "/" + directory + "/fault.hpcvm" } ;
+
+ try {
+ Process p = Runtime.getRuntime().exec( command ) ;
+ p.waitFor() ;
+
+ if( p.exitValue() == 0 )
+ {
+ System.out.println( "Successfully deletion of the fault mark." ) ;
+ } else {
+ System.err.println( "Unsuccessful deletion of the fault mark!" ) ;
+ printProcessError( p ) ;
+ return 1 ;
+ }
+ } catch( IOException e ) {
+ System.err.println( "Error during deletion of the fault mark: " ) ;
+ e.printStackTrace() ;
+ return 1 ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ return 1 ;
+ }
+
+ deployFault = false ;
+
+ // Writing the first start mark
+ try {
+ FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ;
+ fw.write( "first!" ) ;
+ fw.flush() ;
+ fw.close() ;
+ fw = null ;
+ first = true ;
+ return 0 ;
+ } catch( IOException e1 ) {
+ e1.printStackTrace() ;
+ System.err.println( "Unable to mark the first run of the VM!" ) ;
+ }
+ }
+
return 1 ;
}
+
+
+ private void printProcessError( Process _p )
+ {
+ if( _p != null )
+ {
+ System.err.println( "Error: " + _p.exitValue() ) ;
+ BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
+ String line ;
+ try {
+ while( (line = br.readLine()) != null )
+ {
+ System.err.println( line ) ;
+ }
+ } catch( IOException e ) {
+ e.printStackTrace() ;
+ }
+ }
+ }
}