JsonNode flattenSwarmNode(JsonNode n) { ObjectNode out = mapper.createObjectNode(); out.put("swarmNodeId", n.path("ID").asText()); out.put("swarmClusterId", getSwarmClusterId().get()); out.put("role", n.path("Spec").path("Role").asText()); out.put("availability", n.path("Spec").path("Availability").asText()); out.put("hostname", n.path("Description").path("Hostname").asText()); out.put("engineVersion", n.path("Description").path("Engine").path("EngineVersion").asText()); out.put("state", n.path("Status").path("State").asText()); out.put("addr", n.path("Status").path("Addr").asText()); out.put("leader", n.path("ManagerStatus").path("Leader").asBoolean()); return out; }
protected ObjectNode flattenTask(JsonNode n) { ObjectNode out = new ObjectMapper().createObjectNode(); out.put("swarmClusterId", getSwarmClusterId().get()); out.put("taskId", n.path("ID").asText()); out.put("versionIndex", n.path("Version").path("Index").asText()); out.put("createdAt", n.path("CreatedAt").asText()); out.put("updatedAt", n.path("UpdatedAt").asText()); n.path("Labels").fields().forEachRemaining(it -> { out.put(formatLabelKey(it.getKey()), it.getValue().asText()); }); n.path("Spec").path("ContainerSepc"); out.put("serviceId", n.path("ServiceID").asText()); out.put("swarmNodeId", n.path("NodeID").asText()); out.put("state", n.path("Status").path("State").asText()); out.put("message", n.path("Status").path("Message").asText()); out.put("containerId", n.path("Status").path("ContainerStatus").path("ContainerID").asText()); out.put("desiredState", n.path("DesiredState").asText()); n.path("Status").path("PortStatus").path("Ports").forEach(it -> { String mode = it.path("PublishMode").asText(); String protocol = it.path("Protocol").asText(); if (mode.equals("host") && protocol.equals("tcp")) { int targetPort = it.path("TargetPort").asInt(); int publishedPort = it.path("PublishedPort").asInt(); String key = String.format("hostTcpPortMap_%d", targetPort); out.put(key, publishedPort); } }); ; return out; }
public void deleteTask(String id) { Optional<String> scid = getSwarmClusterId(); if (scid.isPresent()) { dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{swarmClusterId}})--(x:DockerTask {taskId:{taskId}}) detach delete x", "taskId", id, "swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "match (x:DockerTask {taskId:{taskId}, swarmClusterId:{swarmClusterId}}) detach delete x", "taskId", id, "swarmClusterId", scid.get()); } return; }
public void deleteNode(String id) { Optional<String> scid = getSwarmClusterId(); if (scid.isPresent()) { dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{swarmClusterId}})--(x:DockerHost {swarmNodeId:{swarmNodeId}}) detach delete x", "swarmNodeId", id, "swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "match (x:DockerHost {swarmNodeId:{swarmNodeId}, swarmClusterId:{swarmClusterId}}) detach delete x", "swarmNodeId", id, "swarmClusterId", scid.get()); } return; }
public void deleteService(String id) { Optional<String> scid = getSwarmClusterId(); if (scid.isPresent()) { logger.info("deleting neo4j DockerService id/name={}", id); dockerScanner.getNeoRxClient().execCypher( "match (x:DockerService {name:{name},swarmClusterId:{swarmClusterId}}) detach delete x", "name", id, "swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "match (x:DockerService {serviceId:{serviceId},swarmClusterId:{swarmClusterId}}) detach delete x", "serviceId", id, "swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{swarmClusterId}})--(x:DockerService {serviceId:{serviceId}}) detach delete x", "serviceId", id, "swarmClusterId", scid.get()); dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{swarmClusterId}})--(x:DockerService {name:{name}}) detach delete x", "name", id, "swarmClusterId", scid.get()); } }
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; } }
n.put("swarmClusterId", getSwarmClusterId().get());