use of com.sequenceiq.cloudbreak.service.image.StatedImage in project cloudbreak by hortonworks.
the class MeteringAzureMetadataPatchService method isAffected.
@Override
public boolean isAffected(Stack stack) {
try {
boolean affected = false;
if (StackType.WORKLOAD.equals(stack.getType()) && CloudPlatform.AZURE.equalsIgnoreCase(stack.getCloudPlatform())) {
try {
final Long dateBeforeTimestamp = dateStringToTimestampForImage(meteringAzureMetadataPatchConfig.getDateBefore());
Optional<StatedImage> statedImageOpt = stackImageService.getStatedImageInternal(stack);
if (statedImageOpt.isEmpty() || statedImageOpt.get().getImage() == null || statedImageOpt.get().getImage().getCreated() < dateBeforeTimestamp) {
affected = true;
}
} catch (Exception e) {
String errorMessage = String.format("Cannot determine stack with crn %s is affected by azure metadata issue", stack.getResourceCrn());
LOGGER.debug(errorMessage, e);
throw new ExistingStackPatchApplyException(errorMessage, e);
}
}
return affected;
} catch (Exception e) {
LOGGER.warn("Error during obtaining image / catalog for stack " + stack.getResourceCrn(), e);
throw new CloudbreakRuntimeException("Error during obtaining image / catalog for stack " + stack.getResourceCrn(), e);
}
}
use of com.sequenceiq.cloudbreak.service.image.StatedImage in project cloudbreak by hortonworks.
the class StackCreatorService method determineImageCatalog.
private Future<StatedImage> determineImageCatalog(String stackName, String platformString, StackV4Request stackRequest, Blueprint blueprint, User user, Workspace workspace) {
ClusterV4Request clusterRequest = stackRequest.getCluster();
if (clusterRequest == null) {
return null;
}
boolean shouldUseBaseCMImage = shouldUseBaseCMImage(clusterRequest, platformString);
boolean baseImageEnabled = imageCatalogService.baseImageEnabled();
Map<String, String> mdcContext = MDCBuilder.getMdcContextMap();
CloudbreakUser cbUser = restRequestThreadLocalService.getCloudbreakUser();
return executorService.submit(() -> {
MDCBuilder.buildMdcContextFromMap(mdcContext);
LOGGER.info("The stack with name {} has base images enabled: {} and should use base images: {}", stackName, baseImageEnabled, shouldUseBaseCMImage);
StatedImage statedImage = ThreadBasedUserCrnProvider.doAs(user.getUserCrn(), () -> {
try {
restRequestThreadLocalService.setCloudbreakUser(cbUser);
return imageService.determineImageFromCatalog(workspace.getId(), stackRequest.getImage(), platformString, stackRequest.getVariant(), blueprint, shouldUseBaseCMImage, baseImageEnabled, user, image -> true);
} catch (CloudbreakImageNotFoundException | CloudbreakImageCatalogException e) {
throw new RuntimeException(e);
}
});
MDCBuilder.cleanupMdc();
return statedImage;
});
}
use of com.sequenceiq.cloudbreak.service.image.StatedImage in project cloudbreak by hortonworks.
the class StackCreatorService method createStack.
public StackV4Response createStack(User user, Workspace workspace, StackV4Request stackRequest, boolean distroxRequest) {
long start = System.currentTimeMillis();
String stackName = stackRequest.getName();
nodeCountLimitValidator.validateProvision(stackRequest);
measure(() -> validateRecipeExistenceOnInstanceGroups(stackRequest.getInstanceGroups(), workspace.getId()), LOGGER, "Check that recipes do exist took {} ms");
measure(() -> ensureStackDoesNotExists(stackName, workspace), LOGGER, "Stack does not exist check took {} ms");
Stack stackStub = measure(() -> stackV4RequestToStackConverter.convert(stackRequest), LOGGER, "Stack request converted to stack took {} ms for stack {}", stackName);
stackStub.setWorkspace(workspace);
stackStub.setCreator(user);
StackType stackType = determineStackTypeBasedOnTheUsedApi(stackStub, distroxRequest);
stackStub.setType(stackType);
String platformString = stackStub.getCloudPlatform().toLowerCase();
MDCBuilder.buildMdcContext(stackStub);
Stack savedStack;
try {
Blueprint blueprint = measure(() -> determineBlueprint(stackRequest, workspace), LOGGER, "Blueprint determined in {} ms for stack {}", stackName);
Future<StatedImage> imgFromCatalogFuture = determineImageCatalog(stackName, platformString, stackRequest, blueprint, user, workspace);
hueWorkaroundValidatorService.validateForStackRequest(getHueHostGroups(blueprint.getBlueprintText()), stackStub.getName());
savedStack = transactionService.required(() -> {
Stack stack = measure(() -> stackDecorator.decorate(stackStub, stackRequest, user, workspace), LOGGER, "Decorate Stack with data took {} ms");
DetailedEnvironmentResponse environment = measure(() -> ThreadBasedUserCrnProvider.doAsInternalActor(regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), () -> environmentClientService.getByCrn(stack.getEnvironmentCrn())), LOGGER, "Get Environment from Environment service took {} ms");
if (stack.getOrchestrator() != null && stack.getOrchestrator().getApiEndpoint() != null) {
measure(() -> stackService.validateOrchestrator(stack.getOrchestrator()), LOGGER, "Validate orchestrator took {} ms");
}
stack.setUseCcm(environment.getTunnel().useCcm());
stack.setTunnel(environment.getTunnel());
if (stackRequest.getCluster() != null) {
measure(() -> setStackType(stack, blueprint), LOGGER, "Set stacktype for stack object took {} ms");
measure(() -> clusterCreationService.validate(stackRequest.getCluster(), stack, user, workspace, environment), LOGGER, "Validate cluster rds and autotls took {} ms");
}
measure(() -> fillInstanceMetadata(environment, stack), LOGGER, "Fill up instance metadata took {} ms");
StatedImage imgFromCatalog = measure(() -> getImageCatalog(imgFromCatalogFuture), LOGGER, "Select the correct image took {} ms");
stackRuntimeVersionValidator.validate(stackRequest, imgFromCatalog.getImage(), stackType);
Stack newStack = measure(() -> stackService.create(stack, platformString, imgFromCatalog, user, workspace, Optional.ofNullable(stackRequest.getResourceCrn())), LOGGER, "Save the remaining stack data took {} ms");
try {
LOGGER.info("Create cluster entity in the database with name {}.", stackName);
long clusterSaveStart = System.currentTimeMillis();
createClusterIfNeeded(user, stackRequest, newStack, stackName, blueprint);
LOGGER.info("Cluster save took {} ms", System.currentTimeMillis() - clusterSaveStart);
} catch (CloudbreakImageCatalogException | IOException | TransactionExecutionException e) {
throw new RuntimeException(e.getMessage(), e);
}
measure(() -> assignOwnerRoleOnDataHub(user, stackRequest, newStack), LOGGER, "assignOwnerRoleOnDataHub to stack took {} ms with name {}.", stackName);
return newStack;
});
} catch (TransactionExecutionException e) {
stackUnderOperationService.off();
if (e.getCause() instanceof DataIntegrityViolationException) {
String msg = String.format("Error with resource [%s], error: [%s]", APIResourceType.STACK, getProperSqlErrorMessage((DataIntegrityViolationException) e.getCause()));
throw new BadRequestException(msg, e.getCause());
}
throw new TransactionRuntimeExecutionException(e);
}
StackV4Response response = measure(() -> stackToStackV4ResponseConverter.convert(savedStack), LOGGER, "Stack response has been created for stack took {} ms with name {}", stackName);
LOGGER.info("Generated stack response after creation: {}", JsonUtil.writeValueAsStringSilentSafe(response));
FlowIdentifier flowIdentifier = measure(() -> flowManager.triggerProvisioning(savedStack.getId()), LOGGER, "Stack triggerProvisioning took {} ms with name {}", stackName);
response.setFlowIdentifier(flowIdentifier);
metricService.submit(STACK_PREPARATION, System.currentTimeMillis() - start);
return response;
}
use of com.sequenceiq.cloudbreak.service.image.StatedImage in project cloudbreak by hortonworks.
the class PackageVersionCheckerTest method compareImageAndInstancesMandatoryPackageVersionPrewarmedOk.
@Test
public void compareImageAndInstancesMandatoryPackageVersionPrewarmedOk() throws JsonProcessingException {
String packageName = "package";
Map<String, String> packageVersions = Collections.singletonMap(packageName, "1");
when(statedImage.getImage()).thenReturn(image);
when(image.isPrewarmed()).thenReturn(true);
when(image.getPackageVersions()).thenReturn(packageVersions);
Package aPackage = new Package();
aPackage.setName(packageName);
aPackage.setPrewarmed(true);
when(instanceMetadataUpdater.getPackages()).thenReturn(Collections.singletonList(aPackage));
when(instanceMetadataUpdater.isPackagesVersionEqual(anyString(), anyString())).thenReturn(true);
InstanceMetaData instanceMetaData = new InstanceMetaData();
instanceMetaData.setImage(new Json(new com.sequenceiq.cloudbreak.cloud.model.Image("image", Collections.emptyMap(), "os", "ostype", "catalogn", "catalogu", "id", packageVersions)));
Set<InstanceMetaData> instanceMetaDataSet = Collections.singleton(instanceMetaData);
CheckResult result = underTest.compareImageAndInstancesMandatoryPackageVersion(statedImage, instanceMetaDataSet);
assertEquals(EventStatus.OK, result.getStatus());
}
use of com.sequenceiq.cloudbreak.service.image.StatedImage in project cloudbreak by hortonworks.
the class PackageVersionCheckerTest method compareImageAndInstancesMandatoryPackageVersionDifferentPackageVersionInImage.
@Test
public void compareImageAndInstancesMandatoryPackageVersionDifferentPackageVersionInImage() throws JsonProcessingException {
String packageName = "package";
Map<String, String> packageVersions = Collections.singletonMap(packageName, "1");
when(statedImage.getImage()).thenReturn(image);
when(image.isPrewarmed()).thenReturn(true);
when(image.getPackageVersions()).thenReturn(Collections.singletonMap(packageName, "2"));
Package aPackage = new Package();
aPackage.setName(packageName);
aPackage.setPrewarmed(true);
when(instanceMetadataUpdater.getPackages()).thenReturn(Collections.singletonList(aPackage));
InstanceMetaData instanceMetaData = new InstanceMetaData();
instanceMetaData.setImage(new Json(new com.sequenceiq.cloudbreak.cloud.model.Image("image", Collections.emptyMap(), "os", "ostype", "catalogn", "catalogu", "id", packageVersions)));
Set<InstanceMetaData> instanceMetaDataSet = Collections.singleton(instanceMetaData);
CheckResult result = underTest.compareImageAndInstancesMandatoryPackageVersion(statedImage, instanceMetaDataSet);
assertEquals(EventStatus.FAILED, result.getStatus());
}
Aggregations