public void scanTask(String task) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanTask(task); } catch (RuntimeException e) { maybeThrow(e); } } public void scanNode(String id) {
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; } }
protected long saveTask(JsonNode it) { ObjectNode n = flattenTask(it); n.put("swarmClusterId", getSwarmClusterId().get()); checkNotEmpty(taskId, "taskId"); checkNotEmpty(serviceId, "serviceId"); removeDockerLabels("DockerTask", "taskId", taskId, n, it); });
public void scan() { WebTarget t = extractWebTarget(dockerScanner.getDockerClient()); logger.info("Scanning {}", t); JsonNode response = t.path("/info").request().buildGet().invoke(JsonNode.class); .blockingFirst(MissingNode.getInstance()); if (isUnixDomainScoket(t.getUri().toString())) { if (!isUnixDomainScoket(swarmNode.path("managerApiUrl").asText())) { try { earliestTimestamp .set(Math.min(earliestTimestamp.get(), saveDockerNode(swarmClusterId, flattenSwarmNode(it)))); } catch (RuntimeException e) { logger.warn("problem", e); scanServicesForSwarm(swarmClusterId); scanTasksForSwarm(swarmClusterId);
public void scan() { WebTarget wt = SwarmScanner.extractWebTarget(getDockerClient()); JsonNode n = wt.path("/info").request().buildGet().invoke(JsonNode.class); if (n.path("Swarm").path("Cluster").isContainerNode()) { logger.info("talking to a docker swarm manager: {}",wt); SwarmScanner ss = new SwarmScanner(this); ss.scan(); } else { // looks like we are talking to a docker engine. We don't support this yet because we need to figure out // how to ensure that we do not get duplicate entries from the Swarm view of the nodes and the engine's view of itself. logger.info("talking to a docker engine: {}",wt); String engineId = n.get("ID").asText(); JsonLogger.logInfo("x", n); updateEngineInfo(n); } }
public void scanServicesForSwarm(String swarmClusterId) { JsonNode response = getRestClient().getServices(); response.forEach(it -> { try { ObjectNode n = flattenService(it); n.put("swarmClusterId", swarmClusterId); dockerScanner.getNeoRxClient().execCypher( "merge (x:DockerService {serviceId:{serviceId}}) set x+={props}, x.updateTs=timestamp() return x", "serviceId", n.get("serviceId").asText(), "props", n).forEach(svc -> { removeDockerLabels("DockerService", "serviceId", n.get("serviceId").asText(), n, svc); earlistUpdate .set(Math.min(earlistUpdate.get(), svc.path("updateTs").asLong(Long.MAX_VALUE)));
+ " \"DesiredState\" : \"shutdown\"\n" + "}"; ObjectNode n = (ObjectNode) mapper.readTree(taskJson); SwarmScanner scanner = new SwarmScanner(null); scanner.swarmClusterId = "abcd123"; JsonNode flattened = scanner.flattenTask(n);
@Test public void testLabels() { // we don't want to use LocalDockerDaemonIntegrationTest because it will only work with a local docker daemon Projector p = getProjector(); SwarmScanner ss = new SwarmScanner(p.createBuilder(DockerScannerBuilder.class).build()); String id = "junit-"+System.currentTimeMillis(); JsonNode actual = p.getNeoRxClient().execCypher("merge (x:DockerService {serviceId:{id}}) set x.label_c='c', x.label_foo='foo', x.junitData=true return x","id",id).blockingFirst(); JsonNode intended = mapper.createObjectNode().put("label_a", "1").put("label_b", "2").put("chinacat","sunflower"); p.getNeoRxClient().execCypher("merge (a:DockerService {serviceId:{id}}) set a+={props} return a","id",id,"props",intended); ss.removeDockerLabels("DockerService","serviceId",id,intended, actual); JsonNode result = p.getNeoRxClient().execCypher("match (x:DockerService {serviceId:{id}}) return x","id",id).blockingFirst(); Assertions.assertThat(result.path("serviceId").asText()).isEqualTo(id); Assertions.assertThat(result.has("label_c")).isFalse(); Assertions.assertThat(result.has("label_foo")).isFalse(); Assertions.assertThat(result.get("label_a").asText()).isEqualTo("1"); } }
public void scanService(String service) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanService(service); } catch (RuntimeException e) { maybeThrow(e); } } public void scan() {
public void scanNode(String id) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanNode(id); } catch (RuntimeException e) { maybeThrow(e); } } public void scanService(String service) {
long saveDockerNode(String swarmClusterId, JsonNode n) { String swarmNodeId = n.get("swarmNodeId").asText(); AtomicLong updateTs = new AtomicLong(Long.MAX_VALUE); dockerScanner.getNeoRxClient() .execCypher( "merge (n:DockerHost {swarmNodeId:{nodeId}}) set n+={props}, n.updateTs=timestamp() return n", "nodeId", swarmNodeId, "props", n) .forEach(actual -> { removeDockerLabels("DockerHost", "swarmNodeId", swarmNodeId, n, actual); updateTs.set(Math.min(updateTs.get(), actual.path("updateTs").asLong(Long.MAX_VALUE))); }); logger.info("connecting swarm={} to node={}", swarmClusterId, swarmNodeId); dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{swarmClusterId}}), (n:DockerHost {swarmNodeId:{nodeId}}) merge (s)-[x:CONTAINS]->(n) set x.updateTs=timestamp()", "swarmClusterId", swarmClusterId, "nodeId", swarmNodeId); return updateTs.get(); }
public static DockerRestClient forDockerClient(DockerClient client) { DockerRestClient x = new DockerRestClient(); x.webTarget = SwarmScanner.extractWebTarget(client); return x; }