Search in sources :

Example 6 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class DiskImageController method waitForTarget.

private String waitForTarget(final OpsTarget target) throws OpsException {
    // Maybe the config CD isn't yet mounted? Maybe SSH isn't yet started? Not sure yet..
    try {
        log.info("Sleeping in the hope of avoiding (not fully understood) SSH issue");
        Thread.sleep(15000);
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new OpsException("Interrupted", e);
    }
    try {
        return TimeoutPoll.poll(TimeSpan.FIVE_MINUTES, TimeSpan.TEN_SECONDS, new PollFunction<String>() {

            @Override
            public String call() throws Exception {
                try {
                    ProcessExecution execution = target.executeCommand("uname -srp");
                    return execution.getStdOut();
                } catch (ProcessExecutionException e) {
                    log.info("Waiting for machine; got process execution error", e);
                    return null;
                }
            }
        });
    } catch (ExecutionException e) {
        throw new OpsException("Error while waiting for machine", e);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout while waiting for machine", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) OpsException(org.platformlayer.ops.OpsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class OpsTargetBase method getFileHash.

@Override
public Md5Hash getFileHash(File filePath) throws OpsException {
    Command command = Command.build("md5sum {0}", filePath);
    ProcessExecution execution = executeCommandUnchecked(command);
    if (execution.getExitCode() == 1) {
        if (execution.getStdErr().contains("No such file or directory")) {
            return null;
        }
    }
    execution.checkExitCode();
    String stdout = execution.getStdOut();
    // Format is "hash filename"
    String[] items = stdout.split(" ");
    return new Md5Hash(items[0]);
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 8 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class OpsTargetBase method doFind.

protected List<FilesystemInfo> doFind(File path, Integer maxDepth) throws OpsException {
    Command command = maybeSudo("find");
    String[] fields = new String[] { "T@", "s", "m", "u", "g", "n", "p", "l", "y", "d" };
    StringBuilder format = new StringBuilder();
    for (int i = 0; i < fields.length; i++) {
        if (i != 0) {
            format.append("\\t");
        }
        format.append("%");
        format.append(fields[i]);
    }
    format.append("\\n");
    command.addFile(path);
    if (maxDepth != null) {
        command.addLiteral("-maxdepth");
        command.addQuoted(maxDepth.toString());
    }
    command.addLiteral("-printf");
    command.addQuoted(format.toString());
    ProcessExecution execution;
    try {
        execution = executeCommand(command);
    } catch (ProcessExecutionException e) {
        execution = e.getExecution();
        if (execution != null && execution.getExitCode() == 1 && execution.getStdErr().contains("No such file or directory")) {
            return null;
        }
        throw new OpsException("Error executing find command", e);
    }
    List<FilesystemInfo> filesystemInfos = Lists.newArrayList();
    String stdout = execution.getStdOut();
    for (String line : stdout.split("\n")) {
        String[] fieldValues = line.split("\t");
        if (fieldValues.length != fields.length) {
            throw new OpsException("Cannot parse line: " + line);
        }
        FilesystemInfo filesystemInfo = new FilesystemInfo();
        for (int i = 0; i < fieldValues.length; i++) {
            String field = fields[i];
            String fieldValue = fieldValues[i];
            if (field.equals("u")) {
                filesystemInfo.owner = fieldValue;
            } else if (field.equals("g")) {
                filesystemInfo.group = fieldValue;
            } else if (field.equals("n")) {
                filesystemInfo.links = fieldValue;
            } else if (field.equals("m")) {
                filesystemInfo.mode = fieldValue;
            } else if (field.equals("p")) {
                filesystemInfo.name = fieldValue;
            } else if (field.equals("s")) {
                filesystemInfo.size = Long.parseLong(fieldValue);
            } else if (field.equals("y")) {
                filesystemInfo.type = fieldValue;
            } else if (field.equals("l")) {
                if (!Strings.isNullOrEmpty(fieldValue)) {
                    filesystemInfo.symlinkTarget = fieldValue;
                }
            } else if (field.equals("T@")) {
                filesystemInfo.date = fieldValue;
            } else if (field.equals("d")) {
                filesystemInfo.depth = Integer.parseInt(fieldValue);
            } else {
                throw new IllegalStateException();
            }
        }
        filesystemInfos.add(filesystemInfo);
    }
    return filesystemInfos;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException)

Example 9 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class OpsTargetBase method executeCommand.

@Override
public ProcessExecution executeCommand(Command command) throws OpsException {
    log.info("Executing command: " + command.toString());
    if (command.getEnvironment() != null) {
        String s = Joiner.on(",").join(command.getEnvironment().keys());
        log.info("Environment keys set: " + s);
    }
    ProcessExecution execution = executeCommandUnchecked(command);
    if (execution.getExitCode() != 0) {
        throw new ProcessExecutionException("Unexpected exit code from running command.  Command=" + command.toString(), execution);
    }
    return execution;
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException)

Example 10 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class DirectOpenstackDownload method download.

public void download(OpsTarget target, File targetPath, OpenstackCredentials credentials, String containerName, String objectPath) throws OpsException {
    RemoteCurlOpenstackSession session = new RemoteCurlOpenstackSession(target);
    session.authenticate(credentials, false);
    OpenstackStorageClient storageClient = session.getStorageClient();
    RequestBuilder request = storageClient.root().containers().id(containerName).objects().id(objectPath).buildDownloadRequest();
    CurlRequest curlRequest = session.toCurlRequest(request);
    curlRequest.bareRequest = true;
    Command curlCommand = curlRequest.toCommand();
    curlCommand.addLiteral(">");
    curlCommand.addFile(targetPath);
    ProcessExecution execution = target.executeCommand(curlCommand);
// CurlResult curlResult = curlRequest.parseResponse(execution);
//
// int httpResult = curlResult.getHttpResult();
// switch (httpResult) {
// case 200:
// break;
// case 201:
// break;
// default:
// throw new OpsException("Unexpected result code while downloading file: " + httpResult + " Result=" +
// curlResult);
// }
}
Also used : RequestBuilder(org.openstack.client.common.RequestBuilder) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) OpenstackStorageClient(org.openstack.client.storage.OpenstackStorageClient) CurlRequest(org.platformlayer.ops.helpers.CurlRequest)

Aggregations

ProcessExecution (org.platformlayer.ops.process.ProcessExecution)25 Command (org.platformlayer.ops.Command)16 OpsException (org.platformlayer.ops.OpsException)8 ProcessExecutionException (org.platformlayer.ops.process.ProcessExecutionException)8 File (java.io.File)5 OpsTarget (org.platformlayer.ops.OpsTarget)3 Md5Hash (com.fathomdb.hash.Md5Hash)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 Handler (org.platformlayer.ops.Handler)2 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)2 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)2 SshKey (org.platformlayer.ops.helpers.SshKey)2 ImageFormat (org.platformlayer.ops.images.ImageFormat)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Inet6Address (java.net.Inet6Address)1 List (java.util.List)1