Search in sources :

Example 6 with EChainResponse

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

the class ChainActionItem method unzip.

public EChainResponse unzip(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.ZIP);
    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 7 with EChainResponse

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

the class ChainActionItem method scp.

public EChainResponse scp(List<KeyValue> list) throws Exception {
    List<KeyValue> scpList = list;
    KeyValue scpHost = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue scpPort = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PORT).value("22").build());
    KeyValue scpUser = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue scpPassword = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PASSWORD).value("").build());
    KeyValue scpSrc = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue scpDst = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + scpHost.getValue());
    ctx.addToLog(type, "PORT: " + scpPort.getValue());
    ctx.addToLog(type, "User: " + scpUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, scpPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "SRC: " + scpSrc.getValue());
    ctx.addToLog(type, "DST: " + scpDst.getValue());
    SCPClient scpClient = new SCPClient(Host.builder().address(scpHost.getValue()).port(Integer.parseInt(scpPort.getValue())).username(scpUser.getValue()).password(scpPassword.getValue()).build());
    try {
        scpClient.connect();
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to connect to Host " + e2);
        return EChainResponse.Failed;
    }
    FileTransferData data = null;
    // establish transfer
    try {
        if (type == EActionType.SCP_Download) {
            data = scpClient.download(scpDst.getValue(), scpSrc.getValue());
        } else {
            data = scpClient.upload(scpDst.getValue(), scpSrc.getValue());
        }
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to transfer data " + e2);
        data = FileTransferData.builder().build();
    }
    ctx.addToLog(type, "Transfer: " + data.info());
    return EChainResponse.Continue;
}
Also used : SCPClient(com.fo0.robot.connector.SCPClient) KeyValue(com.fo0.robot.model.KeyValue) FileTransferData(com.fo0.robot.model.FileTransferData)

Example 8 with EChainResponse

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

the class ChainActionItem method copy.

public EChainResponse copy(List<KeyValue> list) throws Exception {
    List<KeyValue> zipList = list;
    KeyValue src = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue dst = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().build());
    ctx.addToLog(type, "SRC: " + src.getValue());
    ctx.addToLog(type, "DST: " + dst.getValue());
    // checking first path
    if (!Paths.get(src.getValue()).toAbsolutePath().toFile().exists()) {
        ctx.addToLog(type, "Stopping Chain. Could not find src " + Paths.get(src.getValue()).toAbsolutePath());
        return EChainResponse.Failed;
    }
    FileUtils.copyFile(Paths.get(src.getValue()).toAbsolutePath().toFile(), Paths.get(dst.getValue()).toAbsolutePath().toFile());
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue)

Example 9 with EChainResponse

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

the class ChainPreAction method preCommand.

@Override
public EChainResponse preCommand(ActionContext ctx) throws Exception {
    ActionItem item = ctx.peek().getValue();
    if (!item.isActive()) {
        ctx.addToLog(item.getType(), "is inactive");
        // to remove the item
        ctx.pop();
        return EChainResponse.Skip;
    }
    return EChainResponse.Continue;
}
Also used : ActionItem(com.fo0.robot.model.ActionItem)

Example 10 with EChainResponse

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

the class ChainActionItem method untargz.

public EChainResponse untargz(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.TAR, CompressionType.GZIP);
    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)

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