Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 77D9D200B80 for ; Tue, 30 Aug 2016 16:25:14 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7652B160ABA; Tue, 30 Aug 2016 14:25:14 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C4321160AA8 for ; Tue, 30 Aug 2016 16:25:13 +0200 (CEST) Received: (qmail 83870 invoked by uid 500); 30 Aug 2016 14:25:13 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 83846 invoked by uid 99); 30 Aug 2016 14:25:12 -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; Tue, 30 Aug 2016 14:25:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BEBDAE040F; Tue, 30 Aug 2016 14:25:12 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: grkvlt@apache.org To: commits@brooklyn.apache.org Date: Tue, 30 Aug 2016 14:25:12 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/3] brooklyn-server git commit: Add new location customizer that creates child entities early archived-at: Tue, 30 Aug 2016 14:25:14 -0000 Repository: brooklyn-server Updated Branches: refs/heads/master b1cafd85f -> ffba2c272 Add new location customizer that creates child entities early Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/1cdf00fa Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/1cdf00fa Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/1cdf00fa Branch: refs/heads/master Commit: 1cdf00fab12033e7431b268a5727e148fee6e4b4 Parents: 70bd8c3 Author: Andrew Donald Kennedy Authored: Sat Aug 13 21:26:12 2016 +0100 Committer: Andrew Donald Kennedy Committed: Tue Aug 30 15:22:27 2016 +0100 ---------------------------------------------------------------------- .../location/jclouds/ChildEntityCustomizer.java | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/1cdf00fa/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/ChildEntityCustomizer.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/ChildEntityCustomizer.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/ChildEntityCustomizer.java new file mode 100644 index 0000000..e30f9de --- /dev/null +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/ChildEntityCustomizer.java @@ -0,0 +1,86 @@ +/* + * 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.location.jclouds; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.TypeToken; + +import org.jclouds.compute.ComputeService; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.mgmt.Task; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.BrooklynConfigKeys; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.trait.Startable; +import org.apache.brooklyn.core.location.LocationConfigKeys; +import org.apache.brooklyn.entity.stock.ConditionalEntity; +import org.apache.brooklyn.util.core.flags.SetFromFlag; +import org.apache.brooklyn.util.time.Duration; + +/** + * Location customizer that optionally creates and starts a child entity. + *

+ * Use this class when the child entity should be created and started + * before the parent entity has begun executing its lifecycle + * methods. This is useful for supporting agent software installation + * or machine configuration tasks that are too complex to be defined + * in a singe {@code pre.install.command} script. + *

+ * The {@link #CREATE_CHILD_ENTITY} configuration key can be set to + * false to disable creation of the child entity. + * + * @see {@link ConditionalEntity} for an alternative mechanism + */ +public class ChildEntityCustomizer extends BasicJcloudsLocationCustomizer { + + private static final Logger LOG = LoggerFactory.getLogger(ChildEntityCustomizer.class); + + @SetFromFlag("childSpec") + public static final ConfigKey> CHILD_ENTITY_SPEC = ConfigKeys.newConfigKey(new TypeToken>() { }, + "child.entitySpec", "The specification for the child entity to be created"); + + @SetFromFlag("create") + public static final ConfigKey CREATE_CHILD_ENTITY = ConfigKeys.newBooleanConfigKey( + "child.create", "Whether the child entity should be created", + Boolean.TRUE); + + @Override + public void customize(JcloudsLocation location, ComputeService computeService, JcloudsMachineLocation machine) { + EntitySpec spec = config().get(CHILD_ENTITY_SPEC); + Boolean create = config().get(CREATE_CHILD_ENTITY); + Duration timeout = config().get(BrooklynConfigKeys.START_TIMEOUT); + Entity parent = getCallerContext(machine); + + if (Boolean.TRUE.equals(create) && spec != null) { + LOG.info("Creating child entity for {} in {}", parent, machine); + Entity child = getBrooklynManagementContext().getEntityManager().createEntity(spec); + child.setParent(parent); + Task start = Entities.invokeEffectorWithArgs(parent, child, Startable.START, ImmutableList.of(machine)); + if (!start.blockUntilEnded(timeout)) { + throw new IllegalStateException(String.format("Timed out while starting child entity for %s", parent)); + } + } + } +} \ No newline at end of file