Search in sources :

Example 11 with KeyValue

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

the class TypeZipChainTest method unzip.

@Test
public void unzip() {
    ActionItem item = ActionItem.builder().type(EActionType.Unzip).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)

Example 12 with KeyValue

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

Example 13 with KeyValue

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

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

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

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