Search in sources :

Example 1 with YorcRestException

use of org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException 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);
    }
}
Also used : DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Event(org.ystia.yorc.alien4cloud.plugin.rest.Response.Event) Map(java.util.Map) HashMap(java.util.HashMap) ZipException(java.util.zip.ZipException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException) ParsingException(alien4cloud.tosca.parser.ParsingException) IOException(java.io.IOException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)

Example 2 with YorcRestException

use of org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException 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);
    }
}
Also used : Event(org.ystia.yorc.alien4cloud.plugin.rest.Response.Event) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)

Example 3 with YorcRestException

use of org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException in project yorc-a4c-plugin by ystia.

the class YorcPaaSProvider method doChangeStatus.

/**
 * Actually change the status of the deployment in YorcRuntimeDeploymentInfo
 * Must be called with lock on jrdi
 * @param paasId
 * @param status
 */
public void doChangeStatus(String paasId, DeploymentStatus status) {
    YorcRuntimeDeploymentInfo jrdi = runtimeDeploymentInfos.get(paasId);
    if (jrdi == null) {
        log.error("YorcRuntimeDeploymentInfo is null for paasId " + paasId);
        return;
    }
    if (status.equals(DeploymentStatus.UNDEPLOYED)) {
        try {
            log.debug("send deployment purge request to yorc");
            restClient.undeploy("/deployments/" + paasId, true);
        } catch (YorcRestException jre) {
            // If 400 code (bad request)  is returned, we retry requesting purge during at most 5 minutes
            if (jre.getHttpStatusCode() == 400) {
                long timeout = System.currentTimeMillis() + 1000 * 60 * 5;
                long timetowait = timeout - System.currentTimeMillis();
                boolean retry = true;
                while (retry && timetowait > 0) {
                    retry = retryDeploymentPurge(paasId);
                }
            } else // 404 status code is ignored for purge failure
            if (jre.getHttpStatusCode() != 404) {
                log.error("undeploy purge returned an exception: " + jre.getMessage());
                changeStatus(paasId, DeploymentStatus.FAILURE);
                return;
            }
        } catch (Exception e) {
            log.error("undeploy purge returned an exception: " + e.getMessage());
            changeStatus(paasId, DeploymentStatus.FAILURE);
            return;
        }
    }
    DeploymentStatus oldDeploymentStatus = jrdi.getStatus();
    log.debug("Deployment [" + paasId + "] moved from status [" + oldDeploymentStatus + "] to [" + status + "]");
    jrdi.setStatus(status);
    PaaSDeploymentStatusMonitorEvent event = new PaaSDeploymentStatusMonitorEvent();
    event.setDeploymentStatus(status);
    postEvent(event, paasId);
}
Also used : PaaSDeploymentStatusMonitorEvent(alien4cloud.paas.model.PaaSDeploymentStatusMonitorEvent) MaintenanceModeException(alien4cloud.paas.exception.MaintenanceModeException) OperationExecutionException(alien4cloud.paas.exception.OperationExecutionException) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException) PluginConfigurationException(alien4cloud.paas.exception.PluginConfigurationException) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus) YorcRestException(org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)

Aggregations

YorcRestException (org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)3 Event (org.ystia.yorc.alien4cloud.plugin.rest.Response.Event)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)1 MaintenanceModeException (alien4cloud.paas.exception.MaintenanceModeException)1 OperationExecutionException (alien4cloud.paas.exception.OperationExecutionException)1 PluginConfigurationException (alien4cloud.paas.exception.PluginConfigurationException)1 DeploymentStatus (alien4cloud.paas.model.DeploymentStatus)1 PaaSDeploymentStatusMonitorEvent (alien4cloud.paas.model.PaaSDeploymentStatusMonitorEvent)1 ParsingException (alien4cloud.tosca.parser.ParsingException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipException (java.util.zip.ZipException)1