/** 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();
 }
  /**
   * 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;
  }
 /** 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);
   }
 }
Beispiel #8
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());
   }
 }
  @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);
  }
 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;
 }
 @Test
 public void nullNameAndParentLocationIsAcceptable() {
   AbstractLocation location =
       new ConcreteLocation(MutableMap.of("name", null, "parentLocation", null));
   assertEquals(location.getDisplayName(), null);
   assertEquals(location.getParent(), null);
 }
 @Test
 public void queryingNameReturnsNameGivenInConstructor() {
   String name = "Outer Mongolia";
   AbstractLocation location = new ConcreteLocation(MutableMap.of("name", "Outer Mongolia"));
   assertEquals(location.getDisplayName(), name);
   ;
 }
  /**
   * 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 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();
 }
  @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");
    }
  }
 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, 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;
  }
 @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();
 }
 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);
 }
  @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;
 }
 @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;
 }
 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);
         }
       });
 }
Beispiel #26
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;
  }
Beispiel #27
0
 public static final Optional<String> httpDelete(Entity framework, String subUrl) {
   String targetUrl =
       Urls.mergePaths(framework.sensors().get(MesosFramework.FRAMEWORK_URL), subUrl);
   LOG.debug("Deleting {}", targetUrl);
   URI deleteUri = URI.create(targetUrl);
   HttpToolResponse response =
       HttpTool.httpDelete(
           buildClient(framework),
           deleteUri,
           MutableMap.of(HttpHeaders.ACCEPT, "application/json"));
   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());
   }
 }
  @Override
  public void launch() {
    List<String> commands = Lists.newLinkedList();

    String appName = getEntity().getConfig(NodeJsWebAppService.APP_NAME);
    String appFile = getEntity().getConfig(NodeJsWebAppService.APP_FILE);
    String appCommand = getEntity().getConfig(NodeJsWebAppService.APP_COMMAND);
    String appCommandLine = getEntity().getConfig(NodeJsWebAppService.APP_COMMAND_LINE);

    if (Strings.isBlank(appCommandLine)) {
      appCommandLine = appCommand + " " + appFile;
    }

    // Ensure global NPM modules are on Node's path.
    commands.add("export NODE_PATH=\"$NODE_PATH:$(npm root -g)\"");
    commands.add(String.format("cd %s", Os.mergePathsUnix(getRunDir(), appName)));
    commands.add("nohup " + appCommandLine + " > console.out 2>&1 &");

    newScript(MutableMap.of(USE_PID_FILE, true), LAUNCHING).body.append(commands).execute();
  }
  /**
   * Default restart implementation for an entity.
   *
   * <p>Stops processes if possible, then starts the entity again.
   */
  public void restart(ConfigBag parameters) {
    ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPING);

    RestartMachineMode isRestartMachine =
        parameters.get(RestartSoftwareParameters.RESTART_MACHINE_TYPED);
    if (isRestartMachine == null) isRestartMachine = RestartMachineMode.AUTO;
    if (isRestartMachine == RestartMachineMode.AUTO)
      isRestartMachine =
          getDefaultRestartStopsMachine() ? RestartMachineMode.TRUE : RestartMachineMode.FALSE;

    // Calling preStopCustom without a corresponding postStopCustom invocation
    // doesn't look right so use a separate callback pair; Also depending on the arguments
    // stop() could be called which will call the {pre,post}StopCustom on its own.
    DynamicTasks.queue("pre-restart", new PreRestartTask());

    if (isRestartMachine == RestartMachineMode.FALSE) {
      DynamicTasks.queue("stopping (process)", new StopProcessesAtMachineTask());
    } else {
      Map<String, Object> stopMachineFlags = MutableMap.of();
      if (Entitlements.getEntitlementContext() != null) {
        stopMachineFlags.put(
            "tags",
            MutableSet.of(
                BrooklynTaskTags.tagForEntitlement(Entitlements.getEntitlementContext())));
      }
      Task<String> stopTask =
          Tasks.<String>builder()
              .displayName("stopping (machine)")
              .body(new StopMachineTask())
              .flags(stopMachineFlags)
              .build();
      DynamicTasks.queue(stopTask);
    }

    DynamicTasks.queue("starting", new StartInLocationsTask());
    restartChildren(parameters);
    DynamicTasks.queue("post-restart", new PostRestartTask());

    DynamicTasks.waitForLast();
    ServiceStateLogic.setExpectedState(entity(), Lifecycle.RUNNING);
  }
  @Test(groups = {"Integration", "Acceptance"})
  public void testUpdateAttributeWithNoopListeners() {
    final int numIterations = numIterations();
    double minRatePerSec = 1000 * PERFORMANCE_EXPECTATION;
    final AtomicInteger i = new AtomicInteger();
    final AtomicInteger lastVal = new AtomicInteger();

    app.subscriptions()
        .subscribe(
            entity,
            TestEntity.SEQUENCE,
            new SensorEventListener<Integer>() {
              @Override
              public void onEvent(SensorEvent<Integer> event) {
                lastVal.set(event.getValue());
              }
            });

    measure(
        PerformanceTestDescriptor.create()
            .summary("EntityPerformanceTest.testUpdateAttributeWithNoopListeners")
            .iterations(numIterations)
            .minAcceptablePerSecond(minRatePerSec)
            .job(
                new Runnable() {
                  public void run() {
                    entity.sensors().set(TestEntity.SEQUENCE, (i.getAndIncrement()));
                  }
                }));

    Asserts.succeedsEventually(
        MutableMap.of("timeout", TIMEOUT_MS),
        new Runnable() {
          public void run() {
            assertTrue(
                lastVal.get() >= numIterations,
                "lastVal=" + lastVal + "; numIterations=" + numIterations);
          }
        });
  }