Search in sources :

Example 6 with DeploymentStatus

use of alien4cloud.paas.model.DeploymentStatus in project alien4cloud by alien4cloud.

the class ManagedServiceResourceService method create.

/**
 * Create a Service resource associated with the given environment.
 *
 * @param environmentId The environment to create a service for, the service version will be the one of the environment current associated version.
 * @param serviceName The name of the service as it should appears.
 * @param fromRuntime If we should try to create the service from the runtime topology related to the environment.
 * @return the id of the created service
 *
 * @throws AlreadyExistException if a service with the given name, or related to the given environment already exists
 * @throws alien4cloud.exception.NotFoundException if <b>fromRuntime</b> is set to true, but the environment is not deployed
 * @throws MissingSubstitutionException if topology related to the environment doesn't define a substitution type
 */
public synchronized String create(String serviceName, String environmentId, boolean fromRuntime) {
    ApplicationEnvironment environment = checkAndGetApplicationEnvironment(environmentId);
    // check that the service does not exists already for this environment
    if (alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environmentId)).count() > 0) {
        throw new AlreadyExistException("A service resource for environment <" + environmentId + "> and version <" + environment.getTopologyVersion() + "> already exists.");
    }
    Topology topology;
    String state = ToscaNodeLifecycleConstants.INITIAL;
    Deployment deployment = null;
    if (fromRuntime) {
        deployment = deploymentService.getActiveDeploymentOrFail(environmentId);
        topology = deploymentRuntimeStateService.getRuntimeTopology(deployment.getId());
        DeploymentStatus currentStatus = deploymentRuntimeStateService.getDeploymentStatus(deployment);
        state = managedServiceResourceEventService.getInstanceStateFromDeploymentStatus(currentStatus);
        if (state == null) {
            // We need a valid deployment state to expose as service.
            throw new InvalidDeploymentStatusException("Creating a service out of a running deployment is possible only when it's status is one of [DEPLOYED, FAILURE, UNDEPLOYED] current was <" + currentStatus + ">", currentStatus);
        }
    } else {
        topology = topologyServiceCore.getOrFail(Csar.createId(environment.getApplicationId(), environment.getTopologyVersion()));
    }
    if (topology.getSubstitutionMapping() == null) {
        throw new MissingSubstitutionException("Substitution is required to expose a topology.");
    }
    // The elementId of the type created out of the substitution is currently the archive name.
    String serviceId = serviceResourceService.create(serviceName, environment.getTopologyVersion(), topology.getArchiveName(), environment.getTopologyVersion(), environmentId);
    // Update the service relationships definition from the topology substitution
    updateServiceRelationship(serviceId, topology);
    if (fromRuntime) {
        managedServiceResourceEventService.updateRunningService((DeploymentTopology) topology, serviceResourceService.getOrFail(serviceId), deployment, state);
    }
    ServiceResource serviceResource = serviceResourceService.getOrFail(serviceId);
    // trigger a ManagedServiceCreatedEvent
    publisher.publishEvent(new ManagedServiceCreatedEvent(this, serviceResource));
    return serviceId;
}
Also used : MissingSubstitutionException(org.alien4cloud.alm.service.exceptions.MissingSubstitutionException) InvalidDeploymentStatusException(org.alien4cloud.alm.service.exceptions.InvalidDeploymentStatusException) Deployment(alien4cloud.model.deployment.Deployment) ServiceResource(alien4cloud.model.service.ServiceResource) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) AlreadyExistException(alien4cloud.exception.AlreadyExistException) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) ManagedServiceCreatedEvent(org.alien4cloud.alm.events.ManagedServiceCreatedEvent) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus)

Example 7 with DeploymentStatus

use of alien4cloud.paas.model.DeploymentStatus in project alien4cloud by alien4cloud.

the class DeploymentController method getDeploymentStatus.

