Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

SubmitScriptHandler.java

Go to the documentation of this file.
00001 package cedar.jetweb.batch;
00002 
00003 import java.io.BufferedWriter;
00004 import java.io.BufferedReader;
00005 import java.io.IOException;
00006 import java.io.FileInputStream;
00007 import java.io.File;
00008 import java.io.FileWriter;
00009 
00010 import java.util.Vector;
00011 import java.util.Hashtable;
00012 
00013 import java.io.InputStreamReader;
00014 
00015 import cedar.jetweb.JetWebConfig;
00016 import cedar.jetweb.job.JobRequest;
00017 import cedar.jetweb.generator.Generator;
00018 
00026 public abstract class SubmitScriptHandler {
00027     
00029     private static Hashtable<String,String> scriptTemplates   = null;
00030 
00032     private static Hashtable<String,String> executableNames = null;
00033 
00034     // return codes.
00035     /* Specifies successful script creation. */
00036     public static final int OK                   = 0;
00037     public static final int INVALIDJOB           = 2;
00038     public static final int CANTWRITESCRIPT      = 3;
00039     public static final int CANTCHMOD            = 4;
00040     public static final int CANTOPENPERMITSCRIPT = 5;
00041     public static final int NODEFINEDFACILITY    = 6;
00042     
00043     public static final int UCLNQS        = 1;
00044     public static final int MCRPBS        = 2;
00045     public static final int GRID          = 3;
00046     public static final int UCLPBS        = 4;
00047     public static final int SHEFPBS       = 5;
00048 
00054     public static int BATCHFACILITY = MCRPBS;
00055 
00056     /* List of available batch facilities, for use by other classes, e.g. HTMLWriter. */
00057     public static Vector<Integer> batchNumbers = new Vector<Integer>();
00058     public static Vector<String> batchNames   = new Vector<String>();
00059     static {
00060     batchNumbers.add(new Integer(MCRPBS)); batchNames.add("Manchester PBS");
00061     batchNumbers.add(new Integer(UCLPBS)); batchNames.add("UCL PBS");
00062     batchNumbers.add(new Integer(UCLNQS)); batchNames.add("UCL NQS");
00063     batchNumbers.add(new Integer(GRID)); batchNames.add("GridPP");
00064     batchNumbers.add(new Integer(SHEFPBS)); batchNames.add("Sheffield PBS");
00065     }
00066 
00077     public static int make(JobRequest request){
00078 
00079     if (BATCHFACILITY==UCLNQS){ 
00080         System.out.println("Making a UCL NQS request");
00081         return makeUCLNQS(request);
00082     } else if (BATCHFACILITY==MCRPBS){ 
00083         System.out.println("Making a Manchester request");
00084         return makeMCRPBS(request);
00085     } else if (BATCHFACILITY==GRID){ 
00086         System.out.println("Making a GridPP request");
00087         return makeGridPP(request);
00088     } else if (BATCHFACILITY==UCLPBS){ 
00089         System.out.println("Making a UCL PBS request");
00090         return makeUCLNQS(request);   // OK since spool script is same for NQS/PBS
00091     } else if (BATCHFACILITY==SHEFPBS){ 
00092         System.out.println("Making a Sheffield PBS request");
00093         return makeSHEFPBS(request);
00094     } else {
00095         System.out.println("Error: "+NODEFINEDFACILITY);
00096         return NODEFINEDFACILITY;
00097     }
00098     }
00099 
00100 
00101     private static int makeUCLNQS(JobRequest request){
00102 
00103     String line;
00104     try {
00105         BufferedWriter spoolscript = new BufferedWriter(new FileWriter(request.getSubmitScript()));
00106         
00107         line = "#!/bin/csh";
00108         spoolscript.write(line); spoolscript.newLine();
00109         
00110         line = "cd ${JETWEB_HOME}/run"+System.getProperty("runsubdir");
00111         spoolscript.write(line); spoolscript.newLine();
00112         
00113         line = "qsub -q bulk "+request.getJobName();
00114         spoolscript.write(line); spoolscript.newLine();
00115         
00116         spoolscript.close();
00117         
00118     } catch ( IOException e ) {
00119         System.out.println("problem creating submit script:"+e);
00120         return CANTWRITESCRIPT;
00121     }
00122     
00123     // Now change the permissions.
00124     return setPermissions(request);
00125     }
00126 
00127 
00128     private static int makeMCRPBS(JobRequest request){
00129 
00130     String line;
00131     try {
00132         BufferedWriter spoolscript = new BufferedWriter(new FileWriter(request.getSubmitScript()));
00133         
00134         line = "#!/bin/csh";
00135         spoolscript.write(line); spoolscript.newLine();
00136         
00137         line = "cd ${JETWEB_HOME}/run"+System.getProperty("runsubdir");
00138         spoolscript.write(line); spoolscript.newLine();
00139 
00140         line = "scp "+request.getJobName()
00141         +" mcuser@dfa.hep.man.ac.uk:/home2/mcuser/JetWeb/run";
00142         spoolscript.write(line); spoolscript.newLine();
00143 
00144         line = "scp "+request.getJobName()
00145         +".input mcuser@dfa.hep.man.ac.uk:/home2/mcuser/JetWeb/run";
00146         spoolscript.write(line); spoolscript.newLine();
00147         
00148         line = "ssh mcuser@dfa.hep.man.ac.uk \" cd /home2/mcuser/JetWeb/run ; qsub -q long \""
00149         +request.getJobName();      
00150         spoolscript.write(line); spoolscript.newLine();
00151         
00152         spoolscript.close();
00153         
00154     } catch ( IOException e ) {
00155         System.out.println("problem creating submit script:"+e);
00156         return CANTWRITESCRIPT;
00157     }
00158     
00159     // Now change the permissions.
00160     return setPermissions(request);
00161     }
00162 
00163     private static int makeSHEFPBS(JobRequest request){
00164 
00165     String line;
00166     try {
00167         BufferedWriter spoolscript = new BufferedWriter(new FileWriter(request.getSubmitScript()));
00168         
00169         line = "#!/bin/csh";
00170         spoolscript.write(line); spoolscript.newLine();
00171         
00172         line = "cd ${JETWEB_HOME}/run"+System.getProperty("runsubdir");
00173         spoolscript.write(line); spoolscript.newLine();
00174 
00175         line = "scp "+request.getJobName()
00176         +" jetweb@hepgrid.shef.ac.uk:/home/jetweb/run";
00177         spoolscript.write(line); spoolscript.newLine();
00178 
00179         line = "scp "+request.getJobName()
00180         +".input jetweb@hepgrid.shef.ac.uk:/home/jetweb/run";
00181         spoolscript.write(line); spoolscript.newLine();
00182         
00183         line = "ssh jetweb@hepgrid.shef.ac.uk \" cd /home/jetweb/run ; qsub -q himem \""
00184         +request.getJobName();
00185         spoolscript.write(line); spoolscript.newLine();
00186         
00187         spoolscript.close();
00188         
00189     } catch ( IOException e ) {
00190         System.out.println("problem creating submit script:"+e);
00191         return CANTWRITESCRIPT;
00192     }
00193     
00194     // Now change the permissions.
00195     return setPermissions(request);
00196     }
00197 
00198     private static int makeGridPP(JobRequest request){
00199 
00200     String line;
00201     try {
00202 
00203         System.out.println("submit script = "+request.getSubmitScript());
00204 
00205         BufferedWriter spoolscript = new BufferedWriter(new FileWriter(request.getSubmitScript()));
00206         
00207 
00208         System.out.println("spoolscript = "+spoolscript);
00209 
00210         line = "#!/bin/csh";
00211         spoolscript.write(line); spoolscript.newLine();
00212 
00213         line = "cd ${JETWEB_HOME}/run"+System.getProperty("runsubdir");
00214         spoolscript.write(line); spoolscript.newLine();
00215 
00216         line = "./"+request.getJobName();
00217         spoolscript.write(line); spoolscript.newLine();
00218 
00219         spoolscript.close();
00220 
00221     } catch ( IOException e ) {
00222         System.out.println("problem creating submit script:"+e);
00223         return CANTWRITESCRIPT;
00224     }
00225 
00226     // Now change the permissions.
00227     return setPermissions(request);
00228     }
00229 
00230 
00237     private static int setPermissions(JobRequest request) {
00238     /*
00239     try{
00240         Process chmod = Runtime.getRuntime().exec( JetWebConfig.getScriptDirName()
00241                                +"/permit "
00242                                +request.getSubmitScript().getPath());
00243         try {
00244         chmod.waitFor();
00245         } catch (InterruptedException e1) {
00246         System.out.println("oof "+e1);  
00247         return CANTCHMOD;
00248         }
00249     } catch (IOException e2) {
00250         System.out.println("Problem submitting job."+e2);   
00251         return CANTOPENPERMITSCRIPT;
00252     }
00253     */
00254     return OK;
00255     }
00256 
00257 
00265     public static void setBatchFacility(String batchFacility) {
00266     if (batchFacility.equalsIgnoreCase("ucl_nqs")){
00267         BATCHFACILITY = UCLNQS;}
00268     else if (batchFacility.equalsIgnoreCase("manchester_pbs")){
00269         BATCHFACILITY = MCRPBS;}
00270     else if (batchFacility.equalsIgnoreCase("grid")){
00271         BATCHFACILITY = GRID;}
00272     else if (batchFacility.equalsIgnoreCase("ucl_pbs")){
00273         BATCHFACILITY = UCLPBS;}
00274     else if (batchFacility.equalsIgnoreCase("sheffield_pbs")){
00275         BATCHFACILITY = SHEFPBS;
00276     }
00277     }
00278 
00279 
00285     public static String getTemplate(){
00286     if (scriptTemplates==null) setScriptTemplates(); // On first call, fill Hashtable of script files.
00287     String template = "";
00288     String key = Integer.toString(SubmitScriptHandler.BATCHFACILITY);
00289     //System.out.println("attempt to get template " + key);
00290     try {
00291         template = (String)scriptTemplates.get(key);
00292         System.out.println(template);
00293         if (template.equals("null")){
00294         System.out.println("ScriptHandlerError: no template for "+key);
00295         return null;
00296         }
00297     } catch (Exception e){
00298         System.out.println("ScriptHandlerError: no template for "+key);
00299     }
00300     return template;
00301     }
00302 
00303 
00310     public static String getExecutable(Generator generator){
00311 
00312     String exec = "jetweb-"+generator.getName()+generator.getVersion(); 
00313     System.out.println("SubmitScriptHandler: executable is "+exec);
00314     return exec;
00315 
00316     }
00317 
00318 
00324     private static void setScriptTemplates(){
00325     scriptTemplates = new Hashtable<String,String>();
00326 
00327     scriptTemplates.put(Integer.toString(SubmitScriptHandler.UCLNQS),
00328                JetWebConfig.templateDirName+"/uclnqs.script"); 
00329     scriptTemplates.put(Integer.toString(SubmitScriptHandler.MCRPBS),
00330                JetWebConfig.templateDirName+"/mcrpbs.script"); 
00331     scriptTemplates.put(Integer.toString(SubmitScriptHandler.GRID),
00332                JetWebConfig.templateDirName+"/gridpp.script"); 
00333     scriptTemplates.put(Integer.toString(SubmitScriptHandler.UCLPBS),
00334                JetWebConfig.templateDirName+"/uclpbs.script"); 
00335     scriptTemplates.put(Integer.toString(SubmitScriptHandler.SHEFPBS),
00336                JetWebConfig.templateDirName+"/shefpbs.script"); 
00337     }
00338 
00339 }

Generated Wed Jan 17 09:14:27 GMT 2007