/**
   * Applies the given security group permissions to the given node with the given compute service.
   *
   * <p>Takes no action if the compute service does not have a security group extension.
   *
   * @param permissions The set of permissions to be applied to the node
   * @param nodeId The id of the node to update
   * @param computeService The compute service to use to apply the changes
   */
  @VisibleForTesting
  Map<String, SecurityGroup> addPermissionsToLocation(
      Iterable<IpPermission> permissions, final String nodeId, ComputeService computeService) {
    if (!computeService.getSecurityGroupExtension().isPresent()) {
      LOG.warn(
          "Security group extension for {} absent; cannot update node {} with {}",
          new Object[] {computeService, nodeId, permissions});
      return ImmutableMap.of();
    }
    final SecurityGroupExtension securityApi = computeService.getSecurityGroupExtension().get();
    final String locationId = computeService.getContext().unwrap().getId();

    // Expect to have two security groups on the node: one shared between all nodes in the location,
    // that is cached in sharedGroupCache, and one created by Jclouds that is unique to the node.
    // Relies on customize having been called before. This should be safe because the arguments
    // needed to call this method are not available until post-instance creation.
    SecurityGroup machineUniqueSecurityGroup = getSecurityGroup(nodeId, securityApi, locationId);
    MutableList<IpPermission> newPermissions = MutableList.copyOf(permissions);
    Iterables.removeAll(newPermissions, machineUniqueSecurityGroup.getIpPermissions());
    MutableMap<String, SecurityGroup> addedSecurityGroups = MutableMap.of();
    for (IpPermission permission : newPermissions) {
      SecurityGroup addedPermission =
          addPermission(permission, machineUniqueSecurityGroup, securityApi);
      addedSecurityGroups.put(addedPermission.getId(), addedPermission);
    }
    return addedSecurityGroups;
  }
  /** Test that a core can be created and used with SolrJ client. */
  @Test(groups = "Integration")
  public void testConnection() throws Exception {
    solr =
        app.createAndManageChild(
            EntitySpec.create(SolrServer.class)
                .configure(
                    SolrServer.SOLR_CORE_CONFIG,
                    ImmutableMap.of("example", "classpath://solr/example.tgz")));
    app.start(ImmutableList.of(testLocation));

    EntityAsserts.assertAttributeEqualsEventually(solr, Startable.SERVICE_UP, true);

    SolrJSupport client = new SolrJSupport(solr, "example");

    Iterable<SolrDocument> results = client.getDocuments();
    assertTrue(Iterables.isEmpty(results));

    client.addDocument(MutableMap.<String, Object>of("id", "1", "description", "first"));
    client.addDocument(MutableMap.<String, Object>of("id", "2", "description", "second"));
    client.addDocument(MutableMap.<String, Object>of("id", "3", "description", "third"));
    client.commit();

    results = client.getDocuments();
    assertEquals(Iterables.size(results), 3);
  }
 @Test
 public void testAddChildToParentLocationReturnsExpectedLocation() {
   AbstractLocation parent = new ConcreteLocation(MutableMap.of("id", "1"));
   AbstractLocation child = new ConcreteLocation(MutableMap.of("id", "2"));
   parent.addChild(child);
   assertEquals(child.getParent(), parent);
   assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child));
 }
 @Test
 public void testEquals() {
   AbstractLocation l1 = new ConcreteLocation(MutableMap.of("id", "1", "name", "bob"));
   AbstractLocation l2 = new ConcreteLocation(MutableMap.of("id", "1", "name", "frank"));
   AbstractLocation l3 = new ConcreteLocation(MutableMap.of("id", "2", "name", "frank"));
   assertEquals(l1, l2);
   assertNotEquals(l2, l3);
 }
 @Test
 public void setParentLocationReturnsExpectedLocation() {
   AbstractLocation parent = new ConcreteLocation(MutableMap.of("name", "Middle Earth"));
   AbstractLocation child = new ConcreteLocation(MutableMap.of("name", "The Shire"));
   child.setParent(parent);
   assertEquals(child.getParent(), parent);
   assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child));
 }
 @Override
 public void stop() {
   newScript(MutableMap.of("usePidFile", false), STOPPING)
       .body
       .append(
           callPgctl(
               (entity.getConfig(PostgreSqlNode.DISCONNECT_ON_STOP) ? "-m immediate " : "")
                   + "stop",
               false))
       .failOnNonZeroResultCode()
       .execute();
   newScript(MutableMap.of("usePidFile", getPidFile(), "processOwner", "postgres"), STOPPING)
       .execute();
 }
 /** error should be json-serializable; exceptions can be problematic */
 protected synchronized void noteErrorObject(Object error) {
   errorMessages.add(
       0, MutableMap.<String, Object>of("error", error, "timestamp", System.currentTimeMillis()));
   while (errorMessages.size() > MAX_ERRORS) {
     errorMessages.remove(errorMessages.size() - 1);
   }
 }
