List of usage examples for com.fasterxml.jackson.databind MappingIterator close
@Override public void close() throws IOException
From source file:org.hexlogic.model.DockerNode.java
@VsoMethod(showInApi = true, name = "pullImage", description = "Pull the image matching the given string from the docker hub repository, saving it on the docker host.") public String pullImage(String imageName) throws Exception { log.debug("Pulling image '" + imageName + "'..."); @SuppressWarnings("rawtypes") MappingIterator<Map> it = null; try {//from w w w .j av a2s .c om configureNode(); DockerClient dockerClient = DockerClientBuilder.getInstance(config).build(); log.debug("Starting pull operation..."); /* * We will check the final result by comparing the initial image id, which is the first ID provided by the stream such as: * * {status=Pulling image (latest) from dockerfile/nodejs, progressDetail={}, id=406eb4a4dcad} * * to the image id of the last entity which owns id AND status which will look something like: * {status=Download complete, progressDetail={}, id=406eb4a4dcad} * * If both IDs match, we know that the latest layer is the same as the requested image layer. * So the next step is to compare the download status of that layer */ String firstId = null; String lastId = ""; String lastStatus = "undefined"; /* * In addition to the download status of the layer, we provide additional information about how the process went by * returning information to the user using the last entity which has no id and only a status, which looks like this: * {status=Status: Image is up to date for dockerfile/nodejs} * or * {status=Status: Downloaded newer image for dockerfile/nodejs} * or * {status=Repository dockerfile/nodejs already being pulled by another client. Waiting.} */ String finalStatus = "undefined"; for (it = new ObjectMapper().readValues( new JsonFactory().createJsonParser(dockerClient.pullImageCmd(imageName).exec()), Map.class); it .hasNext();) { Map<?, ?> element = it.next(); String id = ""; String status = ""; String progress = ""; // info OUTPUT // log.debug("info: " + element); try { id = element.get("id").toString(); } catch (NullPointerException e) {/* catch exception if key was not found */ } try { status = element.get("status").toString(); } catch (NullPointerException e) {/* catch exception if key was not found */ } try { progress = element.get("progress").toString(); } catch (NullPointerException e) {/* catch exception if key was not found */ } // if the key was found and we got some status if (!id.isEmpty() && !status.isEmpty()) { // remember the first id of the output stream, which is the id of the image we want to pull if (firstId == null) { log.debug("Remembering first id: " + id); firstId = id; } // if the same layer is returned multiple times in a row, don't log everything but just the progress if (id.equals(lastId)) { lastId = id; lastStatus = status; if (!progress.isEmpty()) { log.debug("Progress: " + progress); } } else { lastId = id; log.debug("Image '" + id + "' status is: " + status + "."); if (!progress.isEmpty()) { log.debug("Progress: " + progress); } } } if (!status.isEmpty()) { finalStatus = status; } } // TODO find a more robust way to handle downloadStatus and finalStatus String downloadStatus = "undefined"; if (lastId.equals(firstId)) { log.debug("Last download layer id does match the requested image id: " + firstId); if (StringUtils.containsIgnoreCase(lastStatus, "Download complete")) { downloadStatus = "successed"; log.debug("The requested layer was downloaded successfuly."); } else { downloadStatus = "failed"; log.error("The requested layer failed to download."); // throw exception in order for the workflow to fail throw new IllegalStateException("The requested layer failed to download."); } } // reload images from docker node this.reloadImages(); // update inventory - another way to do this would be to update our ArrayList and call notifyElementDeleted on the image object notificationHandler.notifyElementInvalidate(toRef()); log.debug("Pull operation " + downloadStatus + ". " + finalStatus + "."); return "Pull operation " + downloadStatus + ". " + finalStatus + "."; } catch (InternalServerErrorException e) { // image dosn't exist log.error("Error: the image was not found."); // Throw error detail message so vCO can display it throw new Exception("Error: the image was not found."); } catch (Exception e) { final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); e.printStackTrace(pw); log.error("Error while pulling image: " + sw.getBuffer().toString()); // Throw error detail message so vCO can display it throw new Exception("Error while pulling image: " + sw.getBuffer().toString()); } finally { if (it != null) { log.debug("Closeing pullImage stream..."); it.close(); log.debug("Closed pullImage stream."); } } }