Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 694b9c7ecad73ce72fe251d840631d05f6b841d9 (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
/*******************************************************************************
 * 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.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class DockerMachine {

	private static final String DM = "docker-machine"; //$NON-NLS-1$
	private static final String LS = "ls"; //$NON-NLS-1$
	private static final String URL = "url"; //$NON-NLS-1$
	private static final String ENV = "env"; //$NON-NLS-1$

	public static String[] getNames() {
		return call(new String[] { LS, "-q" }); //$NON-NLS-1$
	}

	public static String getHost(String name) {
		String[] res = call(new String[] { URL, name });
		return res.length == 1 ? res[0] : null;
	}

	public static String getCertPath(String name) {
		String[] res = getEnv(name);
		for (String l : res) {
			if (l.contains("DOCKER_CERT_PATH")) { //$NON-NLS-1$
				// DOCKER_CERT_PATH="/path/to/cert-folder"
				return l.split("=")[1].replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
		}
		return null;
	}

	private static String[] getEnv(String name) {
		return call(new String[] { ENV, name });
	}

	private static String[] call(String[] args) {
		List<String> result = new ArrayList<>();
		try {
			List<String> cmd = new ArrayList<>();
			cmd.add(DM);
			cmd.addAll(Arrays.asList(args));
			Process p = Runtime.getRuntime().exec(cmd.toArray(new String[0]));
			BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
			if (p.waitFor(5, TimeUnit.SECONDS) && p.exitValue() == 0) {
				String line;
				while ((line = buff.readLine()) != null) {
					result.add(line);
				}
			} else {
				return new String[0];
			}
		} catch (IOException e) {
		} catch (InterruptedException e) {
		}
		return result.toArray(new String[0]);
	}

}

Back to the top