Search in sources :

Example 6 with Session

use of com.jcraft.jsch.Session in project quickstarts by jboss-switchyard.

the class CamelFtpBindingTest method startUp.

@BeforeClass
public static void startUp() throws Exception {
    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory listenerFactory = new ListenerFactory();
    listenerFactory.setPort(2222);
    serverFactory.addListener("default", listenerFactory.createListener());
    ListenerFactory sslListenerFactory = new ListenerFactory();
    sslListenerFactory.setPort(2221);
    SslConfigurationFactory ssl = new SslConfigurationFactory();
    ssl.setKeystoreFile(new File("src/test/resources/ftpserver.jks"));
    ssl.setKeystorePassword("password");
    sslListenerFactory.setSslConfiguration(ssl.createSslConfiguration());
    // Setting it to true will not read the file
    sslListenerFactory.setImplicitSsl(false);
    serverFactory.addListener("ftps", sslListenerFactory.createListener());
    PropertiesUserManagerFactory managerFactory = new PropertiesUserManagerFactory();
    managerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
    managerFactory.setFile(new File("src/test/resources/users.properties"));
    UserManager createUserManager = managerFactory.createUserManager();
    serverFactory.setUserManager(createUserManager);
    NativeFileSystemFactory fileSystemFactory = new NativeFileSystemFactory();
    fileSystemFactory.setCreateHome(true);
    serverFactory.setFileSystem(fileSystemFactory);
    File file = new File("target/ftp/ftps");
    file.mkdirs();
    file = new File("target/ftp/sftp");
    file.mkdirs();
    ftpServer = serverFactory.createServer();
    ftpServer.start();
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2220);
    sshd.setKeyPairProvider(createTestKeyPairProvider("src/test/resources/hostkey.pem"));
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
    sshd.start();
    JSch sch = new JSch();
    Session session = sch.getSession("camel", "localhost", 2220);
    session.setUserInfo(new SimpleUserInfo("isMyFriend"));
    session.connect();
    ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
    c.connect();
    System.out.println("Home: " + c.getHome());
    c.chmod(777, ".");
    c.chmod(777, "target");
    c.chmod(777, "target/ftp");
    c.chmod(777, "target/ftp/sftp");
    c.disconnect();
    session.disconnect();
}
Also used : FtpServerFactory(org.apache.ftpserver.FtpServerFactory) NativeFileSystemFactory(org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory) NativeFileSystemFactory(org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) PropertiesUserManagerFactory(org.apache.ftpserver.usermanager.PropertiesUserManagerFactory) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory) SslConfigurationFactory(org.apache.ftpserver.ssl.SslConfigurationFactory) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) NamedFactory(org.apache.sshd.common.NamedFactory) JSch(com.jcraft.jsch.JSch) ClearTextPasswordEncryptor(org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor) SshServer(org.apache.sshd.SshServer) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) ChannelSftp(com.jcraft.jsch.ChannelSftp) Command(org.apache.sshd.server.Command) UserManager(org.apache.ftpserver.ftplet.UserManager) PropertiesUserManagerFactory(org.apache.ftpserver.usermanager.PropertiesUserManagerFactory) SslConfigurationFactory(org.apache.ftpserver.ssl.SslConfigurationFactory) File(java.io.File) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory) ServerSession(org.apache.sshd.server.session.ServerSession) Session(com.jcraft.jsch.Session) BeforeClass(org.junit.BeforeClass)

Example 7 with Session

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

the class AzureDockerUIResources method updateDockerHost.

