Search in sources :

Example 1 with DockerServiceUnavailableException

use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException in project aws-greengrass-nucleus by aws-greengrass.

the class DefaultDockerClient method login.

/**
 * Login to given docker registry.
 *
 * @param registry Registry to log into, with credentials encapsulated
 * @throws DockerLoginException                error in authenticating with the registry
 * @throws UserNotAuthorizedForDockerException when current user is not authorized to use docker
 * @throws DockerServiceUnavailableException   an error that can be potentially fixed through retries
 * @throws IOException                         unexpected error
 */
public void login(Registry registry) throws DockerLoginException, UserNotAuthorizedForDockerException, DockerServiceUnavailableException, IOException {
    Map<String, String> credEnvMap = new HashMap<>();
    credEnvMap.put("dockerUsername", registry.getCredentials().getUsername());
    credEnvMap.put("dockerPassword", registry.getCredentials().getPassword());
    Platform platform = Platform.getInstance();
    String loginCommand = String.format("docker login %s -u %s -p %s", registry.getEndpoint(), platform.formatEnvironmentVariableCmd("dockerUsername"), platform.formatEnvironmentVariableCmd("dockerPassword"));
    CliResponse response = runDockerCmd(loginCommand, credEnvMap);
    Optional<UserNotAuthorizedForDockerException> userAuthorizationError = checkUserAuthorizationError(response);
    if (userAuthorizationError.isPresent()) {
        throw userAuthorizationError.get();
    }
    if (response.exit.isPresent()) {
        if (response.exit.get() == 0) {
            return;
        } else {
            if (response.getOut().contains("Service Unavailable")) {
                // engine has issues or proxy config is bad etc. Not entirely reliable to determine retry behavior
                throw new DockerServiceUnavailableException(String.format("Error logging into the registry using credentials - %s", response.err));
            }
            throw new DockerLoginException(String.format("Error logging into the registry using credentials - %s", response.err));
        }
    } else {
        throw new IOException("Unexpected error while trying to perform docker login", response.failureCause);
    }
}
Also used : DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) Platform(com.aws.greengrass.util.platforms.Platform) HashMap(java.util.HashMap) IOException(java.io.IOException) DockerLoginException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerLoginException) UserNotAuthorizedForDockerException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.UserNotAuthorizedForDockerException)

Example 2 with DockerServiceUnavailableException

use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageDownloaderTest method GIVEN_connectivity_available_WHEN_failed_to_pull_image_consistently_THEN_fail_after_finite_retries.

@Test
void GIVEN_connectivity_available_WHEN_failed_to_pull_image_consistently_THEN_fail_after_finite_retries(ExtensionContext extensionContext) throws Exception {
    ignoreExceptionOfType(extensionContext, DockerServiceUnavailableException.class);
    URI artifactUri = new URI("docker:registry.hub.docker.com/library/alpine:sometag");
    Image image = Image.fromArtifactUri(ComponentArtifact.builder().artifactUri(artifactUri).build());
    when(dockerClient.dockerInstalled()).thenReturn(true);
    // fail first attempt, succeed on next retry attempt, device is connected the whole time
    doThrow(new DockerServiceUnavailableException("Service Unavailable")).when(dockerClient).pullImage(image);
    DockerImageDownloader downloader = getDownloader(artifactUri);
    Throwable err = assertThrows(PackageDownloadException.class, () -> downloader.download());
    assertThat(err.getMessage(), containsString("Failed to download docker image"));
    assertTrue(err.getCause() instanceof DockerServiceUnavailableException);
    assertEquals("library/alpine", image.getName());
    assertEquals("sometag", image.getTag());
    assertNull(image.getDigest());
    assertFalse(image.getRegistry().isEcrRegistry());
    assertFalse(image.getRegistry().isPrivateRegistry());
    assertEquals("registry.hub.docker.com", image.getRegistry().getEndpoint());
    verify(ecrAccessor, never()).getCredentials(anyString());
    verify(dockerClient, never()).login(any());
    // Invocations as many as the retry count should be expected
    verify(dockerClient, times(2)).pullImage(any());
    verify(mqttClient, times(2)).getMqttOnline();
}
Also used : DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with DockerServiceUnavailableException

use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageDownloaderTest method GIVEN_a_container_component_WHENn_failed_to_pull_image_THEN_fail_deployment.

