private static Integration integrationOf(IntegrationDeployment integrationDeployment) { return integrationDeployment.getSpec(); }
static boolean isNotWebhook(IntegrationDeployment integrationDeployment) { return !integrationDeployment.getSpec().getUsedConnectorIds().contains("webhook"); }
private String getLabel(IntegrationDeployment integrationDeployment) { return String.format("Integration [%s]", Names.sanitize(integrationDeployment.getSpec().getName())); }
@Value.Default default Optional<String> getIntegrationId() { return getSpec().getId(); }
private IntegrationDeploymentState determineState(IntegrationDeployment integrationDeployment) { Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.INTEGRATION_ID_LABEL, Labels.validate(integrationDeployment.getIntegrationId().get())); labels.put(OpenShiftService.DEPLOYMENT_VERSION_LABEL, String.valueOf(integrationDeployment.getVersion())); if (!openShiftService.exists(integrationDeployment.getSpec().getName()) || !openShiftService.isScaled(integrationDeployment.getSpec().getName(), 1, labels)) { return IntegrationDeploymentState.Unpublished; } else { return IntegrationDeploymentState.Published; } }
/** * 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(); }
static boolean needsExposure(final IntegrationDeployment integrationDeployment) { return integrationDeployment.getSpec() .getFlows().stream().flatMap(f -> f.getSteps().stream()) .flatMap(step -> Optionals.asStream(step.getAction())) .flatMap(action -> action.getTags().stream()) .anyMatch("expose"::equals); } }
public boolean isRunning(IntegrationDeployment integrationDeployment) { Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.INTEGRATION_ID_LABEL, Labels.validate(integrationDeployment.getIntegrationId().get())); labels.put(OpenShiftService.DEPLOYMENT_VERSION_LABEL, String.valueOf(integrationDeployment.getVersion())); return openShiftService().isScaled(integrationDeployment.getSpec().getName(), 1, labels); }
/** * Check if Integration has active deployments. * @param deployment The specified {@link IntegrationDeployment}. * @return The true if there are, false otherwise. */ private boolean hasPublishedDeployments(IntegrationDeployment deployment) { Integration integration = deployment.getSpec(); String id = Labels.validate(integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration"))); String version = String.valueOf(integration.getVersion()); Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.INTEGRATION_ID_LABEL, id); return (int) openShiftService().getDeploymentsByLabel(labels) .stream() .filter(d -> !version.equals(d.getMetadata().getLabels().get(OpenShiftService.DEPLOYMENT_VERSION_LABEL))) .filter(d -> d.getSpec().getReplicas() > 0) .count() > 0; }
/** * 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.validate(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(); }
private String build(IntegrationDeployment integration, DeploymentData data) throws IOException { InputStream tarInputStream = createProjectFiles(integration.getSpec()); logInfo(integration, "Created project files and starting build"); try { return openShiftService().build(integration.getSpec().getName(), data, tarInputStream); } catch (InterruptedException e) { throw SyndesisServerException.launderThrowable(e); } }
private void updateIntegrationEndpoint(IntegrationDeployment deployment) { if (deployment.getId().isPresent()) { IntegrationEndpoint endpoint = dataManager.fetch(IntegrationEndpoint.class, deployment.getId().get()); boolean endpointPresent = endpoint != null; Optional<String> host = openShiftService.getExposedHost(deployment.getSpec().getName()); Optional<IntegrationEndpoint> expectedEndpoint = expectedEndpoint(deployment, host); // Synchronize internal model if (!endpointPresent && expectedEndpoint.isPresent()) { LOG.info("Adding endpoint {} to integration deployment {}", expectedEndpoint.get(), deployment.getId().get()); dataManager.create(expectedEndpoint.get()); } else if (endpointPresent && expectedEndpoint.isPresent()) { if (!endpoint.equals(expectedEndpoint.get())) { LOG.info("Updating endpoint for deployment {} to {}", deployment.getId().get(), expectedEndpoint.get()); dataManager.update(expectedEndpoint.get()); } } else if (endpointPresent) { LOG.info("Deleting endpoint for deployment {}", deployment.getId().get()); dataManager.delete(IntegrationEndpoint.class, deployment.getId().get()); } } }
private boolean isBuildStarted(IntegrationDeployment integrationDeployment) { return openShiftService().isBuildStarted(integrationDeployment.getSpec().getName()); }
private boolean isBuildFailed(IntegrationDeployment integrationDeployment) { return openShiftService().isBuildFailed(integrationDeployment.getSpec().getName()); }
@Override public StateUpdate execute(IntegrationDeployment integrationDeployment) { Map<String, String> stepsDone = new HashMap<>(integrationDeployment.getStepsDone()); stepsDone.remove("deploy"); //we are literally undoing this step. IntegrationDeploymentState currentState = IntegrationDeploymentState.Pending; Map<String, String> labels = new HashMap<>(); labels.put(OpenShiftService.INTEGRATION_ID_LABEL, Labels.validate(integrationDeployment.getIntegrationId().get())); labels.put(OpenShiftService.DEPLOYMENT_VERSION_LABEL, String.valueOf(integrationDeployment.getVersion())); List<DeploymentConfig> deployments = openShiftService().getDeploymentsByLabel(labels); Boolean isDeployed = !deployments.stream().filter(d -> d.getSpec().getReplicas() != 0).collect(Collectors.toList()).isEmpty(); if (isDeployed) { try { LOG.info("Undeploying integration deployment:{} version:{}", integrationDeployment.getSpec().getName(), integrationDeployment.getVersion()); openShiftService().scale(integrationDeployment.getSpec().getName(), labels, 0, 1, TimeUnit.MINUTES); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return new StateUpdate(currentState, stepsDone); } } Boolean isUndeployed = !deployments.stream().filter(d -> d.getStatus().getAvailableReplicas() == 0).collect(Collectors.toList()).isEmpty(); if(isUndeployed){ currentState = IntegrationDeploymentState.Unpublished; } return new StateUpdate(currentState, stepsDone); }
private boolean isReady(IntegrationDeployment integrationDeployment) { return openShiftService().isDeploymentReady(integrationDeployment.getSpec().getName()); }
private Optional<IntegrationEndpoint> expectedEndpoint(IntegrationDeployment integrationDeployment, Optional<String> host) { if (!host.isPresent()) { return Optional.empty(); } Integration integ = integrationDeployment.getSpec(); return Optional.of(integrationDeployment) .map(deployment -> new IntegrationEndpoint.Builder() .id(deployment.getId()) .host(host) .protocol("https") .contextPath(join(serverBasePath(integ), contextPath(integ))) .build()); }
private String deploy(IntegrationDeployment integration, DeploymentData data) throws IOException { logInfo(integration, "Starting deployment"); String revision = openShiftService().deploy(integration.getSpec().getName(), data); logInfo(integration, "Deployment done"); return revision; }
default IntegrationDeployment deleted() { final Integration integration = new Integration.Builder().createFrom(getSpec()).isDeleted(true).build(); return builder().spec(integration).build(); }
IntegrationDeployment instance = (IntegrationDeployment) object; integrationId(instance.getIntegrationId()); Integration specValue = instance.getSpec(); if (specValue != null) { spec(specValue);