Search in sources :

Example 1 with RemoteApplicationProcess

use of com.oracle.bedrock.runtime.remote.RemoteApplicationProcess in project oracle-bedrock by coherence-community.

the class JSchRemoteTerminal method launch.

@Override
public RemoteApplicationProcess launch(Launchable launchable, Class<? extends Application> applicationClass, OptionsByType optionsByType) {
    // acquire the remote platform on which to launch the application
    RemotePlatform platform = getRemotePlatform();
    // establish a specialized SocketFactory for JSch
    JSchSocketFactory socketFactory = new JSchSocketFactory();
    // initially there's no session
    Session session = null;
    try {
        // create the remote session
        session = sessionFactory.createSession(platform.getAddress().getHostName(), platform.getPort(), platform.getUserName(), platform.getAuthentication(), socketFactory, optionsByType);
        ChannelExec execChannel = (ChannelExec) session.openChannel("exec");
        // (re)define the "local.address" variable so that we can use for resolving the platform
        optionsByType.add(Variable.with("local.address", socketFactory.getLastLocalAddress().getHostAddress()));
        // ----- establish the remote environment variables -----
        String environmentVariables = "";
        // get the remote environment variables for the remote application
        Properties variables = launchable.getEnvironmentVariables(platform, optionsByType);
        // determine the format to use for setting variables
        String format;
        Shell shell = optionsByType.getOrSetDefault(Shell.class, Shell.isUnknown());
        switch(shell.getType()) {
            case SH:
            case BASH:
                format = "export %s=%s ; ";
                break;
            case CSH:
            case TSCH:
                format = "setenv %s %s ; ";
                break;
            default:
                // when we don't know, assume something bash-like
                format = "export %s=%s ; ";
                break;
        }
        List<String> arguments = launchable.getCommandLineArguments(platform, optionsByType);
        CommandInterceptor interceptor = optionsByType.get(CommandInterceptor.class);
        String executableName = launchable.getCommandToExecute(platform, optionsByType);
        File workingDirectory = optionsByType.get(WorkingDirectory.class).resolve(platform, optionsByType);
        String remoteCommand;
        if (interceptor == null) {
            for (String variableName : variables.stringPropertyNames()) {
                environmentVariables += String.format(format, variableName, StringHelper.doubleQuoteIfNecessary(variables.getProperty(variableName)));
            }
            // ----- establish the application command line to execute -----
            // determine the command to execute remotely
            StringBuilder command = new StringBuilder(executableName);
            // add the arguments
            for (String arg : arguments) {
                command.append(" ").append(arg);
            }
            // the actual remote command must include changing to the remote directory
            remoteCommand = environmentVariables + String.format("cd %s ; %s", workingDirectory, command);
        } else {
            remoteCommand = interceptor.onExecute(executableName, arguments, variables, workingDirectory);
        }
        execChannel.setCommand(remoteCommand);
        // ----- establish the remote application process to represent the remote application -----
        // establish a RemoteApplicationProcess representing the remote application
        RemoteApplicationProcess process = new JschRemoteApplicationProcess(session, execChannel);
        if (optionsByType.get(LaunchLogging.class).isEnabled()) {
            Table diagnosticsTable = optionsByType.get(Table.class);
            if (diagnosticsTable != null && LOGGER.isLoggable(Level.INFO)) {
                diagnosticsTable.addRow("Application Executable ", executableName);
                LOGGER.log(Level.INFO, "Oracle Bedrock " + Bedrock.getVersion() + ": Starting Application...\n" + "------------------------------------------------------------------------\n" + diagnosticsTable.toString() + "\n" + "------------------------------------------------------------------------\n");
            }
        }
        // connect the channel
        execChannel.connect(session.getTimeout());
        return process;
    } catch (JSchException e) {
        if (session != null) {
            session.disconnect();
        }
        throw new RuntimeException("Failed to create remote application", e);
    }
}
Also used : RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) JSchException(com.jcraft.jsch.JSchException) WorkingDirectory(com.oracle.bedrock.runtime.options.WorkingDirectory) Table(com.oracle.bedrock.table.Table) CommandInterceptor(com.oracle.bedrock.runtime.options.CommandInterceptor) Properties(java.util.Properties) ChannelExec(com.jcraft.jsch.ChannelExec) Shell(com.oracle.bedrock.runtime.options.Shell) LaunchLogging(com.oracle.bedrock.options.LaunchLogging) File(java.io.File) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) Session(com.jcraft.jsch.Session)

Example 2 with RemoteApplicationProcess

use of com.oracle.bedrock.runtime.remote.RemoteApplicationProcess in project oracle-bedrock by coherence-community.

the class JSchRemoteTerminal method moveFile.

