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);
}
}
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);
}
}
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();
}
}
}
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);
}
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();
}
}
}
Aggregations