Commit 18b3dd54 authored by Mohamed Khalil Labidi's avatar Mohamed Khalil Labidi
Browse files

Merge branch 'ip-addr' into 'proactive-dev'

Add ip addresses retrieval in getAllNodes endpoint

See merge request !113
parents 54e64786 6cf5d878
......@@ -667,6 +667,7 @@ public class PAGateway {
* @return List of all table Deployment's entries
*/
public List<Deployment> getAllNodes() {
resourceManagerGateway.synchronizeDeploymentsIPAddresses(schedulerGateway);
return EntityManagerHelper.createQuery("SELECT d FROM Deployment d", Deployment.class).getResultList();
}
......@@ -990,15 +991,16 @@ public class PAGateway {
EmsDeploymentRequest newEmsDeploymentReq =
oldDeployment.getEmsDeployment() == null ? null : oldDeployment.getEmsDeployment().clone(nodeName);
return new Deployment(nodeName,
oldDeployment.getLocationName(),
oldDeployment.getImageProviderId(),
oldDeployment.getHardwareProviderId(),
newEmsDeploymentReq,
oldDeployment.getPaCloud(),
oldDeployment.getTask(),
false,
null,
null
oldDeployment.getLocationName(),
oldDeployment.getImageProviderId(),
oldDeployment.getHardwareProviderId(),
newEmsDeploymentReq,
oldDeployment.getPaCloud(),
oldDeployment.getTask(),
false,
null,
null,
null
);
})
.forEach(deployment -> {
......
package org.activeeon.morphemic.infrastructure.deployment;
import lombok.extern.slf4j.Slf4j;
import org.activeeon.morphemic.application.deployment.PAFactory;
import org.activeeon.morphemic.application.deployment.PASchedulerGateway;
import org.activeeon.morphemic.model.Deployment;
import org.activeeon.morphemic.model.IpAddress;
import org.activeeon.morphemic.model.IpAddressType;
import org.activeeon.morphemic.model.IpVersion;
import org.activeeon.morphemic.service.EntityManagerHelper;
import org.activeeon.morphemic.service.RMConnectionHelper;
import org.apache.http.conn.ConnectTimeoutException;
import org.ow2.proactive.resourcemanager.common.NSState;
......@@ -9,15 +16,17 @@ import org.ow2.proactive.resourcemanager.common.event.dto.RMStateFull;
import org.ow2.proactive.resourcemanager.exception.RMException;
import org.ow2.proactive.resourcemanager.exception.RMNodeException;
import org.ow2.proactive.scheduler.common.exception.NotConnectedException;
import org.ow2.proactive.scheduler.common.exception.UserException;
import org.ow2.proactive.scheduler.common.job.TaskFlowJob;
import org.ow2.proactive.scheduler.common.task.ScriptTask;
import org.ow2.proactive.scheduler.common.task.TaskResult;
import org.ow2.proactive_grid_cloud_portal.common.RMRestInterface;
import org.ow2.proactive_grid_cloud_portal.scheduler.exception.PermissionRestException;
import org.ow2.proactive_grid_cloud_portal.scheduler.exception.RestException;
import javax.security.auth.login.LoginException;
import java.security.KeyException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
@Slf4j
......@@ -133,9 +142,7 @@ public class PAResourceManagerGateway {
private List<String> getDeployedNodesInformation(String nodeSource) throws NotConnectedException, PermissionRestException {
reconnectIfDisconnected();
List<String> deployedNodes = new ArrayList<>();
LOGGER.debug("Getting full RM state ...");
RMStateFull rmState = rmRestInterface.getRMStateFull(RMConnectionHelper.getSessionId());
LOGGER.debug("Full monitoring got.");
RMStateFull rmState = getFullMonitoring();
LOGGER.debug("Searching for deployed nodes information ...");
for (RMNodeEvent rmNodeEvent : rmState.getNodesEvents()) {
if (rmNodeEvent.getNodeSource().equals(nodeSource)) {
......@@ -148,6 +155,13 @@ public class PAResourceManagerGateway {
return deployedNodes;
}
private RMStateFull getFullMonitoring() throws NotConnectedException, PermissionRestException {
LOGGER.debug("Getting full RM state ...");
RMStateFull rmState = rmRestInterface.getRMStateFull(RMConnectionHelper.getSessionId());
LOGGER.debug("Full monitoring got.");
return rmState;
}
/**
* Deploy a simple AWS node source
* @param awsUsername A valid AWS user name
......@@ -322,4 +336,66 @@ public class PAResourceManagerGateway {
public String getSessionId(){
return RMConnectionHelper.getSessionId();
}
public void synchronizeDeploymentsIPAddresses(PASchedulerGateway schedulerGateway) {
List<Deployment> deployments = EntityManagerHelper.createQuery("SELECT d FROM Deployment d", Deployment.class).getResultList();
EntityManagerHelper.begin();
deployments.parallelStream().forEach(deployment -> {
if (deployment.getIsDeployed()) {
try {
List<String> nodeURLs = searchNodes(Collections.singletonList(deployment.getNodeName()), true);
if (!nodeURLs.isEmpty()) {
if (deployment.getIpAddress() != null) {
LOGGER.info("Deployment " + deployment.getNodeName() +
"already synchronized. IP: " + deployment.getIpAddress());
} else {
TaskFlowJob paIPJob = createIPAddrGetterWorkflow(deployment);
if (!paIPJob.getTasks().isEmpty()) {
long submittedJobId = schedulerGateway.submit(paIPJob).longValue();
LOGGER.info("Public ip getter job submitted successfully. ID = " + submittedJobId);
TaskResult taskResult = schedulerGateway.waitForTask(Long.toString(submittedJobId),
"get_ip_addr_task",
60000);
String publicIPAddr = taskResult.getValue().toString();
LOGGER.info("Task result value to string: " + publicIPAddr);
IpAddress ipAddress = new IpAddress(IpAddressType.PUBLIC_IP,
IpVersion.V4,
publicIPAddr);
LOGGER.info("ipAddress: " + ipAddress.toString());
deployment.setIpAddress(ipAddress);
EntityManagerHelper.persist(deployment);
}
}
} else {
LOGGER.warn("The node " + deployment.getNodeName() + " is not reachable in RM.");
}
} catch (NotConnectedException nce) {
LOGGER.error("ERROR: Not able to search for a node due to a NotConnectedException: " + Arrays.toString(nce.getStackTrace()));
} catch (RestException re) {
LOGGER.error("ERROR: Not able to search for a node due to a RestException: " + Arrays.toString(re.getStackTrace()));
} catch (Throwable throwable) {
LOGGER.error("ERROR: Not able to access task result due to a Throwable: " + Arrays.toString(throwable.getStackTrace()));
}
}
});
EntityManagerHelper.commit();
}
private TaskFlowJob createIPAddrGetterWorkflow(Deployment deployment) {
ScriptTask ipAddrGetterTask = PAFactory.createGroovyScriptTaskFromFile("get_ip_addr_task",
"get_ip_addr_script.groovy");
ipAddrGetterTask.addGenericInformation("NODE_ACCESS_TOKEN", deployment.getNodeAccessToken());
TaskFlowJob paJob = new TaskFlowJob();
try {
paJob.addTask(ipAddrGetterTask);
} catch (UserException ue) {
LOGGER.error("Task " + ipAddrGetterTask.getName() + " could not be added due to: " + Arrays.toString(ue.getStackTrace()));
}
paJob.setName(deployment.getNodeName() + "_ip_getter_job");
paJob.setMaxNumberOfExecution(2);
paJob.setProjectName("Public-ip-getter");
LOGGER.info("Job created: " + paJob.toString());
return paJob;
}
}
......@@ -47,6 +47,9 @@ public class Deployment implements Serializable {
@Column(name = "NUMBER")
private Long number;
@Embedded
private IpAddress ipAddress = null;
@Override
public String toString() {
return "Deployment{" +
......@@ -55,6 +58,7 @@ public class Deployment implements Serializable {
", imageProviderId='" + imageProviderId + '\'' +
", hardwareProviderId='" + hardwareProviderId + '\'' +
", isDeployed='" + isDeployed.toString() + '\'' +
", ipAddress='" + ipAddress + '\'' +
", nodeAccessToken='" + nodeAccessToken + '\'' +
", number='" + number + '\'' +
", paCloud='" + paCloud.getNodeSourceNamePrefix() + '\'' +
......
......@@ -14,25 +14,18 @@ def nodeConfigJson = variables.get("nodeConfigJson")
def nodeToken = variables.get("token")
// Enforcing ....
try {
print "(2/3) Acquiring nodes ..."
def nodeURLs = rmapi.acquireNodes(nodeSourceName,
nVMs,
synchronous,
timeout,
nodeConfigJson)
print "(2/3) Acquiring nodes ..."
def nodeURLs = rmapi.acquireNodes(nodeSourceName,
nVMs,
synchronous,
timeout,
nodeConfigJson)
nodeURLs.each { nodeURL ->
rmapi.addNodeToken(nodeURL, nodeToken)
}
println " ... OK !"
} catch (Exception e) {
println " ... Error: " + e.getMessage();
retCode = 1;
} finally {
print "(3/3) Logging out ..."
rmapi.disconnect();
println " ... OK !"
nodeURLs.each { nodeURL ->
rmapi.addNodeToken(nodeURL, nodeToken)
}
println " ... OK !"
print "(3/3) Logging out ..."
rmapi.disconnect();
println " ... OK !"
return retCode;
\ No newline at end of file
URL url_name = new URL("http://bot.whatismyipaddress.com");
BufferedReader sc = new BufferedReader(new InputStreamReader(url_name.openStream()));
// reads system IPAddress
systemipaddress = sc.readLine().trim();
System.out.println("Public IP Address: " + systemipaddress +"\n");
result = systemipaddress
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment