Search in sources :

Example 6 with BuildExecutionConfiguration

use of org.jboss.pnc.spi.executor.BuildExecutionConfiguration in project pnc by project-ncl.

the class LocalBuildScheduler method startBuilding.

@Override
public void startBuilding(BuildTask buildTask) throws CoreException {
    Consumer<BuildExecutionStatusChangedEvent> onBuildExecutionStatusChangedEvent = (statusChangedEvent) -> {
        try {
            log.debug("Received execution status update {}.", statusChangedEvent);
            if (statusChangedEvent.getNewStatus().isCompleted()) {
                BuildResult buildResult = statusChangedEvent.getBuildResult().get();
                log.debug("Notifying build execution completed {}.", statusChangedEvent);
                buildCoordinator.completeBuild(buildTask, buildResult);
            }
        } catch (Throwable t) {
            log.error("Failed to notify build completion.", t);
        }
    };
    String contentId = ContentIdentityManager.getBuildContentId(buildTask.getId());
    BuildConfigurationAudited configuration = buildTask.getBuildConfigurationAudited();
    BuildExecutionConfiguration buildExecutionConfiguration = BuildExecutionConfiguration.build(buildTask.getId(), contentId, buildTask.getUser().getId().toString(), configuration.getBuildScript(), configuration.getId().toString(), configuration.getName(), configuration.getRepositoryConfiguration().getInternalUrl(), configuration.getScmRevision(), null, configuration.getRepositoryConfiguration().getExternalUrl(), configuration.getRepositoryConfiguration().isPreBuildSyncEnabled(), configuration.getBuildEnvironment().getSystemImageId(), configuration.getBuildEnvironment().getSystemImageRepositoryUrl(), configuration.getBuildEnvironment().getSystemImageType(), configuration.getBuildType(), buildTask.getBuildOptions().isKeepPodOnFailure(), configuration.getGenericParameters(), buildTask.getBuildOptions().isTemporaryBuild(), TimeUtils.generateTimestamp(buildTask.getBuildOptions().isTimestampAlignment(), buildTask.getBuildSetTask().getStartTime()), configuration.isBrewPullActive(), configuration.getDefaultAlignmentParams(), buildTask.getBuildOptions().getAlignmentPreference());
    try {
        buildExecutor.startBuilding(buildExecutionConfiguration, onBuildExecutionStatusChangedEvent, buildTask.getUser().getLoginToken());
    } catch (ExecutorException e) {
        throw new CoreException("Could not start build execution.", e);
    }
}
Also used : TimeUtils(org.jboss.pnc.common.util.TimeUtils) Logger(org.slf4j.Logger) BuildScheduler(org.jboss.pnc.coordinator.builder.BuildScheduler) ContentIdentityManager(org.jboss.pnc.model.utils.ContentIdentityManager) BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) BuildResult(org.jboss.pnc.spi.BuildResult) LoggerFactory(org.slf4j.LoggerFactory) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) BuildConfigurationAudited(org.jboss.pnc.model.BuildConfigurationAudited) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) CoreException(org.jboss.pnc.spi.exception.CoreException) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) BuildConfigurationAudited(org.jboss.pnc.model.BuildConfigurationAudited) BuildResult(org.jboss.pnc.spi.BuildResult) ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) CoreException(org.jboss.pnc.spi.exception.CoreException) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent)

Example 7 with BuildExecutionConfiguration

use of org.jboss.pnc.spi.executor.BuildExecutionConfiguration in project pnc by project-ncl.

the class BuildResultMapper method toDTO.

public BuildResultRest toDTO(BuildResult buildResult) {
    CompletionStatus completionStatus = buildResult.getCompletionStatus();
    ProcessException processException = buildResult.getProcessException().orElse(null);
    String processLog = buildResult.getProcessLog();
    BuildExecutionConfigurationRest buildExecutionConfiguration;
    if (buildResult.getBuildExecutionConfiguration().isPresent()) {
        BuildExecutionConfiguration bec = buildResult.getBuildExecutionConfiguration().get();
        buildExecutionConfiguration = new BuildExecutionConfigurationRest(bec);
    } else {
        buildExecutionConfiguration = null;
    }
    BuildDriverResultRest buildDriverResult;
    if (buildResult.getBuildDriverResult().isPresent()) {
        BuildDriverResult result = buildResult.getBuildDriverResult().get();
        buildDriverResult = new BuildDriverResultRest(result);
    } else {
        buildDriverResult = null;
    }
    RepositoryManagerResultRest repositoryManagerResult;
    if (buildResult.getRepositoryManagerResult().isPresent()) {
        RepositoryManagerResult result = buildResult.getRepositoryManagerResult().get();
        repositoryManagerResult = repositoryManagerResultMapper.toDTO(result);
    } else {
        repositoryManagerResult = null;
    }
    EnvironmentDriverResult environmentDriverResult;
    if (buildResult.getEnvironmentDriverResult().isPresent()) {
        environmentDriverResult = buildResult.getEnvironmentDriverResult().get();
    } else {
        environmentDriverResult = null;
    }
    RepourResult repourResult = buildResult.getRepourResult().orElse(null);
    return new BuildResultRest(completionStatus, processException, processLog, buildExecutionConfiguration, buildDriverResult, repositoryManagerResult, environmentDriverResult, repourResult);
}
Also used : BuildResultRest(org.jboss.pnc.bpm.model.BuildResultRest) ProcessException(org.jboss.pnc.spi.coordinator.ProcessException) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) RepositoryManagerResultRest(org.jboss.pnc.bpm.model.RepositoryManagerResultRest) EnvironmentDriverResult(org.jboss.pnc.spi.environment.EnvironmentDriverResult) CompletionStatus(org.jboss.pnc.spi.coordinator.CompletionStatus) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) BuildExecutionConfigurationRest(org.jboss.pnc.bpm.model.BuildExecutionConfigurationRest) RepourResult(org.jboss.pnc.spi.repour.RepourResult) BuildDriverResultRest(org.jboss.pnc.bpm.model.BuildDriverResultRest)

