Search in sources :

Example 6 with KeyValue

use of com.fo0.robot.model.KeyValue in project Robot by fo0.

the class ChainActionItem method download.

public EChainResponse download(List<KeyValue> list) throws Exception {
    List<KeyValue> downloads = list;
    KeyValue url = downloads.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue path = downloads.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.DESTINATION).value(FilenameUtils.getName(url.getValue())).build());
    KeyValue timeout = downloads.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.TIMEOUT)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.TIMEOUT).value(String.valueOf(TimeUnit.SECONDS.toMillis(5))).build());
    ctx.addToLog(type, "SRC: " + url);
    ctx.addToLog(type, "DST: " + path);
    ctx.addToLog(type, "TIMEOUT (sec): " + DateFormatUtils.format(Long.valueOf(timeout.getValue()), "s"));
    // create file
    File file = new File(Paths.get(path.getValue()).toAbsolutePath().toString());
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    Stopwatch timer = Stopwatch.createStarted();
    try {
        FileUtils.copyURLToFile(new URL(url.getValue()), file, Integer.valueOf(timeout.getValue()), Integer.valueOf(timeout.getValue()));
        timer.stop();
        ctx.addToLog(type, "Finished Download: " + file.getName() + ", Size: " + FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(file)) + ", Speed: " + Utils.humanReadableBandwith(timer.elapsed(TimeUnit.MILLISECONDS), file.length()));
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to download: " + url.getValue());
    } finally {
        try {
            timer.stop();
        } catch (Exception e3) {
        }
    }
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) Stopwatch(com.google.common.base.Stopwatch) File(java.io.File) URL(java.net.URL)

Example 7 with KeyValue

use of com.fo0.robot.model.KeyValue 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 8 with KeyValue

use of com.fo0.robot.model.KeyValue 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 9 with KeyValue

use of com.fo0.robot.model.KeyValue 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 10 with KeyValue

use of com.fo0.robot.model.KeyValue in project Robot by fo0.

the class TypeZipChainTest method zip.

@Test
public void zip() {
    ActionItem item = ActionItem.builder().type(EActionType.Zip).value("$SRC(/home/max/Schreibtisch/Robot/test/agent.zip) $DST(/home/max/Schreibtisch/Robot/test/x)").description("description example").build();
    List<KeyValue> list = item.parsedValue();
    KeyValue zipFile = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    Assert.assertEquals(CONSTANTS_PATTERN.SOURCE, zipFile.getKey());
    Assert.assertEquals("/home/max/Schreibtisch/Robot/test/agent.zip", zipFile.getValue());
    KeyValue zipDstFolder = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    Assert.assertEquals(CONSTANTS_PATTERN.DESTINATION, zipDstFolder.getKey());
    Assert.assertEquals("/home/max/Schreibtisch/Robot/test/x", zipDstFolder.getValue());
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) ActionItem(com.fo0.robot.model.ActionItem) Test(org.junit.Test)

Aggregations

KeyValue (com.fo0.robot.model.KeyValue)22 File (java.io.File)9 Archiver (org.rauschig.jarchivelib.Archiver)8 Test (org.junit.Test)5 ActionItem (com.fo0.robot.model.ActionItem)3 Commander (com.fo0.robot.commander.Commander)2 FileTransferData (com.fo0.robot.model.FileTransferData)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 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