Search in sources :

Example 1 with RepositorySession

use of org.jboss.pnc.spi.repositorymanager.model.RepositorySession in project pnc by project-ncl.

the class DefaultBuildExecutor method configureRepository.

private RepositorySession configureRepository(DefaultBuildExecutionSession buildExecutionSession) {
    if (buildExecutionSession.isCanceled()) {
        return null;
    }
    ProcessStageUtils.logProcessStageBegin(BuildExecutionStatus.REPO_SETTING_UP.toString(), "Setting up repository...");
    buildExecutionSession.setStatus(BuildExecutionStatus.REPO_SETTING_UP);
    BuildType buildType = buildExecutionSession.getBuildExecutionConfiguration().getBuildType();
    if (buildType == null) {
        throw new BuildProcessException("Missing required value buildExecutionConfiguration.buildType");
    }
    RepositoryType repositoryType = BuildTypeToRepositoryType.getRepositoryType(buildType);
    try {
        RepositoryManager repositoryManager = repositoryManagerFactory.getRepositoryManager(repositoryType);
        BuildExecution buildExecution = buildExecutionSession.getBuildExecutionConfiguration();
        String serviceAccountToken = (serviceClient == null ? null : serviceClient.getAuthToken());
        RepositorySession buildRepository = repositoryManager.createBuildRepositoryWithRetries(buildExecution, buildExecutionSession.getAccessToken(), serviceAccountToken, repositoryType, buildExecutionSession.getBuildExecutionConfiguration().getGenericParameters(), buildExecutionSession.getBuildExecutionConfiguration().isBrewPullActive());
        return buildRepository;
    } catch (Throwable e) {
        throw new BuildProcessException(e);
    } finally {
        ProcessStageUtils.logProcessStageEnd(BuildExecutionStatus.REPO_SETTING_UP.toString(), "Repository setup complete.");
    }
}
Also used : RepositoryType(org.jboss.pnc.enums.RepositoryType) BuildType(org.jboss.pnc.enums.BuildType) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) RepositoryManager(org.jboss.pnc.spi.repositorymanager.RepositoryManager) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 2 with RepositorySession

use of org.jboss.pnc.spi.repositorymanager.model.RepositorySession in project pnc by project-ncl.

the class DefaultBuildExecutor method completeExecution.

private Void completeExecution(DefaultBuildExecutionSession buildExecutionSession, Throwable e) {
    String buildExecutionId = buildExecutionSession.getId();
    try {
        // Ends when the result is stored by the Orchestrator
        ProcessStageUtils.logProcessStageBegin("FINALIZING_BUILD", "Finalizing build ...");
        if (e != null) {
            log.debug("Finalizing FAILED execution. Exception: ", e);
        } else {
            log.debug("Finalizing SUCCESS execution.");
        }
        buildExecutionSession.setStatus(BuildExecutionStatus.FINALIZING_EXECUTION);
        if (buildExecutionSession.getStartTime() == null) {
            buildExecutionSession.setException(new ExecutorException("Missing start time."));
        }
        if (e != null) {
            stopRunningEnvironment(e);
        } else {
            try {
                destroyEnvironment(buildExecutionSession);
            } catch (BuildProcessException destroyException) {
                e = destroyException;
            }
        }
        if (e != null) {
            buildExecutionSession.setException(new ExecutorException(e));
        }
        if (buildExecutionSession.getEndTime() != null) {
            buildExecutionSession.setException(new ExecutorException("End time already set."));
        } else {
            buildExecutionSession.setEndTime(new Date());
        }
        // check if any of previous statuses indicated "failed" state
        if (buildExecutionSession.isCanceled()) {
            buildExecutionSession.setStatus(BuildExecutionStatus.CANCELLED);
            userLog.info("Build execution completed (canceled).");
        } else if (buildExecutionSession.hasFailed()) {
            buildExecutionSession.setStatus(BuildExecutionStatus.DONE_WITH_ERRORS);
            userLog.warn("Build execution completed with errors.");
        } else {
            buildExecutionSession.setStatus(BuildExecutionStatus.DONE);
            userLog.info("Build execution completed successfully.");
        }
        log.debug("Removing buildExecutionTask [" + buildExecutionId + "] from list of running tasks.");
        runningExecutions.remove(buildExecutionId);
        userLog.info("Build execution completed.");
    } catch (Throwable t) {
        userLog.error("Unable to complete execution!", t);
        String executorException = "Unable to recover, see system log for the details.";
        if (t.getMessage() != null) {
            executorException += " " + t.getMessage();
        }
        buildExecutionSession.setException(new ExecutorException(executorException));
        buildExecutionSession.setEndTime(new Date());
        buildExecutionSession.setStatus(BuildExecutionStatus.SYSTEM_ERROR);
        runningExecutions.remove(buildExecutionId);
    } finally {
        RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
        if (runningEnvironment == null) {
            log.warn("Could not close Maven repository session [" + buildExecutionId + "]. Running environment was not set!");
        } else {
            RepositorySession repositorySession = runningEnvironment.getRepositorySession();
            if (repositorySession == null) {
                log.warn("Could not close Maven repository session [" + buildExecutionId + "]. Repository session was not set!");
            } else {
                log.debug("Closing Maven repository session [" + buildExecutionId + "].");
                repositorySession.close();
            }
        }
    }
    return null;
}
Also used : ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) Date(java.util.Date) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 3 with RepositorySession

