Search in sources :

Example 26 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 27 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 28 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 29 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)

Example 30 with Session

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

the class SSHClient method scpTo.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param lfile
   * @param rfile
   */
public static void scpTo(String user, String host, File keyFile, String lfile, String rfile) {
    if (verbose) {
        System.out.println("Remote copy file from " + lfile + " to " + host + "@" + user + ":" + rfile);
    }
    FileInputStream fis = null;
    try {
        JSch jsch = new JSch();
        Session session = authenticateWithKey(jsch, user, host, keyFile);
        // username and password will be given via UserInfo interface.
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        boolean ptimestamp = true;
        // exec 'scp -t rfile' remotely
        String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rfile;
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        // get I/O streams for remote scp
        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();
        channel.connect();
        int ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        File _lfile = new File(lfile);
        if (ptimestamp) {
            command = "T " + (_lfile.lastModified() / 1000) + " 0";
            // The access time should be sent here,
            // but it is not accessible with JavaAPI ;-<
            command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
            out.write(command.getBytes());
            out.flush();
            ack = checkAck(in);
            if (ack != 0) {
                System.out.println("ACK was " + ack);
                return;
            }
        }
        // send "C0644 filesize filename", where filename should not include '/'
        long filesize = _lfile.length();
        command = "C0644 " + filesize + " ";
        if (lfile.lastIndexOf('/') > 0) {
            command += lfile.substring(lfile.lastIndexOf('/') + 1);
        } else {
            command += lfile;
        }
        command += "\n";
        out.write(command.getBytes());
        out.flush();
        ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        // send a content of lfile
        fis = new FileInputStream(lfile);
        byte[] buf = new byte[1024];
        while (true) {
            int len = fis.read(buf, 0, buf.length);
            if (len <= 0) {
                break;
            }
            //out.flush();
            out.write(buf, 0, len);
        }
        fis.close();
        fis = null;
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        out.close();
        channel.disconnect();
        session.disconnect();
    } catch (JSchException | IOException e) {
        GNSConfig.getLogger().severe(e.toString());
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception ee) {
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

Session (com.jcraft.jsch.Session)41 JSch (com.jcraft.jsch.JSch)23 JSchException (com.jcraft.jsch.JSchException)20 IOException (java.io.IOException)16 Channel (com.jcraft.jsch.Channel)13 ChannelSftp (com.jcraft.jsch.ChannelSftp)11 File (java.io.File)9 ChannelExec (com.jcraft.jsch.ChannelExec)7 SftpException (com.jcraft.jsch.SftpException)7 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 UserInfo (com.jcraft.jsch.UserInfo)5 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 AzureDockerPreferredSettings (com.microsoft.azure.docker.model.AzureDockerPreferredSettings)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2 AzureInputDockerLoginCredsDialog (com.microsoft.azuretools.docker.ui.dialogs.AzureInputDockerLoginCredsDialog)2 AzureInputDockerLoginCredsDialog (com.microsoft.intellij.docker.dialogs.AzureInputDockerLoginCredsDialog)2