r2134 - isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher
Author: jcouteau Date: 2009-04-22 08:44:10 +0000 (Wed, 22 Apr 2009) New Revision: 2134 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java Log: Temporary bug fix on caparmor simulations Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java 2009-04-21 12:55:06 UTC (rev 2133) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java 2009-04-22 08:44:10 UTC (rev 2134) @@ -58,18 +58,16 @@ /** * Use a remote simulation server. * - * Upload zip simulation file on server and launch - * simulation on that file. + * Upload zip simulation file on server and launch simulation on that file. * * Isis-Fish must be installed on remote server. - * + * * @see JSch * * @author chatellier * @version $Revision: 1.0 $ * - * Last update : $Date: 18 nov. 2008 $ - * By : $Author: chatellier $ + * Last update : $Date: 18 nov. 2008 $ By : $Author: chatellier $ */ public class SSHSimulatorLauncher implements SimulatorLauncher { @@ -78,15 +76,14 @@ /** Freemarker configuration */ protected Configuration freemarkerConfiguration; - + /** Freemarker qsub template. */ protected static final String QSUB_SCRIPT_TEMPLATE = "templates/ssh/launch-isis-qsub.seq"; //protected static final String QSUB_SCRIPT_TEMPLATE = "templates/ssh/launch-isis-cron.seq"; - + /** - * Opened session. - * Stored in static context to not reask passphrase - * at each connection. + * Opened session. Stored in static context to not reask passphrase at each + * connection. */ protected static Session sshSession; @@ -121,8 +118,10 @@ /** * Display message both in commons-logging and control text progress. * - * @param control control - * @param message message to display + * @param control + * control + * @param message + * message to display */ protected void message(SimulationControl control, String message) { // log @@ -171,19 +170,15 @@ /** * {@inheritDoc} * - * Dans le cas de ssh: - * - upload la simulation - * - construit le script pour qsub - * - upload le script qsub - * - ajoute le script a qsub + * Dans le cas de ssh: - upload la simulation - construit le script pour + * qsub - upload le script qsub - ajoute le script a qsub * - * Et : - * - lance le thread de control de la simulation + * Et : - lance le thread de control de la simulation */ @Override public void simulate(SimulationService simulationService, - SimulationControl control, File simulationZip, String simulationPrescript) - throws RemoteException { + SimulationControl control, File simulationZip, + String simulationPrescript) throws RemoteException { // start ssh session try { @@ -199,13 +194,13 @@ // build du contenu du script message(control, _("isisfish.simulation.remote.message.waitingstart")); - String simulationPreScriptPath = uploadPreScriptIfNecessary(sshSession, - control.getId(), simulationPrescript); + String simulationPreScriptPath = uploadPreScriptIfNecessary( + sshSession, control.getId(), simulationPrescript); File simulationScript = getSimulationScriptFile(control.getId(), simulationRemotePath, simulationPreScriptPath); String scriptRemotePath = uploadSimulationScript(sshSession, simulationScript); - + addScriptToQsubQueue(sshSession, scriptRemotePath); } catch (Exception e) { @@ -220,7 +215,8 @@ /** * {@inheritDoc} * - * Se connecte au serveur distance et tétécharge les résultats de la simulation. + * Se connecte au serveur distance et tétécharge les résultats de la + * simulation. */ @Override public SimulationStorage getSimulationStorage( @@ -249,8 +245,7 @@ /** * {@inheritDoc} * - * Se connecte au serveur distance et télécharge le - * fichier de control. + * Se connecte au serveur distance et télécharge le fichier de control. * Injecte ensuite ce fichier dans le {@link SimulationControl}. */ @Override @@ -260,12 +255,13 @@ try { try { // download control file - File controlFile = downloadSimulationFile(control.getId(), SimulationStorage.CONTROL_FILENAME); + File controlFile = downloadSimulationFile(control.getId(), + SimulationStorage.CONTROL_FILENAME); if (log.isDebugEnabled()) { log.debug("Control have been downloaded : " + controlFile.getAbsolutePath()); } - + synchronized (control) { // le thread principal a pu le modifier pendant le sleep //if (control.isRunning()) { @@ -274,7 +270,7 @@ SimulationStorage.readControl(controlFile, control, "stop"); //} } - + // deleteTempFile controlFile.delete(); } catch (SSHException e) { @@ -285,10 +281,11 @@ .getMessage())); } } - + try { // download information file - File infoFile = downloadSimulationFile(control.getId(), SimulationStorage.INFORMATION_FILENAME); + File infoFile = downloadSimulationFile(control.getId(), + SimulationStorage.INFORMATION_FILENAME); if (log.isDebugEnabled()) { log.debug("Information have been downloaded : " + infoFile.getAbsolutePath()); @@ -299,13 +296,13 @@ // d'arret pour qu'elle s'arrete dans l'UI Properties infoProperties = new Properties(); infoProperties.load(new FileInputStream(infoFile)); - if (!StringUtils.isEmpty(infoProperties.getProperty("exception"))) { + if (!StringUtils.isEmpty(infoProperties + .getProperty("exception"))) { synchronized (control) { control.setStopSimulationRequest(true); } } - - + // deleteTempFile infoFile.delete(); } catch (SSHException e) { @@ -330,7 +327,7 @@ * This method must synchronized. * * @return opened ssh session. - * @throws JSchException + * @throws JSchException */ protected synchronized Session getSSHSession() throws JSchException { @@ -397,8 +394,7 @@ try { passphrase = SSHAgent.getAgent().getPassphrase(sshKey); ui.setPassphrase(passphrase); - } - catch(InvalidPassphraseException e) { + } catch (InvalidPassphraseException e) { if (log.isWarnEnabled()) { log.warn("Can't key passphrase for key", e); } @@ -417,7 +413,8 @@ /** * Close ssh session. * - * @param session session to close + * @param session + * session to close */ protected void closeSSHSession(Session session) { if (session != null) { @@ -429,9 +426,11 @@ * Add ssh key into $HOME/.ssh/authorized_keys file. * * Just connect and do an "echo xx >> .ssh/authorized_keys" - * @param session opened session + * + * @param session + * opened session * @param sshKey - * @throws JSchException + * @throws JSchException */ protected void putSshKeyOnRemoteServer(Session session, File sshKey) throws JSchException { @@ -464,12 +463,16 @@ } } catch (IOException e) { if (log.isErrorEnabled()) { - log.error(_("Error while uploading public key to remote serveur authorized_keys"), + log + .error( + _("Error while uploading public key to remote serveur authorized_keys"), e); } } catch (SSHException e) { if (log.isErrorEnabled()) { - log.error(_("Error while uploading public key to remote serveur authorized_keys"), + log + .error( + _("Error while uploading public key to remote serveur authorized_keys"), e); } } @@ -478,11 +481,14 @@ /** * Upload simulation file to server. * - * @param session already open valid ssh session - * @param simulationFile simulation file to upload + * @param session + * already open valid ssh session + * @param simulationFile + * simulation file to upload * * @return remote file path or <tt>null</tt> if errors - * @throws SSHException if upload fail + * @throws SSHException + * if upload fail */ protected String uploadSimulation(Session session, File simulationFile) throws SSHException { @@ -491,9 +497,14 @@ // first check that remote directory exists String remotePath = IsisFish.config.getSimulatorSshTmpPath(); + + // FIXME This is a workaround to make the simulations work on caparmor + // FIXME We need to find a way to make it work the old way. - String command = "if [ ! -d \"" + remotePath + "\" ] ; then mkdir \"" - + remotePath + "\"; fi"; + //String command = "if [ ! -d \"" + remotePath + "\" ] ; then mkdir \"" + // + remotePath + "\"; fi"; + String command = "if ( ! -d \"" + remotePath + "\" ) mkdir \"" + + remotePath + "\""; if (log.isInfoEnabled()) { log.info("Creating remote temp directory (if not exists) " + remotePath); @@ -511,7 +522,7 @@ if (!remotePath.endsWith("/")) { remotePath += "/"; } - + if (localPath.lastIndexOf("/") > 0) { remotePath += localPath.substring(localPath.lastIndexOf("/") + 1); } else if (localPath.lastIndexOf("\\") > 0) { // windows @@ -530,7 +541,8 @@ * * Simulation result is save in {@link SimulationStorage#SIMULATION_PATH}. * - * @throws SSHException if download fail (can happen if remote file doesn't exist + * @throws SSHException + * if download fail (can happen if remote file doesn't exist */ protected void downloadResults(Session session, String simulationId) throws SSHException { @@ -542,43 +554,46 @@ if (log.isDebugEnabled()) { log.debug("Downloading results in " + localFile.getAbsolutePath()); } - + // Cant happen if result have already been downloded // Or try to, but has been interrupted // delete and download again File simulationBaseDirectory = new File(localFile, simulationId); if (simulationBaseDirectory.exists()) { if (log.isWarnEnabled()) { - log.warn("Warning, simulation results " + simulationBaseDirectory.getAbsolutePath() + " already exists"); + log.warn("Warning, simulation results " + + simulationBaseDirectory.getAbsolutePath() + + " already exists"); log.warn("Delete it, and download again"); } - + FileUtil.deleteRecursively(simulationBaseDirectory); } - + // build remote file path // FIXME this path should be given by remote IsisFish app String remoteFile = IsisFish.config.getSimulatorSshDataPath(); remoteFile += "/" + SimulationStorage.SIMULATION_PATH; remoteFile += "/" + simulationId; - - SSHUtils.scpFrom(session, remoteFile, localFile); } /** - * Download remote simulation control file and store - * its content into temp file. + * Download remote simulation control file and store its content into temp + * file. * - * @param simulationId id de la simulation - * @param fileName nom du fichier a telecharger + * @param simulationId + * id de la simulation + * @param fileName + * nom du fichier a telecharger * @return downloaded temp file (file have to be manually deleted) - * @throws IOException - * @throws SSHException if remote file doesn't exists + * @throws IOException + * @throws SSHException + * if remote file doesn't exists */ - protected File downloadSimulationFile(String simulationId, String fileName) throws IOException, - SSHException { + protected File downloadSimulationFile(String simulationId, String fileName) + throws IOException, SSHException { File localFile = null; @@ -605,10 +620,13 @@ /** * Upload script on remote server. * - * @param session valid opened ssh session - * @param simulationScript file to upload + * @param session + * valid opened ssh session + * @param simulationScript + * file to upload * - * @throws SSHException if upload fail + * @throws SSHException + * if upload fail */ protected String uploadSimulationScript(Session session, File simulationScript) throws SSHException { @@ -623,7 +641,7 @@ if (!remotePath.endsWith("/")) { remotePath += "/"; } - + if (localPath.lastIndexOf("/") > 0) { remotePath += localPath.substring(localPath.lastIndexOf("/") + 1); } else if (localPath.lastIndexOf("\\") > 0) { // windows @@ -636,27 +654,33 @@ return remotePath; } - + /** * Upload pre script on remote server. * * Return path if uploaded or null if no upload needed. * - * @param session valid opened ssh session - * @param simulationPreScript script content + * @param session + * valid opened ssh session + * @param simulationPreScript + * script content * - * @throws SSHException if upload fail - * @throws IOException if upload fail + * @throws SSHException + * if upload fail + * @throws IOException + * if upload fail */ protected String uploadPreScriptIfNecessary(Session session, - String simulationId, String simulationPreScript) throws SSHException, IOException { + String simulationId, String simulationPreScript) + throws SSHException, IOException { // if there is no pre script, do nothings if (StringUtils.isEmpty(simulationPreScript)) { return null; } - - File tempPreScriptFile = File.createTempFile("isis-" + simulationId + "-", ".prescript"); + + File tempPreScriptFile = File.createTempFile("isis-" + simulationId + + "-", ".prescript"); FileUtil.writeString(tempPreScriptFile, simulationPreScript); String localPath = tempPreScriptFile.getAbsolutePath(); @@ -668,7 +692,7 @@ if (!remotePath.endsWith("/")) { remotePath += "/"; } - + if (localPath.lastIndexOf("/") > 0) { remotePath += localPath.substring(localPath.lastIndexOf("/") + 1); } else if (localPath.lastIndexOf("\\") > 0) { // windows @@ -683,18 +707,22 @@ } /** - * Retourne un fichier temporaire contenant le script de lancement - * de simulation. + * Retourne un fichier temporaire contenant le script de lancement de + * simulation. * * Le fichier temporaire est configuré pour se supprimer tout seul. * - * @param simuationId id de la simulation - * @param simulationZip zip de la simulation - * @param preScriptPath simulation pre script path (can be null) + * @param simuationId + * id de la simulation + * @param simulationZip + * zip de la simulation + * @param preScriptPath + * simulation pre script path (can be null) * * @return un Fichier temporaire ou <tt>null</tt> en cas d'exception * - * @throws IOException if can't build script + * @throws IOException + * if can't build script */ protected File getSimulationScriptFile(String simuationId, String simulationZip, String preScriptPath) throws IOException { @@ -710,20 +738,25 @@ } /** - * Utilise freemarker pour recuperer le contenu - * du script. + * Utilise freemarker pour recuperer le contenu du script. * * Remplace aussi la variable $simulation du template. * - * @param templateName url du template - * @param simuationId id de la simulation - * @param simulationZip zip de la simulation - * @param preScriptPath simulation pre script path (can be null) + * @param templateName + * url du template + * @param simuationId + * id de la simulation + * @param simulationZip + * zip de la simulation + * @param preScriptPath + * simulation pre script path (can be null) * - * @throws IOException if can't get script content + * @throws IOException + * if can't get script content */ protected String getSimulationScriptLaunchContent(String templateName, - String simuationId, String simulationZip, String preScriptPath) throws IOException { + String simuationId, String simulationZip, String preScriptPath) + throws IOException { String scriptContent = null; @@ -732,7 +765,7 @@ if (localPreScript == null) { localPreScript = ""; } - + try { // get template Template template = freemarkerConfiguration @@ -765,10 +798,13 @@ /** * Add script in remote qsub queue. * - * @param session valid opened session - * @param scriptRemotePath remote script path + * @param session + * valid opened session + * @param scriptRemotePath + * remote script path * - * @throws SSHException if call fail + * @throws SSHException + * if call fail */ protected void addScriptToQsubQueue(Session session, String scriptRemotePath) throws SSHException {
participants (1)
-
jcouteau@users.labs.libre-entreprise.org