Search in sources :

Example 21 with OptionsByType

use of com.oracle.bedrock.OptionsByType 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 22 with OptionsByType

use of com.oracle.bedrock.OptionsByType in project oracle-bedrock by coherence-community.

the class FileShareDeployer method deploy.

@Override
public DeployedArtifacts deploy(List<DeploymentArtifact> artifactsToDeploy, String remoteDirectory, Platform platform, Option... deploymentOptions) {
    DeployedArtifacts deployedArtifacts = new DeployedArtifacts();
    OptionsByType combinedOptions = platform == null ? OptionsByType.empty() : OptionsByType.of(platform.getOptions());
    Table deploymentTable = new Table();
    combinedOptions.addAll(optionsByType);
    combinedOptions.addAll(deploymentOptions);
    PlatformSeparators separators = combinedOptions.get(PlatformSeparators.class);
    File remoteShareFile = new File(remoteShareName);
    for (DeploymentArtifact artifact : artifactsToDeploy) {
        double start = System.currentTimeMillis();
        try {
            File sourceFile = artifact.getSourceFile();
            Path localCopy = new File(localShareName, sourceFile.getName()).toPath();
            Files.copy(artifact.getSourceFile().toPath(), localCopy, StandardCopyOption.REPLACE_EXISTING);
            String destination;
            String sourceName = artifact.getSourceFile().getName();
            File destinationFile = artifact.getDestinationFile();
            if (destinationFile == null) {
                destination = remoteDirectory + separators.getFileSeparator() + sourceName;
            } else {
                String destinationFilePath = separators.asPlatformFileName(destinationFile.getParent());
                String dirName;
                if (destinationFilePath == null) {
                    dirName = separators.asPlatformFileName(remoteDirectory);
                    destination = dirName + separators.getFileSeparator() + destinationFile.getPath();
                } else {
                    destination = separators.asPlatformFileName(destinationFile.getCanonicalPath());
                }
            }
            String source = new File(remoteShareFile, sourceName).getCanonicalPath();
            if (!source.equals(destination)) {
                boolean cleanup = performRemoteCopy(source, destination, platform, combinedOptions);
                if (cleanup) {
                    Files.delete(localCopy);
                }
            }
            // add the file as a deployed artifact
            deployedArtifacts.add(new File(destination));
            double time = (System.currentTimeMillis() - start) / 1000.0d;
            deploymentTable.addRow(sourceFile.toString(), String.valueOf(destination), String.format("%.3f s", time));
        } catch (IOException e) {
            throw new RuntimeException("Failed to deploy " + artifact, e);
        }
    }
    Table diagnosticsTable = optionsByType.get(Table.class);
    if (diagnosticsTable != null) {
        diagnosticsTable.addRow("Application Deployments ", deploymentTable.toString());
    }
    return deployedArtifacts;
}
Also used : Path(java.nio.file.Path) PlatformSeparators(com.oracle.bedrock.runtime.options.PlatformSeparators) Table(com.oracle.bedrock.table.Table) DeployedArtifacts(com.oracle.bedrock.runtime.remote.DeployedArtifacts) IOException(java.io.IOException) OptionsByType(com.oracle.bedrock.OptionsByType) File(java.io.File) DeploymentArtifact(com.oracle.bedrock.runtime.remote.DeploymentArtifact)

Example 23 with OptionsByType

use of com.oracle.bedrock.OptionsByType in project oracle-bedrock by coherence-community.

the class JschRemoteApplicationProcess method waitFor.

@Override
public int waitFor(Option... options) {
    if (exitStatus == null) {
        if (channel == null || session == null) {
            throw new RuntimeException("The remote application has terminated.  No exit status is available");
        } else {
            int status = channel.getExitStatus();
            OptionsByType optionsByType = OptionsByType.of(options);
            Timeout timeout = optionsByType.get(Timeout.class);
            long timeRemaining = timeout.to(TimeUnit.MILLISECONDS);
            while (status == -1 && timeRemaining >= 0) {
                try {
                    // wait a little bit for the channel to exit
                    long waitTime = 500;
                    Thread.sleep(waitTime);
                    timeRemaining = timeRemaining - waitTime;
                    // for partial wait times, wait one cycle
                    if (timeRemaining > 0 && timeRemaining < waitTime) {
                        timeRemaining = waitTime;
                    }
                    status = channel.getExitStatus();
                } catch (InterruptedException e) {
                    throw new RuntimeException("Interrupted while waiting for application to terminate", e);
                }
            }
            exitStatus = status;
        }
    }
    return exitStatus;
}
Also used : Timeout(com.oracle.bedrock.options.Timeout) OptionsByType(com.oracle.bedrock.OptionsByType)

