+
+ 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 ;
+ }
+