void handleError(@Nonnull String error) throws DockerException { log.error(error); throw new DockerException(error); }
/** * URL-encodes a string when used as a URL query parameter's value. * * @param unencoded A string that may contain characters not allowed in URL query parameters. * @return URL-encoded String * @throws DockerException if there's an UnsupportedEncodingException */ private String urlEncode(final String unencoded) throws DockerException { try { final String encode = URLEncoder.encode(unencoded, UTF_8.name()); return encode.replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { throw new DockerException(e); } }
@Override public void progress(ProgressMessage message) throws DockerException { if (message.error() != null) { throw new DockerException(message.toString()); } log.info("build: {}", message); }
@Override public void progress(ProgressMessage message) throws DockerException { if (message.error() != null) { throw new DockerException(message.toString()); } log.info("load: {}", message); }
private String authHeader(final RegistryAuth registryAuth) throws DockerException { // the docker daemon requires that the X-Registry-Auth header is specified // with a non-empty string even if your registry doesn't use authentication if (registryAuth == null) { return "null"; } try { return Base64.encodeBase64String(ObjectMapperProvider .objectMapper() .writeValueAsBytes(registryAuth)); } catch (JsonProcessingException ex) { throw new DockerException("Could not encode X-Registry-Auth header", ex); } }
@Override public RegistryAuth authFor(final String imageName) throws DockerException { final String[] imageParts = imageName.split("/", 2); if (imageParts.length < 2 || !GCR_REGISTRIES.contains(imageParts[0])) { // not an image on GCR return null; } final AccessToken accessToken; try { accessToken = getAccessToken(); } catch (IOException e) { throw new DockerException(e); } return authForAccessToken(accessToken); }
@Override public void leaveSwarm(final boolean force) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("swarm").path("leave").queryParam("force", force); request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 500: throw new DockerException("server error", e); case 503: throw new DockerException("node is not part of a swarm", e); default: throw e; } } }
public ProgressMessage nextMessage(final String method, final URI uri) throws DockerException { try { return iterator.nextValue(); } catch (SocketTimeoutException e) { throw new DockerTimeoutException(method, uri, e); } catch (IOException e) { throw new DockerException(e); } }
@Override public void joinSwarm(final SwarmJoin swarmJoin) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("swarm").path("join"); request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(swarmJoin)); } catch (DockerRequestException e) { switch (e.status()) { case 400: throw new DockerException("bad parameter", e); case 500: throw new DockerException("server error", e); case 503: throw new DockerException("node is already part of a swarm", e); default: throw e; } } }
public boolean hasNextMessage(final String method, final URI uri) throws DockerException { try { return iterator.hasNextValue(); } catch (SocketTimeoutException e) { throw new DockerTimeoutException(method, uri, e); } catch (IOException e) { throw new DockerException(e); } }
@Override public RegistryConfigs authForBuild() throws DockerException { if (!configFileExists()) { return null; } try { return reader.authForAllRegistries(path); } catch (IOException e) { throw new DockerException(e); } } }
@Override public UnlockKey unlockKey() throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("swarm").path("unlockkey"); return request(GET, UnlockKey.class, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 500: throw new DockerException("server error", e); case 503: throw new DockerException("node is not part of a swarm", e); default: throw e; } } }
@Override public String initSwarm(final SwarmInit swarmInit) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("swarm").path("init"); return request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(swarmInit)); } catch (DockerRequestException e) { switch (e.status()) { case 400: throw new DockerException("bad parameter", e); case 500: throw new DockerException("server error", e); case 503: throw new DockerException("node is already part of a swarm", e); default: throw e; } } }
/** * Takes a map of filters and URL-encodes them. If the map is empty or an exception occurs, return * null. * * @param filters A map of filters. * @return String * @throws DockerException if there's an IOException */ private String urlEncodeFilters(final Map<String, List<String>> filters) throws DockerException { try { final String unencodedFilters = objectMapper().writeValueAsString(filters); if (!unencodedFilters.isEmpty()) { return urlEncode(unencodedFilters); } } catch (IOException e) { throw new DockerException(e); } return null; }
@Override public void unlock(final UnlockKey unlockKey) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); try { final WebTarget resource = resource().path("swarm").path("unlock"); request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(unlockKey)); } catch (DockerRequestException e) { switch (e.status()) { case 500: throw new DockerException("server error", e); case 503: throw new DockerException("node is not part of a swarm", e); default: throw e; } } }
@Override public ContainerCreation createContainer(final ContainerConfig config, final String name) throws DockerException, InterruptedException { WebTarget resource = resource() .path("containers").path("create"); if (name != null) { checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(), "Invalid container name: \"%s\"", name); resource = resource.queryParam("name", name); } log.debug("Creating container with ContainerConfig: {}", config); try { return request(POST, ContainerCreation.class, resource, resource .request(APPLICATION_JSON_TYPE), Entity.json(config)); } catch (DockerRequestException e) { switch (e.status()) { case 404: throw new ImageNotFoundException(config.image(), e); case 406: throw new DockerException("Impossible to attach. Container not running.", e); default: throw e; } } }
@Override public Set<String> load(final InputStream imagePayload, final ProgressHandler handler) throws DockerException, InterruptedException { final WebTarget resource = resource() .path("images") .path("load") .queryParam("quiet", "false"); final LoadProgressHandler loadProgressHandler = new LoadProgressHandler(handler); final Entity<InputStream> entity = Entity.entity(imagePayload, APPLICATION_OCTET_STREAM); try (final ProgressStream load = request(POST, ProgressStream.class, resource, resource.request(APPLICATION_JSON_TYPE), entity)) { load.tail(loadProgressHandler, POST, resource.getUri()); return loadProgressHandler.getImageNames(); } catch (IOException e) { throw new DockerException(e); } finally { IOUtils.closeQuietly(imagePayload); } }
@Override public EventStream events(EventsParam... params) throws DockerException, InterruptedException { WebTarget resource = noTimeoutResource().path("events"); resource = addParameters(resource, params); try { final CloseableHttpClient client = (CloseableHttpClient) ApacheConnectorProvider .getHttpClient(noTimeoutClient); final CloseableHttpResponse response = client.execute(new HttpGet(resource.getUri())); return new EventStream(response, objectMapper()); } catch (IOException exception) { throw new DockerException(exception); } }
@Override public ServiceCreateResponse createService(final ServiceSpec spec, final RegistryAuth config) throws DockerException, InterruptedException { assertApiVersionIsAbove("1.24"); final WebTarget resource = resource().path("services").path("create"); try { return request(POST, ServiceCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE) .header("X-Registry-Auth", authHeader(config)), Entity.json(spec)); } catch (DockerRequestException e) { switch (e.status()) { case 406: throw new DockerException("Server error or node is not part of swarm.", e); case 409: throw new DockerException("Name conflicts with an existing object.", e); default: throw e; } } }
private RegistryAuth createRegistryAuth(Server server) throws DockerException { SettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(server); SettingsDecryptionResult decryptionResult = settingsDecrypter.decrypt(decryptionRequest); if (decryptionResult.getProblems().isEmpty()) { log.debug("Successfully decrypted Maven server password"); } else { for (SettingsProblem problem : decryptionResult.getProblems()) { log.error("Settings problem for server {}: {}", server.getId(), problem); } throw new DockerException("Failed to decrypt Maven server password"); } return RegistryAuth.builder() .username(server.getUsername()) .password(decryptionResult.getServer().getPassword()) .build(); } }