/** * Executes docker-machine ls command * * @return set of machines */ public Set<Machine> list() { return this.list(null); }
/** * Executes docker-machine ls --filter field=value command * * @param cliPathExec * location of docker-machine or null if it is on PATH. * @param field * to use in condition * @param value * value that the field should have * * @return set of machines */ public Set<Machine> list(String cliPathExec, String field, String value) { final Set<Machine> machines = new HashSet<>(); List<String> output = commandLineExecutor.execCommandAsArray(createDockerMachineCommand(cliPathExec), "ls", "--filter", field + "=" + value); output = findHeader(output); if (!output.isEmpty()) { final Map<String, Index> headerIndex = calculateStartingFieldsIndex(output.get(0)); for (String fields : output.subList(1, output.size())) { machines.add(parse(headerIndex, fields)); } } return machines; }
private String resolveDockerMachine(String tag, String machineName, String dockerMachinePath) { dockerMachine.setMachineName(machineName); return tag.replaceAll(AbstractCliInternetAddressResolver.DOCKERHOST_TAG, dockerMachine.ip(dockerMachinePath, false)); }
/** * Executes docker-machine ls command * * @param cliPathExec * location of docker-machine or null if it is on PATH. * * @return set of machines */ public Set<Machine> list(String cliPathExec) { Set<Machine> machines = new HashSet<>(); List<String> output = commandLineExecutor.execCommandAsArray(createDockerMachineCommand(cliPathExec), "ls"); Map<String, Index> headerIndex = calculateStartingFieldsIndex(output.get(0)); for (String fields : output.subList(1, output.size())) { machines.add(parse(headerIndex, fields)); } return machines; }
public void configure(@Observes(precedence = 100) ManagerStarted managerStarted) { dockerMachineInstanceProducer.set(new DockerMachine(new CommandLineExecutor())); } }
@Test public void shouldParseStoppedMachines() { when(executor.execCommandAsArray("docker-machine", "ls")).thenReturn(Arrays.asList( "NAME ACTIVE DRIVER STATE URL SWARM", "dev - virtualbox Stopped " )); DockerMachine dockerMachine = new DockerMachine(executor); final Set<Machine> list = dockerMachine.list(); assertThat(list, hasSize(1)); final Machine[] machines = list.toArray(new Machine[1]); assertThat(machines[0].getName(), is("dev")); assertThat(machines[0].getState(), is("Stopped")); assertThat(machines[0].getUrl(), is("")); }
private Map<String, String> resolveAutoStartDockerMachine(Map<String, String> config) { final String cliPathExec = config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH); if (dockerMachine.isDockerMachineInstalled(cliPathExec)) { final Set<Machine> allMachines = dockerMachine.list(cliPathExec); Optional<Machine> machine = Optional.empty(); if (config.containsKey(CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { String configuredMachineName = config.get(CubeDockerConfiguration.DOCKER_MACHINE_NAME); machine = allMachines.stream() .filter(m -> configuredMachineName.equals(m.getName())) .filter(m -> "Stopped".equalsIgnoreCase(m.getState())) .findFirst(); } else { if (allMachines.size() == 1 && "Stopped".equalsIgnoreCase(allMachines.iterator().next().getState())) { machine = Optional.of(allMachines.iterator().next()); } } machine.ifPresent(m -> dockerMachine.startDockerMachine(cliPathExec, m.getName())); } return config; }
private Map<String, String> resolveDefaultDockerMachine(Map<String, String> config) { // if user has not specified Docker URI host not a docker machine // setting DOCKER_URI to avoid using docker machine although it is installed if (!config.containsKey(CubeDockerConfiguration.DOCKER_URI) && !config.containsKey( CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { log.fine( "No DockerUri or DockerMachine has been set, let's see if there is only one Docker Machine Running."); if (dockerMachine.isDockerMachineInstalled(config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH))) { // we can inspect if docker machine has one and only one docker machine running, which means that would like to use that one Set<Machine> machines = this.dockerMachine.list(config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH), "state", "Running"); // if there is only one machine running we can use that one. // if not Cube will resolve the default URI depending on OS (linux socket, boot2docker, ...) if (machines.size() == 1) { log.fine(String.format("One Docker Machine is running (%s) and it is going to be used for tests", machines.iterator().next().getName())); config.put(CubeDockerConfiguration.DOCKER_MACHINE_NAME, getFirstMachine(machines).getName()); } } } return config; }
private Map<String, String> resolveDownloadDockerMachine(Map<String, String> config) { if (config.containsKey(CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { final String cliPathExec = config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH); if (!dockerMachine.isDockerMachineInstalled(cliPathExec)) { String machineVersion = GitHubUtil.getDockerMachineLatestVersion(); String machineCustomPath = config.get(CubeDockerConfiguration.DOCKER_MACHINE_CUSTOM_PATH); File dockerMachineFile = CubeDockerConfiguration.resolveMachinePath(machineCustomPath, machineVersion); String dockerMachinePath = dockerMachineFile.getPath(); boolean dockerMachineFileExist = dockerMachineFile != null && dockerMachineFile.exists(); String machineName = config.get(CubeDockerConfiguration.DOCKER_MACHINE_NAME); String machineUrl = CubeDockerConfiguration.resolveUrl(machineVersion); if (!dockerMachineFileExist) { dockerMachineFile.getParentFile().mkdirs(); Spacelift.task(DownloadTool.class) .from(machineUrl) .to(dockerMachineFile) .execute() .await(); config.put(CubeDockerConfiguration.DOCKER_MACHINE_PATH, dockerMachinePath); dockerMachine.grantPermissionToDockerMachine(dockerMachinePath); String machineDriver = config.get(CubeDockerConfiguration.DOCKER_MACHINE_DRIVER); dockerMachine.createMachine(dockerMachinePath, machineDriver, machineName); } else { config.put(CubeDockerConfiguration.DOCKER_MACHINE_PATH, dockerMachinePath); } } } return config; }
@Override protected String[] getCommandArguments(String cliPathExec) { if (machineName == null) { throw new IllegalArgumentException("Machine Name cannot be null and couldn't be autoresolved."); } return new String[] {createDockerMachineCommand(cliPathExec), "ip", machineName}; }
/** * Checks if Docker Machine is installed by running docker-machine and inspect the result. * * @return true if it is installed, false otherwise. */ public boolean isDockerMachineInstalled() { return isDockerMachineInstalled(null); }
public void configure(@Observes(precedence = 100) ManagerStarted managerStarted) { dockerMachineInstanceProducer.set(new DockerMachine(new CommandLineExecutor())); } }
@Test public void shouldListDockerMachines() { when(executor.execCommandAsArray("docker-machine", "ls")).thenReturn(Arrays.asList( "NAME ACTIVE DRIVER STATE URL SWARM", "dev * virtualbox Running tcp://192.168.99.100:2376 ", "qa * virtualbox Running tcp://192.168.99.101:2376 swarm-master" )); DockerMachine dockerMachine = new DockerMachine(executor); final Set<Machine> list = dockerMachine.list(); assertThat(list, hasSize(2)); final Machine[] machines = list.toArray(new Machine[2]); assertThat(machines[0].getName(), is("qa")); assertThat(machines[0].getState(), is("Running")); assertThat(machines[0].getSwarm(), is("swarm-master")); assertThat(machines[1].getName(), is("dev")); assertThat(machines[1].getState(), is("Running")); assertThat(machines[1].getSwarm(), is("")); }
private Map<String, String> resolveAutoStartDockerMachine(Map<String, String> config) { final String cliPathExec = config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH); if (dockerMachine.isDockerMachineInstalled(cliPathExec)) { final Set<Machine> allMachines = dockerMachine.list(cliPathExec); Optional<Machine> machine = Optional.empty(); if (config.containsKey(CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { String configuredMachineName = config.get(CubeDockerConfiguration.DOCKER_MACHINE_NAME); machine = allMachines.stream() .filter(m -> configuredMachineName.equals(m.getName())) .filter(m -> "Stopped".equalsIgnoreCase(m.getState())) .findFirst(); } else { if (allMachines.size() == 1 && "Stopped".equalsIgnoreCase(allMachines.iterator().next().getState())) { machine = Optional.of(allMachines.iterator().next()); } } machine.ifPresent(m -> dockerMachine.startDockerMachine(cliPathExec, m.getName())); } return config; }
/** * Executes docker-machine ls command * * @param cliPathExec * location of docker-machine or null if it is on PATH. * * @return set of machines */ public Set<Machine> list(String cliPathExec) { Set<Machine> machines = new HashSet<>(); List<String> output = commandLineExecutor.execCommandAsArray(createDockerMachineCommand(cliPathExec), "ls"); Map<String, Index> headerIndex = calculateStartingFieldsIndex(output.get(0)); for (String fields : output.subList(1, output.size())) { machines.add(parse(headerIndex, fields)); } return machines; }
private Map<String, String> resolveDefaultDockerMachine(Map<String, String> config) { // if user has not specified Docker URI host not a docker machine // setting DOCKER_URI to avoid using docker machine although it is installed if (!config.containsKey(CubeDockerConfiguration.DOCKER_URI) && !config.containsKey( CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { log.fine( "No DockerUri or DockerMachine has been set, let's see if there is only one Docker Machine Running."); if (dockerMachine.isDockerMachineInstalled(config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH))) { // we can inspect if docker machine has one and only one docker machine running, which means that would like to use that one Set<Machine> machines = this.dockerMachine.list(config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH), "state", "Running"); // if there is only one machine running we can use that one. // if not Cube will resolve the default URI depending on OS (linux socket, boot2docker, ...) if (machines.size() == 1) { log.fine(String.format("One Docker Machine is running (%s) and it is going to be used for tests", machines.iterator().next().getName())); config.put(CubeDockerConfiguration.DOCKER_MACHINE_NAME, getFirstMachine(machines).getName()); } } } return config; }
private Map<String, String> resolveDownloadDockerMachine(Map<String, String> config) { if (config.containsKey(CubeDockerConfiguration.DOCKER_MACHINE_NAME)) { final String cliPathExec = config.get(CubeDockerConfiguration.DOCKER_MACHINE_PATH); if (!dockerMachine.isDockerMachineInstalled(cliPathExec)) { String machineVersion = GitHubUtil.getDockerMachineLatestVersion(); String machineCustomPath = config.get(CubeDockerConfiguration.DOCKER_MACHINE_CUSTOM_PATH); File dockerMachineFile = CubeDockerConfiguration.resolveMachinePath(machineCustomPath, machineVersion); String dockerMachinePath = dockerMachineFile.getPath(); boolean dockerMachineFileExist = dockerMachineFile != null && dockerMachineFile.exists(); String machineName = config.get(CubeDockerConfiguration.DOCKER_MACHINE_NAME); String machineUrl = CubeDockerConfiguration.resolveUrl(machineVersion); if (!dockerMachineFileExist) { dockerMachineFile.getParentFile().mkdirs(); Spacelift.task(DownloadTool.class) .from(machineUrl) .to(dockerMachineFile) .execute() .await(); config.put(CubeDockerConfiguration.DOCKER_MACHINE_PATH, dockerMachinePath); dockerMachine.grantPermissionToDockerMachine(dockerMachinePath); String machineDriver = config.get(CubeDockerConfiguration.DOCKER_MACHINE_DRIVER); dockerMachine.createMachine(dockerMachinePath, machineDriver, machineName); } else { config.put(CubeDockerConfiguration.DOCKER_MACHINE_PATH, dockerMachinePath); } } } return config; }
@Override protected String[] getCommandArguments(String cliPathExec) { if (machineName == null) { throw new IllegalArgumentException("Machine Name cannot be null and couldn't be autoresolved."); } return new String[] {createDockerMachineCommand(cliPathExec), "ip", machineName}; }
/** * Checks if Docker Machine is installed by running docker-machine and inspect the result. * * @return true if it is installed, false otherwise. */ public boolean isDockerMachineInstalled() { return isDockerMachineInstalled(null); }
public DockerRequirement() { this.commandLineExecutor = new CommandLineExecutor(); this.resolver = new CubeDockerConfigurationResolver(new Top(), new DockerMachine(commandLineExecutor), new Boot2Docker(commandLineExecutor), new DefaultDocker(), new OperatingSystemResolver().currentOperatingSystem() ); }