@Override public void customize(JcloudsLocation location, ComputeService computeService, JcloudsMachineLocation machine) { JcloudsSshMachineLocation ssh = (JcloudsSshMachineLocation) machine; String name = ssh.getOptionalNode().get().getName(); List<String> commands = ImmutableList.of( String.format("echo %s | ( %s )", name, BashCommands.sudo("tee /etc/hostname")), String.format("echo 127.0.0.1 localhost | ( %s )", BashCommands.sudo("tee /etc/hosts"))); DynamicTasks.queue(SshEffectorTasks.ssh(commands) .machine(ssh) .requiringExitCodeZero()).block(); LOG.debug("Set {} hostname to {}", new Object[] { ssh, name }); } }
@Override public synchronized ProcessTaskWrapper<RET> newTask() { Entity entity = BrooklynTaskTags.getTargetOrContextEntity(Tasks.current()); if (machine==null) { if (log.isDebugEnabled()) log.debug("Using an ssh task not in an effector without any machine; will attempt to infer the machine: "+this); if (entity!=null) machine(EffectorTasks.getSshMachine(entity)); } applySshFlags(getConfig(), entity, getMachine()); return super.newTask(); }
@Override public Task<Integer> sshTaskApply(AmbariAgent ambariAgent) { return SshEffectorTasks .ssh(installPackageOr(ImmutableMap.of(), "mysql-connector-java", installPackageOrFail(ImmutableMap.of(), "libmysql-java"))) .summary("Initialise Ranger requirements on " + ambariAgent.getId()) .machine(EffectorTasks.getSshMachine(ambariAgent)) .newTask() .asTask(); } }
"cat $PRIVATE_KEY.pub") .environmentVariable("RUN_DIR", sourceRunDir) .machine(sourceMachine) .summary("generate private key for slave access") .requiringZeroAndReturningStdout()) dumpId)) .environmentVariable("RUN_DIR", sourceRunDir) .machine(sourceMachine) .summary("copy database dump to slave") .newTask(); "rm " + privateKeyFile) .environmentVariable("RUN_DIR", sourceRunDir) .machine(sourceMachine) .summary("remove private key")); "sed -i'' -e '/%s/d' ~/.ssh/authorized_keys", dumpId)) .machine(destMachine) .summary("remove private key from authorized_keys")).asTask();
.machine(machine.get()) .requiringZeroAndReturningStdout() .summary("group-" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, type))
} else { ProcessTaskWrapper<Integer> task = SshEffectorTasks.ssh(format("mkdir -p %s", imageDir)) .machine(getMachine()) .newTask(); DynamicTasks.queueIfPossible(task).executionContext(getEntity()).orSubmitAndBlock();
@Override public String execCommandTimeout(String command, Duration timeout) { AbstractSoftwareProcessSshDriver driver = (AbstractSoftwareProcessSshDriver) getDriver(); if (driver == null) { throw new NullPointerException("No driver for "+this); } ProcessTaskWrapper<String> task = SshEffectorTasks.ssh(command) .environmentVariables(driver.getShellEnvironment()) .requiringZeroAndReturningStdout() .machine(getMachine()) .summary(command) .newTask(); try { String result = DynamicTasks.queueIfPossible(task) .executionContext(this) .orSubmitAsync() .asTask() .get(timeout); return result; } catch (TimeoutException te) { throw new IllegalStateException("Timed out running command: " + command); } catch (Exception e) { Integer exitCode = task.getExitCode(); LOG.warn("Command failed, return code {}: {}", exitCode == null ? -1 : exitCode, task.getStderr()); throw Exceptions.propagate(e); } }
@Override public String execCommandTimeout(String command, Duration timeout) { ProcessTaskWrapper<String> task = SshEffectorTasks.ssh(command) .environmentVariables(((AbstractSoftwareProcessSshDriver) getDriver()).getShellEnvironment()) .configure(SshTool.PROP_ALLOCATE_PTY, true) // TODO configure globally .requiringZeroAndReturningStdout() .machine(getMachine()) .summary(command) .newTask(); try { String result = DynamicTasks.queueIfPossible(task) .executionContext(this) .orSubmitAsync() .asTask() .get(timeout); return result; } catch (TimeoutException te) { throw new IllegalStateException("Timed out running command: " + command); } catch (Exception e) { Integer exitCode = task.getExitCode(); LOG.warn("Command failed, return code {}: {}", exitCode == null ? -1 : exitCode, task.getStderr()); throw Exceptions.propagate(e); } }
@Override public int execCommandStatusTimeout(String command, Duration timeout) { ProcessTaskWrapper<Object> task = SshEffectorTasks.ssh(command) .environmentVariables(((AbstractSoftwareProcessSshDriver) getDriver()).getShellEnvironment()) .returning(ScriptReturnType.EXIT_CODE) .allowingNonZeroExitCode() .machine(getMachine()) .summary(command) .newTask(); try { Object result = DynamicTasks.queueIfPossible(task) .executionContext(this) .orSubmitAsync() .asTask() .get(timeout); return (Integer) result; } catch (TimeoutException te) { throw new IllegalStateException("Timed out running command: " + command); } catch (Exception e) { Integer exitCode = task.getExitCode(); LOG.warn("Command failed, return code {}: {}", exitCode == null ? -1 : exitCode, task.getStderr()); throw Exceptions.propagate(e); } }
@Test(groups="Live") public void testPostgresStartsAndStops() throws Exception { ChefLiveTestSupport.installBrooklynChefHostedConfig(app); psql = app.createAndManageChild(PostgreSqlSpecs.specChef()); app.start(ImmutableList.of(targetLocation)); Entities.submit(psql, SshEffectorTasks.ssh("ps aux | grep [p]ostgres").requiringExitCodeZero()); SshMachineLocation targetMachine = EffectorTasks.getSshMachine(psql); psql.stop(); try { // if host is still contactable ensure postgres is not running ProcessTaskWrapper<Integer> t = Entities.submit(app, SshEffectorTasks.ssh("ps aux | grep [p]ostgres").machine(targetMachine).allowingNonZeroExitCode()); t.getTask().blockUntilEnded(Duration.TEN_SECONDS); if (!t.isDone()) Assert.fail("Task not finished yet: "+t.getTask()); Assert.assertNotEquals(t.get(), 0, "Task ended with code "+t.get()+"; output: "+t.getStdout() ); } catch (Exception e) { // host has been killed, that is fine log.info("Machine "+targetMachine+" destroyed on stop (expected - "+e+")"); } }
@Override public Task<Integer> sshTaskApply(AmbariAgent ambariAgent) { return SshEffectorTasks .ssh( installExecutable("mysql-server"), installPackageOr(ImmutableMap.of(), "mysql", installPackageOrFail(ImmutableMap.of(), "mysql-client")), alternatives(sudo("service mysqld restart"), sudo("service mysql restart")), String.format("mysql -u root -e 'create user `%s`@`%s` identified by \"%s\";'", getConfig(DB_USER), DB_HOST, getConfig(DB_PASSWORD)), String.format("mysql -u root -e 'grant all privileges on *.* to `%s`@`%s` identified by \"%s\" with grant option; flush privileges;'", getConfig(DB_USER), DB_HOST, getConfig(DB_PASSWORD))) .summary("Initialise MySQL requirements on " + ambariAgent.getId()) .machine(EffectorTasks.getSshMachine(ambariAgent)) .newTask() .asTask(); } }
protected void checkIsRunningAndStops(Entity mysql, SshMachineLocation lh) { Integer pid = getPid(mysql); Assert.assertNotNull(pid, "PID should be set as an attribute (or getPid() overridden to supply)"); Entities.submit(app, SshEffectorTasks.requirePidRunning(pid).machine(lh).newTask() ).get(); app.stop(); // let the kill -1 take effect Time.sleep(Duration.ONE_SECOND); // and assert it has died log.info("mysql in pid "+pid+" should be dead now"); // (app has stopped, so submit on mgmt context) ProcessTaskWrapper<Integer> t = SshEffectorTasks.codePidRunning(pid).machine(lh).newTask(); mgmt.getExecutionManager().submit(t); Assert.assertNotEquals(t.block().getExitCode(), 0); } }
public String callMany(Collection<Entity> targets, ConfigBag params) { TaskBuilder<Object> ptb = Tasks.builder().parallel(true).displayName("effector "+effector.getName()+" ssh to targets"); for (Entity target: targets) { if (Entities.isNoLongerManaged(target)) continue; Lifecycle state = target.getAttribute(Attributes.SERVICE_STATE_ACTUAL); if (state==Lifecycle.STOPPING || state==Lifecycle.STOPPED) continue; Maybe<SshMachineLocation> machine = Locations.findUniqueSshMachineLocation(target.getLocations()); if (machine.isAbsent()) continue; SshEffectorTaskFactory<String> t = makePartialTaskFactory(params, target); t.summary("effector "+effector.getName()+" at "+target); t.machine( machine.get() ); ptb.add(t.newTask()); } queue(ptb.build()).getUnchecked(); return null; }
private void deleteSnapshot(MySqlCluster cluster) { ReplicationSnapshot replicationSnapshot = cluster.getAttribute(MySqlCluster.REPLICATION_LAST_SLAVE_SNAPSHOT); Entity snapshotEntity = mgmt.getEntityManager().getEntity(replicationSnapshot.getEntityId()); SshMachineLocation machine = EffectorTasks.getSshMachine(snapshotEntity); Entities.submit(snapshotEntity, SshEffectorTasks.ssh( "cd $RUN_DIR", "rm " + replicationSnapshot.getSnapshotPath()) .summary("clear snapshot") .machine(machine) .environmentVariable("RUN_DIR", snapshotEntity.getAttribute(MySqlNode.RUN_DIR)) .requiringExitCodeZero()) .asTask() .getUnchecked(); }
@Override public String execCalicoCommand(MesosSlave slave, String command) { String etcdUrl = sensors().get(ETCD_CLUSTER_URL); Maybe<SshMachineLocation> machine = Machines.findUniqueMachineLocation(slave.getLocations(), SshMachineLocation.class); TaskWrapper<String> process = SshEffectorTasks.ssh(BashCommands.sudo(command)) .environmentVariable("ETCD_AUTHORITY", etcdUrl) .machine(machine.get()) .configure(SshTool.PROP_ALLOCATE_PTY, true) .requiringZeroAndReturningStdout() .newTask(); String output = DynamicTasks.queue(process).asTask().getUnchecked(); return output; }
@Override public void run() { String publicKey = tempKeyTask.getUnchecked(); DynamicTasks.queue(SshEffectorTasks.ssh(String.format( "cat >> ~/.ssh/authorized_keys <<EOF\n%s\nEOF", publicKey)) .machine(destMachine) .summary("Add key to authorized_keys") .requiringExitCodeZero()); } });
@Override public Task<Integer> sshTaskApply(AmbariServer ambariServer) { return SshEffectorTasks .ssh( installPackageOr(ImmutableMap.of(), "mysql-connector-java", installPackageOrFail(ImmutableMap.of(), "libmysql-java")), sudo("ambari-server setup --jdbc-db=mysql --jdbc-driver=/usr/share/java/mysql-connector-java.jar")) .summary("Initialise Ranger requirements on " + ambariServer.getId()) .machine(EffectorTasks.getSshMachine(ambariServer)) .newTask() .asTask(); } }
private Result runCommands(SshMachineLocation machine, List<String> commands, Map<String, String> env) { @SuppressWarnings({ "unchecked", "rawtypes" }) SshEffectorTasks.SshEffectorTaskFactory<Integer> etf = SshEffectorTasks.ssh(commands.toArray(new String[]{})) .environmentVariables(env) .machine(machine); ProcessTaskWrapper<Integer> job = DynamicTasks.queue(etf); job.asTask().blockUntilEnded(); return new Result(job); }
/** convenience for generating an {@link PlainSshExecTaskFactory} which can be further customised if desired, and then (it must be explicitly) queued */ public ProcessTaskFactory<Integer> ssh(String ...commands) { return new SshEffectorTaskFactory<Integer>(commands).machine(machine()); } }
@Override public ProcessTaskWrapper<RET> newTask(Entity entity, Effector<RET> effector, ConfigBag parameters) { markDirty(); if (summary==null) summary(effector.getName()+" (ssh)"); machine(EffectorTasks.getSshMachine(entity)); return newTask(); } @Override