Example 24 with OptionsByType

use of com.oracle.bedrock.OptionsByType in project oracle-bedrock by coherence-community.

the class VagrantPlatform method start.

/**
 * Start this {@link VagrantPlatform}.
 * When this method returns the virtual machine this {@link VagrantPlatform}
 * represents will be in a running state.
 */
public void start() {
    OptionsByType options = getDefaultOptions().add(Argument.of("up"));
    execute(options);
    Properties sshProperties = detectSSH();
    try {
        HostName hostName = getOptions().get(HostName.class);
        // settings configured by Vagrant
        if (hostName == null || hostName.get().isEmpty()) {
            // Important:  At this point all we know is that we can connect to the local loopback
            // NAT'd address so we can SSH into the Vagrant Box.   We don't know what the
            // address of the Vagrant Box is.  It may not even have an address we can access.
            this.address = InetAddress.getLoopbackAddress();
            this.port = Integer.parseInt(sshProperties.getProperty("Port"));
            this.userName = sshProperties.getProperty("User");
            this.authentication = SecureKeys.fromPrivateKeyFile(sshProperties.getProperty("IdentityFile"));
        } else {
            this.address = InetAddress.getByName(hostName.get());
            this.userName = sshProperties.getProperty("User");
            this.authentication = SecureKeys.fromPrivateKeyFile(sshProperties.getProperty("IdentityFile"));
        }
    } catch (UnknownHostException e) {
        throw new RuntimeException("Error setting public InetAddress", e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Properties(java.util.Properties) OptionsByType(com.oracle.bedrock.OptionsByType) HostName(com.oracle.bedrock.runtime.remote.options.HostName)

Example 25 with OptionsByType

use of com.oracle.bedrock.OptionsByType in project oracle-bedrock by coherence-community.

the class AbstractAssembly method clone.

/**
 * Creates the specified number of "clones" of the specified {@link Application}s using the
 * provided override {@link Option}s.
 *
 * @param applications  the {@link Application}s to clone
 * @param count         the number of clones of each application
 * @param options       the override {@link Option}s
 */
protected void clone(List<? extends A> applications, int count, Option... options) {
    OptionsByType optionsByType = OptionsByType.of(options);
    boolean definesCustomDiscriminator = optionsByType.contains(Discriminator.class);
    // clone each application the desired amount
    applications.forEach(application -> {
        // obtain some information about the application
        Platform platform = application.getPlatform();
        OptionsByType launchOptions = OptionsByType.of(application.getOptions()).addAll(options);
        // remove the existing Discriminator when a custom one hasn't been provided
        if (!definesCustomDiscriminator) {
            launchOptions.remove(Discriminator.class);
        }
        // we'll create the same class of application
        Class<A> applicationClass = (Class<A>) application.getClass();
        // expand the assembly by the desired amount
        expand(count, platform, applicationClass, launchOptions.asArray());
    });
}
Also used : OptionsByType(com.oracle.bedrock.OptionsByType)

Aggregations

OptionsByType (com.oracle.bedrock.OptionsByType)171 Test (org.junit.Test)102 Platform (com.oracle.bedrock.runtime.Platform)56 LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)30 Arguments (com.oracle.bedrock.runtime.options.Arguments)25 MetaClass (com.oracle.bedrock.runtime.MetaClass)24 Properties (java.util.Properties)19 File (java.io.File)18 Application (com.oracle.bedrock.runtime.Application)13 Option (com.oracle.bedrock.Option)12 DeployedArtifacts (com.oracle.bedrock.runtime.remote.DeployedArtifacts)10 DeploymentArtifact (com.oracle.bedrock.runtime.remote.DeploymentArtifact)10 EnvironmentVariables (com.oracle.bedrock.runtime.options.EnvironmentVariables)9 RemotePlatform (com.oracle.bedrock.runtime.remote.RemotePlatform)9 Session (com.jcraft.jsch.Session)8 Timeout (com.oracle.bedrock.options.Timeout)8 DisplayName (com.oracle.bedrock.runtime.options.DisplayName)8 SimpleJUnitTestRun (com.oracle.bedrock.testsupport.junit.SimpleJUnitTestRun)8 ArrayList (java.util.ArrayList)8 AbstractRemoteTest (com.oracle.bedrock.runtime.remote.AbstractRemoteTest)7