Author: chatellier Date: 2009-03-13 11:16:31 +0000 (Fri, 13 Mar 2009) New Revision: 1959 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/SimulationMonitor.java isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-cron.seq isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-qsub.seq isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/launcher/SshSimulatorLauncherTest.java Log: Gere les prescript pour les simulations SSH 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-03-13 09:58:41 UTC (rev 1958) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SSHSimulatorLauncher.java 2009-03-13 11:16:31 UTC (rev 1959) @@ -30,6 +30,7 @@ import java.util.Map; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.util.FileUtil; @@ -76,8 +77,9 @@ protected Configuration freemarkerConfiguration; /** Freemarker qsub template. */ - protected static final String SQUB_SCRIPT_TEMPLATE = "templates/ssh/launch-isis-qsub.seq"; - + 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 @@ -194,10 +196,13 @@ // build du contenu du script message(control, _("isisfish.simulation.remote.message.waitingstart")); + String simulationPreScriptPath = uploadPreScriptIfNecessary(sshSession, + control.getId(), simulationPrescript); File simulationScript = getSimulationScriptFile(control.getId(), - simulationRemotePath); + simulationRemotePath, simulationPreScriptPath); String scriptRemotePath = uploadSimulationScript(sshSession, simulationScript); + addScriptToQsubQueue(sshSession, scriptRemotePath); } catch (Exception e) { @@ -561,7 +566,7 @@ String remotePath = IsisFish.config.getSimulatorSshTmpPath(); // remote temp directory should have been created - // by #downloadResults(Session, String) + // by #uploadSimulation(Session, String) // upload directory in that dir if (!remotePath.endsWith("/")) { @@ -580,7 +585,52 @@ 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 + * + * @throws SSHException if upload fail + * @throws IOException if upload fail + */ + protected String uploadPreScriptIfNecessary(Session session, + 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"); + FileUtil.writeString(tempPreScriptFile, simulationPreScript); + String localPath = tempPreScriptFile.getAbsolutePath(); + + String remotePath = IsisFish.config.getSimulatorSshTmpPath(); + // remote temp directory should have been created + // by #uploadSimulation(Session, String) + + // upload directory in that dir + if (!remotePath.endsWith("/")) { + remotePath += "/"; + } + + if (localPath.lastIndexOf("/") > 0) { + remotePath += localPath.substring(localPath.lastIndexOf("/") + 1); + } else if (localPath.lastIndexOf("\\") > 0) { // windows + remotePath += localPath.substring(localPath.lastIndexOf("\\") + 1); + } else { + remotePath += localPath; + } + + SSHUtils.scpTo(session, tempPreScriptFile, remotePath); + + return remotePath; + } + /** * Retourne un fichier temporaire contenant le script de lancement * de simulation. @@ -589,23 +639,20 @@ * * @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 */ protected File getSimulationScriptFile(String simuationId, - String simulationZip) throws IOException { + String simulationZip, String preScriptPath) throws IOException { File tempScript = File.createTempFile("launch-isis", ".seq"); tempScript.deleteOnExit(); // auto delete - // uncomment this to test on a cron server - //String scriptName = "ssh/launch-isis-cron.seq"; - // uncomment this to test on a qsub server - String fileContent = getSimulationScriptLaunchContent( - SQUB_SCRIPT_TEMPLATE, simuationId, simulationZip); + QSUB_SCRIPT_TEMPLATE, simuationId, simulationZip, preScriptPath); FileUtil.writeString(tempScript, fileContent); return tempScript; @@ -620,14 +667,21 @@ * @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 */ protected String getSimulationScriptLaunchContent(String templateName, - String simuationId, String simulationZip) throws IOException { + String simuationId, String simulationZip, String preScriptPath) throws IOException { String scriptContent = null; + // test null values for prescript + String localPreScript = preScriptPath; + if (localPreScript == null) { + localPreScript = ""; + } + try { // get template Template template = freemarkerConfiguration @@ -638,6 +692,7 @@ root.put("isishome", IsisFish.config.getSimulatorSshIsisHome()); root.put("simulationid", simuationId); root.put("simulationzip", simulationZip); + root.put("simulationprescript", localPreScript); // process template Writer out = new StringWriter(); Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationMonitor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationMonitor.java 2009-03-13 09:58:41 UTC (rev 1958) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationMonitor.java 2009-03-13 11:16:31 UTC (rev 1959) @@ -50,7 +50,6 @@ import fr.ifremer.isisfish.simulator.launcher.SimulationJob.PostAction; import fr.ifremer.isisfish.simulator.sensitivity.SensitivityCalculator; import fr.ifremer.isisfish.simulator.sensitivity.SensitivityException; -import fr.ifremer.isisfish.simulator.sensitivity.SensitivityScenarios; /** * Moniteur singleton pour pouvoir sauvegarder Modified: isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-cron.seq =================================================================== --- isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-cron.seq 2009-03-13 09:58:41 UTC (rev 1958) +++ isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-cron.seq 2009-03-13 11:16:31 UTC (rev 1959) @@ -1,7 +1,9 @@ #!/bin/bash cd "${isishome}" -#source /usr/share/modules/init/csh -#module load java/1.6.0 +<#if simulationprescript?length > 0> +java -jar isis-fish*.jar --option launch.ui false --simulateWithSimulationAndScript "${simulationid}" "${simulationzip}" "${simulationprescript}" +<#else> java -jar isis-fish*.jar --option launch.ui false --simulateWithSimulation "${simulationid}" "${simulationzip}" +</#if> \ No newline at end of file Modified: isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-qsub.seq =================================================================== --- isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-qsub.seq 2009-03-13 09:58:41 UTC (rev 1958) +++ isis-fish/trunk/src/main/resources/templates/ssh/launch-isis-qsub.seq 2009-03-13 11:16:31 UTC (rev 1959) @@ -6,4 +6,8 @@ source /usr/share/modules/init/csh module load java/1.6.0 +<#if simulationprescript?length > 0> +java -jar isis-fish*.jar --option launch.ui false --simulateWithSimulationAndScript "${simulationid}" "${simulationzip}" "${simulationprescript}" +<#else> java -jar isis-fish*.jar --option launch.ui false --simulateWithSimulation "${simulationid}" "${simulationzip}" +</#if> \ No newline at end of file Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/launcher/SshSimulatorLauncherTest.java =================================================================== --- isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/launcher/SshSimulatorLauncherTest.java 2009-03-13 09:58:41 UTC (rev 1958) +++ isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/launcher/SshSimulatorLauncherTest.java 2009-03-13 11:16:31 UTC (rev 1959) @@ -22,8 +22,10 @@ import junit.framework.Assert; +import org.junit.BeforeClass; import org.junit.Test; +import fr.ifremer.TestUtils; import fr.ifremer.isisfish.IsisFish; import freemarker.template.TemplateException; @@ -39,6 +41,11 @@ */ public class SshSimulatorLauncherTest { + @BeforeClass + public static void init() throws Exception { + TestUtils.init(); + } + /** * Test l'instantiation du script qsub (caparmor). * @@ -49,16 +56,18 @@ * @throws TemplateException when freemarker throws it */ @Test - public void testFreemarkerTemplate() throws IOException, TemplateException { + public void testFreemarkerTemplateWithScript() throws IOException, + TemplateException { final String SIMULATIONID = "testid"; final String SIMULATIONZIP = "test.zip"; + final String SIMULATIONPRESCRIPT = "prescript-1234567890.txt"; final String ISISHOME = IsisFish.config.getSimulatorSshIsisHome(); SSHSimulatorLauncher launcher = new SSHSimulatorLauncher(); String content = launcher.getSimulationScriptLaunchContent( - SSHSimulatorLauncher.SQUB_SCRIPT_TEMPLATE, SIMULATIONID, - SIMULATIONZIP); + SSHSimulatorLauncher.QSUB_SCRIPT_TEMPLATE, SIMULATIONID, + SIMULATIONZIP, SIMULATIONPRESCRIPT); // simulation parameters Assert.assertTrue("String \"" + SIMULATIONID @@ -67,9 +76,74 @@ Assert.assertTrue("String \"" + SIMULATIONZIP + "\" not found in template", content.indexOf(SIMULATIONZIP) > 0); + Assert.assertTrue("String \"" + SIMULATIONPRESCRIPT + + "\" not found in template", content + .indexOf(SIMULATIONPRESCRIPT) > 0); + Assert.assertTrue( + "Action --simulateWithSimulationAndScript not on script", + content.indexOf(" --simulateWithSimulationAndScript ") > 0); // isis location Assert.assertTrue("String \"" + ISISHOME + "\" not found in template", content.indexOf(ISISHOME) > 0); } + + /** + * Test l'instantiation du script qsub (caparmor). + * + * Test sans script de pre simu. + * + * Le script contient une variable $simulation qui + * doit être remplacée. + * + * @throws IOException when freemarker throws it + * @throws TemplateException when freemarker throws it + */ + @Test + public void testFreemarkerTemplateWithoutScript() throws IOException, + TemplateException { + + final String SIMULATIONID = "testid"; + final String SIMULATIONZIP = "test.zip"; + final String SIMULATIONPRESCRIPT = null; + final String ISISHOME = IsisFish.config.getSimulatorSshIsisHome(); + + SSHSimulatorLauncher launcher = new SSHSimulatorLauncher(); + String content = launcher.getSimulationScriptLaunchContent( + SSHSimulatorLauncher.QSUB_SCRIPT_TEMPLATE, SIMULATIONID, + SIMULATIONZIP, SIMULATIONPRESCRIPT); + + // simulation parameters + Assert.assertTrue("String \"" + SIMULATIONID + + "\" not found in template", content + .indexOf(SIMULATIONID) > 0); + Assert.assertTrue("String \"" + SIMULATIONZIP + + "\" not found in template", + content.indexOf(SIMULATIONZIP) > 0); + Assert.assertTrue("Action --simulateWithSimulation not on script", + content.indexOf(" --simulateWithSimulation ") > 0); + + // isis location + Assert.assertTrue("String \"" + ISISHOME + "\" not found in template", + content.indexOf(ISISHOME) > 0); + } + + /** + * Pour tester, un autre script est present, mais + * celui ci ne doit jamais être utilisé hors des tests. + * + * Test que le script contient "!/bin/csh". + * @throws IOException + */ + @Test + public void testValidQsubScript() throws IOException { + SSHSimulatorLauncher launcher = new SSHSimulatorLauncher(); + String content = launcher.getSimulationScriptLaunchContent( + SSHSimulatorLauncher.QSUB_SCRIPT_TEMPLATE, "", + "", ""); + + // simulation parameters + Assert.assertTrue("Invalid qsub script is used", content + .indexOf("!/bin/csh") > 0); + } } \ No newline at end of file