@ApiOperation(value = "Get deployment status from its id.", authorizations = { @Authorization("ADMIN"), @Authorization("APPLICATION_MANAGER") })
@RequestMapping(value = "/{deploymentId}/status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<DeploymentStatus> getDeploymentStatus(@ApiParam(value = "Deployment id.", required = true) @Valid @NotBlank @PathVariable String deploymentId) {
    Deployment deployment = alienDAO.findById(Deployment.class, deploymentId);
    if (deployment != null) {
        try {
            return deploymentLockService.doWithDeploymentReadLock(deployment.getOrchestratorDeploymentId(), () -> {
                final SettableFuture<DeploymentStatus> statusSettableFuture = SettableFuture.create();
                deploymentRuntimeStateService.getDeploymentStatus(deployment, new IPaaSCallback<DeploymentStatus>() {

                    @Override
                    public void onSuccess(DeploymentStatus result) {
                        statusSettableFuture.set(result);
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        statusSettableFuture.setException(t);
                    }
                });
                try {
                    DeploymentStatus currentStatus = statusSettableFuture.get();
                    if (DeploymentStatus.UNDEPLOYED.equals(currentStatus)) {
                        deploymentService.markUndeployed(deployment);
                    }
                    return RestResponseBuilder.<DeploymentStatus>builder().data(currentStatus).build();
                } catch (Exception e) {
                    throw new PaaSTechnicalException("Could not retrieve status from PaaS", e);
                }
            });
        } catch (OrchestratorDisabledException e) {
            return RestResponseBuilder.<DeploymentStatus>builder().data(null).error(new RestError(RestErrorCode.CLOUD_DISABLED_ERROR.getCode(), e.getMessage())).build();
        }
    } else {
        return RestResponseBuilder.<DeploymentStatus>builder().data(null).error(new RestError(RestErrorCode.NOT_FOUND_ERROR.getCode(), "Deployment with id <" + deploymentId + "> was not found.")).build();
    }
}
Also used : OrchestratorDisabledException(alien4cloud.paas.exception.OrchestratorDisabledException) PaaSTechnicalException(alien4cloud.paas.exception.PaaSTechnicalException) RestError(alien4cloud.rest.model.RestError) Deployment(alien4cloud.model.deployment.Deployment) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus) OrchestratorDisabledException(alien4cloud.paas.exception.OrchestratorDisabledException) PaaSTechnicalException(alien4cloud.paas.exception.PaaSTechnicalException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with DeploymentStatus

use of alien4cloud.paas.model.DeploymentStatus in project alien4cloud by alien4cloud.

the class MockPaaSProvider method getStatus.

@Override
public void getStatus(PaaSDeploymentContext deploymentContext, IPaaSCallback<DeploymentStatus> callback) {
    DeploymentStatus status = doGetStatus(deploymentContext.getDeploymentPaaSId(), false);
    callback.onSuccess(status);
}
Also used : DeploymentStatus(alien4cloud.paas.model.DeploymentStatus)

Example 9 with DeploymentStatus

use of alien4cloud.paas.model.DeploymentStatus in project alien4cloud by alien4cloud.

the class MockPaaSProvider method doChangeStatus.

@Override
protected synchronized DeploymentStatus doChangeStatus(final String deploymentPaaSId, final DeploymentStatus status) {
    MockRuntimeDeploymentInfo runtimeDeploymentInfo = runtimeDeploymentInfos.get(deploymentPaaSId);
    DeploymentStatus oldDeploymentStatus = runtimeDeploymentInfo.getStatus();
    log.info("Deployment [" + deploymentPaaSId + "] moved from status [" + oldDeploymentStatus + "] to [" + status + "]");
    runtimeDeploymentInfo.setStatus(status);
    PaaSDeploymentLog deploymentLog = new PaaSDeploymentLog();
    deploymentLog.setDeploymentId(paaSDeploymentIdToAlienDeploymentIdMap.get(deploymentPaaSId));
    deploymentLog.setContent("Change deployment status to " + status);
    deploymentLog.setDeploymentPaaSId(deploymentPaaSId);
    deploymentLog.setLevel(PaaSDeploymentLogLevel.INFO);
    deploymentLog.setTimestamp(new Date());
    deploymentLog.setType("deployment_status_change");
    deploymentLog.setWorkflowId("install");
    alienMonitorDao.getClient().admin().indices().prepareRefresh(PaaSDeploymentLog.class.getSimpleName().toLowerCase()).execute().actionGet();
    deploymentLoggingService.save(deploymentLog);
    executorService.schedule(new Runnable() {

        @Override
        public void run() {
            PaaSDeploymentStatusMonitorEvent event = new PaaSDeploymentStatusMonitorEvent();
            event.setDeploymentStatus(status);
            event.setDate((new Date()).getTime());
            event.setDeploymentId(paaSDeploymentIdToAlienDeploymentIdMap.get(deploymentPaaSId));
            toBeDeliveredEvents.add(event);
            PaaSMessageMonitorEvent messageMonitorEvent = new PaaSMessageMonitorEvent();
            messageMonitorEvent.setDate((new Date()).getTime());
            messageMonitorEvent.setDeploymentId(paaSDeploymentIdToAlienDeploymentIdMap.get(deploymentPaaSId));
            messageMonitorEvent.setMessage("APPLICATIONS.RUNTIME.EVENTS.MESSAGE_EVENT.STATUS_DEPLOYMENT_CHANGED");
            toBeDeliveredEvents.add(messageMonitorEvent);
        }
    }, 2, TimeUnit.SECONDS);
    return oldDeploymentStatus;
}
Also used : PaaSDeploymentStatusMonitorEvent(alien4cloud.paas.model.PaaSDeploymentStatusMonitorEvent) PaaSMessageMonitorEvent(alien4cloud.paas.model.PaaSMessageMonitorEvent) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus) Date(java.util.Date) PaaSDeploymentLog(alien4cloud.paas.model.PaaSDeploymentLog)