示例#8
0
  @Override
  public void doStart(Collection<? extends Location> locs) {
    List<Location> locations = MutableList.of();

    sensors().set(SERVICE_UP, Boolean.FALSE);
    ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);

    LOG.info("Creating new MesosLocation");
    createLocation(MutableMap.<String, Object>of());

    // Start frameworks
    try {
      Group frameworks = sensors().get(MESOS_FRAMEWORKS);
      Entities.invokeEffectorList(
              this,
              frameworks.getMembers(),
              Startable.START,
              ImmutableMap.of("locations", locations))
          .getUnchecked();
    } catch (Exception e) {
      LOG.warn("Error starting frameworks", e);
      ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
      Exceptions.propagate(e);
    }

    super.doStart(locations);

    connectSensors();

    ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    sensors().set(SERVICE_UP, Boolean.TRUE);
  }
示例#9
0
 public static final Optional<String> httpPost(
     Entity framework, String subUrl, String dataUrl, Map<String, Object> substitutions) {
   String targetUrl =
       Urls.mergePaths(framework.sensors().get(MesosFramework.FRAMEWORK_URL), subUrl);
   String templateContents = ResourceUtils.create().getResourceAsString(dataUrl);
   String processedJson =
       TemplateProcessor.processTemplateContents(templateContents, substitutions);
   LOG.debug("Posting JSON to {}: {}", targetUrl, processedJson);
   URI postUri = URI.create(targetUrl);
   HttpToolResponse response =
       HttpTool.httpPost(
           MesosUtils.buildClient(framework),
           postUri,
           MutableMap.of(
               HttpHeaders.CONTENT_TYPE, "application/json",
               HttpHeaders.ACCEPT, "application/json"),
           processedJson.getBytes());
   LOG.debug("Response: " + response.getContentAsString());
   if (!HttpTool.isStatusCodeHealthy(response.getResponseCode())) {
     LOG.warn(
         "Invalid response code {}: {}", response.getResponseCode(), response.getReasonPhrase());
     return Optional.absent();
   } else {
     LOG.debug("Successfull call to {}: {}", targetUrl);
     return Optional.of(response.getContentAsString());
   }
 }
 @Test
 public void queryingNameReturnsNameGivenInConstructor() {
   String name = "Outer Mongolia";
   AbstractLocation location = new ConcreteLocation(MutableMap.of("name", "Outer Mongolia"));
   assertEquals(location.getDisplayName(), name);
   ;
 }
  /**
   * Given an enricher, extracts its state for serialization.
   *
   * @deprecated since 0.7.0, see {@link #newBasicMemento(BrooklynObject)}
   */
  @Deprecated
  public static EnricherMemento newEnricherMemento(Enricher enricher) {
    BasicEnricherMemento.Builder builder = BasicEnricherMemento.builder();
    populateBrooklynObjectMementoBuilder(enricher, builder);

    // TODO persist config keys as well? Or only support those defined on policy class;
    // current code will lose the ConfigKey type on rebind for anything not defined on class.
    // Whereas entities support that.
    // TODO Do we need the "nonPersistableFlagNames" that locations use?
    Map<ConfigKey<?>, Object> config = ((AbstractEnricher) enricher).getConfigMap().getAllConfig();
    for (Map.Entry<ConfigKey<?>, Object> entry : config.entrySet()) {
      ConfigKey<?> key = checkNotNull(entry.getKey(), "config=%s", config);
      Object value = configValueToPersistable(entry.getValue());
      builder.config.put(key.getName(), value);
    }

    Map<String, Object> persistableFlags =
        MutableMap.<String, Object>builder()
            .putAll(
                FlagUtils.getFieldsWithFlagsExcludingModifiers(
                    enricher, Modifier.STATIC ^ Modifier.TRANSIENT))
            .remove("id")
            .remove("name")
            .build();
    builder.config.putAll(persistableFlags);

    return builder.build();
  }
 public static Map<String, Object> getSensorMap(String sensor, Iterable<Entity> descs) {
   if (Iterables.isEmpty(descs)) return Collections.emptyMap();
   Map<String, Object> result = MutableMap.of();
   Iterator<Entity> di = descs.iterator();
   Sensor<?> s = null;
   while (di.hasNext()) {
     Entity potentialSource = di.next();
     s = potentialSource.getEntityType().getSensor(sensor);
     if (s != null) break;
   }
   if (s == null) s = Sensors.newSensor(Object.class, sensor);
   if (!(s instanceof AttributeSensor<?>)) {
     log.warn("Cannot retrieve non-attribute sensor " + s + " for entities; returning empty map");
     return result;
   }
   for (Entity e : descs) {
     Object v = null;
     try {
       v = e.getAttribute((AttributeSensor<?>) s);
     } catch (Exception exc) {
       Exceptions.propagateIfFatal(exc);
       log.warn("Error retrieving sensor " + s + " for " + e + " (ignoring): " + exc);
     }
     if (v != null) result.put(e.getId(), v);
   }
   return result;
 }
  /**
   * Method which returns a Future containing a string formatted using String.format, where the
   * arguments can be normal objects or tasks; tasks will be waited on (submitted if necessary) and
   * their results substituted in the call to String.format.
   *
   * <p>Example:
   *
   * <pre>{@code
   * setConfig(URL, DependentConfiguration.formatString("%s:%s",
   *         DependentConfiguration.attributeWhenReady(target, Target.HOSTNAME),
   *         DependentConfiguration.attributeWhenReady(target, Target.PORT) ) );
   * }</pre>
   */
  @SuppressWarnings("unchecked")
  public static Task<String> formatString(final String spec, final Object... args) {
    List<TaskAdaptable<Object>> taskArgs = Lists.newArrayList();
    for (Object arg : args) {
      if (arg instanceof TaskAdaptable) taskArgs.add((TaskAdaptable<Object>) arg);
      else if (arg instanceof TaskFactory)
        taskArgs.add(((TaskFactory<TaskAdaptable<Object>>) arg).newTask());
    }

    return transformMultiple(
        MutableMap.<String, String>of(
            "displayName",
            "formatting '"
                + spec
                + "' with "
                + taskArgs.size()
                + " task"
                + (taskArgs.size() != 1 ? "s" : "")),
        new Function<List<Object>, String>() {
          @Override
          public String apply(List<Object> input) {
            Iterator<?> tri = input.iterator();
            Object[] vv = new Object[args.length];
            int i = 0;
            for (Object arg : args) {
              if (arg instanceof TaskAdaptable || arg instanceof TaskFactory) vv[i] = tri.next();
              else if (arg instanceof DeferredSupplier) vv[i] = ((DeferredSupplier<?>) arg).get();
              else vv[i] = arg;
              i++;
            }
            return String.format(spec, vv);
          }
        },
        taskArgs);
  }
 @Override
 public Map<String, String> getShellEnvironment() {
   return MutableMap.<String, String>builder()
       .putAll(super.getShellEnvironment())
       .put("PORT", Integer.toString(getHttpPort()))
       .build();
 }
 @Test
 public void nullNameAndParentLocationIsAcceptable() {
   AbstractLocation location =
       new ConcreteLocation(MutableMap.of("name", null, "parentLocation", null));
   assertEquals(location.getDisplayName(), null);
   assertEquals(location.getParent(), null);
 }
