Search in sources :

Example 6 with EmulatorClusterDetail

use of com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail in project azure-tools-for-java by Microsoft.

the class SparkSubmissionToolWindowProcessor method initialize.

public void initialize() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    // TODO: Fix deprecated API "addUISettingsListener"
    UISettings.getInstance().addUISettingsListener(new UISettingsListener() {

        @Override
        public void uiSettingsChanged(final UISettings uiSettings) {
            synchronized (this) {
                for (final IHtmlElement htmlElement : cachedInfo) {
                    htmlElement.changeTheme();
                }
                setToolWindowText(parserHtmlElementList(cachedInfo));
            }
        }
    }, ApplicationManager.getApplication());
    fontFace = jEditorPanel.getFont().getFamily();
    final JPanel jPanel = new JPanel();
    jPanel.setLayout(new GridBagLayout());
    jEditorPanel.setMargin(JBUI.insetsLeft(10));
    final JBScrollPane scrollPane = new JBScrollPane(jEditorPanel);
    stopButton = new JButton(PluginUtil.getIcon(CommonConst.StopIconPath));
    stopButton.setDisabledIcon(PluginUtil.getIcon(CommonConst.StopDisableIconPath));
    stopButton.setEnabled(false);
    stopButton.setToolTipText("stop execution of current application");
    stopButton.addActionListener(e -> DefaultLoader.getIdeHelper().executeOnPooledThread(() -> {
        if (clusterDetail != null) {
            AppInsightsClient.create(HDInsightBundle.message("SparkSubmissionStopButtionClickEvent"), null);
            EventUtil.logEvent(EventType.info, HDINSIGHT, HDInsightBundle.message("SparkSubmissionStopButtionClickEvent"), null);
            try {
                final String livyUrl = clusterDetail instanceof LivyCluster ? ((LivyCluster) clusterDetail).getLivyBatchUrl() : null;
                final HttpResponse deleteResponse = SparkBatchSubmission.getInstance().killBatchJob(livyUrl, batchId);
                if (deleteResponse.getCode() == 201 || deleteResponse.getCode() == 200) {
                    jobStatusManager.setJobKilled();
                    setInfo("========================Stop application successfully" + "=======================");
                } else {
                    setError(String.format("Error : Failed to stop spark application. error code : %d, reason :  %s.", deleteResponse.getCode(), deleteResponse.getContent()));
                }
            } catch (final IOException exception) {
                setError("Error : Failed to stop spark application. exception : " + exception.toString());
            }
        }
    }));
    openSparkUIButton = new JButton(PluginUtil.getIcon(IconPathBuilder.custom(CommonConst.OpenSparkUIIconName).build()));
    openSparkUIButton.setDisabledIcon(PluginUtil.getIcon(CommonConst.OpenSparkUIDisableIconPath));
    openSparkUIButton.setEnabled(false);
    openSparkUIButton.setToolTipText("open the corresponding Spark UI page");
    openSparkUIButton.addActionListener(e -> {
        if (Desktop.isDesktopSupported()) {
            try {
                if (jobStatusManager.isApplicationGenerated()) {
                    final String connectionURL = clusterDetail.getConnectionUrl();
                    final String sparkApplicationUrl = clusterDetail.isEmulator() ? String.format(yarnRunningUIEmulatorUrlFormat, ((EmulatorClusterDetail) clusterDetail).getSparkHistoryEndpoint(), jobStatusManager.getApplicationId()) : String.format(yarnRunningUIUrlFormat, connectionURL, jobStatusManager.getApplicationId());
                    Desktop.getDesktop().browse(new URI(sparkApplicationUrl));
                }
            } catch (final Exception browseException) {
                DefaultLoader.getUIHelper().showError("Failed to browse spark application yarn url", "Spark Submission");
            }
        }
    });
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    buttonPanel.add(stopButton);
    buttonPanel.add(openSparkUIButton);
    final GridBagConstraints c00 = new GridBagConstraints();
    c00.fill = GridBagConstraints.VERTICAL;
    c00.weighty = 1;
    c00.gridx = 0;
    c00.gridy = 0;
    jPanel.add(buttonPanel, c00);
    final GridBagConstraints c10 = new GridBagConstraints();
    c10.fill = GridBagConstraints.BOTH;
    c10.weightx = 1;
    c10.weighty = 1;
    c10.gridx = 1;
    c10.gridy = 0;
    jPanel.add(scrollPane, c10);
    toolWindow.getComponent().add(jPanel);
    jEditorPanel.setEditable(false);
    jEditorPanel.setOpaque(false);
    jEditorPanel.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
    jEditorPanel.addHyperlinkListener(e -> {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            if (Desktop.isDesktopSupported()) {
                try {
                    final String protocol = e.getURL().getProtocol();
                    if ("https".equals(protocol) || "http".equals(protocol)) {
                        Desktop.getDesktop().browse(e.getURL().toURI());
                    } else if ("file".equals(protocol)) {
                        final String path = e.getURL().getFile();
                        final File localFile = new File(path);
                        final File parentFile = localFile.getParentFile();
                        if (parentFile.exists() && parentFile.isDirectory()) {
                            Desktop.getDesktop().open(parentFile);
                        }
                    }
                } catch (final Exception exception) {
                    DefaultLoader.getUIHelper().showError(exception.getMessage(), "Open Local Folder Error");
                }
            }
        }
    });
    final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {

        @Override
        public void propertyChange(final PropertyChangeEvent evt) {
            if (ApplicationManager.getApplication().isDispatchThread()) {
                changeSupportHandler(evt);
            } else {
                try {
                    SwingUtilities.invokeAndWait(() -> changeSupportHandler(evt));
                } catch (final InterruptedException ignore) {
                } catch (final InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

        private void changeSupportHandler(final PropertyChangeEvent evt) {
            if ("toolWindowText".equals(evt.getPropertyName())) {
                jEditorPanel.setText(evt.getNewValue().toString());
            } else if ("isStopButtonEnable".equals(evt.getPropertyName())) {
                stopButton.setEnabled(Boolean.parseBoolean(evt.getNewValue().toString()));
            } else if ("isBrowserButtonEnable".equals(evt.getPropertyName())) {
                openSparkUIButton.setEnabled(Boolean.parseBoolean(evt.getNewValue().toString()));
            }
        }
    };
    jEditorPanel.addPropertyChangeListener(propertyChangeListener);
    changeSupport = new PropertyChangeSupport(jEditorPanel);
    changeSupport.addPropertyChangeListener(propertyChangeListener);
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) PropertyChangeSupport(java.beans.PropertyChangeSupport) HttpResponse(com.microsoft.azure.hdinsight.sdk.common.HttpResponse) LivyCluster(com.microsoft.azure.hdinsight.sdk.cluster.LivyCluster) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UISettings(com.intellij.ide.ui.UISettings) UISettingsListener(com.intellij.ide.ui.UISettingsListener) File(java.io.File) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Example 7 with EmulatorClusterDetail

use of com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail in project azure-tools-for-java by Microsoft.

the class JobUtils method sftpFileToEmulator.

public static String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    final EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (final FileInputStream fileInputStream = new FileInputStream(file)) {
        try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            final String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            final URL url = new URL(sshEndpoint);
            final String host = url.getHost();
            final int port = url.getPort();
            final JSch jsch = new JSch();
            final Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            final java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            final String[] folders = folderPath.split("/");
            for (final String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (final 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) URL(java.net.URL) java.util(java.util) LivySession(com.microsoft.azure.hdinsight.spark.jobs.livy.LivySession) SparkSession(com.microsoft.azure.hdinsight.sdk.common.livy.interactive.SparkSession)

Aggregations

EmulatorClusterDetail (com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail)7 URL (java.net.URL)3 File (java.io.File)2 java.util (java.util)2 UISettings (com.intellij.ide.ui.UISettings)1 UISettingsListener (com.intellij.ide.ui.UISettingsListener)1 JBScrollPane (com.intellij.ui.components.JBScrollPane)1 ChannelSftp (com.jcraft.jsch.ChannelSftp)1 JSch (com.jcraft.jsch.JSch)1 Session (com.jcraft.jsch.Session)1 SftpException (com.jcraft.jsch.SftpException)1 ClusterDetail (com.microsoft.azure.hdinsight.sdk.cluster.ClusterDetail)1 HDInsightAdditionalClusterDetail (com.microsoft.azure.hdinsight.sdk.cluster.HDInsightAdditionalClusterDetail)1 IClusterDetail (com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail)1 LivyCluster (com.microsoft.azure.hdinsight.sdk.cluster.LivyCluster)1 HttpResponse (com.microsoft.azure.hdinsight.sdk.common.HttpResponse)1 SparkSession (com.microsoft.azure.hdinsight.sdk.common.livy.interactive.SparkSession)1 LivySession (com.microsoft.azure.hdinsight.spark.jobs.livy.LivySession)1 NodeActionEvent (com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent)1 NodeActionListener (com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener)1