|
| 1 | +package org.eclipse.ice.item.jobLauncher; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Properties; |
| 12 | + |
| 13 | +import org.eclipse.core.runtime.Platform; |
| 14 | +import org.osgi.framework.Bundle; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import com.google.common.net.HostAndPort; |
| 19 | +import com.spotify.docker.client.DefaultDockerClient; |
| 20 | +import com.spotify.docker.client.DefaultDockerClient.Builder; |
| 21 | +import com.spotify.docker.client.DockerCertificateException; |
| 22 | +import com.spotify.docker.client.DockerCertificates; |
| 23 | +import com.spotify.docker.client.DockerClient; |
| 24 | + |
| 25 | +/** |
| 26 | + * This class provides a factory method for creating |
| 27 | + * the DefaultDockerClient in a way that is OS independent. |
| 28 | + * |
| 29 | + * @author Alex McCAskey |
| 30 | + * |
| 31 | + */ |
| 32 | +public class DockerClientFactory { |
| 33 | + |
| 34 | + /** |
| 35 | + * Logger for handling event messages and other information. |
| 36 | + */ |
| 37 | + protected static final Logger logger = LoggerFactory.getLogger(DockerClientFactory.class); |
| 38 | + |
| 39 | + /** |
| 40 | + * This method returns the DockerClient dependent on the current OS. |
| 41 | + * |
| 42 | + * @return |
| 43 | + * @throws DockerCertificateException |
| 44 | + * @throws IOException |
| 45 | + * @throws InterruptedException |
| 46 | + */ |
| 47 | + public DockerClient getDockerClient() throws DockerCertificateException, IOException, InterruptedException { |
| 48 | + DockerClient client = null; |
| 49 | + |
| 50 | + // If this is not Linux, then we have to find DOCKER_HOST |
| 51 | + if (!Platform.getOS().equals(Platform.OS_LINUX)) { |
| 52 | + |
| 53 | + // See if we can get the DOCKER_HOST environment variaable |
| 54 | + String dockerHost = System.getenv("DOCKER_HOST"); |
| 55 | + if (dockerHost == null) { |
| 56 | + |
| 57 | + // If not, run a script to see if we can get it |
| 58 | + File script = getDockerConnectionScript(); |
| 59 | + String[] scriptExec = null; |
| 60 | + if (Platform.getOS().equals(Platform.OS_MACOSX)) { |
| 61 | + scriptExec = new String[]{script.getAbsolutePath()}; |
| 62 | + } else if (Platform.getOS().equals(Platform.OS_WIN32)) { |
| 63 | + scriptExec = new String[]{"cmd.exe", "/C", script.getAbsolutePath()}; |
| 64 | + } |
| 65 | + |
| 66 | + // Execute the script to get the DOCKER vars. |
| 67 | + Process process = new ProcessBuilder(scriptExec).start(); |
| 68 | + process.waitFor(); |
| 69 | + int exitValue = process.exitValue(); |
| 70 | + if (exitValue == 0) { |
| 71 | + |
| 72 | + // Read them into a Properties object |
| 73 | + InputStream processInputStream = process.getInputStream(); |
| 74 | + Properties dockerSettings = new Properties(); |
| 75 | + dockerSettings.load(processInputStream); |
| 76 | + |
| 77 | + // Create the Builder object that wil build the DockerClient |
| 78 | + Builder builder = new Builder(); |
| 79 | + |
| 80 | + // Get the DOCKER_HOST and CERT_PATH vars |
| 81 | + String endpoint = dockerSettings.getProperty("DOCKER_HOST"); |
| 82 | + Path dockerCertPath = Paths.get(dockerSettings.getProperty("DOCKER_CERT_PATH")); |
| 83 | + |
| 84 | + // Set up the certificates |
| 85 | + DockerCertificates certs = DockerCertificates.builder().dockerCertPath(dockerCertPath).build(); |
| 86 | + |
| 87 | + // Set the data need for the builder. |
| 88 | + final String stripped = endpoint.replaceAll(".*://", ""); |
| 89 | + final HostAndPort hostAndPort = HostAndPort.fromString(stripped); |
| 90 | + final String hostText = hostAndPort.getHostText(); |
| 91 | + final String scheme = certs != null ? "https" : "http"; |
| 92 | + final int port = hostAndPort.getPortOrDefault(2375); |
| 93 | + final String address = hostText; |
| 94 | + builder.uri(scheme + "://" + address + ":" + port); |
| 95 | + if (certs != null) { |
| 96 | + builder.dockerCertificates(certs); |
| 97 | + } |
| 98 | + |
| 99 | + // Build the Dockerclient! |
| 100 | + client = builder.build(); |
| 101 | + |
| 102 | + } else { |
| 103 | + // log what happened if the process did not end as expected |
| 104 | + // an exit value of 1 should indicate no connection found |
| 105 | + InputStream processErrorStream = process.getErrorStream(); |
| 106 | + String errorMessage = streamToString(processErrorStream); |
| 107 | + logger.error("Error in getting DOCKER variables: " + errorMessage); |
| 108 | + } |
| 109 | + } else { |
| 110 | + client = DefaultDockerClient.fromEnv().build(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return client; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * This method gets the script file for finding the Docker |
| 119 | + * environment variables. |
| 120 | + * |
| 121 | + * @return |
| 122 | + */ |
| 123 | + private File getDockerConnectionScript() { |
| 124 | + String scriptName = ""; |
| 125 | + if (Platform.getOS().equals(Platform.OS_MACOSX)) { |
| 126 | + scriptName = "script-macosx.sh"; |
| 127 | + } else if (Platform.getOS().equals(Platform.OS_WIN32)) { |
| 128 | + scriptName = "script.bat"; |
| 129 | + } |
| 130 | + |
| 131 | + Bundle bundle = Platform.getBundle("org.eclipse.ice.item"); |
| 132 | + final File script = bundle |
| 133 | + .getDataFile(scriptName); |
| 134 | + |
| 135 | + // if the script file does not exist or is outdated. |
| 136 | + if (script != null |
| 137 | + && (!script.exists() || script.lastModified() < bundle.getLastModified())) { |
| 138 | + try (final FileOutputStream output = new FileOutputStream( |
| 139 | + script); |
| 140 | + final InputStream is = DockerClientFactory.class |
| 141 | + .getResourceAsStream( |
| 142 | + "/resources/" + scriptName)) { |
| 143 | + byte[] buff = new byte[1024]; |
| 144 | + int n; |
| 145 | + while ((n = is.read(buff)) > 0) { |
| 146 | + output.write(buff, 0, n); |
| 147 | + } |
| 148 | + script.setExecutable(true); |
| 149 | + } catch (IOException e) { |
| 150 | + logger.error("Error in getting input file.", e); |
| 151 | + } |
| 152 | + } |
| 153 | + return script; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Private utility for getting string from InputStream. |
| 158 | + * @param stream |
| 159 | + * @return |
| 160 | + */ |
| 161 | + private String streamToString(InputStream stream) { |
| 162 | + BufferedReader buff = new BufferedReader(new InputStreamReader(stream)); |
| 163 | + StringBuffer res = new StringBuffer(); |
| 164 | + String line = ""; //$NON-NLS-1$ |
| 165 | + try { |
| 166 | + while ((line = buff.readLine()) != null) { |
| 167 | + res.append(System.getProperty("line.separator")); |
| 168 | + res.append(line); |
| 169 | + } |
| 170 | + buff.close(); |
| 171 | + } catch (IOException e) { |
| 172 | + } |
| 173 | + return res.length() > 0 ? res.substring(1) : ""; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments