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 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; }
@Override public SchemaManager getSchemaManager() { return new DockerSchemaManager(getProjector().getNeoRxClient()); }
public void scan() { WebTarget t = extractWebTarget(dockerScanner.getDockerClient()); logger.info("Scanning {}", t); JsonNode response = t.path("/info").request().buildGet().invoke(JsonNode.class); props.put("updatedAt", updatedAt); JsonNode swarmNode = dockerScanner.getNeoRxClient() .execCypher( "merge (c:DockerSwarm {swarmClusterId:{id}}) set c+={props},c.updateTs=timestamp() return c", dockerScanner.getNeoRxClient().execCypher("match (c:DockerSwarm {name:{name}}) return c", "name", name) .forEach(it -> { String oldSwarmClusterId = it.path("swarmClusterId").asText(); if (!swarmClusterId.equals(oldSwarmClusterId)) { dockerScanner.getNeoRxClient().execCypher( "match (c:DockerSwarm {swarmClusterId:{swarmClusterId}}) detach delete c", "swarmClusterId", oldSwarmClusterId); dockerScanner.getNeoRxClient().execCypher( "match (c:DockerSwarm {swarmClusterId:{id}}) set c.managerApiUrl={managerApiUrl},c.name={name},c.tridentClusterId={name} return c", "id", swarmClusterId, "managerApiUrl", LOCAL_DOCKER_DAEMON_SOCKET_URL, "name", name); if (earliestTimestamp.get() < System.currentTimeMillis()) { logger.info("deleting DockerHost nodes before with updateTs<{}", earliestTimestamp.get()); dockerScanner.getNeoRxClient().execCypher( "match (s:DockerSwarm {swarmClusterId:{id}})--(x:DockerHost) where s.updateTs>x.updateTs detach delete x", "id", swarmClusterId);
@Test public void testIt() { getDockerScanner().getSchemaManager().applyConstraints(); // this will not run if neo4j or docker is not available getDockerScanner().scan(); } }
public void run() { projector.createBuilder(DockerScannerBuilder.class).withLocalDockerDaemon().build().scan(); } };
public DockerRestClient getRestClient() { return DockerRestClient.forDockerClient(dockerScanner.getDockerClient()); }
public void scanService(String service) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanService(service); } catch (RuntimeException e) { maybeThrow(e); } } public void scan() {
@Test public void testNotFoundException() { try { getDockerScanner().getRestClient().get("/nodes/nodenotofound"); Assertions.failBecauseExceptionWasNotThrown(NotFoundException.class); } catch (Exception e) { Assertions.assertThat(e).isInstanceOf(NotFoundException.class); } }
@Override public DockerScanner build() { if (dockerClient==null) { DefaultDockerClientConfig cc = configBuilder.build(); this.dockerClient = DockerClientBuilder.getInstance(cc).build(); } return new DockerScanner(this); }
@Test public void testUnnownTask() { getDockerScanner().scanTask("foobar"); } @Test
@Test public void testTargetServiceScan() { getDockerScanner().scanService("foo"); }
@BeforeClass public static void setupDockerScanner() { DockerScanner ds = new Projector.Builder().build().createBuilder(DockerScannerBuilder.class).withLocalDockerDaemon().build(); boolean b = ds.getNeoRxClient().checkConnection(); if (b==false) { logger.warn("neo4j is not available...integration tests will be skipped"); Assume.assumeTrue("neo4j available", b); } else { logger.info("neo4j is available for integration tests"); } try { Info info = ds.getDockerClient().infoCmd().exec(); logger.info("local docker daemon is available for integration tests"); dockerScanner = ds; } catch (Exception e) { logger.warn("neo4j is not available for integration tests",e); Assume.assumeTrue(false); return; } } }
@Test public void testIt2() { getDockerScanner().scan(); }
public DockerRestClient getRestClient() { return DockerRestClient.forDockerClient(getDockerClient()); } }
public void scanTask(String task) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanTask(task); } catch (RuntimeException e) { maybeThrow(e); } } public void scanNode(String id) {
@Test public void test() throws Exception { DockerEventProcessor p = new DockerEventProcessor.Builder().withDockerRestClient(getDockerScanner().getRestClient()).build(); p.addConsumer(it->{ JsonLogger.logInfo("event", it); }); } }
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 scanNode(String id) { try { SwarmScanner ss = new SwarmScanner(this); ss.scanNode(id); } catch (RuntimeException e) { maybeThrow(e); } } public void scanService(String service) {
private void updateEngineInfo(JsonNode nodeInfo) { ObjectNode n = new ObjectMapper().createObjectNode(); n.set("engineId", nodeInfo.get("ID")); n.put("architecture", nodeInfo.path("Architecture").asText()); n.put("osType", nodeInfo.path("OSType").asText()); n.put("operatingSystem", nodeInfo.path("OperatingSystem").asText()); n.put("kernelVersion", nodeInfo.path("KernelVersion").asText()); n.put("serverVersion", nodeInfo.path("ServerVersion").asText()); n.put("nCPU", nodeInfo.path("NCPU").asLong()); n.put("memTotal", nodeInfo.path("MemTotal").asLong()); n.path("Labels").fields().forEachRemaining(it -> { String labelKey = "label_" + it.getKey(); n.set(labelKey, it.getValue()); }); getNeoRxClient().execCypher("merge (h:DockerHost {engineId:{engineId}}) set h+={props}, h.updateTs=timestamp()","engineId",n.path("engineId").asText(),"props",n); } @Override