Search in sources :

Example 1 with DockerConnectionException

use of com.epam.pipeline.exception.docker.DockerConnectionException in project cloud-pipeline by epam.

the class DockerClient method getImageTags.

public List<String> getImageTags(String registryPath, String image) {
    String url = String.format(TAGS_LIST, registryPath, image);
    try {
        URI uri = new URI(url);
        HttpEntity entity = getAuthHeaders();
        ResponseEntity<TagsListing> response = getRestTemplate().exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<TagsListing>() {
        });
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody().getTags();
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (URISyntaxException | HttpClientErrorException | UnexpectedResponseStatusException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DockerConnectionException(url, e.getMessage());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) TagsListing(com.epam.pipeline.entity.docker.TagsListing) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 2 with DockerConnectionException

use of com.epam.pipeline.exception.docker.DockerConnectionException in project cloud-pipeline by epam.

the class DockerClient method executeDeletion.

private void executeDeletion(String url, String image) {
    try {
        URI uri = new URI(url);
        HttpStatus status = getRestTemplate().execute(uri, HttpMethod.DELETE, request -> request.getHeaders().putAll(getAuthHeaders().getHeaders()), ClientHttpResponse::getStatusCode);
        if (status != HttpStatus.ACCEPTED) {
            throw new UnexpectedResponseStatusException(status);
        }
    } catch (URISyntaxException | UnexpectedResponseStatusException e) {
        throw new DockerConnectionException(url, e.getMessage());
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
            LOGGER.error("Image not found:" + image);
            return;
        }
        throw new DockerConnectionException(url, e.getMessage());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpStatus(org.springframework.http.HttpStatus) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 3 with DockerConnectionException

use of com.epam.pipeline.exception.docker.DockerConnectionException in project cloud-pipeline by epam.

the class DockerClient method getRawImageDescription.

private RawImageDescription getRawImageDescription(DockerRegistry registry, String imageName, String tag, HttpEntity headers) {
    String url = String.format(IMAGE_DESCRIPTION_URL, registry.getPath(), imageName, tag);
    try {
        URI uri = new URI(url);
        ResponseEntity<RawImageDescription> response = getRestTemplate().exchange(uri, HttpMethod.GET, headers, new ParameterizedTypeReference<RawImageDescription>() {
        });
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (URISyntaxException | UnexpectedResponseStatusException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DockerConnectionException(url, e.getMessage());
    }
}
Also used : RawImageDescription(com.epam.pipeline.entity.docker.RawImageDescription) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 4 with DockerConnectionException

use of com.epam.pipeline.exception.docker.DockerConnectionException in project cloud-pipeline by epam.

the class DockerClient method getManifest.

/**
 * Gets a V2 Manifest for a specified image and tag
 * @param registry a registry, where image is located
 * @param imageName a name of an image (repository)
 * @param tag tag name
 * @return image's manifest
 */
public Optional<ManifestV2> getManifest(DockerRegistry registry, String imageName, String tag) {
    String url = String.format(IMAGE_DESCRIPTION_URL, registry.getPath(), imageName, tag);
    try {
        URI uri = new URI(url);
        ResponseEntity<ManifestV2> response = getRestTemplate().exchange(uri, HttpMethod.GET, getV2AuthHeaders(), new ParameterizedTypeReference<ManifestV2>() {
        });
        if (response.getStatusCode() == HttpStatus.OK) {
            List<String> digest = response.getHeaders().get("docker-content-digest");
            ManifestV2 manifest = response.getBody();
            manifest.setDigest(digest.get(0));
            return Optional.of(manifest);
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (URISyntaxException | UnexpectedResponseStatusException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DockerConnectionException(url, e.getMessage());
    } catch (HttpClientErrorException e) {
        LOGGER.error(e.getMessage(), e);
        return Optional.empty();
    }
}
Also used : ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 5 with DockerConnectionException

use of com.epam.pipeline.exception.docker.DockerConnectionException in project cloud-pipeline by epam.

the class DockerClient method checkAvailability.

public void checkAvailability() {
    HttpEntity entity = getAuthHeaders();
    String uri = String.format(HEALTH_ENTRY_POINT, hostName);
    try {
        getRestTemplate().exchange(uri, HttpMethod.GET, entity, String.class);
    } catch (HTTPException | ResourceAccessException e) {
        if (e.getCause() instanceof SSLHandshakeException) {
            throw new DockerCertificateException(hostName, e.getCause());
        } else {
            throw new DockerConnectionException(hostName, e.getMessage(), e);
        }
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) {
            throw new DockerCredentialsException(hostName, userName, password, e);
        } else {
            throw new DockerConnectionException(hostName, e.getMessage(), e);
        }
    }
}
Also used : HTTPException(javax.xml.ws.http.HTTPException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) DockerCredentialsException(com.epam.pipeline.exception.docker.DockerCredentialsException) HttpEntity(org.springframework.http.HttpEntity) DockerCertificateException(com.epam.pipeline.exception.docker.DockerCertificateException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Aggregations

DockerConnectionException (com.epam.pipeline.exception.docker.DockerConnectionException)6 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)4 HttpEntity (org.springframework.http.HttpEntity)2 ManifestV2 (com.epam.pipeline.entity.docker.ManifestV2)1 RawImageDescription (com.epam.pipeline.entity.docker.RawImageDescription)1 TagsListing (com.epam.pipeline.entity.docker.TagsListing)1 DockerRegistry (com.epam.pipeline.entity.pipeline.DockerRegistry)1 ToolGroup (com.epam.pipeline.entity.pipeline.ToolGroup)1 DockerCertificateException (com.epam.pipeline.exception.docker.DockerCertificateException)1 DockerCredentialsException (com.epam.pipeline.exception.docker.DockerCredentialsException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 HTTPException (javax.xml.ws.http.HTTPException)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 Transactional (org.springframework.transaction.annotation.Transactional)1 ResourceAccessException (org.springframework.web.client.ResourceAccessException)1