Map<String, ?> args = ImmutableMap.of("locations", MutableList.builder().addIfNotNull(loc).buildImmutable()); Task<?> task = newThrottledEffectorTask(entity, Startable.START, args, privileged); tasks.put(entity, task); .addAll(addedEntities) .removeAll(errors.keySet()) .build();
/** builds the command to run the given script; * note that some modes require \$RESULT passed in order to access a variable, whereas most just need $ */ protected List<String> buildRunScriptCommand() { String scriptInvocationCmd; if (runAsRoot) { if (authSudo) { scriptInvocationCmd = BashCommands.authSudo(scriptPath, password); } else { scriptInvocationCmd = BashCommands.sudo(scriptPath) + " < /dev/null"; } } else { scriptInvocationCmd = scriptPath + " < /dev/null"; } MutableList.Builder<String> cmds = MutableList.<String>builder() .add(scriptInvocationCmd) .add("RESULT=$?"); if (!noExtraOutput) cmds.add("echo Executed "+scriptPath+", result $RESULT"); if (!noDeleteAfterExec) { // use "-f" because some systems have "rm" aliased to "rm -i" // use "< /dev/null" to guarantee doesn't hang cmds.add("rm -f "+scriptPath+" < /dev/null"); } cmds.add("exit $RESULT"); return cmds.build(); }
tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredMethods()), new Predicate<Method>() { @Override public boolean apply(Method input) { return Modifier.isPublic(input.getModifiers()); List<Method> resultList = result.build(); Collections.sort(resultList, new Comparator<Method>() { @Override public int compare(Method m1, Method m2) {
@Test public void testExistingChildrenStartedRoundRobiningAcrossLocations() throws Exception { List<Location> locs = ImmutableList.of(loc1, loc2); DynamicFabric fabric = app.createAndManageChild(EntitySpec.create(DynamicFabric.class) .configure(DynamicFabric.MEMBER_SPEC, EntitySpec.create(TestEntity.class))); List<TestEntity> existingChildren = Lists.newArrayList(); for (int i = 0; i < 4; i++) { existingChildren.add(fabric.addChild(EntitySpec.create(TestEntity.class))); } app.start(locs); // Expect only these existing children Asserts.assertEqualsIgnoringOrder(fabric.getChildren(), existingChildren); Asserts.assertEqualsIgnoringOrder(fabric.getMembers(), existingChildren); // Expect one location per existing child (round-robin) // Expect one location per existing child List<Location> remainingLocs = MutableList.<Location>builder().addAll(locs).addAll(locs).build(); for (Entity existingChild : existingChildren) { Collection<Location> childLocs = existingChild.getLocations(); assertEquals(childLocs.size(), 1, "childLocs="+childLocs); assertTrue(remainingLocs.remove(Iterables.get(childLocs, 0)), "childLocs="+childLocs+"; remainingLocs="+remainingLocs+"; allLocs="+locs); } }
tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), filter!=null ? filter : Predicates.<Field>alwaysTrue())); List<Field> resultList = result.build(); Collections.sort(resultList, fieldOrdering != null ? fieldOrdering : FieldOrderings.SUB_BEST_FIELD_LAST_THEN_ALPHABETICAL);
@Override public void install() { LOG.info("Installing Node.JS {}", getVersion()); List<String> commands = MutableList.<String>builder() .add(BashCommands.INSTALL_CURL) .add(BashCommands.ifExecutableElse0("apt-get", BashCommands.chain( BashCommands.installPackage("software-properties-common python-software-properties python g++ make"), BashCommands.sudo("add-apt-repository ppa:chris-lea/node.js")))) .add(BashCommands.installPackage(MutableMap.of("yum", "git nodejs npm", "apt", "git-core nodejs npm"), null)) .add("mkdir -p \"$HOME/.npm\"") .add(BashCommands.sudo("npm install -g n")) .add(BashCommands.sudo("n " + getVersion())) .build(); newScript(INSTALLING) .body.append(commands) .execute(); }
/** * Builds the command to run the given script, asynchronously. * The executed command will return immediately, but the output from the script * will continue to be written * note that some modes require \$RESULT passed in order to access a variable, whereas most just need $ */ @Override protected List<String> buildRunScriptCommand() { String touchCmd = String.format("touch %s %s %s %s", stdoutPath, stderrPath, exitStatusPath, pidPath); String cmd = String.format("nohup sh -c \"( %s > %s 2> %s < /dev/null ) ; echo \\$? > %s \" > /dev/null 2>&1 < /dev/null &", scriptPath, stdoutPath, stderrPath, exitStatusPath); MutableList.Builder<String> cmds = MutableList.<String>builder() .add(runAsRoot ? BashCommands.sudo(touchCmd) : touchCmd) .add(runAsRoot ? BashCommands.sudo(cmd) : cmd) .add("echo $! > "+pidPath) .add("RESULT=$?"); if (!noExtraOutput) { cmds.add("echo Executing async "+scriptPath); } cmds.add("exit $RESULT"); return cmds.build(); }
@Override public void removeLocations(Collection<? extends Location> removedLocations) { synchronized (locations) { List<Location> oldLocations = locations.get(); Set<Location> trulyRemovedLocations = Sets.intersection(ImmutableSet.copyOf(removedLocations), ImmutableSet.copyOf(oldLocations)); locations.set(MutableList.<Location>builder().addAll(oldLocations).removeAll(removedLocations).buildImmutable()); for (Location loc : trulyRemovedLocations) { sensors().emit(AbstractEntity.LOCATION_REMOVED, loc); } } // TODO Not calling `Entities.unmanage(removedLocation)` because this location might be shared with other entities. // Relying on abstractLocation.removeChildLocation unmanaging it, but not ideal as top-level locations will stick // around forever, even if not referenced. // Same goes for AbstractEntity#clearLocations(). getManagementSupport().getEntityChangeListener().onLocationsChanged(); }
@Test public void testExistingChildrenToppedUpWhenNewMembersIfMoreLocations() throws Exception { List<Location> locs = ImmutableList.of(loc1, loc2, loc3); DynamicFabric fabric = app.createAndManageChild(EntitySpec.create(DynamicFabric.class) .configure(DynamicFabric.MEMBER_SPEC, EntitySpec.create(TestEntity.class))); TestEntity existingChild = fabric.addChild(EntitySpec.create(TestEntity.class)); app.start(locs); // Expect three children: the existing one, and one per other location assertEquals(fabric.getChildren().size(), 3, "children="+fabric.getChildren()); assertTrue(fabric.getChildren().contains(existingChild), "children="+fabric.getChildren()+"; existingChild="+existingChild); Asserts.assertEqualsIgnoringOrder(fabric.getMembers(), fabric.getChildren()); List<Location> remainingLocs = MutableList.<Location>builder().addAll(locs).build(); for (Entity child : fabric.getChildren()) { Collection<Location> childLocs = child.getLocations(); assertEquals(childLocs.size(), 1, "childLocs="+childLocs); assertTrue(remainingLocs.remove(Iterables.get(childLocs, 0)), "childLocs="+childLocs+"; remainingLocs="+remainingLocs+"; allLocs="+locs); } }
public void testBuilderAddIfNotNull() throws Exception { List<Object> vals = MutableList.builder().addIfNotNull(1).addIfNotNull(null).build(); Assert.assertEquals(vals, ImmutableList.of(1)); }