示例#16
0
  @Override
  public void init() {
    super.init();

    ConfigToAttributes.apply(this, DOCKER_HOST);
    ConfigToAttributes.apply(this, DOCKER_REGISTRY_PORT);

    DockerHost host = (DockerHost) sensors().get(DOCKER_HOST);
    String installDir = host.sensors().get(SoftwareProcess.INSTALL_DIR);
    SshMachineLocation sshMachine = host.getDynamicLocation().getMachine();
    String sshMachineInstallDir = getSSHMachineInstallDir();

    config()
        .set(
            DockerAttributes.DOCKER_PORT_BINDINGS,
            MutableMap.of(sensors().get(DOCKER_REGISTRY_PORT), 5000));
    config()
        .set(
            DockerAttributes.DOCKER_HOST_VOLUME_MAPPING,
            MutableMap.of(Os.mergePaths(installDir, "certs"), "/certs"));

    sshMachine.installTo(
        ImmutableMap.of(SshTool.PROP_PERMISSIONS.getName(), "0755"),
        SCRIPT_LOCATION,
        Os.mergePaths(sshMachineInstallDir, CREATE_CERTS_SCRIPT_NAME));
    sshMachine.installTo(
        config().get(DockerInfrastructure.DOCKER_CA_CERTIFICATE_PATH),
        Os.mergePaths(sshMachineInstallDir, "ca-cert.pem"));
    sshMachine.installTo(
        config().get(DockerInfrastructure.DOCKER_CA_KEY_PATH),
        Os.mergePaths(sshMachineInstallDir, "ca-key.pem"));

    int result =
        sshMachine.execCommands(
            "installCerts",
            ImmutableList.of(
                BashCommands.sudo(
                    String.format(
                        "%s %s %s",
                        Os.mergePaths(sshMachineInstallDir, CREATE_CERTS_SCRIPT_NAME),
                        host.sensors().get(Attributes.ADDRESS),
                        sshMachineInstallDir))));
    if (result != 0) {
      throw new IllegalStateException("Could not create certificates for docker registry");
    }
  }
 @Override
 public boolean isRunning() {
   return newScript(MutableMap.of("usePidFile", getPidFile()), CHECK_RUNNING)
           .body
           .append(getStatusCmd())
           .execute()
       == 0;
 }
 @Override
 public void launch() {
   log.info(String.format("Starting entity %s at %s", this, getLocation()));
   newScript(MutableMap.of("usePidFile", false), LAUNCHING)
       .body
       .append(callPgctl("start", false))
       .execute();
 }
 protected Map<String, Object> obtainProvisioningFlags(
     final MachineProvisioningLocation<?> location) {
   if (entity() instanceof ProvidesProvisioningFlags) {
     return ((ProvidesProvisioningFlags) entity())
         .obtainProvisioningFlags(location)
         .getAllConfig();
   }
   return MutableMap.<String, Object>of();
 }
 @Override
 public Map<String, BrooklynObject> getAllBrooklynObjects() {
   MutableMap<String, BrooklynObject> result = MutableMap.of();
   result.putAll(locations);
   result.putAll(entities);
   result.putAll(policies);
   result.putAll(enrichers);
   result.putAll(feeds);
   result.putAll(catalogItems);
   return result.asUnmodifiable();
 }
 protected void runTest(Map<String, ?> flags) throws Exception {
   String tag = getClass().getSimpleName().toLowerCase();
   Map<String, ?> allFlags =
       MutableMap.<String, Object>builder()
           .put("tags", ImmutableList.of(tag))
           .putAll(flags)
           .build();
   jcloudsLocation = ctx.getLocationRegistry().getLocationManaged(PROVIDER, allFlags);
   doTest(jcloudsLocation);
 }
  @Override
  public Map<String, Object> getMetrics() {
    Map<String, Object> result = MutableMap.of();

    result.put("rebind", rebindMetrics.asMap());
    result.put("persist", persistMetrics.asMap());

    if (readOnlyRebindCount.get() >= 0) result.put("rebindReadOnlyCount", readOnlyRebindCount);

    // include first rebind counts, so we know whether we rebinded or not
    result.put(
        "firstRebindCounts",
        MutableMap.of(
            "applications", firstRebindAppCount,
            "entities", firstRebindEntityCount,
            "allItems", firstRebindItemCount));

    return result;
  }
 public static Task<String> regexReplacement(Object source, Object pattern, Object replacement) {
   List<TaskAdaptable<Object>> taskArgs = getTaskAdaptable(source, pattern, replacement);
   Function<List<Object>, String> transformer =
       new RegexTransformerString(source, pattern, replacement);
   return transformMultiple(
       MutableMap.of(
           "displayName",
           String.format("creating regex replacement function (%s:%s)", pattern, replacement)),
       transformer,
       taskArgs);
 }
 @Override
 public void stop() {
   String customCommand = getEntity().getConfig(VanillaSoftwareProcess.STOP_COMMAND);
   ScriptHelper script = null;
   if (customCommand == null) {
     script = newScript(MutableMap.of(USE_PID_FILE, getPidFile()), STOPPING);
   } else {
     // TODO: template substitutions?
     script = newScript(STOPPING).body.append(customCommand);
   }
   script.execute();
 }
 @Override
 public boolean isRunning() {
   String customCommand = getEntity().getConfig(VanillaSoftwareProcess.CHECK_RUNNING_COMMAND);
   ScriptHelper script = null;
   if (customCommand == null) {
     script = newScript(MutableMap.of(USE_PID_FILE, getPidFile()), CHECK_RUNNING);
   } else {
     // TODO: template substitutions?
     script = newScript(CHECK_RUNNING).body.append(customCommand);
   }
   return script.execute() == 0;
 }
  @Test
  public void testCustomAttrs() throws IOException {
    LinkDto l = LinkDto.newInstance("http://foo", "Foo", MutableMap.of("bar", "bee"));

    JsonNode t = BasicDtoTest.tree(l);
    Assert.assertEquals(t.size(), 3);
    Assert.assertEquals(t.get("href").asText(), l.getHref());
    Assert.assertEquals(t.get("targetName").asText(), l.getTargetName());
    Assert.assertEquals(t.get("bar").asText(), l.getCustomAttributes().get("bar"));

    Assert.assertEquals(l, new ObjectMapper().readValue(t.toString(), LinkDto.class));
  }
 public synchronized Map<String, Object> asMap() {
   Map<String, Object> result = MutableMap.of();
   result.put("count", count);
   result.put("lastSuccessTimeUtc", lastSuccessTime);
   result.put("lastSuccessTimeMillisSince", since(lastSuccessTime));
   result.put("lastDuration", lastDuration);
   result.put("failureCount", failureCount);
   result.put("lastFailureTimeUtc", lastFailureTime);
   result.put("lastFailureTimeMillisSince", since(lastFailureTime));
   result.put("errorMessages", MutableList.copyOf(errorMessages));
   return result;
 }
 private void assertHasEventEventually(
     final Sensor<?> sensor,
     final Predicate<Object> componentPredicate,
     final Predicate<? super CharSequence> descriptionPredicate) {
   Asserts.succeedsEventually(
       MutableMap.of("timeout", TIMEOUT_MS),
       new Runnable() {
         @Override
         public void run() {
           assertHasEvent(sensor, componentPredicate, descriptionPredicate);
         }
       });
 }