use of org.jboss.pnc.spi.repositorymanager.model.RepositorySession in project pnc by project-ncl.

the class OpenshiftStartedEnvironment method monitorInitialization.

/**
 * retries is decremented in retryPod in case of pod failing to start
 *
 * @param onComplete
 * @param onError
 * @param retries
 */
private void monitorInitialization(Consumer<RunningEnvironment> onComplete, Consumer<Exception> onError, int retries) {
    cancelHook = () -> onComplete.accept(null);
    CompletableFuture<Void> podFuture = creatingPod.thenComposeAsync(nul -> {
        CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isPodRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
        addFuture(monitor);
        return monitor;
    }, executor);
    CompletableFuture<Void> serviceFuture = creatingService.thenComposeAsync(nul -> {
        CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isServiceRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
        addFuture(monitor);
        return monitor;
    }, executor);
    CompletableFuture<Void> routeFuture;
    if (creatingRoute.isPresent()) {
        routeFuture = creatingRoute.get().thenComposeAsync(nul -> {
            CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isRouteRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
            addFuture(monitor);
            return monitor;
        }, executor);
    } else {
        routeFuture = CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> openshiftDefinitionsError = new CompletableFuture<>();
    openshiftDefinitions.exceptionally(t -> {
        openshiftDefinitionsError.completeExceptionally(t);
        return null;
    });
    CancellableCompletableFuture<Void> isBuildAgentUpFuture = pollingMonitor.monitor(this::isInternalServletAvailable, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
    addFuture(isBuildAgentUpFuture);
    CompletableFuture<RunningEnvironment> runningEnvironmentFuture = CompletableFutureUtils.allOfOrException(podFuture, serviceFuture, routeFuture).thenComposeAsync(nul -> isBuildAgentUpFuture, executor).thenApplyAsync(nul -> RunningEnvironment.createInstance(pod.getMetadata().getName(), Integer.parseInt(environmentConfiguration.getContainerPort()), route.getSpec().getHost(), getPublicEndpointUrl(), getInternalEndpointUrl(), repositorySession, Paths.get(environmentConfiguration.getWorkingDirectory()), this::destroyEnvironment, debugData), executor);
    CompletableFuture.anyOf(runningEnvironmentFuture, openshiftDefinitionsError).handleAsync((runningEnvironment, throwable) -> {
        if (throwable != null) {
            logger.info("Error while trying to create an OpenShift environment... ", throwable);
            cancelAndClearMonitors();
            // no more retries, execute the onError consumer
            if (retries == 0) {
                logger.info("No more retries left, giving up!");
                onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, true), throwable));
            } else {
                PodFailedStartException podFailedStartExc = null;
                if (throwable instanceof PodFailedStartException) {
                    podFailedStartExc = (PodFailedStartException) throwable;
                } else if (throwable.getCause() instanceof PodFailedStartException) {
                    podFailedStartExc = (PodFailedStartException) throwable.getCause();
                }
                if (podFailedStartExc != null && !Arrays.asList(POD_RETRYABLE_STATUSES).contains(podFailedStartExc.getPodStatus())) {
                    logger.info("The detected pod error status '{}' is not considered among the ones to be retried, giving up!", podFailedStartExc.getPodStatus());
                    // the status is not to be retried
                    onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, false), throwable));
                } else {
                    if (!cancelRequested) {
                        logger.warn("Creating build environment failed with error '{}'! Retrying ({} retries left)...", throwable, retries);
                        retryEnvironment(onComplete, onError, retries);
                    } else {
                        logger.info("Build was cancelled, not retrying environment!");
                    }
                }
            }
        } else {
            logger.info("Environment successfully initialized. Pod [{}]; Service [{}].", pod.getMetadata().getName(), service.getMetadata().getName());
            // openshiftDefinitionsError
            onComplete.accept((RunningEnvironment) runningEnvironment);
        // completes only with error
        }
        gaugeMetric.ifPresent(g -> g.incrementMetric(METRICS_POD_STARTED_SUCCESS_KEY));
        return null;
    }, executor);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ConcurrentSet(org.jboss.util.collection.ConcurrentSet) Arrays(java.util.Arrays) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) GaugeMetric(org.jboss.pnc.pncmetrics.GaugeMetric) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) MDCUtils(org.jboss.pnc.common.logging.MDCUtils) StringUtils(org.jboss.pnc.common.util.StringUtils) Route(io.fabric8.openshift.api.model.Route) OpenshiftBuildAgentConfig(org.jboss.pnc.common.json.moduleconfig.OpenshiftBuildAgentConfig) Map(java.util.Map) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) BuildConfigurationParameterKeys(org.jboss.pnc.api.constants.BuildConfigurationParameterKeys) OpenshiftEnvironmentDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.OpenshiftEnvironmentDriverModuleConfig) JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder) MetricsConfiguration(org.jboss.pnc.pncmetrics.MetricsConfiguration) Instant(java.time.Instant) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Config(io.fabric8.kubernetes.client.Config) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RandomUtils(org.jboss.pnc.common.util.RandomUtils) Service(io.fabric8.kubernetes.api.model.Service) ExecutorService(java.util.concurrent.ExecutorService) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) StringPropertyReplacer(org.jboss.util.StringPropertyReplacer) Logger(org.slf4j.Logger) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) IOException(java.io.IOException) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) PollingMonitor(org.jboss.pnc.common.monitor.PollingMonitor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) StartedEnvironment(org.jboss.pnc.spi.environment.StartedEnvironment) Paths(java.nio.file.Paths) CompletableFutureUtils(org.jboss.pnc.common.util.CompletableFutureUtils) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) IOException(java.io.IOException)