public static void updateDockerHost(Project project, EditableDockerHost editableDockerHost, AzureDockerHostsManager dockerManager, boolean doReset) {
    AppInsightsClient.createByType(AppInsightsClient.EventType.DockerHost, "", "Update");
    AzureInputDockerLoginCredsDialog loginCredsDialog = new AzureInputDockerLoginCredsDialog(project, editableDockerHost, dockerManager, doReset);
    loginCredsDialog.show();
    if (loginCredsDialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        // Update Docker host log in credentials
        DockerHost updateHost = editableDockerHost.originalDockerHost;
        updateHost.isUpdating = true;
        DefaultLoader.getIdeHelper().runInBackground(project, String.format("Updating %s Log In Credentials", updateHost.name), false, true, String.format("Updating log in credentials for %s...", updateHost.name), new Runnable() {

            @Override
            public void run() {
                try {
                    AzureDockerVMOps.updateDockerHostVM(dockerManager.getSubscriptionsMap().get(updateHost.sid).azureClient, editableDockerHost.updatedDockerHost);
                    updateHost.certVault = editableDockerHost.updatedDockerHost.certVault;
                    updateHost.hasPwdLogIn = editableDockerHost.updatedDockerHost.hasPwdLogIn;
                    updateHost.hasSSHLogIn = editableDockerHost.updatedDockerHost.hasSSHLogIn;
                    updateHost.hasKeyVault = false;
                    updateHost.certVault.uri = "";
                    updateHost.certVault.name = "";
                    Session session = AzureDockerSSHOps.createLoginInstance(updateHost);
                    AzureDockerVMOps.UpdateCurrentDockerUser(session);
                    updateHost.session = session;
                } catch (Exception ee) {
                    if (AzureDockerUtils.DEBUG)
                        ee.printStackTrace();
                    LOGGER.error("onEditDockerHostAction", ee);
                }
                updateHost.isUpdating = false;
            }
        });
    }
}
Also used : AzureInputDockerLoginCredsDialog(com.microsoft.intellij.docker.dialogs.AzureInputDockerLoginCredsDialog) DockerHost(com.microsoft.azure.docker.model.DockerHost) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) Session(com.jcraft.jsch.Session)

Example 8 with Session

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

the class AzureSelectDockerWizard method deploy.

public String deploy() {
    AzureDockerPreferredSettings dockerPreferredSettings = dockerManager.getDockerPreferredSettings();
    if (dockerPreferredSettings == null) {
        dockerPreferredSettings = new AzureDockerPreferredSettings();
    }
    dockerPreferredSettings.dockerApiName = dockerImageDescription.host.apiUrl;
    dockerPreferredSettings.dockerfileOption = dockerImageDescription.predefinedDockerfile;
    dockerPreferredSettings.region = dockerImageDescription.host.hostVM.region;
    dockerPreferredSettings.vmSize = dockerImageDescription.host.hostVM.vmSize;
    dockerPreferredSettings.vmOS = dockerImageDescription.host.hostOSType.name();
    dockerManager.setDockerPreferredSettings(dockerPreferredSettings);
    DefaultLoader.getIdeHelper().runInBackground(project, "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() {
                        if (!dockerImageDescription.hasNewDockerHost) {
                            Session session = null;
                            do {
                                try {
                                    // check if the Docker host is accessible
                                    session = AzureDockerSSHOps.createLoginInstance(dockerImageDescription.host);
                                } catch (Exception e) {
                                    session = null;
                                }
                                if (session == null) {
                                    EditableDockerHost editableDockerHost = new EditableDockerHost(dockerImageDescription.host);
                                    AzureInputDockerLoginCredsDialog loginCredsDialog = new AzureInputDockerLoginCredsDialog(PluginUtil.getParentShell(), project, editableDockerHost, dockerManager, false);
                                    if (loginCredsDialog.open() == Window.OK) {
                                        // Update Docker host log in credentials
                                        dockerImageDescription.host.certVault = editableDockerHost.updatedDockerHost.certVault;
                                        dockerImageDescription.host.hasSSHLogIn = editableDockerHost.updatedDockerHost.hasSSHLogIn;
                                        dockerImageDescription.host.hasPwdLogIn = editableDockerHost.updatedDockerHost.hasPwdLogIn;
                                    //											AzureDockerVMOps.updateDockerHostVM(dockerManager.getSubscriptionsMap().get(dockerImageDescription.sid).azureClient, editableDockerHost.updatedDockerHost);
                                    } else {
                                        return;
                                    }
                                }
                            } while (session == null);
                        }
                        //							Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerImageDescription.sid).azureClient;
                        //							DockerContainerDeployTask task = new DockerContainerDeployTask(project, azureClient, dockerImageDescription);
                        //							task.queue();
                        createDockerContainerDeployTask(project, dockerImageDescription, dockerManager);
                    }
                });
            } catch (Exception e) {
                String msg = "An error occurred while attempting to deploy to the selected Docker host." + "\n" + e.getMessage();
                PluginUtil.displayErrorDialogWithAzureMsg(PluginUtil.getParentShell(), "Failed to Deploy Web App as Docker Container", msg, e);
            }
        }
    });
    return AzureDockerUtils.getUrl(dockerImageDescription);
}
Also used : AzureInputDockerLoginCredsDialog(com.microsoft.azuretools.docker.ui.dialogs.AzureInputDockerLoginCredsDialog) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) AzureDockerPreferredSettings(com.microsoft.azure.docker.model.AzureDockerPreferredSettings) Session(com.jcraft.jsch.Session)

