Search in sources :

Example 21 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class SshAgentPeerToPeerCopy method copy.

@Override
public void copy(final OpsTarget src, final File srcFile, final OpsTarget dest, final File finalDestFile) throws OpsException {
    File tempDir = dest.createTempDir();
    try {
        final File tempDest = new File(tempDir, finalDestFile.getName());
        {
            SshOpsTarget srcSshOpsTarget = (SshOpsTarget) src;
            String sshSrc = srcSshOpsTarget.getUsername() + "@";
            InetAddress srcHost = srcSshOpsTarget.getHost();
            if (InetAddressUtils.isIpv6(srcHost)) {
                sshSrc += "[" + InetAddresses.toAddrString(srcHost) + "]";
            } else {
                sshSrc += InetAddresses.toAddrString(srcHost);
            }
            sshSrc += ":" + srcFile.getAbsolutePath();
            Command pullCommand = Command.build("scp -o StrictHostKeyChecking=no {0} {1}", sshSrc, tempDest);
            pullCommand.setKeyPair(srcSshOpsTarget.getKeyPair());
            dest.executeCommand(pullCommand.setTimeout(TimeSpan.TEN_MINUTES));
            Md5Hash targetHash = dest.getFileHash(tempDest);
            Md5Hash srcHash = src.getFileHash(srcFile);
            if (!Objects.equal(srcHash, targetHash)) {
                dest.rm(tempDest);
                throw new OpsException("Files did not match after transfer");
            }
        }
        dest.mv(tempDest, finalDestFile);
    } finally {
        dest.rmdir(tempDir);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) File(java.io.File) InetAddress(java.net.InetAddress) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 22 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class FilesystemCasStore method copyTo.

void copyTo(FilesystemCasObject src, OpsTarget target, File targetFilePath, boolean cacheOnTarget) throws OpsException {
    File fileOnTarget;
    if (!host.isSameMachine(target)) {
        File downloadTo;
        if (cacheOnTarget) {
            // Copy to host cache
            File cachePath = new File(PATH_CACHE, toRelativePath(src.getHash(), 2));
            target.mkdir(cachePath.getParentFile());
            downloadTo = cachePath;
        } else {
            downloadTo = targetFilePath;
        }
        PeerToPeerCopy peerToPeerCopy = Injection.getInstance(PeerToPeerCopy.class);
        peerToPeerCopy.copy(host, src.getPath(), target, downloadTo);
        fileOnTarget = downloadTo;
    } else {
        fileOnTarget = src.getPath();
    }
    if (!fileOnTarget.equals(targetFilePath)) {
        Command copy = Command.build("cp {0} {1}", fileOnTarget, targetFilePath);
        target.executeCommand(copy);
    } else {
        log.info("File is in destination path: " + fileOnTarget);
    }
}
Also used : PeerToPeerCopy(org.platformlayer.ops.images.direct.PeerToPeerCopy) Command(org.platformlayer.ops.Command) File(java.io.File)

Example 23 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class JenkinsCasObject method copyTo0.

@Override
public void copyTo0(OpsTarget target, File remoteFilePath) throws OpsException {
    InetAddress host;
    try {
        host = InetAddress.getByName(uri.getHost());
    } catch (UnknownHostException e) {
        throw new OpsException("Unable to resolve host: " + uri, e);
    }
    if (InetAddressUtils.isPublic(host)) {
        CurlRequest curlRequest = new CurlRequest(uri);
        curlRequest.bareRequest = true;
        CommandEnvironment commandEnvironment = httpProxies.getHttpProxyEnvironment(target, Usage.General, uri);
        Command curlCommand = curlRequest.toCommand();
        curlCommand.addLiteral(">");
        curlCommand.addFile(remoteFilePath);
        curlCommand.setEnvironment(commandEnvironment);
        curlCommand.setTimeout(TimeSpan.FIVE_MINUTES);
        target.executeCommand(curlCommand);
    } else {
        log.warn("Address was not public: " + host + ", doing copy via ops system");
        File tempFile;
        try {
            tempFile = File.createTempFile("jenkins", "dat");
        } catch (IOException e) {
            throw new OpsException("Error creating temp file", e);
        }
        try {
            InputStream is = uri.toURL().openStream();
            try {
                IoUtils.copyStream(is, tempFile);
            } finally {
                Closeables.closeQuietly(is);
            }
            FileUpload.upload(target, remoteFilePath, tempFile);
        } catch (IOException e) {
            throw new OpsException("Error copying jenkins artifact", e);
        } finally {
            tempFile.delete();
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) UnknownHostException(java.net.UnknownHostException) Command(org.platformlayer.ops.Command) InputStream(java.io.InputStream) CurlRequest(org.platformlayer.ops.helpers.CurlRequest) CommandEnvironment(org.platformlayer.ops.CommandEnvironment) IOException(java.io.IOException) InetAddress(java.net.InetAddress) File(java.io.File)

Example 24 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class SshPostgresTarget method execute.

public ProcessExecution execute(String sql, String maskedSql) throws OpsException {
    OpsTarget target = getOpsTarget();
    // We probably want psql -A -t -c "command"
    Command command = Command.build("psql");
    command.addQuoted("--username=", username);
    command.addQuoted("--host=", hostname);
    command.addArgument(Argument.buildQuoted("--command=", sql).setMasked("--command=" + Command.MASKED));
    CommandEnvironment env = new CommandEnvironment();
    env.put("PGPASSWORD", password.plaintext());
    command.setEnvironment(env);
    return target.executeCommand(command);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) CommandEnvironment(org.platformlayer.ops.CommandEnvironment)

Example 25 with Command

use of org.platformlayer.ops.Command in project platformlayer by platformlayer.

the class ExpandArchive method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        target.mkdir(extractPath);
        String archiveFileName = archiveFile.getName();
        if (archiveFileName.endsWith(".zip")) {
            // -u = update, for (something close to) idempotency
            // -o = overwrite (no prompt)
            Command unzipCommand = Command.build("unzip -u -o {0} -d {1}", archiveFile, extractPath);
            target.executeCommand(unzipCommand.setTimeout(TimeSpan.FIVE_MINUTES));
        } else if (archiveFileName.endsWith(".tgz") || archiveFileName.endsWith(".tar.gz")) {
            Command unzipCommand = Command.build("cd {0}; tar zxf {1}", extractPath, archiveFile);
            target.executeCommand(unzipCommand.setTimeout(TimeSpan.FIVE_MINUTES));
        } else {
            throw new UnsupportedOperationException();
        }
    }
}
Also used : Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Aggregations

Command (org.platformlayer.ops.Command)72 File (java.io.File)21 Handler (org.platformlayer.ops.Handler)17 OpsException (org.platformlayer.ops.OpsException)16 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)16 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)11 OpsTarget (org.platformlayer.ops.OpsTarget)9 InetAddress (java.net.InetAddress)4 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)4 Md5Hash (com.fathomdb.hash.Md5Hash)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Map (java.util.Map)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ImageFormat (org.platformlayer.ops.images.ImageFormat)3 List (java.util.List)2 Properties (java.util.Properties)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 AddressModel (org.platformlayer.core.model.AddressModel)2 Tag (org.platformlayer.core.model.Tag)2