import com.akraino.bpm.Exception.TaskExecutorException;
import com.akraino.bpm.service.DeploymentVerificationService;
-
@Service("deploymentVerificationService")
public class DeploymentverificationServiceImpl implements DeploymentVerificationService{
private static Logger logger = LoggerFactory.getLogger(DeploymentverificationServiceImpl.class);
-
- public void executeScript(String filepatch,int waitttime,int iterations) {
-
+
+ /**
+ * Execute a script. potentially several times.
+ * @param filepatch the file to execute
+ * @param waitttime how long to wait (in seconds) between executions
+ * @param iterations the maximum number of iterations
+ */
+ public void executeScript(String filepatch, int waitttime, int iterations) {
+
boolean issuccess=false;
for( int i=0;i<=iterations;i++) {
try {
Process p = Runtime.getRuntime().exec(filepatch);
p.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
- String line = "";
- while ((line = input.readLine()) != null) {
- if(line.equals("0")) {
- issuccess=true;
- }
- }
-
- logger.debug("Script exit code :"+p.exitValue());
- if(p.exitValue()!=0) {
- throw new TaskExecutorException("problem while executing the script . exist code :"+p.exitValue());
- }
- } catch (IOException e) {
- throw new TaskExecutorException(filepatch + " not found.");
+ String line = "";
+ while ((line = input.readLine()) != null) {
+ if (line.equals("0")) {
+ issuccess=true;
+ }
+ }
+
+ logger.debug("Script exit code :"+p.exitValue());
+ if(p.exitValue()!=0) {
+ throw new TaskExecutorException("problem while executing the script. exit code :"+p.exitValue());
+ }
+ } catch (IOException e) {
+ throw new TaskExecutorException(filepatch + " not found.");
} catch (InterruptedException e) {
- throw new TaskExecutorException("problem while executing the script "+filepatch);
+ throw new TaskExecutorException("problem while executing the script "+filepatch);
}
if(issuccess) {
break;
}
-
}
-
- if(!issuccess) {
- logger.debug("verification script returned 1 ");
- throw new TaskExecutorException("1");
- }
- }
+ if(!issuccess) {
+ logger.debug("verification script returned 1 ");
+ throw new TaskExecutorException("1");
+ }
+ }
}
@Service("fileTransferService")
public class FileTransferServiceImpl implements FileTransferService {
-
+
private static Logger logger = LoggerFactory.getLogger(FileTransferServiceImpl.class);
public void transferFile(String srcdir, String destdir, String filename,String servername,String username,String password,int port) {
-
- logger.debug("file transfor filename={},srcdir={},destdir={}",filename,srcdir,destdir);
- ChannelSftp sftpChannel=null;
- try {
- JSch jsch = new JSch();
- Session session = jsch.getSession(username, servername, port);
- session.setConfig("StrictHostKeyChecking", "no");
- session.setPassword(password);
- session.connect();
- sftpChannel = (ChannelSftp)session.openChannel("sftp");
- sftpChannel.setPty(true);
- sftpChannel.connect();
- sftpChannel.cd(destdir);
- sftpChannel.put(new FileInputStream(new File (srcdir+"/"+filename)), filename);
- sftpChannel.chmod(Integer.parseInt("777", 8),filename);
- } catch (Exception e) {
- logger.error("Exception occured while ftp : "+e);
- throw new TaskExecutorException("problem while transfering the file to remote machine :"+e.getMessage());
- } finally {
- sftpChannel.disconnect();
- }
+
+ logger.debug("file transfor filename={},srcdir={},destdir={}",filename,srcdir,destdir);
+ ChannelSftp sftpChannel=null;
+ try {
+ JSch jsch = new JSch();
+ Session session = jsch.getSession(username, servername, port);
+ session.setConfig("StrictHostKeyChecking", "no");
+ session.setPassword(password);
+ session.connect();
+ sftpChannel = (ChannelSftp)session.openChannel("sftp");
+ sftpChannel.setPty(true);
+ sftpChannel.connect();
+ sftpChannel.cd(destdir);
+ sftpChannel.put(new FileInputStream(new File (srcdir+"/"+filename)), filename);
+ sftpChannel.chmod(Integer.parseInt("777", 8),filename);
+ } catch (Exception e) {
+ logger.error("Exception occurred while FTP : "+e);
+ throw new TaskExecutorException("problem while transferring the file to remote machine :"+e.getMessage());
+ } finally {
+ if (sftpChannel != null)
+ sftpChannel.disconnect();
+ }
}
-
public void transferFile(String srcdir, String destdir,String servername,String username,String password,int port) {
-
+
List<File> files=getAllfiles(new File(srcdir));
for(File file: files) {
transferFile(srcdir,destdir,file.getName(),servername,username,password,port);
}
-
-
}
-
+
private List<File> getAllfiles(File rootDirectory){
List<File> results = new ArrayList<File>();
-
+
if(rootDirectory==null) {
- throw new TaskExecutorException("problem while transfering the file to remote machine : src diectory Not found");
- }
-
- for(File currentItem : rootDirectory.listFiles()){
- if(currentItem.isDirectory()){
- results.addAll(getAllfiles(currentItem));
- }
- else{
- results.add(currentItem);
- }
+ throw new TaskExecutorException("problem while transferring the file to remote machine : src directory Not found");
}
+
+ for(File currentItem : rootDirectory.listFiles()){
+ if(currentItem.isDirectory()){
+ results.addAll(getAllfiles(currentItem));
+ }
+ else{
+ results.add(currentItem);
+ }
+ }
return results;
}
-
-
}
import java.io.InputStream;
import java.io.InputStreamReader;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
private static Logger logger = LoggerFactory.getLogger(RemoteScriptExecutionServiceImpl.class);
+ /**
+ * Execute a command remotely
+ * @param remoteserver the remote server to execute on
+ * @param username the user name on the remote server
+ * @param password the password for the user
+ * @param portnumber the port number on the remote server
+ * @param filename not used
+ * @param fileparams not used
+ * @param srcdir not used
+ * @param destdir not used
+ * @param command the command to execute
+ */
public void executeRemoteScript(String remoteserver,String username,String password,int portnumber,String filename,
String fileparams,String srcdir,String destdir,String command) {
String line;
logger.debug("Script output......................");
while ((line = reader.readLine()) != null){
- logger.debug(line);
+ logger.debug(line);
}
channelExec.disconnect();
- while (!channelExec.isClosed()) {
-
- }
+ while (!channelExec.isClosed()) {
+ // nothing?!
+ }
int exitStatus = channelExec.getExitStatus();
logger.debug("Script exit code :"+exitStatus);
if(exitStatus!=0) {
- throw new TaskExecutorException("problem while executing the script . exist code :"+exitStatus);
+ throw new TaskExecutorException("problem while executing the script. exit code :"+exitStatus);
}
throw new TaskExecutorException(filename + " not found.");
} catch (Exception e) {
throw new TaskExecutorException("Problem while executing script"+e.getMessage());
- }finally{
+ } finally {
if(reader!=null) {
try {
reader.close();
if(session!=null) {
session.disconnect();
}
- }
-
-
-
+ }
}
-
-
-
-
}
-
-
private static Logger logger = LoggerFactory.getLogger(ScriptExecutionServiceImpl.class);
+ /**
+ * Execute a script
+ * @param filepatch the script to execute
+ */
public void executeScript(String filepatch) {
try {
p.waitFor();
logger.debug("Script exit code :"+p.exitValue());
if(p.exitValue()!=0) {
- throw new TaskExecutorException("problem while executing the script . exit code :"+p.exitValue());
+ throw new TaskExecutorException("problem while executing the script. exit code :"+p.exitValue());
}
} catch (IOException e) {
}
}
- public void executeCDScript(String dir,String cmd) {
-
- try {
- logger.debug("Executing the script.............dir:{},command:{}",dir,cmd);
-
- String[] command = { "/bin/bash", "-c", "bash "+cmd };
- Process p = Runtime.getRuntime().exec(command, null, new File(dir));
- p.waitFor();
- BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
- String line = "";
- while ((line = input.readLine()) != null) {
- logger.debug(line);
- }
- logger.debug("Script exit code :"+p.exitValue());
- if(p.exitValue()!=0) {
- throw new TaskExecutorException("problem while executing the script . exist code :"+p.exitValue());
- }
-
-
- } catch (IOException e) {
- throw new TaskExecutorException(cmd + " not found.");
- } catch (InterruptedException e) {
- throw new TaskExecutorException("problem while executing the script "+cmd);
- }
-
- }
-
- public void executeCDBashScript(String dir,String cmd) {
-
- try {
- logger.debug("Executing the script.............dir:{},command:{}",dir,cmd);
-
- String[] command = { "/bin/bash", "-c", "bash "+cmd };
- Process p = Runtime.getRuntime().exec(command, null, new File(dir));
- p.waitFor();
- BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
- String line = "";
- while ((line = input.readLine()) != null) {
- logger.debug(line);
- }
- logger.debug("Script exit code :"+p.exitValue());
- if(p.exitValue()!=0) {
- throw new TaskExecutorException("problem while executing the script . exit code :"+p.exitValue());
- }
+ /**
+ * Execute a command in a directory
+ * @param dir the directory to execute in
+ * @param cmd the command to execute
+ */
+ public void executeCDScript(String dir,String cmd) {
+ try {
+ logger.debug("Executing the script.............dir:{},command:{}",dir,cmd);
- } catch (IOException e) {
- throw new TaskExecutorException(cmd + " not found.");
- } catch (InterruptedException e) {
- throw new TaskExecutorException("problem while executing the script "+cmd);
+ String[] command = { "/bin/bash", "-c", "bash "+cmd };
+ Process p = Runtime.getRuntime().exec(command, null, new File(dir));
+ p.waitFor();
+ BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ while ((line = input.readLine()) != null) {
+ logger.debug(line);
}
-
+ logger.debug("Script exit code :"+p.exitValue());
+ if(p.exitValue()!=0) {
+ throw new TaskExecutorException("problem while executing the script. exit code :"+p.exitValue());
+ }
+ } catch (IOException e) {
+ throw new TaskExecutorException(cmd + " not found.");
+ } catch (InterruptedException e) {
+ throw new TaskExecutorException("problem while executing the script "+cmd);
+ }
}
+ /**
+ * Execute a command in a directory
+ * @param dir the directory to execute in
+ * @param cmd the command to execute
+ */
+ public void executeCDBashScript(String dir,String cmd) {
+ try {
+ logger.debug("Executing the script.............dir:{},command:{}",dir,cmd);
+ String[] command = { "/bin/bash", "-c", "bash "+cmd };
+ Process p = Runtime.getRuntime().exec(command, null, new File(dir));
+ p.waitFor();
+ BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ while ((line = input.readLine()) != null) {
+ logger.debug(line);
+ }
+ logger.debug("Script exit code :"+p.exitValue());
+ if(p.exitValue()!=0) {
+ throw new TaskExecutorException("problem while executing the script . exit code :"+p.exitValue());
+ }
+ } catch (IOException e) {
+ throw new TaskExecutorException(cmd + " not found.");
+ } catch (InterruptedException e) {
+ throw new TaskExecutorException("problem while executing the script "+cmd);
+ }
+ }
- /*public void executeAirshipScript(String cmd) {
-
- try {
- logger.debug("Executing the script.............{}",cmd);
- ProcessBuilder pb = new ProcessBuilder(cmd);
- Process shellProcess = pb.start();
-
- shellProcess.waitFor();
- BufferedReader input = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()));
- String line = "";
- while ((line = input.readLine()) != null) {
- logger.debug(line);
- }
- logger.debug("Script exit code :"+shellProcess.exitValue());
- if(shellProcess.exitValue()!=0) {
- throw new TaskExecutorException("problem while executing the script . exist code :"+shellProcess.exitValue());
- }
-
+ /*public void executeAirshipScript(String cmd) {
- } catch (IOException e) {
- throw new TaskExecutorException(cmd + " not found.");
- } catch (InterruptedException e) {
- throw new TaskExecutorException("problem while executing the script "+cmd);
+ try {
+ logger.debug("Executing the script.............{}",cmd);
+ ProcessBuilder pb = new ProcessBuilder(cmd);
+ Process shellProcess = pb.start();
+
+ shellProcess.waitFor();
+ BufferedReader input = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()));
+ String line = "";
+ while ((line = input.readLine()) != null) {
+ logger.debug(line);
}
+ logger.debug("Script exit code :"+shellProcess.exitValue());
+ if(shellProcess.exitValue()!=0) {
+ throw new TaskExecutorException("problem while executing the script. exit code :"+shellProcess.exitValue());
+ }
+
+ } catch (IOException e) {
+ throw new TaskExecutorException(cmd + " not found.");
+ } catch (InterruptedException e) {
+ throw new TaskExecutorException("problem while executing the script "+cmd);
+ }
}*/
}