Search in sources :

Example 1 with EChainResponse

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

the class ChainActionItem method targz.

public EChainResponse targz(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, CompressionType.GZIP);
    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 2 with EChainResponse

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

the class ChainActionItem method move.

public EChainResponse move(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());
    KeyValue force = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.FORCE)).findFirst().orElse(KeyValue.builder().value("false").build());
    ctx.addToLog(type, "SRC: " + src.getValue());
    ctx.addToLog(type, "DST: " + dst.getValue());
    ctx.addToLog(type, "FORCE: " + force.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.Break;
    }
    if (Paths.get(dst.getValue()).toAbsolutePath().toFile().exists()) {
        if (StringUtils.equalsAnyIgnoreCase(force.getValue(), "true")) {
            // force :: remove destination file
            ctx.addToLog(type, "found destination file or folder. Force option has been found, deleting the destination: " + Paths.get(dst.getValue()).toAbsolutePath());
            FileUtils.forceDelete(Paths.get(dst.getValue()).toAbsolutePath().toFile());
        } else {
            ctx.addToLog(type, "Stopping Chain. Destination File exists already and no force has been set." + Paths.get(dst.getValue()).toAbsolutePath());
            return EChainResponse.Failed;
        }
    }
    FileUtils.moveFile(Paths.get(src.getValue()).toAbsolutePath().toFile(), Paths.get(dst.getValue()).toAbsolutePath().toFile());
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue)

Example 3 with EChainResponse

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

the class ChainActionItem method simple_commandline.

public EChainResponse simple_commandline(List<KeyValue> list) throws Exception {
    List<KeyValue> cmdList = list;
    // @formatter:off
    KeyValue HOME = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.HOME, System.getProperty("user.dir"));
    KeyValue CMD = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.CMD, cmdList.stream().map(KeyValue::getValue).findFirst().orElse(null));
    // @formatter:on
    ctx.addToLog(type, "HOME: " + HOME.getValue());
    ctx.addToLog(type, "CMD: " + CMD.getValue());
    Commander commander = new Commander(log -> {
        ctx.addToLogPlain(type, log);
    });
    commander.execute(true, true, HOME.getValue(), CMD.getValue());
    if (commander == null || commander.isError()) {
        ctx.addToLog(type, "error at commander: " + CMD.getKey());
        return EChainResponse.Failed;
    }
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) Commander(com.fo0.robot.commander.Commander)

Example 4 with EChainResponse

use of com.fo0.robot.chain.EChainResponse 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 5 with EChainResponse

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

the class ChainActionItem method command.

@Override
public EChainResponse command(ActionContext ctx) throws Exception {
    // get latest action
    this.ctx = ctx;
    Entry<Integer, ActionItem> item = ctx.pop();
    // info
    Logger.info("Action[" + item.getKey() + "] " + item.getValue());
    type = item.getValue().getType();
    EChainResponse response = EChainResponse.Break;
    switch(type) {
        case Simple_Commandline:
            response = simple_commandline(item.getValue().parsedValue());
            break;
        case Commandline:
            response = commandline(item.getValue().parsedValue());
            break;
        case Sleep:
            response = sleep(item.getValue().parsedValue());
            break;
        case COPY:
            response = copy(item.getValue().parsedValue());
            break;
        case MOVE:
            response = move(item.getValue().parsedValue());
            break;
        case Download:
            response = download(item.getValue().parsedValue());
            break;
        case Zip:
            response = zip(item.getValue().parsedValue());
            break;
        case Unzip:
            response = unzip(item.getValue().parsedValue());
            break;
        case TAR:
            response = tar(item.getValue().parsedValue());
            break;
        case UNTAR:
            response = untar(item.getValue().parsedValue());
            break;
        case TARGZ:
            response = targz(item.getValue().parsedValue());
            break;
        case UNTARGZ:
            response = untargz(item.getValue().parsedValue());
            break;
        case SEVEN_ZIP:
            response = sevenZip(item.getValue().parsedValue());
            break;
        case UNSEVEN_ZIP:
            response = unSevenZip(item.getValue().parsedValue());
            break;
        case SSH:
            response = ssh(item.getValue().parsedValue());
            break;
        case SCP_Download:
        case SCP_Upload:
            response = scp(item.getValue().parsedValue());
            break;
        case FTP_Download:
            response = ftp(item.getValue().parsedValue());
            break;
        default:
            ctx.addToLog(type, "Currently not implemented, you may check for updates");
            break;
    }
    return response;
}
Also used : EChainResponse(com.fo0.robot.chain.EChainResponse) ActionItem(com.fo0.robot.model.ActionItem)

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