brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Capture exit code in AbstractSoft...
Date Fri, 18 Sep 2015 09:36:10 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/906#discussion_r39838605
  
    --- Diff: software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/WinRmExecuteHelper.java
---
    @@ -0,0 +1,217 @@
    +/*
    + * 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.brooklyn.entity.software.base.lifecycle;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Predicate;
    +import com.google.common.base.Predicates;
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.mgmt.ExecutionContext;
    +import org.apache.brooklyn.api.mgmt.Task;
    +import org.apache.brooklyn.api.mgmt.TaskQueueingContext;
    +import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
    +import org.apache.brooklyn.util.core.task.DynamicTasks;
    +import org.apache.brooklyn.util.core.task.TaskBuilder;
    +import org.apache.brooklyn.util.core.task.Tasks;
    +import org.apache.brooklyn.util.stream.Streams;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import javax.annotation.Nullable;
    +import java.io.ByteArrayOutputStream;
    +import java.io.IOException;
    +import java.util.LinkedHashMap;
    +import java.util.Map;
    +import java.util.concurrent.Callable;
    +
    +import static java.lang.String.format;
    +
    +/**
    + * <code>org.apache.brooklyn.entity.software.base.lifecycle.ScriptHelper</code>
analog for WinRM
    + */
    +public class WinRmExecuteHelper {
    +    public static final Logger LOG = LoggerFactory.getLogger(WinRmExecuteHelper.class);
    +
    +    private Entity entity;
    +    private Task<Integer> task;
    +
    +    protected final NativeWindowsScriptRunner runner;
    +    public final String summary;
    +
    +    private String command;
    +    private String psCommand;
    +
    +    @SuppressWarnings("rawtypes")
    +    protected final Map flags = new LinkedHashMap();
    +    protected Predicate<? super Integer> resultCodeCheck = Predicates.alwaysTrue();
    +    protected ByteArrayOutputStream stdout, stderr, stdin;
    +
    +    public WinRmExecuteHelper(NativeWindowsScriptRunner runner, String summary) {
    +        this.runner = runner;
    +        this.summary = summary;
    +    }
    +
    +    public WinRmExecuteHelper setCommand(String command) {
    +        this.command = command;
    +        return this;
    +    }
    +
    +    public WinRmExecuteHelper setPsCommand(String psCommand) {
    +        this.psCommand = psCommand;
    +        return this;
    +    }
    +
    +    protected Entity getEntity() {
    +        return entity;
    +    }
    +
    +    /** queues the task for execution if we are in a {@link TaskQueueingContext} (e.g.
EffectorTaskFactory);
    +     * or if we aren't in a queueing context, it will submit the task (assuming there
is an {@link ExecutionContext}
    +     * _and_ block until completion, throwing on error */
    +    @Beta
    +    public Task<Integer> queue() {
    +        return DynamicTasks.queueIfPossible(newTask()).orSubmitAndBlock().getTask();
    +    }
    +
    +    /** creates a task which will execute this script; note this can only be run once
per instance of this class */
    +    public synchronized Task<Integer> newTask() {
    +        if (task!=null) throw new IllegalStateException("task can only be generated once");
    +        TaskBuilder<Integer> tb = Tasks.<Integer>builder().displayName("winrm:
"+summary).body(
    +                new Callable<Integer>() {
    +                    public Integer call() throws Exception {
    +                        return executeInternal();
    +                    }
    +                });
    +
    +        try {
    +            ByteArrayOutputStream stdin = new ByteArrayOutputStream();
    +            stdin.write((command != null ? command : psCommand).getBytes());
    +            tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDIN, stdin));
    +        } catch (IOException e) {
    +            LOG.warn("Error registering stream "+BrooklynTaskTags.STREAM_STDIN+" on "+tb+":
"+e, e);
    +        }
    +
    +        Map flags = getFlags();
    +
    +        Map<?,?> env = (Map<?,?>) flags.get("env");
    +        if (env!=null) {
    +            // if not explicitly set, env will come from getShellEnv in AbstractSoftwareProcessSshDriver.execute,
    +            // which will also update this tag appropriately
    +            tb.tag(BrooklynTaskTags.tagForEnvStream(BrooklynTaskTags.STREAM_ENV, env));
    +        }
    +
    +        if (gatherOutput) {
    +            stdout = new ByteArrayOutputStream();
    +            tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDOUT,
stdout));
    --- End diff --
    
    Why do we now have this code here and in `AbstractSoftwareProcessWinRmDriver.executeCommand`?
Is there a way to just have it in one place? e.g. could `AbstractSoftwareProcessWinRmDriver.executeCommand`
use the `WinRmExecuteHelper` somehow?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message