Example 9 with Session

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

the class AzureDockerUIResources method updateDockerHost.

public static void updateDockerHost(Shell shell, IProject project, EditableDockerHost editableDockerHost, AzureDockerHostsManager dockerManager, boolean doReset) {
    DockerHost updateHost = editableDockerHost.originalDockerHost;
    AzureInputDockerLoginCredsDialog loginCredsDialog = new AzureInputDockerLoginCredsDialog(PluginUtil.getParentShell(), project, editableDockerHost, dockerManager, doReset);
    if (loginCredsDialog.open() == Window.OK) {
        // Update Docker host log in credentials
        updateHost.isUpdating = true;
        DefaultLoader.getIdeHelper().runInBackground(project, String.format("Updating %s Log In Credentials", updateHost.name), false, true, String.format("Updating log in credentials for %s...", updateHost.name), new Runnable() {

            @Override
            public void run() {
                try {
                    AzureDockerVMOps.updateDockerHostVM(dockerManager.getSubscriptionsMap().get(updateHost.sid).azureClient, editableDockerHost.updatedDockerHost);
                    updateHost.certVault = editableDockerHost.updatedDockerHost.certVault;
                    updateHost.hasPwdLogIn = editableDockerHost.updatedDockerHost.hasPwdLogIn;
                    updateHost.hasSSHLogIn = editableDockerHost.updatedDockerHost.hasSSHLogIn;
                    updateHost.hasKeyVault = false;
                    updateHost.certVault.uri = "";
                    updateHost.certVault.name = "";
                    Session session = AzureDockerSSHOps.createLoginInstance(updateHost);
                    AzureDockerVMOps.UpdateCurrentDockerUser(session);
                    updateHost.session = session;
                } catch (Exception ee) {
                    if (AzureDockerUtils.DEBUG)
                        ee.printStackTrace();
                    log.log(Level.SEVERE, "dockerHostsEditButton.addSelectionListener", ee);
                }
                updateHost.isUpdating = false;
            }
        });
    }
}
Also used : AzureInputDockerLoginCredsDialog(com.microsoft.azuretools.docker.ui.dialogs.AzureInputDockerLoginCredsDialog) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) DockerHost(com.microsoft.azure.docker.model.DockerHost) Session(com.jcraft.jsch.Session)

Example 10 with Session

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

the class AzureDockerVMOps method waitForVirtualMachineStartup.

public static void waitForVirtualMachineStartup(Azure azureClient, DockerHost dockerHost) {
    // vault is not immediately available so the next operation could fail
    // add a retry policy to make sure it got created
    boolean isRunning = false;
    int sleepTime = 50000;
    for (int sleepMs = 5000; sleepMs <= 200000; sleepMs += sleepTime) {
        try {
            if (!isRunning) {
                VirtualMachine vm = getVM(azureClient, dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
                if (vm != null && vm.powerState().toString().split("/")[1].toUpperCase().equals("RUNNING")) {
                    // "PowerState/running"
                    isRunning = true;
                } else {
                    if (DEBUG)
                        System.out.format("Warning: can't connect to %s (%d sec have passed)\n", dockerHost.hostVM.dnsName, sleepMs / 1000);
                    try {
                        Thread.sleep(sleepTime);
                    } catch (Exception e2) {
                    }
                }
            } else {
                // VM is running; try to SSH connect to it
                Session session = AzureDockerSSHOps.createLoginInstance(dockerHost);
                String result = AzureDockerSSHOps.executeCommand("ls -l /", session, true);
                if (DEBUG)
                    System.out.println(result);
                session.disconnect();
                break;
            }
        } catch (Exception e) {
            try {
                if (DEBUG)
                    System.out.format("Warning: can't connect to %s (%d sec have passed)\n", dockerHost.hostVM.dnsName, sleepMs / 1000);
                if (DEBUG)
                    System.out.println(e.getMessage());
                Thread.sleep(sleepTime);
            } catch (Exception e3) {
            }
        }
    }
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) 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