Search in sources :

Example 11 with EChainResponse

use of com.fo0.robot.chain.EChainResponse in project Robot by fo0.

the class ChainActionItem method sleep.

private EChainResponse sleep(List<KeyValue> list) {
    List<KeyValue> zipList = list;
    KeyValue item = zipList.stream().findFirst().orElse(null);
    ctx.addToLog(type, "Sleep (ms): " + item.getValue());
    ctx.addToLog(type, "start sleep (ms): " + item.getValue());
    Utils.sleep(Long.valueOf(item.getValue()));
    ctx.addToLog(type, "stopped sleep (ms): " + item.getValue());
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue)

Example 12 with EChainResponse

use of com.fo0.robot.chain.EChainResponse in project Robot by fo0.

the class ChainActionItem method ssh.

public EChainResponse ssh(List<KeyValue> list) throws Exception {
    List<KeyValue> sshList = list;
    KeyValue sshHost = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue sshPort = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key("$PORT").value("22").build());
    KeyValue sshUser = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue sshPassword = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PASSWORD).value("").build());
    KeyValue sshCmd = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.CMD)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + sshHost.getValue());
    ctx.addToLog(type, "PORT: " + sshPort.getValue());
    ctx.addToLog(type, "User: " + sshUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, sshPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "CMD: " + sshCmd.getValue());
    SimpleSSHClient sshClient = new SimpleSSHClient(Host.builder().address(sshHost.getValue()).port(Integer.parseInt(sshPort.getValue())).username(sshUser.getValue()).password(sshPassword.getValue()).build());
    try {
        sshClient.connect();
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to connect to Host " + e2);
        return EChainResponse.Failed;
    }
    sshClient.command(sshCmd.getValue(), out -> {
        ctx.addToLogPlain(type, out);
    }, error -> {
        ctx.addToLogPlain(type, error);
    });
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) SimpleSSHClient(com.fo0.robot.connector.SimpleSSHClient)

Example 13 with EChainResponse

use of com.fo0.robot.chain.EChainResponse in project Robot by fo0.

the class ChainActionItem method unSevenZip.

public EChainResponse unSevenZip(List<KeyValue> list) throws Exception {
    List<KeyValue> unzipList = list;
    KeyValue unzipSrc = unzipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue unzipDst = unzipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().build());
    ctx.addToLog(type, "SRC: " + unzipSrc.getValue());
    ctx.addToLog(type, "DST: " + unzipDst.getValue());
    Archiver unzipArchive = ArchiverFactory.createArchiver(ArchiveFormat.SEVEN_Z);
    unzipArchive.extract(new File(unzipSrc.getValue()), new File(unzipDst.getValue()));
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) Archiver(org.rauschig.jarchivelib.Archiver) File(java.io.File)

Example 14 with EChainResponse

use of com.fo0.robot.chain.EChainResponse in project Robot by fo0.

the class ChainActionItem method commandline.

public EChainResponse commandline(List<KeyValue> list) throws Exception {
    List<KeyValue> cmdList = list;
    // @formatter:off
    KeyValue WAIT = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.WAIT, "true");
    KeyValue HOME = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.HOME, System.getProperty("user.dir"));
    KeyValue CMDS = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.CMDS, null);
    // formatter:on
    ctx.addToLog(type, "WAIT: " + WAIT.getValue());
    ctx.addToLog(type, "HOME: " + HOME.getValue());
    ctx.addToLog(type, "CMDs: " + CMDS.getValue());
    Commander commander = new Commander(log -> {
        ctx.addToLogPlain(type, log);
    });
    List<String> commands = Arrays.asList(StringUtils.split(CMDS.getValue(), ","));
    // @formatter:off
    commander.execute(Boolean.valueOf(WAIT.getValue()), false, HOME.getValue(), true, commands);
    if (commander == null || commander.isError()) {
        ctx.addToLog(type, "error at commander: " + CMDS.getKey());
        return EChainResponse.Failed;
    }
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) Commander(com.fo0.robot.commander.Commander)

Example 15 with EChainResponse

use of com.fo0.robot.chain.EChainResponse in project Robot by fo0.

the class ChainActionItem method zip.

public EChainResponse zip(List<KeyValue> list) throws Exception {
    List<KeyValue> zipList = list;
    KeyValue zipSrc = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue zipDest = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().build());
    ctx.addToLog(type, "SRC: " + zipSrc.getValue());
    ctx.addToLog(type, "DST: " + zipDest.getValue());
    // old method
    // ZipUtils.zip(zipSrc.getValue(), zipDest.getValue());
    Archiver zipArchive = ArchiverFactory.createArchiver(ArchiveFormat.ZIP);
    zipArchive.create(new File(zipDest.getValue()).getName(), new File(zipDest.getValue()).getParentFile(), new File(zipSrc.getValue()));
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) Archiver(org.rauschig.jarchivelib.Archiver) File(java.io.File)

Aggregations

KeyValue (com.fo0.robot.model.KeyValue)17 File (java.io.File)9 Archiver (org.rauschig.jarchivelib.Archiver)8 Commander (com.fo0.robot.commander.Commander)2 ActionItem (com.fo0.robot.model.ActionItem)2 FileTransferData (com.fo0.robot.model.FileTransferData)2 EChainResponse (com.fo0.robot.chain.EChainResponse)1 FTPClient (com.fo0.robot.connector.FTPClient)1 SCPClient (com.fo0.robot.connector.SCPClient)1 SimpleSSHClient (com.fo0.robot.connector.SimpleSSHClient)1 Stopwatch (com.google.common.base.Stopwatch)1 URL (java.net.URL)1