@Test
void GIVEN_a_container_component_WHENn_failed_to_pull_image_THEN_fail_deployment(ExtensionContext extensionContext) throws Exception {
    ignoreExceptionOfType(extensionContext, DockerServiceUnavailableException.class);
    URI artifactUri = new URI("docker:registry.hub.docker.com/library/alpine:sometag");
    Image image = Image.fromArtifactUri(ComponentArtifact.builder().artifactUri(artifactUri).build());
    when(dockerClient.dockerInstalled()).thenReturn(true);
    // fail all retries
    doThrow(new DockerServiceUnavailableException("Service Unavailable")).when(dockerClient).pullImage(image);
    DockerImageDownloader downloader = getDownloader(artifactUri);
    Throwable err = assertThrows(PackageDownloadException.class, () -> downloader.download());
    assertThat(err.getMessage(), containsString("Failed to download docker image"));
    assertTrue(err.getCause() instanceof DockerServiceUnavailableException);
    assertEquals("library/alpine", image.getName());
    assertEquals("sometag", image.getTag());
    assertNull(image.getDigest());
    assertFalse(image.getRegistry().isEcrRegistry());
    assertFalse(image.getRegistry().isPrivateRegistry());
    assertEquals("registry.hub.docker.com", image.getRegistry().getEndpoint());
    verify(ecrAccessor, never()).getCredentials(anyString());
    verify(dockerClient, never()).login(any());
    // Invocations as many as the retry count should be expected
    verify(dockerClient, times(2)).pullImage(any());
}
Also used : DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 4 with DockerServiceUnavailableException

use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageDownloaderTest method GIVEN_connectivity_missing_WHEN_failed_to_pull_and_connectivity_is_back_THEN_retry_and_succeed.

@Test
void GIVEN_connectivity_missing_WHEN_failed_to_pull_and_connectivity_is_back_THEN_retry_and_succeed(ExtensionContext extensionContext) throws Exception {
    ignoreExceptionOfType(extensionContext, DockerServiceUnavailableException.class);
    ignoreExceptionOfType(extensionContext, ConnectionException.class);
    URI artifactUri = new URI("docker:registry.hub.docker.com/library/alpine:sometag");
    Image image = Image.fromArtifactUri(ComponentArtifact.builder().artifactUri(artifactUri).build());
    when(mqttClient.getMqttOnline()).thenReturn(new AtomicBoolean(false));
    // Fail on first 3 attempts, succeed on fourth attempt to simulate that pull would succeed when connectivity
    // comes back
    when(dockerClient.dockerInstalled()).thenReturn(true);
    doThrow(new DockerServiceUnavailableException("Service Unavailable")).doThrow(new DockerServiceUnavailableException("Service Unavailable")).doThrow(new DockerServiceUnavailableException("Service Unavailable")).doNothing().when(dockerClient).pullImage(image);
    DockerImageDownloader downloader = getDownloader(artifactUri);
    downloader.download();
    assertEquals("library/alpine", image.getName());
    assertEquals("sometag", image.getTag());
    assertNull(image.getDigest());
    assertFalse(image.getRegistry().isEcrRegistry());
    assertFalse(image.getRegistry().isPrivateRegistry());
    assertEquals("registry.hub.docker.com", image.getRegistry().getEndpoint());
    verify(ecrAccessor, never()).getCredentials(anyString());
    verify(dockerClient, never()).login(any());
    // Invocations as many as the retry attempts should be expected
    verify(dockerClient, times(4)).pullImage(any());
    verify(mqttClient, times(3)).getMqttOnline();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with DockerServiceUnavailableException

use of com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageDownloaderTest method GIVEN_connectivity_available_WHEN_failed_to_pull_image_intermittently_THEN_succeed_on_retries.

@Test
void GIVEN_connectivity_available_WHEN_failed_to_pull_image_intermittently_THEN_succeed_on_retries(ExtensionContext extensionContext) throws Exception {
    ignoreExceptionOfType(extensionContext, DockerServiceUnavailableException.class);
    URI artifactUri = new URI("docker:registry.hub.docker.com/alpine:sometag");
    Image image = Image.fromArtifactUri(ComponentArtifact.builder().artifactUri(artifactUri).build());
    when(dockerClient.dockerInstalled()).thenReturn(true);
    // fail first attempt, succeed on next retry attempt, device is connected the whole time but there could be
    // temporary issues with docker cloud which can get resolved in a short span
    doThrow(new DockerServiceUnavailableException("Service Unavailable")).doNothing().when(dockerClient).pullImage(image);
    DockerImageDownloader downloader = getDownloader(artifactUri);
    downloader.download();
    assertEquals("alpine", image.getName());
    assertEquals("sometag", image.getTag());
    assertNull(image.getDigest());
    assertFalse(image.getRegistry().isEcrRegistry());
    assertFalse(image.getRegistry().isPrivateRegistry());
    assertEquals("registry.hub.docker.com", image.getRegistry().getEndpoint());
    verify(ecrAccessor, never()).getCredentials(anyString());
    verify(dockerClient, never()).login(any());
    // Invocations as many as the retry count should be expected
    verify(dockerClient, times(2)).pullImage(any());
    // Connectivity will be checked on failures which is only once for this test
    verify(mqttClient, times(1)).getMqttOnline();
}
Also used : DockerServiceUnavailableException(com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

DockerServiceUnavailableException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerServiceUnavailableException)5 URI (java.net.URI)4 Test (org.junit.jupiter.api.Test)4 DockerLoginException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.DockerLoginException)1 UserNotAuthorizedForDockerException (com.aws.greengrass.componentmanager.plugins.docker.exceptions.UserNotAuthorizedForDockerException)1 Platform (com.aws.greengrass.util.platforms.Platform)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1