Search in sources :

Example 36 with Session

use of com.jcraft.jsch.Session in project azure-tools-for-java by Microsoft.

the class SparkBatchDebugSession method factory.

/**
     * Create a SparkBatchDebugSession instance for specified host and user
     *
     * @param host The SSH host
     * @param user The SSH user
     * @return an SparkBatchDebugSession instance
     * @throws JSchException JSch operation exceptions
     */
public static SparkBatchDebugSession factory(String host, String user) throws JSchException {
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host);
    java.util.Properties config = new java.util.Properties() {

        {
            put("StrictHostKeyChecking", "no");
        }
    };
    session.setConfig(config);
    return new SparkBatchDebugSession(jsch, session);
}
Also used : JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 37 with Session

use of com.jcraft.jsch.Session in project azure-tools-for-java by Microsoft.

the class SparkSubmitHelper method sftpFileToEmulator.

public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            URL url = new URL(sshEndpoint);
            String host = url.getHost();
            int port = url.getPort();
            JSch jsch = new JSch();
            Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            String[] folders = folderPath.split("/");
            for (String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (SftpException e) {
                        channel.mkdir(folder);
                        channel.cd(folder);
                    }
                }
            }
            channel.put(bufferedInputStream, file.getName());
            channel.disconnect();
            session.disconnect();
            return file.getName();
        }
    }
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 38 with Session

use of com.jcraft.jsch.Session in project azure-tools-for-java by Microsoft.

the class AzureSelectDockerWizardDialog method deploy.

public String deploy() {
    AzureDockerImageInstance dockerImageInstance = model.getDockerImageDescription();
    AzureDockerPreferredSettings dockerPreferredSettings = model.getDockerHostsManager().getDockerPreferredSettings();
    if (dockerPreferredSettings == null) {
        dockerPreferredSettings = new AzureDockerPreferredSettings();
    }
    dockerPreferredSettings.dockerApiName = dockerImageInstance.host.apiUrl;
    dockerPreferredSettings.dockerfileOption = dockerImageInstance.predefinedDockerfile;
    dockerPreferredSettings.region = dockerImageInstance.host.hostVM.region;
    dockerPreferredSettings.vmSize = dockerImageInstance.host.hostVM.vmSize;
    dockerPreferredSettings.vmOS = dockerImageInstance.host.hostOSType.name();
    model.getDockerHostsManager().setDockerPreferredSettings(dockerPreferredSettings);
    DefaultLoader.getIdeHelper().runInBackground(model.getProject(), "Deploying Docker Container on Azure", false, true, "Deploying Web app to a Docker host on Azure...", new Runnable() {

        @Override
        public void run() {
            try {
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        AzureDockerImageInstance dockerImageInstance = model.getDockerImageDescription();
                        if (!dockerImageInstance.hasNewDockerHost) {
                            Session session = null;
                            do {
                                try {
                                    // check if the Docker host is accessible
                                    session = AzureDockerSSHOps.createLoginInstance(dockerImageInstance.host);
                                } catch (Exception e) {
                                    session = null;
                                }
                                if (session == null) {
                                    EditableDockerHost editableDockerHost = new EditableDockerHost(dockerImageInstance.host);
                                    AzureInputDockerLoginCredsDialog loginCredsDialog = new AzureInputDockerLoginCredsDialog(model.getProject(), editableDockerHost, model.getDockerHostsManager(), false);
                                    loginCredsDialog.show();
                                    if (loginCredsDialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
                                        // Update Docker host log in credentials
                                        DockerHost dockerHost = model.getDockerHostsManager().getDockerHostForURL(dockerImageInstance.host.apiUrl);
                                        dockerHost.certVault = editableDockerHost.updatedDockerHost.certVault;
                                        dockerHost.hasPwdLogIn = editableDockerHost.updatedDockerHost.hasPwdLogIn;
                                        dockerHost.hasSSHLogIn = editableDockerHost.updatedDockerHost.hasSSHLogIn;
                                        dockerImageInstance.host = dockerHost;
                                    //                    AzureDockerVMOps.updateDockerHostVM(model.getDockerHostsManager().getSubscriptionsMap().get(model.getDockerImageDescription().sid).azureClient, editableDockerHost.updatedDockerHost);
                                    } else {
                                        return;
                                    }
                                }
                            } while (session == null);
                        }
                        Azure azureClient = model.getDockerHostsManager().getSubscriptionsMap().get(model.getDockerImageDescription().sid).azureClient;
                        DockerContainerDeployTask task = new DockerContainerDeployTask(model.getProject(), azureClient, model.getDockerImageDescription());
                        task.queue();
                        // Update caches here
                        if (onCreate != null) {
                            onCreate.run();
                        }
                    }
                });
            } catch (Exception e) {
                String msg = "An error occurred while attempting to deploy to the selected Docker host." + "\n" + e.getMessage();
                PluginUtil.displayErrorDialogInAWTAndLog("Failed to Deploy Web App as Docker Container", msg, e);
            }
        }
    });
    return AzureDockerUtils.getUrl(dockerImageInstance);
}
Also used : AzureInputDockerLoginCredsDialog(com.microsoft.intellij.docker.dialogs.AzureInputDockerLoginCredsDialog) AzureDockerImageInstance(com.microsoft.azure.docker.model.AzureDockerImageInstance) Azure(com.microsoft.azure.management.Azure) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) DockerHost(com.microsoft.azure.docker.model.DockerHost) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) DockerContainerDeployTask(com.microsoft.tasks.DockerContainerDeployTask) AzureDockerPreferredSettings(com.microsoft.azure.docker.model.AzureDockerPreferredSettings) Session(com.jcraft.jsch.Session)

