use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class RMCore method executeScript.
/**
* {@inheritDoc}
*/
public <T> List<ScriptResult<T>> executeScript(Script<T> script, String targetType, Set<String> targets) {
// Depending on the target type, select nodes for script execution
final TargetType tType = TargetType.valueOf(targetType);
final HashSet<RMNode> selectedRMNodes = new HashSet<>();
switch(tType) {
case NODESOURCE_NAME:
// If target is a nodesource name select all its nodes
for (String target : targets) {
NodeSource nodeSource = this.deployedNodeSources.get(target);
if (nodeSource != null) {
for (RMNode candidateNode : this.allNodes.values()) {
if (candidateNode.getNodeSource().equals(nodeSource)) {
this.selectCandidateNode(selectedRMNodes, candidateNode);
}
}
}
}
break;
case NODE_URL:
// If target is node url select the node
for (String target : targets) {
RMNode candidateNode = this.allNodes.get(target);
if (candidateNode != null) {
this.selectCandidateNode(selectedRMNodes, candidateNode);
}
}
break;
case HOSTNAME:
// If target is hostname select first node from that host
for (String target : targets) {
for (RMNode node : this.allNodes.values()) {
if (node.getHostName().equals(target)) {
this.selectCandidateNode(selectedRMNodes, node);
break;
}
}
}
break;
default:
throw new IllegalArgumentException("Unable to execute script, unknown target type: " + targetType);
}
// Return a ProActive future on the list of results
return this.selectionManager.executeScript(script, selectedRMNodes, null);
// To avoid blocking rmcore ao the call is delegated to the selection
// manager ao and each node is unlocked as soon as the script has
// finished it's execution.
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class AutoUpdateInfrastructure method startNodeImpl.
/**
* Internal node acquisition method
* <p>
* Starts a PA runtime on remote host using a custom script, register it
* manually in the nodesource.
*
* @param hostTracker The host on which one the node will be started
* @param nbNodes number of nodes to deploy
* @param depNodeURLs list of deploying or lost nodes urls created
* @throws org.ow2.proactive.resourcemanager.exception.RMException
* acquisition failed
*/
protected void startNodeImpl(HostTracker hostTracker, int nbNodes, final List<String> depNodeURLs) throws RMException {
final String nodeName = this.nodeSource.getName() + "-" + ProActiveCounter.getUniqID();
String credentials = "";
try {
credentials = new String(nodeSource.getAdministrator().getCredentials().getBase64());
} catch (KeyException e) {
logger.error("Invalid credentials");
return;
}
Properties localProperties = new Properties();
localProperties.put(NODE_NAME, nodeName);
localProperties.put(HOST_NAME, hostTracker.getResolvedAddress().getHostName());
localProperties.put(NODESOURCE_CREDENTIALS, credentials);
localProperties.put(NODESOURCE_NAME, nodeSource.getName());
localProperties.put(NB_NODES, nbNodes);
String filledCommand = replaceProperties(command, localProperties);
filledCommand = replaceProperties(filledCommand, System.getProperties());
final List<String> createdNodeNames = RMNodeStarter.getWorkersNodeNames(nodeName, nbNodes);
depNodeURLs.addAll(addMultipleDeployingNodes(createdNodeNames, filledCommand, "Deploying node on host " + hostTracker.getResolvedAddress(), this.nodeTimeOut));
addTimeouts(depNodeURLs);
Process p;
try {
logger.debug("Deploying node: " + nodeName);
logger.debug("Launching the command: " + filledCommand);
p = Runtime.getRuntime().exec(new String[] { "bash", "-c", filledCommand });
} catch (IOException e1) {
multipleDeclareDeployingNodeLost(depNodeURLs, "Cannot run command: " + filledCommand + " - \n The following exception occurred: " + getStackTraceAsString(e1));
throw new RMException("Cannot run command: " + filledCommand, e1);
}
String lf = System.lineSeparator();
int circuitBreakerThreshold = 5;
while (!anyTimedOut(depNodeURLs) && circuitBreakerThreshold > 0) {
try {
int exitCode = p.exitValue();
if (exitCode != 0) {
logger.error("Child process at " + hostTracker.getResolvedAddress().getHostName() + " exited abnormally (" + exitCode + ").");
} else {
logger.error("Launching node script has exited normally whereas it shouldn't.");
}
String pOutPut = Utils.extractProcessOutput(p);
String pErrPut = Utils.extractProcessErrput(p);
final String description = "Script failed to launch a node on host " + hostTracker.getResolvedAddress().getHostName() + lf + " >Error code: " + exitCode + lf + " >Errput: " + pErrPut + " >Output: " + pOutPut;
logger.error(description);
if (super.checkNodeIsAcquiredAndDo(nodeName, null, new Runnable() {
public void run() {
multipleDeclareDeployingNodeLost(depNodeURLs, description);
}
})) {
return;
} else {
// there isn't any race regarding node registration
throw new RMException("A node " + nodeName + " is not expected anymore because of an error.");
}
} catch (IllegalThreadStateException e) {
logger.trace("IllegalThreadStateException while waiting for " + nodeName + " registration");
}
if (super.checkNodeIsAcquiredAndDo(nodeName, null, null)) {
// registration is ok, we destroy the process
logger.debug("Destroying the process: " + p);
try {
ProcessTree.get().get(p).kill();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
circuitBreakerThreshold--;
logger.trace("An exception occurred while monitoring a child process", e);
}
}
// if we exit because of a timeout
if (anyTimedOut(depNodeURLs)) {
// we remove it
removeTimeouts(depNodeURLs);
// we destroy the process
p.destroy();
throw new RMException("Deploying Node " + nodeName + " not expected any more");
}
if (circuitBreakerThreshold <= 0) {
logger.error("Circuit breaker threshold reached while monitoring a child process.");
throw new RMException("Several exceptions occurred while monitoring a child process.");
}
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class CLIInfrastructure method killNodeImpl.
/**
* {@inheritDoc}
*/
@Override
protected void killNodeImpl(Node node, InetAddress h) {
final Node n = node;
final InetAddress host = h;
incrementNbRemovalThread();
this.nodeSource.executeInParallel(new Runnable() {
public void run() {
try {
final String commandLine = interpreter + " " + removalScript.getAbsolutePath() + " " + host.getHostName() + " " + n.getNodeInformation().getURL();
Process p;
try {
logger.debug("Launching the command: " + commandLine);
p = Runtime.getRuntime().exec(commandLine);
// TODO add timeout behavior
int exitCode = p.waitFor();
String pOutPut = Utils.extractProcessOutput(p);
String pErrPut = Utils.extractProcessErrput(p);
String lf = System.lineSeparator();
final String description = "Removal script ouput" + lf + " >Error code: " + exitCode + lf + " >Errput: " + pErrPut + " >Output: " + pOutPut;
if (exitCode != 0) {
logger.error("Child process at " + host.getHostName() + " exited abnormally (" + exitCode + ").");
logger.error(description);
} else {
logger.info("Removal node process has exited normally for " + n.getNodeInformation().getURL());
logger.debug(description);
}
} catch (IOException e1) {
logger.error(e1);
}
} catch (Exception e) {
logger.trace("An exception occurred during node removal", e);
}
decrementNbRemovalThread();
}
});
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class CLIInfrastructure method startNodeImpl.
/**
* Internal node acquisition method
* <p>
* Starts a PA runtime on remote host using a custom script, register it
* manually in the nodesource.
*
* @param hostTracker The host on which one the node will be started
* @param nbNodes number of nodes to deploy
* @param depNodeURLs list of deploying or lost nodes urls created
* @throws RMException
* acquisition failed
*/
protected void startNodeImpl(HostTracker hostTracker, int nbNodes, final List<String> depNodeURLs) throws RMException {
final String nodeName = "SCR-" + this.nodeSource.getName() + "-" + ProActiveCounter.getUniqID();
final String commandLine = interpreter + " " + deploymentScript.getAbsolutePath() + " " + hostTracker.getResolvedAddress().getHostName() + " " + nodeName + " " + this.nodeSource.getName() + " " + getRmUrl() + " " + nbNodes;
final List<String> createdNodeNames = RMNodeStarter.getWorkersNodeNames(nodeName, nbNodes);
depNodeURLs.addAll(addMultipleDeployingNodes(createdNodeNames, commandLine, "Deploying node on host " + hostTracker.getResolvedAddress(), this.nodeTimeOut));
addTimeouts(depNodeURLs);
Process p;
try {
logger.debug("Launching the command: " + commandLine);
p = Runtime.getRuntime().exec(commandLine);
} catch (IOException e1) {
multipleDeclareDeployingNodeLost(depNodeURLs, "Cannot run command: " + commandLine + " - \n The following exception occured: " + getStackTraceAsString(e1));
throw new RMException("Cannot run command: " + commandLine, e1);
}
String lf = System.lineSeparator();
int circuitBreakerThreshold = 5;
while (!anyTimedOut(depNodeURLs) && circuitBreakerThreshold > 0) {
try {
int exitCode = p.exitValue();
if (exitCode != 0) {
logger.error("Child process at " + hostTracker.getResolvedAddress().getHostName() + " exited abnormally (" + exitCode + ").");
} else {
logger.error("Launching node script has exited normally whereas it shouldn't.");
}
String pOutPut = Utils.extractProcessOutput(p);
String pErrPut = Utils.extractProcessErrput(p);
final String description = "Script failed to launch a node on host " + hostTracker.getResolvedAddress().getHostName() + lf + " >Error code: " + exitCode + lf + " >Errput: " + pErrPut + " >Output: " + pOutPut;
logger.error(description);
if (super.checkNodeIsAcquiredAndDo(nodeName, null, new Runnable() {
public void run() {
multipleDeclareDeployingNodeLost(depNodeURLs, description);
}
})) {
return;
} else {
// there isn't any race regarding node registration
throw new RMException("A node " + nodeName + " is not expected anymore because of an error.");
}
} catch (IllegalThreadStateException e) {
logger.trace("IllegalThreadStateException while waiting for " + nodeName + " registration");
}
if (super.checkNodeIsAcquiredAndDo(nodeName, null, null)) {
// registration is ok, we destroy the process
logger.debug("Destroying the process: " + p);
p.destroy();
return;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
circuitBreakerThreshold--;
logger.trace("An exception occurred while monitoring a child process", e);
}
}
// if we exit because of a timeout
if (this.anyTimedOut(depNodeURLs)) {
// we remove it
removeTimeouts(depNodeURLs);
// we destroy the process
p.destroy();
throw new RMException("Deploying Node " + nodeName + " not expected any more");
}
if (circuitBreakerThreshold <= 0) {
logger.error("Circuit breaker threshold reached while monitoring a child process.");
throw new RMException("Several exceptions occurred while monitoring a child process.");
}
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class LocalInfrastructure method startNodeProcess.
private void startNodeProcess(int numberOfNodes) {
int currentIndex = getIndexAndIncrement();
String baseNodeName = "local-" + this.nodeSource.getName() + "-" + currentIndex;
OperatingSystem os = OperatingSystem.UNIX;
// assuming no cygwin, windows or the "others"...
if (System.getProperty("os.name").contains("Windows")) {
os = OperatingSystem.WINDOWS;
}
String rmHome = PAResourceManagerProperties.RM_HOME.getValueAsString();
if (!rmHome.endsWith(os.fs)) {
rmHome += os.fs;
}
CommandLineBuilder clb = this.getDefaultCommandLineBuilder(os);
// RM_Home set in bin/unix/env script
clb.setRmHome(rmHome);
ArrayList<String> paPropList = new ArrayList<>();
if (!this.paProperties.contains(CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getName())) {
paPropList.add(CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getCmdLine() + rmHome + "config" + os.fs + "security.java.policy-client");
}
if (!this.paProperties.contains(CentralPAPropertyRepository.PA_CONFIGURATION_FILE.getName())) {
paPropList.add(CentralPAPropertyRepository.PA_CONFIGURATION_FILE.getCmdLine() + rmHome + "config" + os.fs + "network" + os.fs + "node.ini");
}
if (!this.paProperties.contains(PAResourceManagerProperties.RM_HOME.getKey())) {
paPropList.add(PAResourceManagerProperties.RM_HOME.getCmdLine() + rmHome);
}
if (!this.paProperties.contains("java.library.path")) {
paPropList.add("-Djava.library.path=" + System.getProperty("java.library.path"));
}
if (!paProperties.isEmpty()) {
Collections.addAll(paPropList, this.paProperties.split(" "));
}
clb.setPaProperties(paPropList);
clb.setNodeName(baseNodeName);
clb.setNumberOfNodes(numberOfNodes);
try {
clb.setCredentialsValueAndNullOthers(new String(this.credentials.getBase64()));
} catch (KeyException e) {
createLostNodes(baseNodeName, numberOfNodes, "Cannot decrypt credentials value", e);
return;
}
List<String> cmd;
try {
cmd = clb.buildCommandLineAsList(false);
} catch (IOException e) {
createLostNodes(baseNodeName, numberOfNodes, "Cannot build command line", e);
return;
}
// The printed cmd with obfuscated credentials
final String obfuscatedCmd = Joiner.on(' ').join(cmd);
List<String> depNodeURLs = new ArrayList<>(numberOfNodes);
final List<String> createdNodeNames = RMNodeStarter.getWorkersNodeNames(baseNodeName, numberOfNodes);
ProcessExecutor processExecutor = null;
try {
depNodeURLs.addAll(addMultipleDeployingNodes(createdNodeNames, obfuscatedCmd, "Node launched locally", this.nodeTimeout));
// Deobfuscate the cred value
Collections.replaceAll(cmd, CommandLineBuilder.OBFUSC, clb.getCredentialsValue());
processExecutor = new ProcessExecutor(baseNodeName, cmd, false, true);
processExecutor.start();
processExecutors.put(processExecutor, depNodeURLs);
final ProcessExecutor tmpProcessExecutor = processExecutor;
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (tmpProcessExecutor != null && !tmpProcessExecutor.isProcessFinished()) {
tmpProcessExecutor.killProcess();
}
}
}));
logger.info("Local Nodes command started : " + obfuscatedCmd);
} catch (IOException e) {
String lf = System.lineSeparator();
String mess = "Cannot launch rm node " + baseNodeName + lf + Throwables.getStackTraceAsString(e);
multipleDeclareDeployingNodeLost(depNodeURLs, mess);
if (processExecutor != null) {
cleanProcess(processExecutor);
}
return;
}
// watching process
int threshold = 10;
while (!allNodesAcquiredOrLost()) {
if (processExecutor.isProcessFinished()) {
int exit = processExecutor.getExitCode();
if (exit != 0) {
String lf = System.lineSeparator();
String message = "RMNode exit code == " + exit + lf;
message += "Command: " + obfuscatedCmd + lf;
String out = Joiner.on('\n').join(processExecutor.getOutput());
String err = Joiner.on('\n').join(processExecutor.getErrorOutput());
message += "stdout: " + out + lf + "stderr: " + err;
multipleDeclareDeployingNodeLost(depNodeURLs, message);
}
} else {
logger.debug("Waiting for nodes " + baseNodeName + " acquisition");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
logger.warn("Interrupted while waiting for local process status", e);
threshold--;
if (threshold <= 0) {
break;
}
}
}
logger.debug("Local Infrastructure manager exits watching loop for nodes " + baseNodeName);
logNodeOutput(baseNodeName + " stdout: ", processExecutor.getOutput());
logNodeOutput(baseNodeName + " stderr: ", processExecutor.getErrorOutput());
if (allNodesLost(numberOfNodes)) {
// clean up the process
cleanProcess(processExecutor);
}
}
Aggregations