public int getIntegrationVersion() { return deployment.getSpec().getVersion(); }
/** * Counts active integrations (in DB) of the owner of the specified integration. * * @param deployment The specified IntegrationDeployment. * @return The number of integrations (excluding the current). */ private int countActiveIntegrationsOfSameUserAs(IntegrationDeployment deployment) { Integration integration = deployment.getSpec(); String integrationId = integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration.")); String username = deployment.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return (int) dataManager.fetchAll(IntegrationDeployment.class).getItems() .stream() .filter(i -> !i.getIntegrationId().get().equals(integrationId)) //The "current" integration will already be in the database. .filter(i -> IntegrationDeploymentState.Published == i.getCurrentState()) .filter(i -> i.getUserId().map(username::equals).orElse(Boolean.FALSE)) .count(); }
private String getIntegrationMarkerKey(IntegrationDeployment integrationDeployment) { return integrationDeployment.getTargetState() + ":" + integrationDeployment.getId().orElseThrow(() -> new IllegalArgumentException("No id set in integration " + integrationDeployment)); }
private boolean stale(StateChangeHandler handler, IntegrationDeployment integrationDeployment) { if (integrationDeployment == null || handler == null) { return true; } IntegrationDeploymentState desiredState = integrationDeployment.getTargetState(); return desiredState.equals(integrationDeployment.getCurrentState()); } }
private void deactivatePreviousDeployments(IntegrationDeployment integrationDeployment) { dataManager.fetchIdsByPropertyValue(IntegrationDeployment.class, "integrationId", integrationDeployment.getIntegrationId().get(), "currentState", "Active") .stream() .map(id -> dataManager.fetch(IntegrationDeployment.class, id)) .filter(r -> r.getVersion().orElse(0) >= integrationDeployment.getVersion().orElse(0)) .map(r -> r.withCurrentState(IntegrationDeploymentState.Undeployed)) .forEach(r -> dataManager.update(r)); }
private ResourceIdentifier toIntegrationResourceIdentifier(IntegrationDeployment integrationDeployment) { return new ResourceIdentifier.Builder() .id(integrationDeployment.getIntegrationId()) .kind(Kind.Integration) .name(Optional.ofNullable(integrationDeployment.getSpec().getName())) .build(); }
private void checkIntegrationStatus(IntegrationDeployment integrationDeployment) { if (integrationDeployment == null) { return; } IntegrationDeploymentState desiredState = integrationDeployment.getTargetState(); IntegrationDeploymentState currentState = integrationDeployment.getCurrentState(); if (!currentState.equals(desiredState)) { integrationDeployment.getId().ifPresent(integrationDeploymentId -> { StateChangeHandler statusChangeHandler = handlers.get(desiredState); if (statusChangeHandler != null) { LOG.info("Integration {} : Desired status \"{}\" != current status \"{}\" --> calling status change handler", integrationDeploymentId, desiredState.toString(), currentState); callStateChangeHandler(statusChangeHandler, integrationDeploymentId); } }); } else { scheduledChecks.remove(getIntegrationMarkerKey(integrationDeployment)); } }
private void setVersion(IntegrationDeployment integrationDeployment) { Integration integration = integrationDeployment.getIntegrationId().map(i -> dataManager.fetch(Integration.class, i)).orElseThrow(()-> new IllegalStateException("Integration not found!")); dataManager.update(new Integration.Builder().createFrom(integration).version(integrationDeployment.getVersion()).build()); }
private void deactivatePreviousDeployments(IntegrationDeployment integrationDeployment) { dataManager.fetchIdsByPropertyValue(IntegrationDeployment.class, "integrationId", integrationDeployment.getIntegrationId().get(), "targetState", IntegrationDeploymentState.Published.name()) .stream() .map(id -> dataManager.fetch(IntegrationDeployment.class, id)) .filter(r -> r.getVersion() != integrationDeployment.getVersion()) .map(r -> r.withTargetState(IntegrationDeploymentState.Unpublished)) .forEach(r -> dataManager.update(r)); }
private boolean isIntegrationActiveAndUsingExtension(IntegrationDeployment integrationDeployment, Extension extension) { if (integrationDeployment == null || extension == null) { return false; } if (IntegrationDeploymentState.Published != integrationDeployment.getTargetState()) { return false; } return integrationDeployment.getSpec().getSteps().stream().anyMatch(step -> { boolean usedAsStep = extension.getExtensionId().equals( Optional.ofNullable(step) .flatMap(Step::getExtension) .map(Extension::getExtensionId) .orElse(null) ); boolean usedAsConnector = extension.getExtensionId().equals( Optional.ofNullable(step) .flatMap(Step::getConnection) .flatMap(Connection::getConnector) .flatMap(c -> c.getDependencies().stream().filter(Dependency::isExtension).findFirst()) .map(Dependency::getId) .orElse(null) ); return usedAsStep || usedAsConnector; }); }
private DeploymentData createDeploymentData(Integration integration, IntegrationDeployment integrationDeployment) { Properties applicationProperties = projectGenerator.generateApplicationProperties(integration); String username = integrationDeployment.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return DeploymentData.builder() .addLabel(OpenShiftService.INTEGRATION_ID_LABEL, Labels.sanitize(integrationDeployment.getIntegrationId().orElseThrow(() -> new IllegalStateException("IntegrationDeployment should have an integrationId")))) .addLabel(OpenShiftService.DEPLOYMENT_ID_LABEL, Integer.toString(integrationDeployment.getVersion())) .addLabel(OpenShiftService.USERNAME_LABEL, Labels.sanitize(username)) .addAnnotation(OpenShiftService.INTEGRATION_NAME_ANNOTATION, integration.getName()) .addAnnotation(OpenShiftService.INTEGRATION_ID_ANNOTATION, integrationDeployment.getIntegrationId().get()) .addAnnotation(OpenShiftService.DEPLOYMENT_VERSION_ANNOTATION, Integer.toString(integrationDeployment.getVersion())) .addSecretEntry("application.properties", propsToString(applicationProperties)) .build(); }
private String getLabel(IntegrationDeployment integrationDeployment) { return "Integration " + integrationDeployment.getIntegrationId().orElse("[none]"); }
final String integrationId = integrationDeployment.getIntegrationId().get(); LOG.info("Integration {} : Start processing integration: {}, version: {} with handler:{}", integrationId, integrationId, integrationDeployment.getVersion(), handler.getClass().getSimpleName()); handler.execute(integrationDeployment, update->{ if (LOG.isInfoEnabled()) { dataManager.update(current.builder() .statusMessage(Optional.ofNullable(update.getStatusMessage())) .currentState(update.getState())
LOG.info("Integration {} : Start processing integration: {}, version: {} with handler:{}", integrationDeployment.getIntegrationId().get(), integrationDeployment.getVersion().get(), handler.getClass().getSimpleName()); StateUpdate update = handler.execute(integrationDeployment); if (update!=null) { .createdDate(IntegrationDeploymentState.Active.equals(update.getState()) ? now : integrationDeployment.getCreatedDate()) .lastUpdated(new Date()) .build();
/** * Counts active integrations (in DB) of the owner of the specified integration. * * @param integration The specified integration. * @return The number of integrations (excluding the current). */ private int countActiveIntegrationsOfSameUserAs(Integration integration) { String integrationId = integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration.")); String username = integration.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return (int) dataManager.fetchAll(IntegrationDeployment.class).getItems() .stream() .filter(i -> !i.getIntegrationId().get().equals(integrationId)) //The "current" integration will already be in the database. .filter(i -> IntegrationDeploymentState.Active == i.getCurrentState()) .map(i -> i.getIntegrationId().get()) .distinct() .map(i -> dataManager.fetch(Integration.class, i)) .filter(i -> i.getUserId().map(username::equals).orElse(Boolean.FALSE)) .count(); }
/** * Count the deployments of the owner of the specified integration. * * @param deployment The specified IntegrationDeployment. * @return The number of deployed integrations (excluding the current). */ private int countDeployments(IntegrationDeployment deployment) { Integration integration = deployment.getSpec(); String id = Labels.sanitize(integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration"))); String username = deployment.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.USERNAME_LABEL, Labels.sanitize(username)); return (int) openShiftService().getDeploymentsByLabel(labels) .stream() .filter(d -> !id.equals(d.getMetadata().getLabels().get(OpenShiftService.INTEGRATION_ID_LABEL))) .filter(d -> d.getSpec().getReplicas() > 0) .count(); }
@PUT @Produces(MediaType.APPLICATION_JSON) @Path("/{id}/deployments") public IntegrationDeployment putDeployment(@Context SecurityContext sec, @NotNull @PathParam("id") @ApiParam(required = true) String id) { Integration integration = Getter.super.get(id); int nextDeploymentVersion = 1; // Update previous deployments targetState=Undeployed and make sure nextDeploymentVersion is larger than all previous ones. Set<String> deploymentIds = getDataManager().fetchIdsByPropertyValue(IntegrationDeployment.class, "integrationId", id); if (deploymentIds != null && !deploymentIds.isEmpty()) { Stream<IntegrationDeployment> deployments = deploymentIds.stream() .map(i -> getDataManager().fetch(IntegrationDeployment.class, i)) .filter(r -> r != null); for (IntegrationDeployment d : deployments.toArray(IntegrationDeployment[]::new)) { nextDeploymentVersion = Math.max(nextDeploymentVersion, d.getVersion()+1); getDataManager().update(d.withTargetState(IntegrationDeploymentState.Unpublished)); } } IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, nextDeploymentVersion)) .spec(integration) .version(nextDeploymentVersion) // .userId(SecurityContextHolder.getContext().getAuthentication().getName()) .userId(sec.getUserPrincipal().getName()) .build(); deployment = getDataManager().create(deployment); return deployment; }
private void updateDeploymentState(IntegrationDeployment integrationDeployment, IntegrationDeploymentState state) { IntegrationDeployment d = dataManager.fetch(IntegrationDeployment.class, integrationDeployment.getId().get()); dataManager.update(d.withCurrentState(state)); }
public int getVersion() { return deployment.getVersion(); }
public IntegrationDeploymentState getTargetState() { return deployment.getTargetState(); } }