Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ffe43c3031cf75a4e3703a559ea30f370a614d7 (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
/*******************************************************************************
 * Copyright (c) 2016 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.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;

/**
 * A utility class to run the {@code docker-compose} command line.
 */
public class DockerCompose {

	private static DockerCompose instance = new DockerCompose();

	private ProcessLauncher processLauncher = new ProcessLauncher();

	public static DockerCompose getInstance() {
		return instance;
	}

	private DockerCompose() {

	}

	/**
	 * Replace the default {@link ProcessLauncher} with another instance. Used
	 * to testing by injecting a mock instance here.
	 * 
	 * @param processLauncher
	 *            the new {@link ProcessLauncher}.
	 */
	public void setProcessLauncher(final ProcessLauncher processLauncher) {
		this.processLauncher = processLauncher;
	}

	/**
	 * Checks that the given {@code dockerMachineInstallDir} contains the
	 * {@code docker-compose} command
	 * 
	 * @param dockerComposeInstallDir
	 *            the directory to check
	 * @return <code>true</code> if the system-specific command was found,
	 *         <code>false</code> otherwise.
	 */
	public boolean checkPathToDockerCompose(
			final String dockerComposeInstallDir) {
		return this.processLauncher.checkPathToCommand(dockerComposeInstallDir,
				getDockerComposeCommandName());
	}

	/**
	 * Runs the {@code docker-compose up} command from the specified
	 * {@code baseDir}
	 * 
	 * @param dockerComposeInstallDir
	 *            the directory in which the {@code docker-compose} command is
	 *            located
	 * @param baseDir
	 *            the base directory in which the {@code docker-compose} command
	 *            should be ran. This directory should contain a
	 *            {@code docker-compose.yml} file.
	 * @throws DockerException
	 *             if something wrong happened
	 */
	public Process up(final IDockerConnection connection,
			final String dockerComposeInstallDir, final String baseDir)
			throws DockerException {
		return this.processLauncher.processBuilder(dockerComposeInstallDir,
				getDockerComposeCommandName(), 
				getArguments(connection, "up")) //$NON-NLS-1$
				.workingDir(baseDir).start();
	}

	public Process stop(final IDockerConnection connection,
			final String dockerComposeInstallDir, final String baseDir)
			throws DockerException {
		return this.processLauncher
				.processBuilder(dockerComposeInstallDir,
						getDockerComposeCommandName(),
						getArguments(connection, "stop")) //$NON-NLS-1$
				.workingDir(baseDir).start();
	}

	private static String[] getArguments(final IDockerConnection connection,
			final String commandName) {
		switch (connection.getSettings().getType()) {
		case UNIX_SOCKET_CONNECTION:
			return new String[] { commandName };
		case TCP_CONNECTION:
			final TCPConnectionSettings connectionSettings = (TCPConnectionSettings) connection
					.getSettings();
			// include the flags to connect to the host
			final List<String> args = new ArrayList<>();
			args.add("-H");
			args.add(connectionSettings.getHost());
			if (connectionSettings.isTlsVerify()
					&& connectionSettings.getPathToCertificates() != null) {
				args.add("--tlsverify"); //$NON-NLS-1$
				args.add("--tlscacert"); //$NON-NLS-1$
				args.add(getFilePath(connectionSettings.getPathToCertificates(),
						"ca.pem")); //$NON-NLS-1$
				args.add("--tlscert"); //$NON-NLS-1$
				args.add(getFilePath(connectionSettings.getPathToCertificates(),
						"cert.pem")); //$NON-NLS-1$
				args.add("--tlskey"); //$NON-NLS-1$
				args.add(getFilePath(connectionSettings.getPathToCertificates(),
						"key.pem")); //$NON-NLS-1$

			}
			args.add(commandName);

			return args.toArray(new String[0]);
		}
		return null;
	}

	private static String getFilePath(final String pathToCertificates,
			final String certificateFileName) {
		if (pathToCertificates.endsWith(File.separator)) {
			return pathToCertificates + certificateFileName;
		}
		return pathToCertificates + File.separator + certificateFileName;
	}

	public static String getDockerComposeCommandName() {
		if (SystemUtils.isWindows()) {
			return "docker-compose.exe"; //$NON-NLS-1$
		}
		return "docker-compose"; //$NON-NLS-1$
	}

}

Back to the top