Example 10 with DeploymentStatus

use of alien4cloud.paas.model.DeploymentStatus in project alien4cloud by alien4cloud.

the class ApplicationsDeploymentStepDefinitions method checkStatus.

@SuppressWarnings("rawtypes")
private void checkStatus(String applicationName, String deploymentId, DeploymentStatus expectedStatus, Set<DeploymentStatus> pendingStatus, long timeout, String applicationEnvironmentName, boolean failover) throws Throwable {
    String statusRequest = null;
    String applicationEnvironmentId = null;
    String applicationId = applicationName != null ? Context.getInstance().getApplicationId(applicationName) : null;
    if (deploymentId != null) {
        statusRequest = "/rest/v1/deployments/" + deploymentId + "/status";
    } else if (applicationId != null) {
        applicationEnvironmentId = applicationEnvironmentName != null ? Context.getInstance().getApplicationEnvironmentId(applicationName, applicationEnvironmentName) : Context.getInstance().getDefaultApplicationEnvironmentId(applicationName);
        statusRequest = "/rest/v1/applications/" + applicationId + "/environments/" + applicationEnvironmentId + "/status";
    } else {
        throw new ITException("Expected at least application ID OR deployment ID to check the status.");
    }
    long now = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis() - now > timeout) {
            if (failover) {
                log.warn("Expected deployment to be [" + expectedStatus + "] but Test has timeouted");
                return;
            } else {
                throw new ITException("Expected deployment to be [" + expectedStatus + "] but Test has timeouted");
            }
        }
        // get the current status
        String restResponseText = Context.getRestClientInstance().get(statusRequest);
        RestResponse<String> statusResponse = JsonUtil.read(restResponseText, String.class);
        assertNull(statusResponse.getError());
        DeploymentStatus deploymentStatus = DeploymentStatus.valueOf(statusResponse.getData());
        if (deploymentStatus.equals(expectedStatus)) {
            if (applicationId != null) {
                String restInfoResponseText = Context.getRestClientInstance().get("/rest/v1/applications/" + applicationId + "/environments/" + applicationEnvironmentId + "/deployment/informations");
                RestResponse<?> infoResponse = JsonUtil.read(restInfoResponseText);
                assertNull(infoResponse.getError());
            }
            return;
        } else if (pendingStatus.contains(deploymentStatus)) {
            Thread.sleep(1000L);
        } else {
            if (applicationId != null) {
                if (failover) {
                    log.warn("Expected deployment of app [" + applicationId + "] to be [" + expectedStatus + "] but was [" + deploymentStatus + "]");
                    return;
                } else {
                    throw new ITException("Expected deployment of app [" + applicationId + "] to be [" + expectedStatus + "] but was [" + deploymentStatus + "]");
                }
            } else {
                if (failover) {
                    log.warn("Expected deployment [" + deploymentId + "] to be [" + expectedStatus + "] but was [" + deploymentStatus + "]");
                    return;
                } else {
                    throw new ITException("Expected deployment [" + deploymentId + "] to be [" + expectedStatus + "] but was [" + deploymentStatus + "]");
                }
            }
        }
    }
}
Also used : ITException(alien4cloud.it.exception.ITException) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus)

Aggregations

DeploymentStatus (alien4cloud.paas.model.DeploymentStatus)14 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 MaintenanceModeException (alien4cloud.paas.exception.MaintenanceModeException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 Map (java.util.Map)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 AlreadyExistException (alien4cloud.exception.AlreadyExistException)2 Application (alien4cloud.model.application.Application)2 Deployment (alien4cloud.model.deployment.Deployment)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)2 IllegalDeploymentStateException (alien4cloud.paas.exception.IllegalDeploymentStateException)2 OperationExecutionException (alien4cloud.paas.exception.OperationExecutionException)2 OrchestratorDisabledException (alien4cloud.paas.exception.OrchestratorDisabledException)2 PluginConfigurationException (alien4cloud.paas.exception.PluginConfigurationException)2 PaaSDeploymentStatusMonitorEvent (alien4cloud.paas.model.PaaSDeploymentStatusMonitorEvent)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 YorcRestException (org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)2 NotFoundException (alien4cloud.exception.NotFoundException)1 ITException (alien4cloud.it.exception.ITException)1 ServiceResource (alien4cloud.model.service.ServiceResource)1