BufferedOutputStream os = null;
BufferedReader readErr = null;
Process proc = null;
if (!containsParameterKey(JOB_WRAPPER_TEMPLATE_PATH)) {
throw new CommandException("parameter \"JOB_WRAPPER_TEMPLATE_PATH\" not defined!");
}
String cream_sandbox_dir = getParameterValueAsString("CREAM_SANDBOX_DIR");
if (cream_sandbox_dir == null) {
throw new CommandException("parameter \"CREAM_SANDBOX_DIR\" not defined!");
}
job.setCREAMSandboxBasePath(cream_sandbox_dir);
if(!containsParameterKey("CREAM_CREATE_SANDBOX_BIN_PATH")) {
throw new CommandException("parameter \"CREAM_CREATE_SANDBOX_BIN_PATH\" not defined!");
}
try {
String[] cmd = new String[] { "sudo", "-S", "-n", "-u", job.getLocalUser(), getParameterValueAsString("CREAM_CREATE_SANDBOX_BIN_PATH"), cream_sandbox_dir, job.getUserId(), job.getId(), "true" };
try {
proc = runtime.exec(cmd);
} catch (Throwable e) {
logger.error("createJobSandboxDir: " + e.getMessage());
throw (new IOException("Cannot create the sandbox for job " + job.getId() + "! " + e.getMessage()));
}
in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
os = new BufferedOutputStream(proc.getOutputStream());
readErr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
try {
job.setWorkingDirectory(in.readLine());
if(job.getWorkingDirectory() == null) {
job.setWorkingDirectory(Job.NOT_AVAILABLE_VALUE);
throw new IOException();
}
} catch (IOException e) {
throw new IOException("cannot create the job's working directory!");
}
if (gsiFTPcreamURL != null && job.getWorkingDirectory() != null) {
job.setCREAMInputSandboxURI(gsiFTPcreamURL + job.getWorkingDirectory() + "/ISB");
job.setCREAMOutputSandboxURI(gsiFTPcreamURL + job.getWorkingDirectory() + "/OSB");
String dsUploadFile = null;
if (job.containsVolatilePropertyKeys(Job.OUTPUT_DATA)) {
if (Job.NOT_AVAILABLE_VALUE.equals(job.getGridJobId()) || job.getGridJobId() == null) {
dsUploadFile = "DSUpload_" + job.getId() + ".out";
job.getOutputFiles().add(dsUploadFile);
job.addVolatileProperty(Job.DS_UPLOAD_OUTPUT_FILE, dsUploadFile);
} else {
job.addVolatileProperty(Job.DS_UPLOAD_OUTPUT_FILE, "DSUpload_" + job.getGridJobId().substring(job.getGridJobId().lastIndexOf('/') +1) + ".out");
}
}
if (containsParameterKey(SANDBOX_TRANSFER_METHOD)) {
job.addVolatileProperty(SANDBOX_TRANSFER_METHOD, getParameterValueAsString(SANDBOX_TRANSFER_METHOD));
} else {
job.addVolatileProperty(SANDBOX_TRANSFER_METHOD, GSIFTP_SANDBOX_TRANSFER_METHOD);
}
if (GSIFTP_SANDBOX_TRANSFER_METHOD.equalsIgnoreCase((String)job.getVolatileProperty(SANDBOX_TRANSFER_METHOD))) {
if (job.getOutputFiles() != null && job.getOutputFiles().size() > 0) {
if (job.getOutputSandboxBaseDestURI() != null) {
if ("gsiftp://localhost".equalsIgnoreCase(job.getOutputSandboxBaseDestURI())) {
job.setOutputSandboxBaseDestURI(job.getCREAMOutputSandboxURI());
}
} else {
int index = 0, i=0;
String outputFile = null;
ArrayList<String> outputSandboxDestURI = new ArrayList<String>(0);
if (dsUploadFile != null) {
job.getOutputSandboxDestURI().add(job.getCREAMOutputSandboxURI() + File.separator + dsUploadFile);
}
for (String file : job.getOutputSandboxDestURI()) {
if (file.startsWith("gsiftp://localhost")) {
if (file.equals("gsiftp://localhost")) {
outputFile = job.getOutputFiles().get(i);
} else {
outputFile = file.substring("gsiftp://localhost".length()+1);
}
index = outputFile.lastIndexOf("/");
outputFile = index > 0? outputFile.substring(index + 1): outputFile;
outputSandboxDestURI.add(job.getCREAMOutputSandboxURI() + outputFile);
} else {
outputSandboxDestURI.add(file);
}
i++;
}
job.setOutputSandboxDestURI(outputSandboxDestURI);
}
}
} else if (LRMS_SANDBOX_TRANSFER_METHOD.equalsIgnoreCase((String)job.getVolatileProperty(SANDBOX_TRANSFER_METHOD))) {
int index=0;
if (job.getInputFiles() != null && job.getInputFiles().size() > 0) {
String inputFile = null;
StringBuffer transferInput = new StringBuffer();
ArrayList<String> inputFiles = new ArrayList<String>(0);
ArrayList<String> lrmsInputFiles = new ArrayList<String>(0);
for (String file : job.getInputFiles()) {
if (file.startsWith("/") || file.startsWith("file://") ||
(job.getInputSandboxBaseURI() == null && !file.startsWith("gsiftp://") && !file.startsWith("https://"))) {
index = file.lastIndexOf("/");
inputFile = index > 0? file.substring(index + 1): file;
transferInput.append(inputFile).append(",");
lrmsInputFiles.add(inputFile);
} else {
inputFiles.add(file);
}
}
if (transferInput.length() > 0) {
transferInput.replace(transferInput.length()-1, transferInput.length(), "\"");
job.addExtraAttribute("TransferInput", "TransferInput=\"" + transferInput.toString());
job.addExtraAttribute("iwd", "iwd=\"" + job.getWorkingDirectory() + "/ISB\"");
}
if (inputFiles.size() == 0) {
job.setInputSandboxBaseURI(null);
job.setInputFiles(null);
} else {
job.setInputFiles(inputFiles);
}
if (lrmsInputFiles.size() > 0) {
job.addVolatileProperty(LRMS_INPUT_FILES, lrmsInputFiles);
}
}
if (job.getOutputFiles() != null && job.getOutputFiles().size() > 0) {
String outputFile = null;
String osbDir = job.getWorkingDirectory() + "/OSB/";
StringBuffer transferOutput = new StringBuffer();
StringBuffer transferOutputRemaps = new StringBuffer();
ArrayList<String> outputFiles = new ArrayList<String>(0);
ArrayList<String> lrmsOutputFiles = new ArrayList<String>(0);
ArrayList<String> outputSandboxDestURI = new ArrayList<String>(0);
if (job.getOutputSandboxBaseDestURI() != null) {
if ("gsiftp://localhost".equalsIgnoreCase(job.getOutputSandboxBaseDestURI())) {
for (String file : job.getOutputFiles()) {
index = file.lastIndexOf("/");
outputFile = index > 0? file.substring(index + 1): file;
lrmsOutputFiles.add(outputFile);
transferOutput.append(outputFile).append(",");
transferOutputRemaps.append(outputFile).append("=").append(osbDir).append(outputFile).append(";");
}
job.setOutputFiles(null);
job.setOutputSandboxBaseDestURI(null);
}
} else {
int i=0;
if (dsUploadFile != null) {
job.getOutputSandboxDestURI().add("gsiftp://localhost");
}
for (String file : job.getOutputSandboxDestURI()) {
outputFile = job.getOutputFiles().get(i++);
if (file.startsWith("gsiftp://localhost")) {
index = outputFile.lastIndexOf("/");
outputFile = index > 0? outputFile.substring(index + 1): outputFile;
lrmsOutputFiles.add(outputFile);
transferOutput.append(outputFile).append(",");
transferOutputRemaps.append(outputFile).append("=").append(osbDir);
if (file.length() > "gsiftp://localhost".length()) {
outputFile = file.substring("gsiftp://localhost".length()+1);
index = outputFile.lastIndexOf("/");
outputFile = index > 0? file.substring(index + 1): outputFile;
}
transferOutputRemaps.append(outputFile).append(";");
} else {
outputFiles.add(outputFile);
outputSandboxDestURI.add(file);
}
}
job.setOutputFiles(outputFiles.size() > 0 ? outputFiles : null);
job.setOutputSandboxDestURI(outputSandboxDestURI.size() > 0 ? outputSandboxDestURI : null);
}
if (transferOutput.length() > 0) {
transferOutput.replace(transferOutput.length()-1, transferOutput.length(), "\"");
transferOutputRemaps.replace(transferOutputRemaps.length()-1, transferOutputRemaps.length(), "\"");
job.addExtraAttribute("TransferOutput", "TransferOutput=\"" + transferOutput.toString());
job.addExtraAttribute("TransferOutputRemaps", "TransferOutputRemaps=\"" + transferOutputRemaps.toString());
}
if (lrmsOutputFiles.size() > 0) {
job.addVolatileProperty(LRMS_OUTPUT_FILES, lrmsOutputFiles);
}
}
} else {
throw new CommandException("sandbox transfer mode \"" + job.getVolatileProperty(SANDBOX_TRANSFER_METHOD) + "\" not supported!");
}
} else {
job.setCREAMInputSandboxURI(Job.NOT_AVAILABLE_VALUE);
job.setCREAMOutputSandboxURI(Job.NOT_AVAILABLE_VALUE);
}
String id = "" + StrictMath.random();
if (id.charAt(1) == '.') {
id = id.substring(2);
}
if (id.length() > 20) {
id = id.substring(0, 20);
}
if (gsiFTPcreamURL != null && (isEmptyField(job.getICEId()) || (!isEmptyField(job.getICEId()) && !isEmptyField(job.getMyProxyServer())))) {
job.addExtraAttribute("PROXY_RENEWAL", "ENABLED");
job.addVolatileProperty(DELEGATION_PROXY_CERT_SANDBOX_URI, gsiFTPcreamURL + job.getDelegationProxyCertPath());
if(containsParameterKey(JOB_WRAPPER_DELEGATION_TIME_SLOT)) {
job.addVolatileProperty(JOB_WRAPPER_DELEGATION_TIME_SLOT, (String)getParameterValue(JOB_WRAPPER_DELEGATION_TIME_SLOT));
}
} else {
job.addExtraAttribute("PROXY_RENEWAL", "DISABLED");
}
if (containsParameterKey(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT)) {
job.addVolatileProperty(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT, (String)getParameterValue(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT));
}
if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_COUNT_ISB)) {
job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_COUNT_ISB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_COUNT_ISB));
}
if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB)) {
job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB));
}
if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_COUNT_OSB)) {
job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_COUNT_OSB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_COUNT_OSB));
}
if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB)) {
job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB));
}
job.addVolatileProperty(JOB_WRAPPER_TEMPLATE_PATH, getParameterValueAsString(JOB_WRAPPER_TEMPLATE_PATH));
String tmpWrapper = null;
try {
tmpWrapper = JobWrapper.buildWrapper(job);
} catch (Throwable e) {
proc.destroy();
throw new CommandException("cannot generate the job wrapper! the problem seems to be related to the jdl: " + e.getMessage());
}
if (tmpWrapper == null) {
throw new CommandException("cannot generate the job wrapper!");
}
try {
os.write(tmpWrapper.getBytes());
os.flush();
os.close();
os = null;
} catch (Throwable e) {
throw new CommandException("cannot write the job wrapper (jobId = " + job.getId() + ")!");
}
} catch (CommandException ce) {
throw ce;
} catch (Throwable exx) {
if (proc != null) {
proc.waitFor();
if(proc.exitValue() != 0) {
String procErrorMessage = null;
if (readErr.ready()) {
procErrorMessage = " [failure reason = \"" + readErr.readLine() + "\"]";
}
procErrorMessage += " [exit code = " + proc.exitValue() + "]";
throw new CommandException(exx.getMessage() + procErrorMessage);
}
} else {
throw new CommandException(exx.getMessage());
}
} finally {
if (proc != null) {
proc.waitFor();