Search in sources :

Example 6 with TimeSpan

use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.

the class ZookeeperUtils method sendCommand.

public static ZookeeperResponse sendCommand(InetSocketAddress socketAddress, String command) throws IOException {
    TimeSpan connectionTimeout = TimeSpan.TEN_SECONDS;
    Socket s = new Socket();
    s.setTcpNoDelay(true);
    s.setSoTimeout((int) connectionTimeout.getTotalMilliseconds());
    s.connect(socketAddress);
    s.getOutputStream().write(command.getBytes());
    s.getOutputStream().flush();
    // TODO: Timeout?
    String response = IoUtils.readAll(s.getInputStream());
    return new ZookeeperResponse(response);
}
Also used : TimeSpan(com.fathomdb.TimeSpan) Socket(java.net.Socket)

Example 7 with TimeSpan

use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.

the class SshOpsTarget method executeCommandUnchecked.

@Override
protected ProcessExecution executeCommandUnchecked(Command command) throws ProcessExecutionException {
    try {
        String commandString = command.buildCommandString();
        TimeSpan timeout = command.getTimeout();
        if (command.getKeyPair() != null) {
            SshConnection agentConnection = sshConnection.buildAgentConnection(command.getKeyPair());
            try {
                return agentConnection.sshExecute(commandString, timeout);
            } finally {
                agentConnection.close();
            }
        } else {
            return sshConnection.sshExecute(commandString, timeout);
        }
    } catch (IOException e) {
        throw new ProcessExecutionException("Error during command execution", e);
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new ProcessExecutionException("Error during command execution", e);
    } catch (SshException e) {
        throw new ProcessExecutionException("Error during command execution", e);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) SshConnection(org.platformlayer.ops.ssh.SshConnection) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Example 8 with TimeSpan

use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.

the class HttpProxyHelper method chooseProxy.

private String chooseProxy(OpsTarget target, List<String> proxies) {
    String bestProxy = null;
    TimeSpan bestTime = null;
    for (String proxy : proxies) {
        // {
        // // We choose the fastest proxy that gives us a 200 response
        // String url = proxy + "acng-report.html";
        // CurlRequest request = new CurlRequest(url);
        // request.setTimeout(5);
        // try {
        // CurlResult result = request.executeRequest(target);
        // if (result.getHttpResult() != 200) {
        // log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
        // continue;
        // }
        // TimeSpan timeTotal = result.getTimeTotal();
        // if (bestTime == null || timeTotal.isLessThan(bestTime)) {
        // bestProxy = proxy;
        // bestTime = timeTotal;
        // }
        // } catch (ProcessExecutionException e) {
        // log.info("Error while testing proxy: " + proxy, e);
        // }
        // }
        {
            // We choose the fastest proxy that gives us a 200 response
            String url = "http://ftp.debian.org/debian/dists/stable/Release.gpg";
            CurlRequest request = new CurlRequest(url);
            request.proxy = proxy;
            request.timeout = TimeSpan.FIVE_SECONDS;
            try {
                CurlResult result = request.executeRequest(target);
                if (result.getHttpResult() != 200) {
                    log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
                    continue;
                }
                TimeSpan timeTotal = result.getTimeTotal();
                if (bestTime == null || timeTotal.isLessThan(bestTime)) {
                    bestProxy = proxy;
                    bestTime = timeTotal;
                }
            } catch (OpsException e) {
                log.info("Error while testing proxy: " + proxy, e);
            }
        }
    }
    return bestProxy;
}
Also used : TimeSpan(com.fathomdb.TimeSpan) CurlResult(org.platformlayer.ops.helpers.CurlResult) OpsException(org.platformlayer.ops.OpsException) CurlRequest(org.platformlayer.ops.helpers.CurlRequest)

Example 9 with TimeSpan

use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.

the class SchedulerImpl method parseSchedule.

private JobScheduleCalculator parseSchedule(JobSchedule schedule, boolean tolerant) {
    TimeSpan interval = null;
    Date base = null;
    if (schedule == null) {
        if (tolerant) {
            log.warn("Expected schedule; was null");
        } else {
            throw new IllegalArgumentException("Schedule is required");
        }
    } else {
        if (!Strings.isNullOrEmpty(schedule.interval)) {
            try {
                interval = TimeSpan.parse(schedule.interval);
            } catch (IllegalArgumentException e) {
                if (tolerant) {
                    log.warn("Ignoring error parsing interval: " + schedule.interval, e);
                } else {
                    throw new IllegalArgumentException("Invalid interval: " + schedule.interval, e);
                }
            }
        }
        if (schedule.base != null) {
            base = schedule.base;
        }
    }
    if (interval == null) {
        if (tolerant) {
            log.warn("Interval not provided; assuming default");
            interval = TimeSpan.ONE_HOUR;
        } else {
            throw new IllegalArgumentException("Interval is required");
        }
    }
    JobScheduleCalculator scheduleCalculator = new SimpleJobScheduleCalculator(interval, base);
    return scheduleCalculator;
}
Also used : TimeSpan(com.fathomdb.TimeSpan) Date(java.util.Date)

Example 10 with TimeSpan

use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.

the class MinaSshConnection method sshCopyData0.

@Override
protected void sshCopyData0(InputStream fileData, long dataLength, String remoteFile, String mode, boolean sudo) throws IOException, InterruptedException, SshException {
    int lastSlash = remoteFile.lastIndexOf('/');
    if (lastSlash == -1) {
        throw new IllegalArgumentException("Expected dest file to be absolute path: " + remoteFile);
    }
    MinaSshConnectionWrapper sshConnection = ensureConnected();
    MinaScpClient scp = new MinaScpClient(sshConnection);
    String remoteDir = remoteFile.substring(0, lastSlash);
    String filename = remoteFile.substring(lastSlash + 1);
    try {
        TimeSpan timeout = TimeSpan.FIVE_MINUTES;
        scp.put(fileData, dataLength, filename, remoteDir, mode, timeout, sudo);
    } catch (IOException ioException) {
        throw new SshException("Cannot doing scp on file", ioException);
    } catch (RuntimeSshException e) {
        throw new SshException("Error doing scp on file", e);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Aggregations

TimeSpan (com.fathomdb.TimeSpan)10 IOException (java.io.IOException)4 Date (java.util.Date)3 OpsException (org.platformlayer.ops.OpsException)3 SshException (org.platformlayer.ops.ssh.SshException)3 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)2 RetryPolicy (com.netflix.curator.RetryPolicy)1 CuratorFramework (com.netflix.curator.framework.CuratorFramework)1 Builder (com.netflix.curator.framework.CuratorFrameworkFactory.Builder)1 RetryOneTime (com.netflix.curator.retry.RetryOneTime)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Socket (java.net.Socket)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 RepositoryException (org.platformlayer.RepositoryException)1 Action (org.platformlayer.core.model.Action)1 BackupAction (org.platformlayer.core.model.BackupAction)1