Example 39 with Session

use of com.jcraft.jsch.Session in project azure-tools-for-java by Microsoft.

the class AzureDockerVMOps method installDockerOnUbuntuServer.

public static DockerHost installDockerOnUbuntuServer(DockerHost dockerHost) {
    if (dockerHost == null) {
        throw new AzureDockerException("Unexpected param values; dockerHost cannot be null");
    }
    try {
        Session session = AzureDockerSSHOps.createLoginInstance(dockerHost);
        switch(dockerHost.hostOSType) {
            case UBUNTU_SERVER_14_04_LTS:
                installDockerServiceOnUbuntuServer_14_04_LTS(session);
            case UBUNTU_SERVER_16_04_LTS:
                installDockerServiceOnUbuntuServer_16_04_LTS(session);
                break;
            default:
                throw new AzureDockerException("Docker dockerHost OS type is not supported");
        }
        if (dockerHost.isTLSSecured) {
            if (isValid(dockerHost.certVault.tlsServerCert)) {
                // Docker certificates are passed in; copy them to the docker dockerHost
                uploadDockerTlsCertsForUbuntuServer(dockerHost, session);
            } else {
                // Create new TLS certificates and upload them into the current machine representation
                dockerHost = createDockerCertsForUbuntuServer(dockerHost, session);
                dockerHost = downloadDockerTlsCertsForUbuntuServer(dockerHost, session);
            }
            setupDockerTlsCertsForUbuntuServer(session);
            createDockerConfigWithTlsForUbuntuServer(dockerHost, session);
        } else {
            createDockerConfigNoTlsForUbuntuServer(dockerHost, session);
        }
        session.disconnect();
        return dockerHost;
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage(), e);
    }
}
Also used : Session(com.jcraft.jsch.Session)

Example 40 with Session

use of com.jcraft.jsch.Session in project GNS by MobilityFirst.

the class UserAuthPubKey method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
        chooser.setFileHidingEnabled(false);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose " + chooser.getSelectedFile().getAbsolutePath() + ".");
            //			 , "passphrase"
            jsch.addIdentity(//			 , "passphrase"
            chooser.getSelectedFile().getAbsolutePath());
        }
        String host = null;
        if (arg.length > 0) {
            host = arg[0];
        } else {
            host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
        }
        String user = host.substring(0, host.indexOf('@'));
        host = host.substring(host.indexOf('@') + 1);
        Session session = jsch.getSession(user, host, 22);
        // username and passphrase will be given via UserInfo interface.
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();
    } catch (HeadlessException | JSchException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) JFileChooser(javax.swing.JFileChooser) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Aggregations

Session (com.jcraft.jsch.Session)50 JSch (com.jcraft.jsch.JSch)30 JSchException (com.jcraft.jsch.JSchException)22 IOException (java.io.IOException)17 Channel (com.jcraft.jsch.Channel)13 ChannelSftp (com.jcraft.jsch.ChannelSftp)12 ChannelExec (com.jcraft.jsch.ChannelExec)11 File (java.io.File)10 InputStream (java.io.InputStream)8 SftpException (com.jcraft.jsch.SftpException)7 UserInfo (com.jcraft.jsch.UserInfo)7 FileInputStream (java.io.FileInputStream)7 Properties (java.util.Properties)6 OutputStream (java.io.OutputStream)5 EditableDockerHost (com.microsoft.azure.docker.model.EditableDockerHost)4 DockerHost (com.microsoft.azure.docker.model.DockerHost)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 AzureDockerPreferredSettings (com.microsoft.azure.docker.model.AzureDockerPreferredSettings)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2