示例#29
0
  private Map<String, Object> getExtraTemplateSubstitutions(String imageName, Entity context) {
    Map<String, Object> templateSubstitutions =
        MutableMap.<String, Object>of("fullyQualifiedImageName", imageName);
    templateSubstitutions.putAll(
        getOwner().config().get(DockerInfrastructure.DOCKERFILE_SUBSTITUTIONS));

    // Add any extra substitutions on the entity (if present)
    if (context != null) {
      templateSubstitutions.putAll(
          context.config().get(DockerInfrastructure.DOCKERFILE_SUBSTITUTIONS));
    }

    return templateSubstitutions;
  }
  /** @deprecated since 0.7.0; use {@link #newBasicMemento(BrooklynObject)} instead */
  @Deprecated
  public static BasicLocationMemento.Builder newLocationMementoBuilder(Location location) {
    BasicLocationMemento.Builder builder = BasicLocationMemento.builder();
    populateBrooklynObjectMementoBuilder(location, builder);

    Set<String> nonPersistableFlagNames =
        MutableMap.<String, Object>builder()
            .putAll(FlagUtils.getFieldsWithFlagsWithModifiers(location, Modifier.TRANSIENT))
            .putAll(FlagUtils.getFieldsWithFlagsWithModifiers(location, Modifier.STATIC))
            .put("id", String.class)
            .filterValues(Predicates.not(Predicates.instanceOf(ConfigKey.class)))
            .build()
            .keySet();
    Map<String, Object> persistableFlags =
        MutableMap.<String, Object>builder()
            .putAll(
                FlagUtils.getFieldsWithFlagsExcludingModifiers(
                    location, Modifier.STATIC ^ Modifier.TRANSIENT))
            .removeAll(nonPersistableFlagNames)
            .build();
    ConfigBag persistableConfig =
        new ConfigBag()
            .copy(((LocationInternal) location).config().getLocalBag())
            .removeAll(nonPersistableFlagNames);

    builder.copyConfig(persistableConfig);
    builder.locationConfig.putAll(persistableFlags);

    Location parentLocation = location.getParent();
    builder.parent = (parentLocation != null) ? parentLocation.getId() : null;

    for (Location child : location.getChildren()) {
      builder.children.add(child.getId());
    }

    return builder;
  }