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);
}
}
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);
}
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);
}
}
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());
}
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);
}
Aggregations