public void moveFile(String source, String destination, OptionsByType optionsByType) {
    Session session = null;
    try {
        // acquire the remote platform
        RemotePlatform platform = getRemotePlatform();
        // establish a specialized SocketFactory for JSch
        JSchSocketFactory socketFactory = new JSchSocketFactory();
        // create the remote session
        session = sessionFactory.createSession(platform.getAddress().getHostName(), platform.getPort(), platform.getUserName(), platform.getAuthentication(), socketFactory, optionsByType);
        ChannelExec execChannel = (ChannelExec) session.openChannel("exec");
        String moveCommand = String.format("mv %s %s", source, destination);
        execChannel.setCommand(moveCommand);
        RemoteApplicationProcess process = new JschRemoteApplicationProcess(session, execChannel);
        execChannel.connect(session.getTimeout());
        process.waitFor();
    } catch (Exception e) {
        throw new RuntimeException("Error moving file from " + source + " to " + destination, e);
    } finally {
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) ChannelExec(com.jcraft.jsch.ChannelExec) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 3 with RemoteApplicationProcess

use of com.oracle.bedrock.runtime.remote.RemoteApplicationProcess in project oracle-bedrock by coherence-community.

the class DockerRemoteTerminal method launch.

@Override
public RemoteApplicationProcess launch(Launchable launchable, Class<? extends Application> applicationClass, OptionsByType optionsByType) {
    String imageTag = UUID.randomUUID().toString();
    String containerName = UUID.randomUUID().toString();
    Docker docker = optionsByType.get(Docker.class);
    String baseImage = docker.getBaseImage(applicationClass);
    if (baseImage == null || baseImage.trim().isEmpty()) {
        throw new RuntimeException("Cannot find a suitable base image for application class " + applicationClass);
    }
    try {
        InetAddress localAddress = docker.getValidLocalAddress();
        optionsByType.add(Variable.with("local.address", localAddress.getHostAddress()));
        Files.createDirectories(tmpFolder.toPath());
        // Write the Dockerfile
        File dockerFile = writeDockerFile(launchable, baseImage, optionsByType);
        // build image
        DockerImage image = createImage(imageTag, dockerFile, docker, optionsByType);
        // run the container
        ApplicationProcess containerProcess = runContainer(containerName, launchable, image, docker, optionsByType);
        if (containerProcess instanceof RemoteApplicationProcess) {
            return (RemoteApplicationProcess) containerProcess;
        }
        return new WrapperRemoteApplicationProcess(containerProcess);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "An error occurred. Attempting to kill and remove container " + containerName + " and remove image " + imageTag);
        safelyRemoveContainer(containerName, docker);
        safelyRemoveImage(imageTag, docker);
        throw new RuntimeException("An error occurred launching the application inside Docker", e);
    } finally {
        FileHelper.recursiveDelete(tmpFolder);
    }
}
Also used : RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) ApplicationProcess(com.oracle.bedrock.runtime.ApplicationProcess) RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) InetAddress(java.net.InetAddress) File(java.io.File) IOException(java.io.IOException)

Example 4 with RemoteApplicationProcess

use of com.oracle.bedrock.runtime.remote.RemoteApplicationProcess in project oracle-bedrock by coherence-community.

the class JSchRemoteTerminal method makeDirectories.

@Override
public void makeDirectories(String directoryName, OptionsByType optionsByType) {
    Session session = null;
    try {
        // acquire the remote platform
        RemotePlatform platform = getRemotePlatform();
        // establish a specialized SocketFactory for JSch
        JSchSocketFactory socketFactory = new JSchSocketFactory();
        // create the remote session
        session = sessionFactory.createSession(platform.getAddress().getHostName(), platform.getPort(), platform.getUserName(), platform.getAuthentication(), socketFactory, optionsByType);
        ChannelExec execChannel = (ChannelExec) session.openChannel("exec");
        execChannel.setCommand("mkdir -p " + directoryName);
        RemoteApplicationProcess process = new JschRemoteApplicationProcess(session, execChannel);
        execChannel.connect(session.getTimeout());
        process.waitFor();
    } catch (Exception e) {
        throw new RuntimeException("Error creating remote directories " + directoryName, e);
    } finally {
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : RemoteApplicationProcess(com.oracle.bedrock.runtime.remote.RemoteApplicationProcess) RemotePlatform(com.oracle.bedrock.runtime.remote.RemotePlatform) ChannelExec(com.jcraft.jsch.ChannelExec) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Aggregations

RemoteApplicationProcess (com.oracle.bedrock.runtime.remote.RemoteApplicationProcess)4 ChannelExec (com.jcraft.jsch.ChannelExec)3 JSchException (com.jcraft.jsch.JSchException)3 Session (com.jcraft.jsch.Session)3 RemotePlatform (com.oracle.bedrock.runtime.remote.RemotePlatform)3 File (java.io.File)2 LaunchLogging (com.oracle.bedrock.options.LaunchLogging)1 ApplicationProcess (com.oracle.bedrock.runtime.ApplicationProcess)1 CommandInterceptor (com.oracle.bedrock.runtime.options.CommandInterceptor)1 Shell (com.oracle.bedrock.runtime.options.Shell)1 WorkingDirectory (com.oracle.bedrock.runtime.options.WorkingDirectory)1 Table (com.oracle.bedrock.table.Table)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 Properties (java.util.Properties)1