Search in sources :

Example 16 with KeyValue

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

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

Example 18 with KeyValue

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

the class SSHPatternCheck method parsingSCPInputText.

// $HOST(fo0systems.net) $PORT(4444) $USER(root) $PASSWORD(max123Max123)
// $SRC(/root/test) $DST(/home/max/git/Robot/Robot/test/test)
@Test
public void parsingSCPInputText() {
    String inputText = "$HOST(github.com) " + "$PORT(21) " + "$USER(root) " + "$PASSWORD(example) " + "$SRC(/root/test) " + "$DST(/home/max/git/Robot/Robot/test/test)";
    Pattern pattern = CONSTANTS_PATTERN.BASIC_PATTERN;
    Matcher m = pattern.matcher(inputText);
    List<KeyValue> list = new ArrayList<KeyValue>();
    while (m.find()) {
        list.add(KeyValue.builder().key(m.group(1)).value(m.group(2)).build());
    }
    KeyValue host = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue port = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(null);
    KeyValue user = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue password = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(null);
    KeyValue src = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue dst = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    Assert.assertEquals(host.getValue(), "github.com");
    Assert.assertEquals(port.getValue(), "21");
    Assert.assertEquals(user.getValue(), "root");
    Assert.assertEquals(password.getValue(), "example");
    Assert.assertEquals(src.getValue(), "/root/test");
    Assert.assertEquals(dst.getValue(), "/home/max/git/Robot/Robot/test/test");
}
Also used : Pattern(java.util.regex.Pattern) KeyValue(com.fo0.robot.model.KeyValue) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 19 with KeyValue

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

the class ChainActionItem method tar.

public EChainResponse tar(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.TAR);
    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)

Example 20 with KeyValue

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

the class ChainActionItem method ftp.

public EChainResponse ftp(List<KeyValue> list) throws Exception {
    List<KeyValue> ftpList = list;
    KeyValue ftpHost = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue ftpPort = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key("PORT").value("21").build());
    KeyValue ftpUser = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(KeyValue.builder().key("USER").value("anonymous").build());
    KeyValue ftpPassword = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key("PASSWORD").value("").build());
    KeyValue ftpSrc = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue ftpDst = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + ftpHost.getValue());
    ctx.addToLog(type, "PORT: " + ftpPort.getValue());
    ctx.addToLog(type, "User: " + ftpUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, ftpPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "SRC: " + ftpSrc.getValue());
    ctx.addToLog(type, "DST: " + ftpDst.getValue());
    FTPClient ftpClient = new FTPClient(Host.builder().address(ftpHost.getValue()).port(Integer.parseInt(ftpPort.getValue())).username(ftpUser.getValue()).password(ftpPassword.getValue()).build());
    if (!ftpClient.connect()) {
        ctx.addToLog(type, "failed to connect to Host");
        return EChainResponse.Failed;
    }
    FileTransferData ftpData = ftpClient.download(ftpDst.getValue(), ftpSrc.getValue());
    try {
        ctx.addToLog(type, "Transfer: " + ftpData.info());
    } catch (Exception e2) {
        e2.printStackTrace();
    }
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) FileTransferData(com.fo0.robot.model.FileTransferData) FTPClient(com.fo0.robot.connector.FTPClient)

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