|
@@ -8,8 +8,10 @@
|
|
|
|
|
|
package org.elasticsearch.packaging.util.docker;
|
|
package org.elasticsearch.packaging.util.docker;
|
|
|
|
|
|
|
|
+import org.elasticsearch.common.util.ArrayUtils;
|
|
import org.elasticsearch.packaging.util.Shell;
|
|
import org.elasticsearch.packaging.util.Shell;
|
|
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@@ -64,4 +66,34 @@ public class DockerShell extends Shell {
|
|
throw e;
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Execute a command inside the Docker container, but without invoking a local shell. The caller
|
|
|
|
+ * is entirely responsible for correctly escaping command arguments, or for invoking a shell
|
|
|
|
+ * inside the container if required.
|
|
|
|
+ * @param args the command and arguments to execute inside the container
|
|
|
|
+ * @return the result of executing the command
|
|
|
|
+ */
|
|
|
|
+ public static Shell.Result executeCommand(String... args) {
|
|
|
|
+ assert Docker.containerId != null;
|
|
|
|
+
|
|
|
|
+ final String[] prefix = new String[] { "docker", "exec", "--tty", Docker.containerId };
|
|
|
|
+ final String[] command = ArrayUtils.concat(prefix, args);
|
|
|
|
+ final ProcessBuilder pb = new ProcessBuilder(command);
|
|
|
|
+
|
|
|
|
+ final Process p;
|
|
|
|
+ final int exitCode;
|
|
|
|
+ final String stdout;
|
|
|
|
+ final String stderr;
|
|
|
|
+ try {
|
|
|
|
+ p = pb.start();
|
|
|
|
+ exitCode = p.waitFor();
|
|
|
|
+ stdout = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
|
|
|
|
+ stderr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8).trim();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new Shell.Result(exitCode, stdout, stderr);
|
|
|
|
+ }
|
|
}
|
|
}
|