Example 8 with BuildExecutionConfiguration

use of org.jboss.pnc.spi.executor.BuildExecutionConfiguration in project pnc by project-ncl.

the class BpmNotifier method sendBuildExecutionCompleted.

public void sendBuildExecutionCompleted(String uri, BuildResult buildResult, String accessToken) {
    log.debug("Preparing to send build result to BPM {}.", buildResult);
    BuildResultRest buildResultRest = null;
    String errMessage = "";
    try {
        buildResultRest = mapper.toDTO(buildResult);
        if (log.isTraceEnabled()) {
            log.trace("Sending build result to BPM {}.", buildResultRest.toFullLogString());
        } else {
            log.debug("Sending build result to BPM {}.", buildResultRest);
        }
    } catch (Throwable e) {
        log.error("Cannot construct rest result.", e);
        errMessage = "Cannot construct rest result: " + e.getMessage();
    }
    boolean isNewBpmProcess = false;
    if (buildResult.getBuildExecutionConfiguration().isPresent()) {
        BuildExecutionConfiguration buildExecutionConfiguration = buildResult.getBuildExecutionConfiguration().get();
        isNewBpmProcess = ConnectorSelector.useNewProcessForBuild(buildExecutionConfiguration.getGenericParameters(), bpmConfig.isNewBpmForced());
    } else {
        log.error("Missing BuildExecutionConfiguration!");
    }
    HttpPost request = new HttpPost(uri);
    String jsonEntity = buildResultRest != null ? buildResultRest.toFullLogString() : "{\"error\", \"" + errMessage + "\"}";
    if (isNewBpmProcess) {
        StringEntity entity = new StringEntity(jsonEntity, StandardCharsets.UTF_8);
        if (entity != null) {
            request.setEntity(entity);
            log.debug("Json response entity set to the post request for uri: " + uri);
        } else {
            log.error("Missing json response entity to post to: " + uri);
        }
        configureRequestNewBPM(accessToken, request);
    } else {
        List<NameValuePair> parameters = new ArrayList<>();
        parameters.add(new BasicNameValuePair("event", jsonEntity));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8);
        if (entity != null) {
            request.setEntity(entity);
            log.debug("Urlencoded response entity set to the post request for uri: " + uri);
        } else {
            log.error("Missing urlencoded response entity to post to: " + uri);
        }
        request.addHeader("Authorization", getAuthHeader());
    }
    // get id for logging
    String buildExecutionConfigurationId;
    if (buildResult.getBuildExecutionConfiguration().isPresent()) {
        BuildExecutionConfiguration buildExecutionConfiguration = buildResult.getBuildExecutionConfiguration().get();
        buildExecutionConfigurationId = buildExecutionConfiguration.getId() + "";
    } else {
        buildExecutionConfigurationId = "NO BuildExecutionConfiguration.";
    }
    log.info("Sending buildResult of buildExecutionConfiguration.id " + buildExecutionConfigurationId + ": " + request.getRequestLine());
    try (CloseableHttpClient httpClient = HttpUtils.getPermissiveHttpClient()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            log.info(response.getStatusLine().toString());
            try {
                if (response.getStatusLine().getStatusCode() != 200) {
                    InputStream content = response.getEntity().getContent();
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(content, writer);
                    log.debug("Received message: " + writer.toString());
                }
            } catch (Exception e) {
                log.warn("Cannot write http response message to log.", e);
            }
        }
    } catch (IOException e) {
        log.error("Error occurred executing the callback.", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ConfigurationParseException(org.jboss.pnc.common.json.ConfigurationParseException) IOException(java.io.IOException) BuildResultRest(org.jboss.pnc.bpm.model.BuildResultRest) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 9 with BuildExecutionConfiguration

use of org.jboss.pnc.spi.executor.BuildExecutionConfiguration in project pnc by project-ncl.

the class LivenessProbeTest method shouldFailTheBuildWhenAgentIsNotResponding.

@Test
public void shouldFailTheBuildWhenAgentIsNotResponding() throws InterruptedException, BuildDriverException {
    TermdBuildDriverModuleConfig buildDriverModuleConfig = mock(TermdBuildDriverModuleConfig.class);
    doReturn(200L).when(buildDriverModuleConfig).getLivenessProbeFrequencyMillis();
    doReturn(500L).when(buildDriverModuleConfig).getLivenessFailTimeoutMillis();
    ClientMockFactory buildAgentClientMockFactory = new ClientMockFactory();
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, buildAgentClientMockFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    RunningEnvironment runningEnvironment = mock(RunningEnvironment.class);
    doReturn(Paths.get("")).when(runningEnvironment).getWorkingDirectory();
    doReturn(new DebugData(false)).when(runningEnvironment).getDebugData();
    doReturn("http://localhost/").when(runningEnvironment).getInternalBuildAgentUrl();
    doReturn(runningEnvironment).when(buildExecution).getRunningEnvironment();
    BlockingQueue<Throwable> result = new ArrayBlockingQueue(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> Assert.fail("Build should complete with error.");
    Consumer<Throwable> onError = (throwable) -> {
        try {
            result.put(throwable);
        } catch (InterruptedException e) {
            Assert.fail("Error in the test. Unable to add the result to queue.");
        }
    };
    // when
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, runningEnvironment, onComplete, onError);
    // then
    Throwable throwable = result.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull("It should complete with an exception.", throwable);
    Assert.assertEquals("Build Agent has gone away.", throwable.getMessage());
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Logger(org.slf4j.Logger) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) LoggerFactory(org.slf4j.LoggerFactory) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) BlockingQueue(java.util.concurrent.BlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Paths(java.nio.file.Paths) DebugData(org.jboss.pnc.spi.builddriver.DebugData) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) Assert(org.junit.Assert) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.doReturn(org.mockito.Mockito.doReturn) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) DebugData(org.jboss.pnc.spi.builddriver.DebugData) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Test(org.junit.Test)

