public JsonNode getNodes() { return get("/nodes"); }
public Optional<String> getSwarmClusterId() { return Optional.ofNullable(getInfo().path("Swarm").path("Cluster").path("ID").asText(null)); } }
public DockerRestClient getRestClient() { return DockerRestClient.forDockerClient(getDockerClient()); } }
public static DockerRestClient forDockerClient(DockerClient client) { DockerRestClient x = new DockerRestClient(); x.webTarget = SwarmScanner.extractWebTarget(client); return x; }
lastEventNano.set(Math.max(lastEventNano.get(), nanoTime)); String swarmClusterId = getRestClient().getSwarmClusterId().get();
public void scanTasksForSwarm(String swarmClusterId) { logger.info("scanning tasks for swarm: {}", swarmClusterId); AtomicLong earlistUpdate = new AtomicLong(Long.MAX_VALUE); AtomicBoolean error = new AtomicBoolean(false); JsonNode response = getRestClient().getTasks(); response.forEach(it -> { try { earlistUpdate.set(Math.min(earlistUpdate.get(), saveTask(it))); } catch (Exception e) { logger.warn("problem updating task", e); error.set(true); } }); if (error.get() == false) { if (earlistUpdate.get() < System.currentTimeMillis()) { dockerScanner.getNeoRxClient().execCypher( "match (x:DockerTask) where x.swarmClusterId={swarmClusterId} and x.updateTs<{cutoff} detach delete x", "cutoff", earlistUpdate.get(), "swarmClusterId", swarmClusterId); } } }
public void scanServicesForSwarm(String swarmClusterId) { JsonNode response = getRestClient().getServices();
public void scanNode(String nodeId) { try { JsonNode task = getRestClient().getNode(nodeId); saveTask(task); } catch (RuntimeException e) { if (isNotFound(e)) { deleteNode(nodeId); } else { throw e; } } }
public void scanTask(String taskId) { try { JsonNode task = getRestClient().getTask(taskId); saveTask(task); } catch (RuntimeException e) { if (isNotFound(e)) { deleteTask(taskId); } else { throw e; } } }
public void scanService(String id) { try { Optional<String> scid = getSwarmClusterId(); if (scid.isPresent()) { logger.info("performing targeted scan of service={}", id); JsonNode it = getRestClient().getService(id); ObjectNode n = flattenService(it); n.put("swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "merge (x:DockerService {serviceId:{serviceId}}) set x+={props}, x.updateTs=timestamp() return x", "serviceId", n.get("serviceId").asText(), "props", n).forEach(actual -> { try { removeDockerLabels("DockerService", "serviceId", n.get("serviceId").asText(), n, actual); } catch (Exception e) { logger.warn("problem removing labels: " + e.toString()); } }); dockerScanner.getNeoRxClient().execCypher( "match (swarm:DockerSwarm {swarmClusterId:{swarmClusterId}}),(service:DockerService{serviceId:{serviceId}}) merge (swarm)-[x:CONTAINS]->(service) set x.updateTs=timestamp()", "swarmClusterId", scid.get(), "serviceId", n.path("serviceId").asText()); } } catch (RuntimeException e) { if (isNotFound(e)) { deleteService(id); return; } throw e; } }
public Optional<String> getSwarmClusterId() { if (swarmClusterId != null) { return Optional.of(swarmClusterId); } swarmClusterId = getRestClient().getInfo().path("Swarm").path("Cluster").path("ID").asText(null); return Optional.ofNullable(swarmClusterId); }
public DockerRestClient getRestClient() { return DockerRestClient.forDockerClient(dockerScanner.getDockerClient()); }
public JsonNode getNode(String id) { return get("/nodes", id); }
@Override public String get() { return getRestClient().getInfo().path("Swarm").path("Cluster").path("ID").asText(null); }
public JsonNode getContainers() { return get("/containers"); }
public JsonNode getTasks() { return get("/tasks"); }
public JsonNode getServices() { return get("/services"); }
public JsonNode getInfo() { return get("/info"); }
public JsonNode getContainer(String c) { return get("/containers", c); }
public JsonNode getTask(String task) { return get("/tasks", task); }