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 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; } }