Example 10 with BuildExecutionConfiguration

use of org.jboss.pnc.spi.executor.BuildExecutionConfiguration in project pnc by project-ncl.

the class TermdBuildDriverTest method shouldStartAndCancelTheExecutionImmediately.

@Test(timeout = 5_000)
public void shouldStartAndCancelTheExecutionImmediately() throws ConfigurationParseException, BuildDriverException, InterruptedException, IOException {
    // given
    String dirName = "test-workdir";
    String logStart = "Running the command...";
    String logEnd = "Command completed.";
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, clientFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn("echo \"" + logStart + "\"; mvn validate; echo \"" + logEnd + "\";").when(buildExecutionConfiguration).getBuildScript();
    doReturn(dirName).when(buildExecutionConfiguration).getName();
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    AtomicReference<CompletedBuild> buildResult = new AtomicReference<>();
    // when
    CountDownLatch latch = new CountDownLatch(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        buildResult.set(completedBuild);
        latch.countDown();
    };
    Consumer<Throwable> onError = (throwable) -> {
        logger.error("Error received: ", throwable);
        fail(throwable.getMessage());
    };
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer, onComplete, onError);
    runningBuild.cancel();
    latch.await();
    // then
    assertThat(buildResult.get().getBuildResult().getBuildLog()).doesNotContain(logEnd);
    assertThat(buildResult.get().getBuildResult().getBuildStatus()).isEqualTo(CANCELLED);
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) CANCELLED(org.jboss.pnc.enums.BuildStatus.CANCELLED) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationParseException(org.jboss.pnc.common.json.ConfigurationParseException) Assert.fail(org.junit.Assert.fail) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) Path(java.nio.file.Path) Mockito.doReturn(org.mockito.Mockito.doReturn) Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) Files(java.nio.file.Files) MethodHandles(java.lang.invoke.MethodHandles) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) IOException(java.io.IOException) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Status(org.jboss.pnc.buildagent.api.Status) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)24 Consumer (java.util.function.Consumer)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Test (org.junit.Test)10 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)9 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)7 BuildResult (org.jboss.pnc.spi.BuildResult)7 ExecutorException (org.jboss.pnc.spi.executor.exceptions.ExecutorException)7 RepositoryManagerResult (org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult)7 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)6 BuildDriverResult (org.jboss.pnc.spi.builddriver.BuildDriverResult)6 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)6 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)6 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)6 IOException (java.io.IOException)5 BuildExecutionStatusChangedEvent (org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent)5 Inject (javax.inject.Inject)4 TaskStatusUpdateEvent (org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent)4 BuildExecutionStatus (org.jboss.pnc.enums.BuildExecutionStatus)4