Java Code Examples for org.apache.brooklyn.util.text.Strings#getFragmentBetween()

The following examples show how to use org.apache.brooklyn.util.text.Strings#getFragmentBetween() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaSoftwareProcessSshDriver.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public void checkJavaHostnameBug() {
    checkNoHostnameBug();

    try {
        ProcessTaskWrapper<Integer> hostnameTask = DynamicTasks.queue(SshEffectorTasks.ssh("echo FOREMARKER; hostname -f; echo AFTMARKER")).block();
        String stdout = Strings.getFragmentBetween(hostnameTask.getStdout(), "FOREMARKER", "AFTMARKER");
        if (hostnameTask.getExitCode() == 0 && Strings.isNonBlank(stdout)) {
            String hostname = stdout.trim();
            Integer len = hostname.length();
            if (len > 63) {
                // likely to cause a java crash due to java bug 7089443 -- set a new short hostname
                // http://mail.openjdk.java.net/pipermail/net-dev/2012-July/004603.html
                String newHostname = "br-"+getEntity().getId().toLowerCase();
                log.info("Detected likelihood of Java hostname bug with hostname length "+len+" for "+getEntity()+"; renaming "+getMachine()+"  to hostname "+newHostname);
                DynamicTasks.queue(SshEffectorTasks.ssh(BashCommands.setHostname(newHostname, null))).block();
            }
        } else {
            log.debug("Hostname length could not be determined for location "+EffectorTasks.findSshMachine()+"; not doing Java hostname bug check");
        }
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        log.warn("Error checking/fixing Java hostname bug (continuing): "+e, e);
    }
}
 
Example 2
Source File: AbstractSoftwareProcessSshDriver.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public void checkNoHostnameBug() {
    try {
        ProcessTaskWrapper<Integer> hostnameTask = DynamicTasks.queue(SshEffectorTasks.ssh("echo FOREMARKER; hostname; echo AFTMARKER")).block();
        String stdout = Strings.getFragmentBetween(hostnameTask.getStdout(), "FOREMARKER", "AFTMARKER");
        if (hostnameTask.getExitCode() == 0 && Strings.isNonBlank(stdout)) {
            String hostname = stdout.trim();
            if (hostname.equals("(none)")) {
                String newHostname = "br-"+getEntity().getId().toLowerCase();
                log.info("Detected no-hostname bug with hostname "+hostname+" for "+getEntity()+"; renaming "+getMachine()+"  to hostname "+newHostname);
                DynamicTasks.queue(SshEffectorTasks.ssh(BashCommands.setHostname(newHostname, null))).block();
            }
        } else {
            log.debug("Hostname could not be determined for location "+EffectorTasks.findSshMachine()+"; not doing no-hostname bug check");
        }
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        log.warn("Error checking/fixing no-hostname bug (continuing): "+e, e);
    }
}
 
Example 3
Source File: SshMachineLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public String resolveOnBoxDirFor(Entity entity, String unresolvedPath) {
    ProcessTaskWrapper<Integer> baseTask = SshEffectorTasks.ssh(
        BashCommands.alternatives("mkdir -p \"${BASE_DIR}\"",
            BashCommands.chain(
                BashCommands.sudo("mkdir -p \"${BASE_DIR}\""),
                BashCommands.sudo("chown "+getUser()+" \"${BASE_DIR}\""))),
        "cd ~",
        "cd ${BASE_DIR}",
        "echo BASE_DIR_RESULT':'`pwd`:BASE_DIR_RESULT")
        .environmentVariable("BASE_DIR", unresolvedPath)
        .requiringExitCodeZero()
        .summary("initializing on-box base dir "+unresolvedPath).newTask();
    DynamicTasks.queueIfPossible(baseTask).orSubmitAsync(entity);
    return Strings.getFragmentBetween(baseTask.block().getStdout(), "BASE_DIR_RESULT:", ":BASE_DIR_RESULT");
}
 
Example 4
Source File: BrooklynAccessUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** attempts to resolve hostnameTarget from origin
 * @return null if it definitively can't be resolved,  
 * best-effort IP address if possible, or blank if we could not run ssh or make sense of the output */
public static String getResolvedAddress(Entity entity, SshMachineLocation origin, String hostnameTarget) {
    ProcessTaskWrapper<Integer> task = SshTasks.newSshExecTaskFactory(origin, "ping -c 1 -t 1 "+hostnameTarget)
        .summary("checking resolution of "+hostnameTarget).allowingNonZeroExitCode().newTask();
    DynamicTasks.queueIfPossible(task).orSubmitAndBlock(entity).getTask().blockUntilEnded();
    if (task.asTask().isError()) {
        log.warn("ping could not be run, at "+entity+" / "+origin+": "+Tasks.getError(task.asTask()));
        return "";
    }
    if (task.getExitCode()==null || task.getExitCode()!=0) {
        if (task.getExitCode()!=null && task.getExitCode()<10) {
            // small number means ping failed to resolve or ping the hostname
            log.debug("not able to resolve "+hostnameTarget+" from "+origin+" for "+entity+" because exit code was "+task.getExitCode());
            return null;
        }
        // large number means ping probably did not run
        log.warn("ping not run as expected, at "+entity+" / "+origin+" (code "+task.getExitCode()+"):\n"+task.getStdout().trim()+" --- "+task.getStderr().trim());
        return "";
    }
    String out = task.getStdout();
    try {
        String line1 = Strings.getFirstLine(out);
        String ip = Strings.getFragmentBetween(line1, "(", ")");
        if (Strings.isNonBlank(ip)) 
            return ip;
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        /* ignore non-parseable output */ 
    }
    if (out.contains("127.0.0.1")) return "127.0.0.1";
    return "";
}
 
Example 5
Source File: SshToolAbstractIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups = {"Integration"})
public void testOutputAsExpected() throws Exception {
    final String CONTENTS = "hello world\n"
        + "bye bye\n";
    execCommands("cat > "+Os.mergePaths(Os.tmp(), "test1")+" << X\n"
        + CONTENTS
        + "X\n");
    String read = execCommands("echo START_FOO", "cat "+Os.mergePaths(Os.tmp(), "test1"), "echo END_FOO");
    log.debug("read back data written, as:\n"+read);
    String contents = Strings.getFragmentBetween(read, "START_FOO", "END_FOO");
    Assert.assertEquals(CONTENTS.trim(), contents.trim());
}