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 1E61F180A0 for ; Sat, 14 Nov 2015 15:53:28 +0000 (UTC) Received: (qmail 84619 invoked by uid 500); 14 Nov 2015 15:53:28 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 84582 invoked by uid 500); 14 Nov 2015 15:53:28 -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 84571 invoked by uid 99); 14 Nov 2015 15:53:27 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 14 Nov 2015 15:53:27 +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 54F63180A0D for ; Sat, 14 Nov 2015 15:53:27 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.971 X-Spam-Level: X-Spam-Status: No, score=0.971 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-eu-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 ZViMPw7Xnn7u for ; Sat, 14 Nov 2015 15:53:18 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with SMTP id 0E86D20BEF for ; Sat, 14 Nov 2015 15:53:16 +0000 (UTC) Received: (qmail 84549 invoked by uid 99); 14 Nov 2015 15:53:16 -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; Sat, 14 Nov 2015 15:53:16 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 37934DFF41; Sat, 14 Nov 2015 15:53:16 +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: SimpleCommand addition to brookly... Content-Type: text/plain Message-Id: <20151114155316.37934DFF41@git1-us-west.apache.org> Date: Sat, 14 Nov 2015 15:53:16 +0000 (UTC) Github user aledsage commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/1030#discussion_r44859977 --- Diff: usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java --- @@ -0,0 +1,177 @@ +/* + * 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.test.framework; + +import org.apache.brooklyn.api.entity.drivers.DriverDependentEntity; +import org.apache.brooklyn.api.entity.drivers.EntityDriverManager; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.location.MachineLocation; +import org.apache.brooklyn.core.annotation.EffectorParam; +import org.apache.brooklyn.core.entity.AbstractEntity; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.text.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; + +import static org.apache.brooklyn.core.entity.lifecycle.Lifecycle.*; +import static org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.setExpectedState; + +/** + * Implementation for {@link SimpleCommand}. + */ +public class SimpleCommandImpl extends AbstractEntity + implements SimpleCommand, DriverDependentEntity { + + private static final Logger LOG = LoggerFactory.getLogger(SimpleCommandImpl.class); + private static final int A_LINE = 80; + private transient SimpleCommandDriver driver; + + private Collection locations; + + @Override + public SimpleCommandDriver getDriver() { + return driver; + } + + @Override + public Class getDriverInterface() { + return SimpleCommandDriver.class; + } + + /** + * Gives the opportunity to sub-classes to do additional work based on the result of the command. + */ + protected void handle(SimpleCommandDriver.Result result) { + LOG.debug("Result is {}\nwith output [\n{}\n] and error [\n{}\n]", new Object[] { + result.getExitCode(), shorten(result.getStdout()), shorten(result.getStderr()) + }); + } + + private String shorten(String text) { + return Strings.maxlenWithEllipsis(text, A_LINE); + } + + /** + * Does nothing in this class but gives sub-classes the opportunity to filter locations according to some criterion. + */ + public Collection filterLocations(Collection locations) { + return locations; + } + + + @Override + public void init() { + super.init(); + getLifecycleEffectorTasks().attachLifecycleEffectors(this); + } + + + protected void initDriver(MachineLocation machine) { + LOG.debug("Initializing simple command driver"); + SimpleCommandDriver newDriver = doInitDriver(machine); + if (newDriver == null) { + throw new UnsupportedOperationException("cannot start "+this+" on "+machine+": no driver available"); + } + driver = newDriver; + } + + protected SimpleCommandDriver doInitDriver(MachineLocation machine) { + if (driver!=null) { + if (machine.equals(driver.getLocation())) { + return driver; //just reuse + } else { + LOG.warn("driver/location change is untested for {} at {}; changing driver and continuing", this, machine); + return newDriver(machine); + } + } else { + return newDriver(machine); + } + } + + protected SimpleCommandDriver newDriver(MachineLocation machine) { + LOG.debug("Creating new simple command driver for {} from management context", machine); + EntityDriverManager entityDriverManager = getManagementContext().getEntityDriverManager(); + return entityDriverManager.build(this, machine); + } + + @Override + public void start(@EffectorParam(name = "locations") Collection locations) { + this.locations = locations; + startOnLocations(); + } + + protected void startOnLocations() { + setExpectedState(this, STARTING); + int size = locations.size(); + LOG.debug("Starting simple command at {} locations{}", size, + size > 0 ? " beginning " + locations.iterator().next() : ""); + try { + execute(locations); + setUpAndRunState(true, RUNNING); + + } catch (final Exception e) { + setUpAndRunState(false, ON_FIRE); + throw Exceptions.propagate(e); + } + } + + private void execute(Collection locations) { + SimpleCommandDriver.Result result = null; + String downloadUrl = getConfig(DOWNLOAD_URL); + if (Strings.isNonBlank(downloadUrl)) { + String scriptDir = getConfig(SCRIPT_DIR); + result = getDriver().executeDownloadedScript(locations, downloadUrl, scriptDir); + + } else { + String command = getConfig(DEFAULT_COMMAND); + if (Strings.isBlank(command)) { + throw new IllegalArgumentException("No default command and no downloadUrl provided"); + } + + result = getDriver().execute(locations, command); + } + handle(result); + } + + + @Override + public void stop() { + LOG.debug("Stopping simple command"); + setUpAndRunState(false, STOPPED); + } + + @Override + public void restart() { + LOG.debug("Restarting simple command"); + setUpAndRunState(true, RUNNING); + } + + private void setUpAndRunState(boolean up, Lifecycle status) { + sensors().set(SERVICE_UP, up); + setExpectedState(this, status); + } + + protected SimpleCommandLifecycleEffectorTasks getLifecycleEffectorTasks () { --- End diff -- Extra space before brackets. --- 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. ---