/** * Create a {@link DockerHost} from DOCKER_HOST and DOCKER_PORT env vars. * * @return The DockerHost object. */ public static DockerHost fromEnv() { final String host = endpointFromEnv(); final String certPath = certPathFromEnv(); return new DockerHost(host, certPath); }
static String endpointFromEnv() { return firstNonNull(systemDelegate.getenv("DOCKER_HOST"), defaultDockerEndpoint()); }
protected DockerClient getNewDockerClient() throws Exception { if (isNullOrEmpty(DOCKER_HOST.dockerCertPath())) { return new DefaultDockerClient(DOCKER_HOST.uri()); } else { final Path dockerCertPath = java.nio.file.Paths.get(DOCKER_HOST.dockerCertPath()); return new DefaultDockerClient(DOCKER_HOST.uri(), new DockerCertificates(dockerCertPath)); } }
final String endpoint = DockerHost.endpointFromEnv(); final Path dockerCertPath = Paths.get(firstNonNull(DockerHost.certPathFromEnv(), DockerHost.defaultCertPath())); final String scheme = certs.isPresent() ? "https" : "http"; final int port = hostAndPort.getPortOrDefault(DockerHost.defaultPort()); final String address = isNullOrEmpty(hostText) ? DockerHost.defaultAddress() : hostText;
.setDefault(DockerHost.fromEnv().host()) .help("docker endpoint"); .setDefault(DockerHost.fromEnv().dockerCertPath()) .help("directory containing client.pem and client.key for connecting to Docker over HTTPS");
static int portFromEnv() { final String port = systemDelegate.getenv("DOCKER_PORT"); if (port == null) { return defaultPort(); } try { return Integer.parseInt(port); } catch (NumberFormatException e) { return defaultPort(); } }
private String dockerHost() { final String host = dockerHost.host(); if (host.startsWith("unix://")) { return host; } else { return "tcp://" + host; } }
/** * Create a {@link DockerHost} from an explicit address or uri. * * @param endpoint The Docker endpoint. * @param certPath The certificate path. * @return The DockerHost object. */ public static DockerHost from(final String endpoint, final String certPath) { return new DockerHost(endpoint, certPath); }
@Override protected void runOneIteration() throws InterruptedException { final String hostname = exec("uname -n").trim(); final String uname = exec("uname -a").trim(); final HostInfo hostInfo = HostInfo.newBuilder() .setArchitecture(operatingSystemMxBean.getArch()) .setCpus(Runtime.getRuntime().availableProcessors()) .setHostname(hostname) .setLoadAvg(operatingSystemMxBean.getSystemLoadAverage()) .setOsName(operatingSystemMxBean.getName()) .setOsVersion(operatingSystemMxBean.getVersion()) .setMemoryFreeBytes(operatingSystemMxBean.getFreePhysicalMemorySize()) .setMemoryTotalBytes(operatingSystemMxBean.getTotalPhysicalMemorySize()) .setSwapFreeBytes(operatingSystemMxBean.getFreeSwapSpaceSize()) .setSwapTotalBytes(operatingSystemMxBean.getTotalSwapSpaceSize()) .setUname(uname) .setDockerVersion(dockerVersion()) .setDockerHost(dockerHost()) .setDockerCertPath(dockerHost.dockerCertPath()) .build(); nodeUpdater.update(hostInfo.toJsonBytes()); }
/** * Create a new {@link DefaultDockerClient} builder prepopulated with values loaded from the * DOCKER_HOST and DOCKER_CERT_PATH environment variables. * * @return Returns a builder that can be used to further customize and then build the client. * @throws DockerCertificateException if we could not build a DockerCertificates object */ public static Builder fromEnv() throws DockerCertificateException { final String endpoint = DockerHost.endpointFromEnv(); final Path dockerCertPath = Paths.get(firstNonNull(DockerHost.certPathFromEnv(), DockerHost.defaultCertPath())); final Builder builder = new Builder(); final Optional<DockerCertificatesStore> certs = DockerCertificates.builder() .dockerCertPath(dockerCertPath).build(); if (endpoint.startsWith(UNIX_SCHEME + "://")) { builder.uri(endpoint); } else { final String stripped = endpoint.replaceAll(".*://", ""); final HostAndPort hostAndPort = HostAndPort.fromString(stripped); final String hostText = hostAndPort.getHost(); final String scheme = certs.isPresent() ? "https" : "http"; final int port = hostAndPort.getPortOrDefault(DockerHost.defaultPort()); final String address = isNullOrEmpty(hostText) ? DockerHost.defaultAddress() : hostText; builder.uri(scheme + "://" + address + ":" + port); } if (certs.isPresent()) { builder.dockerCertificates(certs.get()); } return builder; }
private DockerClient createDockerClient(final AgentConfig config) throws IOException { final DefaultDockerClient.Builder builder = DefaultDockerClient.builder() .uri(config.getDockerHost().uri()); if (config.getConnectionPoolSize() != -1) { builder.connectionPoolSize(config.getConnectionPoolSize()); } if (!isNullOrEmpty(config.getDockerHost().dockerCertPath())) { final Path dockerCertPath = java.nio.file.Paths.get(config.getDockerHost().dockerCertPath()); final DockerCertificates dockerCertificates; try { dockerCertificates = new DockerCertificates(dockerCertPath); } catch (DockerCertificateException e) { throw new RuntimeException(e); } builder.dockerCertificates(dockerCertificates); } if (config.getGoogleCredentials() != null) { builder.registryAuthSupplier( ContainerRegistryAuthSupplier .forCredentials(config.getGoogleCredentials()) .build() ); } return new PollingDockerClient(builder); }
static String defaultDockerEndpoint() { final String osName = systemDelegate.getProperty("os.name"); final String os = osName.toLowerCase(Locale.ENGLISH); if (os.equalsIgnoreCase("linux") || os.contains("mac")) { return DEFAULT_UNIX_ENDPOINT; } else if (System.getProperty("os.name").equalsIgnoreCase("Windows 10")) { //from Docker doc: Windows 10 64bit: Pro, Enterprise or Education return DEFAULT_WINDOWS_ENDPOINT; } else { return DEFAULT_ADDRESS + ":" + defaultPort(); } }
protected AgentMain startDefaultAgent(final String host, final String... args) throws Exception { final String stateDir = agentStateDirs.resolve(host).toString(); final List<String> argsList = Lists.newArrayList( "-vvvv", "--no-log-setup", "--no-http", "--name", host, "--docker=" + DOCKER_HOST.host(), "--zk", zk.connectString(), "--zk-session-timeout", "100", "--zk-connection-timeout", "100", "--zk-enable-acls", "--zk-acl-master-user", MASTER_USER, "--zk-acl-master-digest", MASTER_DIGEST, "--zk-acl-agent-user", AGENT_USER, "--zk-acl-agent-password", AGENT_PASSWORD, "--state-dir", stateDir, "--domain", "", "--port-range=" + dockerPortRange.lowerEndpoint() + ":" + dockerPortRange.upperEndpoint() ); argsList.addAll(asList(args)); return startAgent(argsList.toArray(new String[argsList.size()])); }
/** * Create a {@link DockerHost} from an explicit address or uri. * * @param endpoint The Docker endpoint. * @param certPath The certificate path. * @return The DockerHost object. */ public static DockerHost from(final String endpoint, final String certPath) { return new DockerHost(endpoint, certPath); }
/** * Create a {@link DockerHost} from DOCKER_HOST and DOCKER_PORT env vars. * * @return The DockerHost object. */ public static DockerHost fromEnv() { final String host = endpointFromEnv(); final String certPath = certPathFromEnv(); return new DockerHost(host, certPath); }
private boolean poke(final int port) { try (Socket ignored = new Socket(DOCKER_HOST.address(), port)) { return true; } catch (IOException e) { return false; } } }
private DockerHost(final String endpoint, final String certPath) { if (endpoint.startsWith("unix://")) { this.port = 0; this.address = DEFAULT_ADDRESS; this.host = endpoint; this.uri = URI.create(endpoint); this.bindUri = URI.create(endpoint); } else { final String stripped = endpoint.replaceAll(".*://", ""); final HostAndPort hostAndPort = HostAndPort.fromString(stripped); final String hostText = hostAndPort.getHost(); final String scheme = isNullOrEmpty(certPath) ? "http" : "https"; this.port = hostAndPort.getPortOrDefault(defaultPort()); this.address = isNullOrEmpty(hostText) ? DEFAULT_ADDRESS : hostText; this.host = address + ":" + port; this.uri = URI.create(scheme + "://" + address + ":" + port); this.bindUri = URI.create("tcp://" + address + ":" + port); } this.certPath = certPath; }
static String endpointFromEnv() { return firstNonNull(systemDelegate.getenv("DOCKER_HOST"), defaultDockerEndpoint()); }