Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2F2FE173D1 for ; Fri, 18 Sep 2015 09:36:20 +0000 (UTC) Received: (qmail 29838 invoked by uid 500); 18 Sep 2015 09:36:20 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 29800 invoked by uid 500); 18 Sep 2015 09:36:19 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 29789 invoked by uid 99); 18 Sep 2015 09:36:19 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Sep 2015 09:36:19 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 45E6B1809A5 for ; Fri, 18 Sep 2015 09:36:19 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.975 X-Spam-Level: X-Spam-Status: No, score=0.975 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.006, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id QmEbBQlBe-kr for ; Fri, 18 Sep 2015 09:36:11 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id C8A93207EA for ; Fri, 18 Sep 2015 09:36:10 +0000 (UTC) Received: (qmail 29293 invoked by uid 99); 18 Sep 2015 09:36:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Sep 2015 09:36:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7BB0DE00AC; Fri, 18 Sep 2015 09:36:10 +0000 (UTC) From: aledsage To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Capture exit code in AbstractSoft... Content-Type: text/plain Message-Id: <20150918093610.7BB0DE00AC@git1-us-west.apache.org> Date: Fri, 18 Sep 2015 09:36:10 +0000 (UTC) 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; + +/** + * org.apache.brooklyn.entity.software.base.lifecycle.ScriptHelper analog for WinRM + */ +public class WinRmExecuteHelper { + public static final Logger LOG = LoggerFactory.getLogger(WinRmExecuteHelper.class); + + private Entity entity; + private Task 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 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 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 newTask() { + if (task!=null) throw new IllegalStateException("task can only be generated once"); + TaskBuilder tb = Tasks.builder().displayName("winrm: "+summary).body( + new Callable() { + 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. ---