use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.
the class ScaleTask method run.
/**
* Execute the Scaling
*/
public void run() {
Throwable error = null;
String paasId = ctx.getDeploymentPaaSId();
String deploymentUrl = "/deployments/" + paasId;
YorcRuntimeDeploymentInfo jrdi = orchestrator.getDeploymentInfo(paasId);
log.info(paasId + " : scaling " + node + " delta=" + nbi);
String taskUrl;
try {
taskUrl = restClient.postScalingToYorc(deploymentUrl, node, nbi);
} catch (Exception e) {
orchestrator.sendMessage(paasId, "Scaling not accepted by Yorc: " + e.getMessage());
callback.onFailure(e);
return;
}
String taskId = taskUrl.substring(taskUrl.lastIndexOf("/") + 1);
synchronized (jrdi) {
// In case we want to undeploy during the scale.
jrdi.setDeployTaskId(taskId);
}
orchestrator.sendMessage(paasId, "Scaling sent to Yorc. taskId=" + taskId);
// wait for end of task
boolean done = false;
long timeout = System.currentTimeMillis() + YORC_SCALE_TIMEOUT;
Event evt;
while (!done && error == null) {
synchronized (jrdi) {
// Check for timeout
long timetowait = timeout - System.currentTimeMillis();
if (timetowait <= 0) {
log.warn("Timeout occured");
error = new Throwable("Scaling timeout");
break;
}
// Wait Events from Yorc
log.debug(paasId + ": Waiting for scaling events.");
try {
jrdi.wait(timetowait);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for scaling");
}
// Check if we received a Scaling Event and process it
evt = jrdi.getLastEvent();
if (evt != null && evt.getType().equals(EventListenerTask.EVT_SCALING)) {
jrdi.setLastEvent(null);
switch(evt.getStatus()) {
case "failed":
log.warn("Scaling failed: " + paasId);
error = new Exception("Scaling failed");
break;
case "canceled":
log.warn("Scaling canceled: " + paasId);
error = new Exception("Scaling canceled");
break;
case "done":
log.debug("Scaling success: " + paasId);
done = true;
break;
default:
orchestrator.sendMessage(paasId, "Scaling status = " + evt.getStatus());
break;
}
continue;
}
}
// We were awaken for some bad reason or a timeout
// Check Task Status to decide what to do now.
String status;
try {
status = restClient.getStatusFromYorc(taskUrl);
log.debug("Returned status:" + status);
} catch (Exception e) {
status = "FAILED";
}
switch(status) {
case "DONE":
// Task OK.
log.debug("Scaling OK");
done = true;
break;
case "FAILED":
log.debug("Scaling failed");
error = new Exception("Scaling failed");
break;
default:
log.debug("Scaling Status is currently " + status);
break;
}
}
synchronized (jrdi) {
// Task is ended: Must remove the taskId and notify a possible undeploy waiting for it.
jrdi.setDeployTaskId(null);
jrdi.notify();
}
// Return result to a4c
if (error == null) {
callback.onSuccess(null);
} else {
callback.onFailure(error);
}
}
use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.
the class DeployTask method run.
/**
* Execute the Deployment
*/
public void run() {
Throwable error = null;
// Keep Ids in a Map
String paasId = ctx.getDeploymentPaaSId();
String alienId = ctx.getDeploymentId();
String deploymentUrl = "/deployments/" + paasId;
log.debug("Deploying " + paasId + "with id : " + alienId);
orchestrator.putDeploymentId(paasId, alienId);
// Init Deployment Info from topology
DeploymentTopology dtopo = ctx.getDeploymentTopology();
Map<String, Map<String, InstanceInformation>> curinfo = setupInstanceInformations(dtopo);
YorcRuntimeDeploymentInfo jrdi = new YorcRuntimeDeploymentInfo(ctx, DeploymentStatus.INIT_DEPLOYMENT, curinfo, deploymentUrl);
orchestrator.putDeploymentInfo(paasId, jrdi);
orchestrator.doChangeStatus(paasId, DeploymentStatus.INIT_DEPLOYMENT);
// Show Topoloy for debug
ShowTopology.topologyInLog(ctx);
MappingTosca.quoteProperties(ctx);
// This operation must be synchronized, because it uses the same files topology.yml and topology.zip
String taskUrl;
synchronized (this) {
// Create the yml of our topology and build our zip topology
try {
buildZip(ctx);
} catch (Throwable e) {
orchestrator.doChangeStatus(paasId, DeploymentStatus.FAILURE);
callback.onFailure(e);
return;
}
// put topology zip to Yorc
log.info("PUT Topology to Yorc");
try {
taskUrl = restClient.sendTopologyToYorc(paasId);
} catch (Exception e) {
orchestrator.sendMessage(paasId, "Deployment not accepted by Yorc: " + e.getMessage());
orchestrator.doChangeStatus(paasId, DeploymentStatus.FAILURE);
callback.onFailure(e);
return;
}
}
String taskId = taskUrl.substring(taskUrl.lastIndexOf("/") + 1);
jrdi.setDeployTaskId(taskId);
orchestrator.sendMessage(paasId, "Deployment sent to Yorc. TaskId=" + taskId);
// wait for Yorc deployment completion
boolean done = false;
long timeout = System.currentTimeMillis() + YORC_DEPLOY_TIMEOUT;
Event evt;
while (!done && error == null) {
synchronized (jrdi) {
// Check deployment timeout
long timetowait = timeout - System.currentTimeMillis();
if (timetowait <= 0) {
log.warn("Deployment Timeout occured");
error = new Throwable("Deployment timeout");
orchestrator.doChangeStatus(paasId, DeploymentStatus.FAILURE);
break;
}
// Wait Deployment Events from Yorc
log.debug(paasId + ": Waiting for deployment events.");
try {
jrdi.wait(timetowait);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for deployment");
}
// Check if we received a Deployment Event and process it
evt = jrdi.getLastEvent();
if (evt != null && evt.getType().equals(EventListenerTask.EVT_DEPLOYMENT)) {
jrdi.setLastEvent(null);
switch(evt.getStatus()) {
case "deployment_failed":
log.warn("Deployment failed: " + paasId);
orchestrator.doChangeStatus(paasId, DeploymentStatus.FAILURE);
error = new Exception("Deployment failed");
break;
case "deployed":
log.debug("Deployment success: " + paasId);
orchestrator.doChangeStatus(paasId, DeploymentStatus.DEPLOYED);
done = true;
break;
case "deployment_in_progress":
orchestrator.doChangeStatus(paasId, DeploymentStatus.DEPLOYMENT_IN_PROGRESS);
break;
default:
orchestrator.sendMessage(paasId, "Deployment status = " + evt.getStatus());
break;
}
continue;
}
}
// We were awaken for some bad reason or a timeout
// Check Deployment Status to decide what to do now.
String status;
try {
status = restClient.getStatusFromYorc(deploymentUrl);
} catch (YorcRestException jre) {
if (jre.getHttpStatusCode() == 404) {
// assumes it is undeployed
status = "UNDEPLOYED";
} else {
log.error("yorc deployment returned an exception: " + jre.getMessage());
error = jre;
break;
}
} catch (Exception e) {
log.error("yorc deployment returned an exception: " + e.getMessage());
error = e;
break;
}
switch(status) {
case "UNDEPLOYED":
orchestrator.changeStatus(paasId, DeploymentStatus.UNDEPLOYED);
error = new Throwable("Deployment has been undeployed");
break;
case "DEPLOYED":
// Deployment is OK.
orchestrator.changeStatus(paasId, DeploymentStatus.DEPLOYED);
done = true;
break;
default:
log.debug("Deployment Status is currently " + status);
break;
}
}
synchronized (jrdi) {
// Task is ended: Must remove the taskId and notify a possible undeploy waiting for it.
jrdi.setDeployTaskId(null);
jrdi.notify();
}
// Return result to a4c
if (error == null) {
callback.onSuccess(null);
} else {
callback.onFailure(error);
}
}
use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.
the class EventListenerTask method run.
public void run() {
int prevIndex = 1;
while (true) {
try {
log.debug("Get events from Yorc from index " + prevIndex);
EventResponse eventResponse = restClient.getEventFromYorc(prevIndex);
if (eventResponse != null) {
prevIndex = eventResponse.getLast_index();
if (eventResponse.getEvents() != null) {
for (Event event : eventResponse.getEvents()) {
String paasId = event.getDeployment_id();
YorcRuntimeDeploymentInfo jrdi = orchestrator.getDeploymentInfo(paasId);
if (jrdi == null) {
log.error("listenYorcEvents: no YorcRuntimeDeploymentInfo for " + paasId);
continue;
}
Map<String, Map<String, InstanceInformation>> instanceInfo = jrdi.getInstanceInformations();
paasId = event.getDeployment_id();
// Check type of Event sent by Yorc and process it
String eState = event.getStatus();
String eMessage = paasId + " - Yorc Event: ";
if (event.getType() == null) {
log.warn("Yorc version is obsolete. Please use a newer version");
event.setType(EVT_INSTANCE);
}
switch(event.getType()) {
case EVT_INSTANCE:
String eNode = event.getNode();
String eInstance = event.getInstance();
eMessage += "instance " + eNode + ":" + eInstance + ":" + eState;
log.debug("Received Event from Yorc <<< " + eMessage);
Map<String, InstanceInformation> ninfo = instanceInfo.get(eNode);
if (ninfo == null) {
// Add a new Node in YorcRuntimeDeploymentInfo
log.debug("Add a node in YorcRuntimeDeploymentInfo: " + eNode);
ninfo = Maps.newHashMap();
instanceInfo.put(eNode, ninfo);
}
InstanceInformation iinfo = ninfo.get(eInstance);
if (iinfo == null) {
// Add a new Instance for this node in YorcRuntimeDeploymentInfo
log.debug("Add an instance in YorcRuntimeDeploymentInfo: " + eInstance);
iinfo = orchestrator.newInstance(new Integer(eInstance));
ninfo.put(eInstance, iinfo);
}
orchestrator.updateInstanceState(paasId, eNode, eInstance, iinfo, eState);
switch(eState) {
case "initial":
case "creating":
case "deleting":
case "starting":
case "stopping":
case "configured":
case "configuring":
case "created":
break;
case "deleted":
ninfo.remove(eInstance);
break;
case "stopped":
orchestrator.updateInstanceAttributes(paasId, iinfo, eNode, eInstance);
break;
case "started":
orchestrator.updateInstanceAttributes(paasId, iinfo, eNode, eInstance);
// persist BS Id
String source = jrdi.getDeploymentContext().getDeployment().getSourceName();
if (source.equals("BLOCKSTORAGE_APPLICATION")) {
PaaSInstancePersistentResourceMonitorEvent prme = new PaaSInstancePersistentResourceMonitorEvent(eNode, eInstance, MapUtil.newHashMap(new String[] { NormativeBlockStorageConstants.VOLUME_ID }, new Object[] { UUID.randomUUID().toString() }));
orchestrator.postEvent(prme, paasId);
}
break;
case "error":
log.warn("Error instance status");
break;
default:
log.warn("Unknown instance status: " + eState);
break;
}
break;
case EVT_DEPLOYMENT:
case EVT_OPERATION:
case EVT_SCALING:
case EVT_WORKFLOW:
eMessage += event.getType() + ":" + eState;
log.debug("Received Event from Yorc <<< " + eMessage);
synchronized (jrdi) {
if (jrdi.getLastEvent() != null) {
log.debug("Event not taken, forgot it: " + jrdi.getLastEvent());
}
jrdi.setLastEvent(event);
jrdi.notifyAll();
}
break;
default:
log.warn("Unknown event type received from Yorc <<< " + event.getType());
break;
}
}
}
}
} catch (Exception e) {
log.error("listenDeploymentEvent Failed", e);
try {
// We will sleep for 2sec in order to limit logs flood if the yorc server went down
Thread.sleep(2000L);
} catch (InterruptedException ex) {
log.error("listenDeploymentEvent wait interrupted", ex);
}
}
}
}
use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.
the class OperationTask method run.
/**
* Execute the Operation (Custom Command)
*/
public void run() {
Throwable error = null;
String paasId = ctx.getDeploymentPaaSId();
String deploymentUrl = "/deployments/" + paasId;
YorcRuntimeDeploymentInfo jrdi = orchestrator.getDeploymentInfo(paasId);
log.info(paasId + " Execute custom command " + request.getOperationName());
String taskUrl;
try {
taskUrl = restClient.postCustomCommandToYorc(deploymentUrl, request);
} catch (Exception e) {
orchestrator.sendMessage(paasId, "Custom Command not accepted by Yorc: " + e.getMessage());
callback.onFailure(e);
return;
}
String taskId = taskUrl.substring(taskUrl.lastIndexOf("/") + 1);
synchronized (jrdi) {
// In case we want to undeploy during the custom command.
jrdi.setDeployTaskId(taskId);
}
orchestrator.sendMessage(paasId, "Operation " + request.getOperationName() + " sent to Yorc. taskId=" + taskId);
// wait for end of task
boolean done = false;
long timeout = System.currentTimeMillis() + YORC_OPE_TIMEOUT;
Event evt;
while (!done && error == null) {
synchronized (jrdi) {
// Check for timeout
long timetowait = timeout - System.currentTimeMillis();
if (timetowait <= 0) {
log.warn("Timeout occured");
error = new Throwable("Operation timeout");
break;
}
// Wait Events from Yorc
log.debug(paasId + ": Waiting for Custom command events.");
try {
jrdi.wait(timetowait);
} catch (InterruptedException e) {
log.error("Interrupted while waiting for task end");
break;
}
evt = jrdi.getLastEvent();
if (evt != null && evt.getType().equals(EventListenerTask.EVT_OPERATION) && evt.getTask_id().equals(taskId)) {
jrdi.setLastEvent(null);
switch(evt.getStatus()) {
case "failed":
case "canceled":
log.warn("Custom command failed: " + paasId);
error = new Exception("Custom command " + request.getOperationName() + " failed");
break;
case "done":
log.debug("Operation success: " + paasId);
done = true;
break;
default:
// could be 'initial' or 'running'
log.warn("An event has been ignored. Status=" + evt.getStatus());
break;
}
continue;
}
}
// We were awaken for some bad reason or a timeout
// Check Task Status to decide what to do now.
String status;
try {
status = restClient.getStatusFromYorc(taskUrl);
log.debug("Returned status:" + status);
} catch (Exception e) {
status = "FAILED";
}
switch(status) {
case "DONE":
// Task OK.
log.debug("Operation OK");
done = true;
break;
case "FAILED":
log.debug("Operation failed");
error = new Exception("Operation failed");
break;
default:
log.debug("Operation Status is currently " + status);
break;
}
}
synchronized (jrdi) {
// Task is ended: Must remove the taskId and notify a possible undeploy waiting for it.
jrdi.setDeployTaskId(null);
jrdi.notify();
}
// Return result to a4c
if (error == null) {
Map<String, String> customResults = new Hashtable<>(1);
customResults.put("result", "Succesfully executed custom " + request.getOperationName() + " on node " + request.getNodeTemplateName());
// TODO Get results returned by the custom command ??
callback.onSuccess(customResults);
} else {
callback.onFailure(error);
}
}
use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.
the class UndeployTask method run.
/**
* Execute the Undeployment
*/
public void run() {
Throwable error = null;
String paasId = ctx.getDeploymentPaaSId();
String deploymentUrl = "/deployments/" + paasId;
YorcRuntimeDeploymentInfo jrdi = orchestrator.getDeploymentInfo(paasId);
// first check if a deployment is still running
synchronized (jrdi) {
if (jrdi.getDeployTaskId() != null) {
// must stop it and wait until yorc returns the task is done
try {
restClient.stopTask(deploymentUrl + "/tasks/" + jrdi.getDeployTaskId());
// do not wait more than 30 sec.
jrdi.wait(1000 * 30);
} catch (Exception e) {
log.error("stopTask returned an exception", e);
}
// Maybe Yorc is stuck. Forget the task and continue.
if (jrdi.getDeployTaskId() != null) {
jrdi.setDeployTaskId(null);
log.warn("A deployment task was stuck. Forget it.");
}
}
}
log.debug("Undeploying deployment Id:" + paasId);
boolean done = false;
String taskUrl = null;
String status;
try {
taskUrl = restClient.undeploy(deploymentUrl, false);
if (taskUrl == null) {
// Assumes already undeployed
orchestrator.changeStatus(paasId, DeploymentStatus.UNDEPLOYED);
done = true;
}
} catch (YorcRestException jre) {
// Deployment is not found or already undeployed
if (jre.getHttpStatusCode() == 404 || jre.getHttpStatusCode() == 400) {
orchestrator.changeStatus(paasId, DeploymentStatus.UNDEPLOYED);
done = true;
} else {
log.debug("undeploy returned an exception: " + jre.getMessage());
orchestrator.changeStatus(paasId, DeploymentStatus.FAILURE);
error = jre;
}
} catch (Exception e) {
log.error("undeploy returned an exception: " + e);
orchestrator.changeStatus(paasId, DeploymentStatus.FAILURE);
error = e;
}
if (!done && error == null) {
String taskId = taskUrl.substring(taskUrl.lastIndexOf("/") + 1);
orchestrator.sendMessage(paasId, "Undeployment sent to Yorc. taskId=" + taskId);
// wait for Yorc undeployment completion
long timeout = System.currentTimeMillis() + YORC_UNDEPLOY_TIMEOUT;
Event evt;
while (!done && error == null) {
// This may occur when undeploy is immediate
try {
status = restClient.getStatusFromYorc(deploymentUrl);
} catch (YorcRestException jre) {
if (jre.getHttpStatusCode() == 404) {
// assumes it is undeployed
status = "UNDEPLOYED";
} else {
log.error("undeploy returned an exception: " + jre.getMessage());
orchestrator.changeStatus(paasId, DeploymentStatus.FAILURE);
error = jre;
break;
}
} catch (Exception e) {
log.error("undeploy returned an exception: " + e.getMessage());
orchestrator.changeStatus(paasId, DeploymentStatus.FAILURE);
error = e;
break;
}
log.debug("Status of deployment: " + status);
switch(status) {
case "UNDEPLOYED":
log.debug("Undeployment OK");
// Undeployment OK.
orchestrator.changeStatus(paasId, DeploymentStatus.UNDEPLOYED);
break;
case "INITIAL":
// No event will be received, and the undeployment should be straightforward
timeout = System.currentTimeMillis() + 3000;
break;
default:
log.debug("Deployment Status is currently " + status);
break;
}
// Wait an Event from Yorc or timeout
synchronized (jrdi) {
long timetowait = timeout - System.currentTimeMillis();
if (timetowait <= 0) {
log.warn("Timeout occured");
break;
}
try {
jrdi.wait(timetowait);
} catch (InterruptedException e) {
log.error("Interrupted while waiting for undeployment");
break;
}
evt = jrdi.getLastEvent();
if (evt != null && evt.getType().equals(EventListenerTask.EVT_DEPLOYMENT)) {
jrdi.setLastEvent(null);
switch(evt.getStatus()) {
case "undeployment_failed":
log.warn("Undeployment failed: " + paasId);
orchestrator.doChangeStatus(paasId, DeploymentStatus.FAILURE);
error = new Exception("Undeployment failed");
break;
case "undeployed":
log.debug("Undeployment success: " + paasId);
orchestrator.doChangeStatus(paasId, DeploymentStatus.UNDEPLOYED);
done = true;
break;
case "undeploying":
case "undeployment_in_progress":
orchestrator.doChangeStatus(paasId, DeploymentStatus.UNDEPLOYMENT_IN_PROGRESS);
break;
default:
orchestrator.sendMessage(paasId, "Undeployment: status=" + evt.getStatus());
break;
}
}
}
}
}
// Return result to a4c
if (error == null) {
callback.onSuccess(null);
orchestrator.removeDeploymentInfo(paasId);
} else {
callback.onFailure(error);
}
}
Aggregations