in software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/InitSlaveTaskBody.java [171:240]
private void copyDumpAsync(Entity source, Entity dest, String sourceDumpPath, String dumpId) {
final SshMachineLocation sourceMachine = EffectorTasks.getSshMachine(source);
final SshMachineLocation destMachine = EffectorTasks.getSshMachine(dest);
String sourceRunDir = source.getAttribute(MySqlNode.RUN_DIR);
String privateKeyFile = dumpId + ".id_rsa";
final Task<String> tempKeyTask = DynamicTasks.queue(SshEffectorTasks.ssh(
"cd $RUN_DIR",
"PRIVATE_KEY=" + privateKeyFile,
"ssh-keygen -t rsa -N '' -f $PRIVATE_KEY -C " + dumpId + " > /dev/null",
"cat $PRIVATE_KEY.pub")
.environmentVariable("RUN_DIR", sourceRunDir)
.machine(sourceMachine)
.summary("generate private key for slave access")
.requiringZeroAndReturningStdout())
.asTask();
DynamicTasks.queue("add key to authorized_keys", new Runnable() {
@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());
}
});
final ProcessTaskWrapper<Integer> copyTask = SshEffectorTasks.ssh(
"cd $RUN_DIR",
String.format(
"scp -o 'BatchMode yes' -o 'StrictHostKeyChecking no' -i '%s' '%s' '%s@%s:%s/%s.sql'",
privateKeyFile,
sourceDumpPath,
destMachine.getUser(),
dest.getAttribute(MySqlNode.SUBNET_ADDRESS),
dest.getAttribute(MySqlNode.RUN_DIR),
dumpId))
.environmentVariable("RUN_DIR", sourceRunDir)
.machine(sourceMachine)
.summary("copy database dump to slave")
.newTask();
// Let next couple of tasks complete even if this one fails so that we can clean up.
TaskTags.markInessential(copyTask);
DynamicTasks.queue(copyTask);
// Delete private key
DynamicTasks.queue(SshEffectorTasks.ssh(
"cd $RUN_DIR",
"rm " + privateKeyFile)
.environmentVariable("RUN_DIR", sourceRunDir)
.machine(sourceMachine)
.summary("remove private key"));
DynamicTasks.queue(SshEffectorTasks.ssh(String.format(
"sed -i'' -e '/%s/d' ~/.ssh/authorized_keys",
dumpId))
.machine(destMachine)
.summary("remove private key from authorized_keys")).asTask();
// The task will fail if copyTask fails, but only after the private key is deleted.
DynamicTasks.queue("check for successful copy", new Runnable() {
@Override
public void run() {
copyTask.asTask().getUnchecked();
}
});
}