Search in sources :

Example 1 with ImageStatusResult

use of com.sequenceiq.common.api.type.ImageStatusResult in project cloudbreak by hortonworks.

the class ServiceProviderSetupAdapter method checkImage.

public ImageStatusResult checkImage(Stack stack) throws Exception {
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = CloudContext.Builder.builder().withId(stack.getId()).withName(stack.getName()).withCrn(stack.getResourceCrn()).withPlatform(stack.getCloudPlatform()).withVariant(stack.getPlatformVariant()).withLocation(location).withWorkspaceId(stack.getWorkspace().getId()).withAccountId(stack.getTenant().getId()).build();
    CloudCredential cloudCredential = stackUtil.getCloudCredential(stack);
    Image image = imageService.getImage(stack.getId());
    CheckImageRequest<CheckImageResult> checkImageRequest = new CheckImageRequest<>(cloudContext, cloudCredential, cloudStackConverter.convert(stack), image);
    LOGGER.debug("Triggering event: {}", checkImageRequest);
    eventBus.notify(checkImageRequest.selector(), eventFactory.createEvent(checkImageRequest));
    try {
        CheckImageResult res = checkImageRequest.await();
        LOGGER.debug("Result: {}", res);
        if (res.getErrorDetails() != null) {
            LOGGER.info("Failed to check image state", res.getErrorDetails());
            throw new OperationException(res.getErrorDetails());
        }
        return new ImageStatusResult(res.getImageStatus(), res.getStatusProgressValue());
    } catch (InterruptedException e) {
        LOGGER.info("Error while executing check image", e);
        throw new OperationException(e);
    }
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CheckImageResult(com.sequenceiq.cloudbreak.cloud.event.setup.CheckImageResult) Image(com.sequenceiq.cloudbreak.cloud.model.Image) OperationException(com.sequenceiq.cloudbreak.service.OperationException) Location(com.sequenceiq.cloudbreak.cloud.model.Location) CheckImageRequest(com.sequenceiq.cloudbreak.cloud.event.setup.CheckImageRequest) ImageStatusResult(com.sequenceiq.common.api.type.ImageStatusResult)

Example 2 with ImageStatusResult

use of com.sequenceiq.common.api.type.ImageStatusResult in project cloudbreak by hortonworks.

the class AzureImageSetupService method getImageStatusResult.

private ImageStatusResult getImageStatusResult(String imageResourceGroupName, AzureClient client, AzureImageInfo azureImageInfo, String imageStorageName) {
    CopyState copyState = client.getCopyStatus(imageResourceGroupName, imageStorageName, IMAGES_CONTAINER, azureImageInfo.getImageName());
    boolean storageContainsImage = storageContainsImage(client, imageResourceGroupName, imageStorageName, azureImageInfo.getImageName());
    if (copyState == null && storageContainsImage) {
        LOGGER.debug("The copy has been finished because the storage account already contains the image.");
        return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
    } else if (copyState == null && !storageContainsImage) {
        throw new CloudConnectorException("Image copy failed because the copy state is not available and the storage account does not contains the image.");
    }
    if (CopyStatus.SUCCESS.equals(copyState.getStatus())) {
        if (!storageContainsImage) {
            LOGGER.error("The image has not been found in the storage account.");
            return new ImageStatusResult(ImageStatus.CREATE_FAILED, ImageStatusResult.COMPLETED);
        }
        LOGGER.info("The image copy has been finished.");
        return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
    } else if (isCopyStatusFailed(copyState)) {
        LOGGER.error("The image copy has failed with status: {}", copyState.getStatus());
        return new ImageStatusResult(ImageStatus.CREATE_FAILED, 0);
    } else {
        int percentage = (int) (((double) copyState.getBytesCopied() * ImageStatusResult.COMPLETED) / copyState.getTotalBytes());
        LOGGER.info("CopyStatus, Total:{} / Pending:{} bytes, {}%", copyState.getTotalBytes(), copyState.getBytesCopied(), percentage);
        return new ImageStatusResult(ImageStatus.IN_PROGRESS, percentage);
    }
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CopyState(com.microsoft.azure.storage.blob.CopyState) ImageStatusResult(com.sequenceiq.common.api.type.ImageStatusResult)

Example 3 with ImageStatusResult

use of com.sequenceiq.common.api.type.ImageStatusResult in project cloudbreak by hortonworks.

the class AzureImageSetupService method checkImageStatus.

public ImageStatusResult checkImageStatus(AuthenticatedContext ac, CloudStack stack, Image image) {
    if (azureImageFormatValidator.isMarketplaceImageFormat(image)) {
        LOGGER.info("Skipping image copy check as target image ({}) is an Azure Marketplace image!", image.getImageName());
        return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
    }
    CloudContext cloudContext = ac.getCloudContext();
    String imageResourceGroupName = azureResourceGroupMetadataProvider.getImageResourceGroupName(cloudContext, stack);
    AzureClient client = ac.getParameter(AzureClient.class);
    AzureImageInfo azureImageInfo = azureImageInfoService.getImageInfo(imageResourceGroupName, image.getImageName(), ac, client);
    Optional<VirtualMachineCustomImage> customImage = azureManagedImageService.findVirtualMachineCustomImage(azureImageInfo, client);
    if (customImage.isPresent()) {
        LOGGER.info("Custom image with id {} already exists in the target resource group {}, bypassing VHD copy check!", customImage.get().id(), imageResourceGroupName);
        return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
    }
    AzureCredentialView acv = new AzureCredentialView(ac.getCloudCredential());
    String imageStorageName = armStorage.getImageStorageName(acv, cloudContext, stack);
    try {
        return getImageStatusResult(imageResourceGroupName, client, azureImageInfo, imageStorageName);
    } catch (RuntimeException ex) {
        String msg = String.format("Failed to check the status of the image in resource group '%s', image storage name '%s'", imageResourceGroupName, imageStorageName);
        LOGGER.error(msg, ex);
        return new ImageStatusResult(ImageStatus.CREATE_FAILED, ImageStatusResult.INIT);
    }
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) VirtualMachineCustomImage(com.microsoft.azure.management.compute.VirtualMachineCustomImage) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ImageStatusResult(com.sequenceiq.common.api.type.ImageStatusResult)

Example 4 with ImageStatusResult

use of com.sequenceiq.common.api.type.ImageStatusResult in project cloudbreak by hortonworks.

the class GcpProvisionSetupTest method testCheckImageStatusWhenImageIsReady.

@Test
public void testCheckImageStatusWhenImageIsReady() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudStack cloudStack = mock(CloudStack.class);
    Compute compute = mock(Compute.class);
    Compute.Images images = mock(Compute.Images.class);
    Image imageGoogle = mock(Image.class);
    Compute.Images.Get imagesGet = mock(Compute.Images.Get.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    com.sequenceiq.cloudbreak.cloud.model.Image image = new com.sequenceiq.cloudbreak.cloud.model.Image("super-image", Map.of(), "centos", "redhat", "http://url", "default", "1234-1234-123-123", Map.of());
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(gcpStackUtil.getProjectId(cloudCredential)).thenReturn("project-id");
    when(gcpStackUtil.getImageName(anyString())).thenReturn("super-image");
    when(gcpComputeFactory.buildCompute(cloudCredential)).thenReturn(compute);
    when(compute.images()).thenReturn(images);
    when(images.get(anyString(), anyString())).thenReturn(imagesGet);
    when(imagesGet.execute()).thenReturn(imageGoogle);
    when(imageGoogle.getStatus()).thenReturn("READY");
    ImageStatusResult imageStatusResult = underTest.checkImageStatus(authenticatedContext, cloudStack, image);
    Assert.assertEquals(ImageStatus.CREATE_FINISHED, imageStatusResult.getImageStatus());
    Assert.assertEquals(Integer.valueOf(100), imageStatusResult.getStatusProgressValue());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Image(com.google.api.services.compute.model.Image) Compute(com.google.api.services.compute.Compute) ImageStatusResult(com.sequenceiq.common.api.type.ImageStatusResult) Test(org.junit.jupiter.api.Test)

Example 5 with ImageStatusResult

use of com.sequenceiq.common.api.type.ImageStatusResult in project cloudbreak by hortonworks.

the class GcpProvisionSetupTest method testCheckImageStatusWhenImageIsDropIOExceptionShouldCreateFailed.

@Test
public void testCheckImageStatusWhenImageIsDropIOExceptionShouldCreateFailed() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudStack cloudStack = mock(CloudStack.class);
    Compute compute = mock(Compute.class);
    Compute.Images images = mock(Compute.Images.class);
    Compute.Images.Get imagesGet = mock(Compute.Images.Get.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    com.sequenceiq.cloudbreak.cloud.model.Image image = new com.sequenceiq.cloudbreak.cloud.model.Image("super-image", Map.of(), "centos", "redhat", "http://url", "default", "1234-1234-123-123", Map.of());
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(gcpStackUtil.getProjectId(cloudCredential)).thenReturn("project-id");
    when(gcpStackUtil.getImageName(anyString())).thenReturn("super-image");
    when(gcpComputeFactory.buildCompute(cloudCredential)).thenReturn(compute);
    when(compute.images()).thenReturn(images);
    when(images.get(anyString(), anyString())).thenReturn(imagesGet);
    when(imagesGet.execute()).thenThrow(IOException.class);
    ImageStatusResult imageStatusResult = underTest.checkImageStatus(authenticatedContext, cloudStack, image);
    Assert.assertEquals(ImageStatus.CREATE_FAILED, imageStatusResult.getImageStatus());
    Assert.assertEquals(Integer.valueOf(0), imageStatusResult.getStatusProgressValue());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Image(com.google.api.services.compute.model.Image) Compute(com.google.api.services.compute.Compute) ImageStatusResult(com.sequenceiq.common.api.type.ImageStatusResult) Test(org.junit.jupiter.api.Test)

Aggregations

ImageStatusResult (com.sequenceiq.common.api.type.ImageStatusResult)8 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)5 Compute (com.google.api.services.compute.Compute)4 Image (com.google.api.services.compute.model.Image)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)4 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)4 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)3 Test (org.junit.jupiter.api.Test)3 CheckImageResult (com.sequenceiq.cloudbreak.cloud.event.setup.CheckImageResult)2 Image (com.sequenceiq.cloudbreak.cloud.model.Image)2 TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)1 Get (com.google.api.services.compute.Compute.Images.Get)1 VirtualMachineCustomImage (com.microsoft.azure.management.compute.VirtualMachineCustomImage)1 CopyState (com.microsoft.azure.storage.blob.CopyState)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)1 CheckImageRequest (com.sequenceiq.cloudbreak.cloud.event.setup.CheckImageRequest)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 Location (com.sequenceiq.cloudbreak.cloud.model.Location)1 OperationException (com.sequenceiq.cloudbreak.service.OperationException)1