Search in sources :

Example 1 with Authentication

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;
}
Also used : DiagnosticsRecording(com.oracle.bedrock.diagnostics.DiagnosticsRecording) JSchException(com.jcraft.jsch.JSchException) DeployedArtifacts(com.oracle.bedrock.runtime.remote.DeployedArtifacts) SftpException(com.jcraft.jsch.SftpException) ChannelSftp(com.jcraft.jsch.ChannelSftp) Authentication(com.oracle.bedrock.runtime.remote.Authentication) OptionsByType(com.oracle.bedrock.OptionsByType) File(java.io.File) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) Session(com.jcraft.jsch.Session)

Example 2 with Authentication

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;
}
Also used : JSchException(com.jcraft.jsch.JSchException) PlatformSeparators(com.oracle.bedrock.runtime.options.PlatformSeparators) Table(com.oracle.bedrock.table.Table) DeployedArtifacts(com.oracle.bedrock.runtime.remote.DeployedArtifacts) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ChannelSftp(com.jcraft.jsch.ChannelSftp) Authentication(com.oracle.bedrock.runtime.remote.Authentication) OptionsByType(com.oracle.bedrock.OptionsByType) File(java.io.File) DeploymentArtifact(com.oracle.bedrock.runtime.remote.DeploymentArtifact) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) Session(com.jcraft.jsch.Session)

Example 3 with Authentication

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));
}
Also used : InOrder(org.mockito.InOrder) HttpBasedAuthentication(com.oracle.bedrock.runtime.remote.http.HttpBasedAuthentication) Authentication(com.oracle.bedrock.runtime.remote.Authentication) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OptionsByType(com.oracle.bedrock.OptionsByType) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) Password(com.oracle.bedrock.runtime.remote.Password) Test(org.junit.Test)

Aggregations

OptionsByType (com.oracle.bedrock.OptionsByType)3 Authentication (com.oracle.bedrock.runtime.remote.Authentication)3 RemotePlatform (com.oracle.bedrock.runtime.remote.RemotePlatform)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 JSchException (com.jcraft.jsch.JSchException)2 Session (com.jcraft.jsch.Session)2 SftpException (com.jcraft.jsch.SftpException)2 DeployedArtifacts (com.oracle.bedrock.runtime.remote.DeployedArtifacts)2 File (java.io.File)2 DiagnosticsRecording (com.oracle.bedrock.diagnostics.DiagnosticsRecording)1 PlatformSeparators (com.oracle.bedrock.runtime.options.PlatformSeparators)1 DeploymentArtifact (com.oracle.bedrock.runtime.remote.DeploymentArtifact)1 Password (com.oracle.bedrock.runtime.remote.Password)1 HttpBasedAuthentication (com.oracle.bedrock.runtime.remote.http.HttpBasedAuthentication)1 Table (com.oracle.bedrock.table.Table)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Test (org.junit.Test)1