List of usage examples for io.vertx.core.buffer Buffer toJsonObject
JsonObject toJsonObject();
From source file:com.hpe.sw.cms.verticle.ScanVerticle.java
License:Apache License
@Override public void start() throws Exception { super.start(); httpClient = vertx.createHttpClient(); final String dricHost = config().getString("dric.host"); final String swarmProtocol = config().getString("swarm.protocol"); final String swarmHost = config().getString("swarm.host"); final String swarmImage = config().getString("swarm.scan.image"); final int scanMax = config().getInteger("scanner.max"); getVertx().setPeriodic(config().getLong("scan.interval", INTERVAL), h -> { httpClient.getAbs(swarmProtocol + swarmHost + "/containers/json", new Handler<HttpClientResponse>() { @Override/* ww w .j a v a 2s . c o m*/ public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer res) { JsonArray containers = res.toJsonArray(); int currentRunning = 0; for (Object obj : containers) { JsonObject container = (JsonObject) obj; if (swarmImage.equals(container.getString("Image"))) { currentRunning++; } } int fetchSize = scanMax - currentRunning; if (fetchSize > 0) { getVertx().eventBus().send(Events.IMAGES_UPDATED.name(), fetchSize, event -> { Message msg = event.result(); if (msg != null) { JsonArray updates = (JsonArray) msg.body(); for (Object obj : updates) { try { JsonObject image = (JsonObject) obj; processImage(dricHost, image.getString("host"), image.getString("name"), image.getString("tag")); } catch (Exception e) { LOG.error("image sent to Scan error", e); } } } }); } } }); } }).end(); }); //delete exited containers of scan. getVertx().setPeriodic(config().getLong("scan.interval", INTERVAL), h -> { Set containerIds = getVertx().sharedData().getLocalMap("scanContainerIds").keySet(); containerIds.forEach(containerId -> { httpClient.getAbs(swarmProtocol + swarmHost + "/containers/" + containerId + "/json", new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer event) { if ("exited".equals( event.toJsonObject().getJsonObject("State").getString("Status"))) { String containerId = event.toJsonObject().getString("Id"); httpClient .deleteAbs("http://" + swarmHost + "/containers/" + containerId, new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse event) { LOG.info("delete container with response code :" + event.statusCode()); getVertx().sharedData() .getLocalMap("scanContainerIds") .remove(containerId); } }) .end(); } } }); } }).end(); }); }); }
From source file:com.hpe.sw.cms.verticle.ScanVerticle.java
License:Apache License
private void processImage(String dricHost, String reigstryHost, String image, String tag) throws Exception { final String swarmProtocol = config().getString("swarm.protocol"); final String swarmHost = config().getString("swarm.host"); JsonObject hostConfig = new JsonObject(); hostConfig.put("Privileged", true); JsonObject restartPolicy = new JsonObject(); restartPolicy.put("Name", "no"); JsonObject createContinaerJson = new JsonObject(); createContinaerJson.put("AttachStdin", true); createContinaerJson.put("AttachStdout", true); createContinaerJson.put("AttachStderr", true); createContinaerJson.put("Tty", true); createContinaerJson.put("OpenStdin", true); createContinaerJson.put("StdinOnce", true); JsonArray cmds = new JsonArray(); cmds.add("-l=file").add(dricHost).add(reigstryHost).add(image).add(tag); createContinaerJson.put("Cmd", cmds); createContinaerJson.put("Image", config().getString("swarm.scan.image")); createContinaerJson.put("StopSignal", "SIGTERM"); createContinaerJson.put("", true); hostConfig.put("RestartPolicy", restartPolicy); createContinaerJson.put("HostConfig", hostConfig); httpClient.postAbs(swarmProtocol + swarmHost + "/containers/create", new Handler<HttpClientResponse>() { @Override//from w w w .j a va 2s . c om public void handle(HttpClientResponse event) { event.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { JsonObject retVal = buffer.toJsonObject(); String containerId = retVal.getString("Id"); getVertx().sharedData().getLocalMap("scanContainerIds").put(containerId, containerId); httpClient.postAbs(swarmProtocol + swarmHost + "/containers/" + containerId + "/start", new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse event) { LOG.info("start container with response code :" + event.statusCode()); } }).end(); } }); } }).putHeader("content-type", "application/json") .putHeader("content-length", String.valueOf(createContinaerJson.toString().length())) .write(createContinaerJson.toString()).end(); }
From source file:com.hpe.sw.cms.verticle.WatcherVerticle.java
License:Apache License
@Override public void start() throws Exception { httpClient = vertx.createHttpClient(); getVertx().setPeriodic(config().getLong("registry.interval", INTERVAL), this::handler); vertx.eventBus().consumer(Events.EVENT_IMAGES_UPDATED.name(), msg -> { JsonArray images = (JsonArray) msg.body(); if (images.size() > 0) { for (Object image : images) { JsonObject imageObj = (JsonObject) image; try { String url = imageObj.getString("eventUrl"); httpClient.getAbs(url, new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { JsonObject maniFestLib = buffer.toJsonObject(); String tag = maniFestLib.getString("tag"); LOG.debug("populateTagToImage " + tag); imageObj.put(Image.TAG, tag); populateAndSendImage(imageObj); }/*www. j av a 2 s. c o m*/ }); } }).end(); } catch (Exception e) { LOG.error("error in populateTagToImage", e); } } } }); }
From source file:com.hpe.sw.cms.verticle.WatcherVerticle.java
License:Apache License
private void handler(Long h) { try {//from www .j a v a 2s.c o m String protocol = config().getString("registry.protocol"); String host = config().getString("registry.host"); httpClient.getAbs(protocol + host + "/v2/_catalog", new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { JsonObject repositoryLib = buffer.toJsonObject(); JsonArray repos = repositoryLib.getJsonArray("repositories"); repos.forEach(repo -> { if (repo != null && !((String) repo).trim().equals("")) { try { repo = ((String) repo).trim(); String[] imagePart = ((String) repo).split("/"); String imageName = String.join("/", imagePart); httpClient.getAbs(protocol + host + "/v2/" + imageName + "/tags/list", new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { JsonObject tagLib = buffer.toJsonObject(); JsonArray tags = tagLib.getJsonArray("tags"); for (Object tag : tags) { if (tag != null && !((String) tag).trim().equals("")) { JsonObject imageObj = new JsonObject(); imageObj.put("name", imageName); imageObj.put("tag", tag); String dest = host; imageObj.put("host", dest); populateAndSendImage(imageObj); } } } }); } }).end(); } catch (Exception e) { LOG.error("error in reading registry", e); } } }); } }); } }).end(); } catch (Exception e) { LOG.error("error in registry handler", e); } }
From source file:com.hpe.sw.cms.verticle.WatcherVerticle.java
License:Apache License
private void populateAndSendImage(JsonObject imageObj) { try {/* w ww. j a va2 s. c om*/ String protocol = config().getString("registry.protocol"); String host = config().getString("registry.host"); httpClient.getAbs(protocol + host + "/v2/" + imageObj.getString("name") + "/manifests/" + imageObj.getString("tag"), new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse httpClientResponse) { httpClientResponse.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { JsonObject maniFestLib = buffer.toJsonObject(); JsonArray signs = maniFestLib.getJsonArray("signatures"); if (signs != null && signs.size() > 0) { StringBuffer fullSign = new StringBuffer(); for (Object sign : signs.getList()) { fullSign.append(((Map) sign).get("signature")).append("|"); } imageObj.put(Image.SIGN, fullSign); imageObj.put(Image.IS_SCANNED, false); imageObj.put(Image.IS_ENRICHED, false); imageObj.put(Image.IS_SCANNED_FAILED, false); if (imageObj.getLong(Image.TIMESTAMP) == null) { imageObj.put(Image.TIMESTAMP, new Date().getTime()); } vertx.eventBus().publish(Events.NEW_IMAGE.name(), imageObj); LOG.info("Event Image with populateSignToImage", imageObj); } } }); } }).end(); } catch (Exception e) { LOG.error("error in populateSignToImage", e); } }