Example 4 with RepositorySession

use of org.jboss.pnc.spi.repositorymanager.model.RepositorySession in project pnc by project-ncl.

the class DestroyableEnvironmentMock method build.

public static DestroyableEnvironment build() {
    RepositorySession repositorySession = new RepositorySessionMock();
    Path workingDir = Paths.get("/root");
    return RunningEnvironment.createInstance("1", 8080, "localhost", "build/agent/url", "internal,build/agent/url", repositorySession, workingDir, () -> {
    }, new DebugData(false));
}
Also used : Path(java.nio.file.Path) DebugData(org.jboss.pnc.spi.builddriver.DebugData) RepositorySessionMock(org.jboss.pnc.mock.repositorymanager.RepositorySessionMock) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession)

Example 5 with RepositorySession

use of org.jboss.pnc.spi.repositorymanager.model.RepositorySession in project pnc by project-ncl.

the class BuildGroupIncludesProductVersionGroupTest method verifyGroupComposition_ProductVersion_NoConfSet.

@Test
public void verifyGroupComposition_ProductVersion_NoConfSet() throws Exception {
    // create a dummy non-chained build execution and repo session based on it
    BuildExecution execution = new TestBuildExecution("build_myproject_12345");
    Indy indy = driver.getIndy(accessToken);
    RepositorySession repositoryConfiguration = driver.createBuildRepository(execution, accessToken, accessToken, RepositoryType.MAVEN, Collections.emptyMap(), false);
    String repoId = repositoryConfiguration.getBuildRepositoryId();
    assertThat(repoId, equalTo(execution.getBuildContentId()));
    // check that the build group includes:
    // - the build's hosted repo
    // - the product version repo group
    // - the "shared-releases" repo group
    // - the "shared-imports" repo
    // - the public group
    // ...in that order
    Group buildGroup = indy.stores().load(new StoreKey(MAVEN_PKG_KEY, StoreType.group, repoId), Group.class);
    System.out.printf("Constituents:\n  %s\n", join(buildGroup.getConstituents(), "\n  "));
    assertGroupConstituents(buildGroup, new StoreKey(MAVEN_PKG_KEY, StoreType.hosted, execution.getBuildContentId()), new StoreKey(MAVEN_PKG_KEY, StoreType.group, IndyRepositoryConstants.COMMON_BUILD_GROUP_CONSTITUENTS_GROUP));
}
Also used : Group(org.commonjava.indy.model.core.Group) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) Indy(org.commonjava.indy.client.core.Indy) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test) ContainerTest(org.jboss.pnc.test.category.ContainerTest)

Aggregations

RepositorySession (org.jboss.pnc.spi.repositorymanager.model.RepositorySession)26 TestBuildExecution (org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution)20 BuildExecution (org.jboss.pnc.spi.repositorymanager.BuildExecution)20 Test (org.junit.Test)19 ContainerTest (org.jboss.pnc.test.category.ContainerTest)18 RepositoryManagerResult (org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult)13 StoreKey (org.commonjava.indy.model.core.StoreKey)12 Artifact (org.jboss.pnc.model.Artifact)11 Indy (org.commonjava.indy.client.core.Indy)8 Group (org.commonjava.indy.model.core.Group)7 File (java.io.File)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ProjectVersionRef (org.commonjava.atlas.maven.ident.ref.ProjectVersionRef)4 SimpleArtifactRef (org.commonjava.atlas.maven.ident.ref.SimpleArtifactRef)4 SimpleProjectVersionRef (org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef)4 IndyFoloContentClientModule (org.commonjava.indy.folo.client.IndyFoloContentClientModule)4 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)4 RepositoryConnectionInfo (org.jboss.pnc.spi.repositorymanager.model.RepositoryConnectionInfo)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 BuildProcessException (org.jboss.pnc.executor.exceptions.BuildProcessException)3