[ 
https://issues.apache.org/jira/browse/BEAM-3327?focusedWorklogId=92973&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-92973
 ]

ASF GitHub Bot logged work on BEAM-3327:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 20/Apr/18 00:36
            Start Date: 20/Apr/18 00:36
    Worklog Time Spent: 10m 
      Work Description: bsidhom commented on a change in pull request #5189: 
[BEAM-3327] Basic Docker environment factory
URL: https://github.com/apache/beam/pull/5189#discussion_r182920826
 
 

 ##########
 File path: 
runners/java-fn-execution/src/main/java/org/apache/beam/runners/fnexecution/environment/DockerWrapper.java
 ##########
 @@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.runners.fnexecution.environment;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/** A docker command wrapper. Simplifies communications with the Docker 
daemon. */
+class DockerWrapper {
+  // TODO: Should we require 64-character container ids? Docker technically 
allows abbreviated ids,
+  // but we _should_ always capture full ids.
+  private static final Pattern CONTAINER_ID_PATTERN = 
Pattern.compile("\\p{XDigit}{64}");
+
+  static DockerWrapper forCommand(String dockerExecutable, Duration 
commandTimeout) {
+    return new DockerWrapper(dockerExecutable, commandTimeout);
+  }
+
+  private final String dockerExecutable;
+  private final Duration commandTimeout;
+
+  private DockerWrapper(String dockerExecutable, Duration commandTimeout) {
+    this.dockerExecutable = dockerExecutable;
+    this.commandTimeout = commandTimeout;
+  }
+
+  /**
+   * Runs the given container image with the given command line arguments. 
Returns the running
+   * container id.
+   */
+  public String runImage(String imageTag, List<String> args)
+      throws IOException, TimeoutException, InterruptedException {
+    checkArgument(!imageTag.isEmpty(), "Docker image tag required");
+    // TODO: Validate args?
+    return runShortCommand(
+        ImmutableList.<String>builder()
+            .add(dockerExecutable)
+            .add("run")
+            .add("-d")
+            .add(imageTag)
+            .addAll(args)
+            .build());
+  }
+
+  /**
+   * Kills a docker container by container id.
+   *
+   * @throws IOException if an IOException occurs or if the given container id 
does not exist
+   */
+  public void killContainer(String containerId)
+      throws IOException, TimeoutException, InterruptedException {
+    checkArgument(containerId != null);
+    checkArgument(
+        CONTAINER_ID_PATTERN.matcher(containerId).matches(),
+        "Container ID must be a 64-character hexadecimal string");
+    runShortCommand(Arrays.asList(dockerExecutable, "kill", containerId));
+  }
+
+  /** Run the given command invocation and return stdout as a String. */
+  private String runShortCommand(List<String> invocation)
+      throws IOException, TimeoutException, InterruptedException {
+    ProcessBuilder pb = new ProcessBuilder(invocation);
+    Process process = pb.start();
+    // TODO: Consider supplying executor service here.
+    CompletableFuture<String> resultString =
+        CompletableFuture.supplyAsync(
+            () -> {
+              // NOTE: We do not own the underlying stream and do not close it.
+              BufferedReader reader =
+                  new BufferedReader(
+                      new InputStreamReader(process.getInputStream(), 
StandardCharsets.UTF_8));
 
 Review comment:
   <!--thread_id:cc_182896313_t; 
commit:eb0cdf4ea2d094d8ab09a0a9ebd742507f71a1a8; resolved:0-->
   <!--section:context-quote-->
   > **jkff** wrote:
   > Should we capture the error stream also?
   
   <!--section:body-->
   We could. I went through several iterations of this previously. We're only 
using stdout for container management, so it's not strictly necessary. I can 
add stderr output, e.g., to exceptions if that would be useful.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 92973)
    Time Spent: 11h 10m  (was: 11h)

> Add abstractions to manage Environment Instance lifecycles.
> -----------------------------------------------------------
>
>                 Key: BEAM-3327
>                 URL: https://issues.apache.org/jira/browse/BEAM-3327
>             Project: Beam
>          Issue Type: New Feature
>          Components: runner-core
>            Reporter: Thomas Groh
>            Assignee: Ben Sidhom
>            Priority: Major
>              Labels: portability
>          Time Spent: 11h 10m
>  Remaining Estimate: 0h
>
> This permits remote stage execution for arbitrary environments



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to