use of com.oracle.bedrock.runtime.remote.Authentication in project oracle-bedrock by coherence-community.
the class SftpDeployer method undeploy.
@Override
public DeployedArtifacts undeploy(DeployedArtifacts deployedArtifacts, Platform platform, Option... deploymentOptions) {
DeployedArtifacts failedArtifacts = new DeployedArtifacts();
if (!(platform instanceof RemotePlatform)) {
throw new IllegalArgumentException("The platform parameter must be an instance of RemotePlatform");
}
JSchSocketFactory socketFactory = new JSchSocketFactory();
RemotePlatform remotePlatform = (RemotePlatform) platform;
String userName = remotePlatform.getUserName();
Authentication authentication = remotePlatform.getAuthentication();
String hostName = remotePlatform.getAddress().getHostName();
int port = remotePlatform.getPort();
// create the deployment options
OptionsByType optionsByType = OptionsByType.empty();
// add the Platform options
optionsByType.addAll(platform.getOptions());
// override with specified Options
optionsByType.addAll(deploymentOptions);
// initially there's no session
Session session = null;
try {
// obtain the connected JSch Session
session = sessionFactory.createSession(hostName, port, userName, authentication, socketFactory, optionsByType);
if (deployedArtifacts.size() > 0) {
ChannelSftp sftpChannel = null;
try (DiagnosticsRecording diagnostics = DiagnosticsRecording.section("Remote Platform")) {
// open an sftp channel that we can use to copy over the artifacts
sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect(session.getTimeout());
for (File file : deployedArtifacts) {
try {
// attempt to delete the file
sftpChannel.rm(file.toString());
// include diagnostics
diagnostics.add(file.toString());
} catch (SftpException exception) {
try {
// attempt to remove the file as a directory
sftpChannel.rmdir(file.toString());
// include diagnostics (directory removed)
diagnostics.add(file.toString() + " (directory)");
} catch (SftpException e) {
// include diagnostics
diagnostics.add(file.toString() + " (failed to undeploy)");
failedArtifacts.add(file);
}
}
}
} finally {
if (sftpChannel != null) {
sftpChannel.disconnect();
}
}
}
} catch (JSchException e) {
throw new RuntimeException("Failed to undeploy application", e);
} finally {
if (session != null) {
session.disconnect();
}
}
return failedArtifacts;
}
use of com.oracle.bedrock.runtime.remote.Authentication in project oracle-bedrock by coherence-community.
the class SftpDeployer method deploy.
@Override
public DeployedArtifacts deploy(List<DeploymentArtifact> artifactsToDeploy, String remoteDirectory, Platform platform, Option... deploymentOptions) {
DeployedArtifacts deployedArtifacts = new DeployedArtifacts();
if (artifactsToDeploy == null || artifactsToDeploy.isEmpty()) {
return deployedArtifacts;
}
if (!(platform instanceof RemotePlatform)) {
throw new IllegalArgumentException("The platform parameter must be an instance of RemotePlatform");
}
Table deploymentTable = new Table();
JSchSocketFactory socketFactory = new JSchSocketFactory();
RemotePlatform remotePlatform = (RemotePlatform) platform;
String userName = remotePlatform.getUserName();
Authentication authentication = remotePlatform.getAuthentication();
String hostName = remotePlatform.getAddress().getHostName();
int port = remotePlatform.getPort();
// Create the deployment options
OptionsByType optionsByType = OptionsByType.empty();
// Add the Platform options
optionsByType.addAll(platform.getOptions());
// Override with specified Options
optionsByType.addAll(deploymentOptions);
// initially there's no session
Session session = null;
try {
// Obtain the connected JSch Session
session = sessionFactory.createSession(hostName, port, userName, authentication, socketFactory, optionsByType);
// ----- deploy remote application artifacts (using sftp) -----
// determine the separators for the platform
PlatformSeparators separators = optionsByType.get(PlatformSeparators.class);
if (artifactsToDeploy.size() > 0) {
ChannelSftp sftpChannel = null;
try {
// open an sftp channel that we can use to copy over the artifacts
sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect(session.getTimeout());
try {
// Obtain the status of the remote directory
sftpChannel.lstat(remoteDirectory);
} catch (SftpException _ignored) {
// the remote directory does not exist so attempt to create it
sftpChannel.mkdir(remoteDirectory);
// add the directory as something to clean up
deployedArtifacts.add(new File(remoteDirectory));
}
// copy deployment artifacts into the remote server
for (DeploymentArtifact artifactToDeploy : artifactsToDeploy) {
// acquire the source file to deploy
File sourceFile = artifactToDeploy.getSourceFile();
// change to the desired remote directory
File destinationFile = artifactToDeploy.getDestinationFile();
String destinationFileName;
if (destinationFile == null) {
sftpChannel.cd(remoteDirectory);
destinationFileName = sourceFile.getName();
// add the file as a deployed artifact
deployedArtifacts.add(new File(remoteDirectory, destinationFileName));
} else {
String destinationFilePath = separators.asPlatformFileName(destinationFile.getParent());
String dirName;
if (destinationFilePath == null) {
dirName = separators.asPlatformFileName(remoteDirectory);
} else {
dirName = separators.asPlatformFileName(destinationFilePath);
}
sftpChannel.cd(dirName);
destinationFileName = destinationFile.getName();
// add the file as a deployed artifact
deployedArtifacts.add(new File(dirName, destinationFileName));
}
// copy the source artifact to the destination file
double start = System.currentTimeMillis();
sftpChannel.put(new FileInputStream(sourceFile), destinationFileName);
double time = (System.currentTimeMillis() - start) / 1000.0d;
deploymentTable.addRow(sourceFile.toString(), String.valueOf(destinationFile), String.format("%.3f s", time));
}
Table diagnosticsTable = optionsByType.get(Table.class);
if (diagnosticsTable != null) {
diagnosticsTable.addRow("Application Deployments ", deploymentTable.toString());
}
} catch (IOException | SftpException e) {
throw new RuntimeException("Failed to deploy application", e);
} finally {
if (sftpChannel != null) {
sftpChannel.disconnect();
}
}
}
} catch (JSchException e) {
throw new RuntimeException("Failed to deploy application", e);
} finally {
if (session != null) {
session.disconnect();
}
}
return deployedArtifacts;
}
use of com.oracle.bedrock.runtime.remote.Authentication in project oracle-bedrock by coherence-community.
the class WindowsRemoteTerminalTest method shouldMakeDirectories.
@Test
public void shouldMakeDirectories() throws Exception {
String userName = "Bob";
Authentication authentication = new Password("secret");
String hostName = "foo.com";
int port = 1234;
OptionsByType optionsByType = OptionsByType.empty();
WindowsSession session = mock(WindowsSession.class);
RemotePlatform platform = mock(RemotePlatform.class);
when(platform.getUserName()).thenReturn(userName);
when(platform.getAuthentication()).thenReturn(authentication);
when(platform.getAddress()).thenReturn(InetAddress.getByName(hostName));
when(platform.getPort()).thenReturn(port);
WindowsRemoteTerminal shell = new WindowsRemoteTerminalStub(platform, session);
shell.makeDirectories("dir1\\dir2", optionsByType);
InOrder inOrder = inOrder(session);
inOrder.verify(session).connect();
inOrder.verify(session).execute(eq("mkdir"), eq(Arrays.asList("dir1\\dir2")), any(InputStream.class), any(OutputStream.class), any(OutputStream.class));
}
Aggregations