Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ffe5a7438274bff462be66dc8e1572a3ae24515 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2015 Red Hat.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.linuxtools.docker.core.Activator;

/**
 * Utility class to discover Docker machines using the 'docker-machine' command
 * line in a process.
 */
public class DockerMachine {

	/**
	 * @param pathToDockerMachine
	 *            the path to 'docker-machine' stored in the preferences
	 * @return the names of the existing Docker Machines
	 */
	public static String[] getNames(final String pathToDockerMachine) {
		return execute(pathToDockerMachine, new String[] { "ls", "-q" }); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @param name
	 *            the name of the Docker Machine to inspect
	 * @param dockerMachineInstallDir
	 *            the installation directory for Docker Machine
	 * @param vmDriverInstallDir
	 *            the installation directory for the underlying VM driver used
	 *            by Docker Machine
	 * @return the host URI to use to connect to it
	 */
	public static String getHost(final String name,
			final String dockerMachineInstallDir,
			final String vmDriverInstallDir) {
		final String[] res = execute(dockerMachineInstallDir,
				new String[] { "url", name }, //$NON-NLS-1$
				vmDriverInstallDir);
		return res.length == 1 ? res[0] : null;
	}

	/**
	 * @param name
	 *            the name of the Docker Machine to inspect
	 * @param pathToDockerMachine
	 *            the path to 'docker-machine' stored in the preferences
	 * @param vmDriverInstallDir
	 *            the installation directory for the underlying VM driver used
	 *            by Docker Machine
	 * @return the path to the directory containing the certificates
	 */
	public static String getCertPath(final String name,
			final String pathToDockerMachine, final String vmDriverInstallDir) {
		final String[] envVariables = execute(pathToDockerMachine,
				new String[] { "env", name }, //$NON-NLS-1$
				vmDriverInstallDir);
		for (String envVariable : envVariables) {
			if (envVariable.contains("DOCKER_CERT_PATH")) { //$NON-NLS-1$
				// DOCKER_CERT_PATH="/path/to/cert-folder"
				return envVariable.split("=")[1].replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
		}
		return null;
	}

	/**
	 * Executes the command given in parameter
	 * 
	 * @param args
	 *            command arguments
	 * @return the lines read in the {@link Process}' {@link InputStream}
	 */
	private static String[] execute(final String dockerMachineInstallDir,
			final String[] args, final String... extraPaths) {
		try {
			final String[] command = new String[args.length + 1];

			command[0] = Paths.get(dockerMachineInstallDir, "docker-machine").toString(); //$NON-NLS-1$
			final String envPath = System.getenv("PATH"); //$NON-NLS-1$
			if (envPath != null) {
				for (String dir : envPath.split(File.pathSeparator)) {
					Path dmPath = Paths.get(dir, "docker-machine"); //$NON-NLS-1$
					if (dmPath.toFile().exists()) {
						command[0] = dmPath.toString();
						break;
					}
				}
			}

			System.arraycopy(args, 0, command, 1, args.length);
			final ProcessBuilder processBuilder = new ProcessBuilder(command);
			final Map<String, String> environment = processBuilder
					.environment();
			final StringBuilder path = new StringBuilder();
			for (String extraPath : extraPaths) {
				path.append(File.pathSeparator).append(extraPath);
			}
			String newEnvPath = environment.get("PATH") + path.toString(); //$NON-NLS-1$
			environment.put("PATH", newEnvPath); //$NON-NLS-1$
			final Process p = processBuilder.start();
			p.waitFor();
			if (p.exitValue() == 0) {
				final List<String> result = new ArrayList<>();
				try (BufferedReader buff = new BufferedReader(
						new InputStreamReader(p.getInputStream()))) {
					String line;
					while ((line = buff.readLine()) != null) {
						result.add(line);
					}
				}
				return result.toArray(new String[0]);
			} else {
				final StringBuffer errorMessage = new StringBuffer();
				try (BufferedReader buff = new BufferedReader(
						new InputStreamReader(p.getErrorStream()))) {
					String line;
					while ((line = buff.readLine()) != null) {
						errorMessage.append(line).append('\n');
					}
				}
				Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
						errorMessage.toString()));
			}
		} catch (IOException | InterruptedException e) {
			Activator.log(e);
		}
		return new String[0];
	}

}

Back to the top