Skip to content

Commit

Permalink
Merge pull request #168 from nocalhost/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ovaldi authored Jan 12, 2022
2 parents be6bebc + e14f41d commit f717d51
Show file tree
Hide file tree
Showing 53 changed files with 962 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ public List<NhctlPVCItem> listPVC(NhctlListPVCOptions opts) throws IOException,
args.add("--svc");
args.add(opts.getSvc());
}
if (StringUtils.isNotEmpty(opts.getControllerType())) {
args.add("--controller-type");
args.add(opts.getControllerType());
}
args.add("--json");
String output = execute(args, opts);
return DataUtils.GSON.fromJson(output, TypeToken.getParameterized(List.class, NhctlPVCItem.class).getType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.nocalhost.plugin.intellij.commands.data;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;

@Getter
public class NhctlCrdKind {
@SerializedName("info")
private Spec info;

@Getter
public class Spec {
@SerializedName("Kind")
private String kind;

@SerializedName("Group")
private String group;

@SerializedName("Version")
private String version;

@SerializedName("Resource")
private String resource;

@SerializedName("Namespaced")
private boolean namespaced;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class NhctlGetResource {
private NhctlDescribeService nhctlDescribeService;
@SerializedName("info")
private KubeResource kubeResource;
@SerializedName("vpn")
private NhctlProxy vpn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class NhctlListPVCOptions extends NhctlGlobalOptions {
private String app;
private String svc;
private String controllerType;

public NhctlListPVCOptions(Path kubeConfigPath, String namespace) {
super(kubeConfigPath, namespace);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.nocalhost.plugin.intellij.commands.data;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;

@Getter
public class NhctlProxy {
@SerializedName("status")
private String status;

@SerializedName("belongsToMe")
private boolean belongsToMe;
}
39 changes: 23 additions & 16 deletions src/main/java/dev/nocalhost/plugin/intellij/nhctl/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import dev.nocalhost.plugin.intellij.topic.NocalhostOutputAppendNotifier;
import dev.nocalhost.plugin.intellij.utils.NhctlOutputUtil;
import dev.nocalhost.plugin.intellij.utils.SudoUtil;
import dev.nocalhost.plugin.intellij.utils.NhctlUtil;
import dev.nocalhost.plugin.intellij.utils.NhctlOutputUtil;
import dev.nocalhost.plugin.intellij.exception.NhctlCommandException;
import dev.nocalhost.plugin.intellij.topic.NocalhostOutputAppendNotifier;
import dev.nocalhost.plugin.intellij.exception.NocalhostExecuteCmdException;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -40,6 +40,8 @@ public abstract class BaseCommand {
protected String namespace;
protected String deployment;

protected AtomicReference<String> stderr = new AtomicReference<>("");

protected BaseCommand(Project project) {
this(project, true);
}
Expand Down Expand Up @@ -85,16 +87,28 @@ protected GeneralCommandLine getCommandline(@NotNull List<String> args) {

protected abstract List<String> compute();

protected void onInput(@NotNull Process process) {
// do nothing
}

protected void onError(@NotNull Process process) {
try (var reader = new InputStreamReader(process.getErrorStream(), Charsets.UTF_8)) {
stderr.set(CharStreams.toString(reader));
} catch (IOException ex) {
// ignore
}
}

public String execute() throws IOException, NocalhostExecuteCmdException, InterruptedException {
return doExecute(compute());
}

protected String doExecute(@NotNull List<String> args) throws IOException, InterruptedException, NocalhostExecuteCmdException {
return doExecute(args, null);
public String execute(String password) throws IOException, NocalhostExecuteCmdException, InterruptedException {
return doExecute(compute(), password);
}

protected void consume(@NotNull Process process) {
// do nothing
protected String doExecute(@NotNull List<String> args) throws IOException, InterruptedException, NocalhostExecuteCmdException {
return doExecute(args, null);
}

protected String doExecute(@NotNull List<String> args, String sudoPassword) throws InterruptedException, NocalhostExecuteCmdException, IOException {
Expand All @@ -116,23 +130,16 @@ protected String doExecute(@NotNull List<String> args, String sudoPassword) thro
throw new NocalhostExecuteCmdException(cmd, -1, e.getMessage());
}

AtomicReference<String> stderr = new AtomicReference<>("");
ApplicationManager.getApplication().executeOnPooledThread(() -> consume(process));
ApplicationManager.getApplication().executeOnPooledThread(() -> {
try (var reader = new InputStreamReader(process.getErrorStream(), Charsets.UTF_8)) {
stderr.set(CharStreams.toString(reader));
} catch (IOException ex) {
// ignore
}
});
ApplicationManager.getApplication().executeOnPooledThread(() -> onError(process));
ApplicationManager.getApplication().executeOnPooledThread(() -> onInput(process));

var stdout = new StringBuilder();
var reader = new InputStreamReader(process.getInputStream(), Charsets.UTF_8);
try (var br = new BufferedReader(reader)) {
String line;
while ((line = br.readLine()) != null) {
stdout.append(line);
print(line);
stdout.append(line);
NhctlOutputUtil.showMessageByCommandOutput(project, line);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ protected List<String> compute() {
}

@Override
protected void consume(@NotNull Process process) {
var output = process.getOutputStream();
try (output) {
output.write(yaml.getBytes(StandardCharsets.UTF_8));
output.flush();
protected void onInput(@NotNull Process process) {
var stream = process.getOutputStream();
try (stream) {
stream.write(yaml.getBytes(StandardCharsets.UTF_8));
stream.flush();
} catch (Exception ex) {
LOG.error("Failed to write dev config to stdin", ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.nocalhost.plugin.intellij.nhctl;

import com.intellij.openapi.project.Project;
import com.google.common.collect.Lists;
import java.util.List;

public class NhctlCrdListCommand extends BaseCommand {
public NhctlCrdListCommand(Project project) {
super(project, false);
}

@Override
protected List<String> compute() {
List<String> args = Lists.newArrayList(getBinaryPath(), "get", "crd-list");
args.add("--outputType");
args.add("json");
return fulfill(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.nocalhost.plugin.intellij.nhctl;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dev.nocalhost.plugin.intellij.ui.dialog.SudoPasswordDialog;
import dev.nocalhost.plugin.intellij.utils.NhctlUtil;
import dev.nocalhost.plugin.intellij.utils.SudoUtil;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class NhctlProxyCommand extends BaseCommand {
private String action;
private String workload;

public NhctlProxyCommand(Project project) {
super(project);
}

@Override
protected List<String> compute() {
List<String> args = Lists.newArrayList(getBinaryPath(), "vpn", action);
if (StringUtils.isNotEmpty(workload)) {
args.add("--workloads");
args.add(workload);
}
return fulfill(args);
}

@Override
protected void onError(@NotNull Process process) {
int b;
var output = new StringBuilder();
var reader = new InputStreamReader(process.getErrorStream(), Charsets.UTF_8);
try (var br = new BufferedReader(reader)) {
while ((b = br.read()) != -1) {
output.append((char) b);
stderr.set(output.toString());
if (SystemInfo.isWindows) {
continue;
}
if (StringUtils.contains(output.toString(), "Password:")) {
output.setLength(0);
ApplicationManager.getApplication().invokeLater(() -> {
var dialog = new SudoPasswordDialog(project, NhctlUtil.binaryPath());
if (dialog.showAndGet()) {
SudoUtil.inputPassword(process, dialog.getPassword());
} else {
process.destroy();
}
});
}
}
} catch (IOException ex) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import com.google.common.collect.Lists;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Paths;
import java.util.List;

import dev.nocalhost.plugin.intellij.api.data.Application;
Expand All @@ -19,28 +20,24 @@
import dev.nocalhost.plugin.intellij.exception.NocalhostNotifier;
import dev.nocalhost.plugin.intellij.topic.NocalhostTreeUpdateNotifier;
import dev.nocalhost.plugin.intellij.utils.ErrorUtil;
import dev.nocalhost.plugin.intellij.utils.Constants;
import lombok.SneakyThrows;

public class InstallApplicationTask extends BaseBackgroundTask {
private static final List<String> BOOKINFO_URLS = Lists.newArrayList(
private static final List<String> BOOKINFO_GITS = Lists.newArrayList(
"https://github.com/nocalhost/bookinfo.git",
"[email protected]:nocalhost/bookinfo.git",
"https://e.coding.net/nocalhost/nocalhost/bookinfo.git",
"[email protected]:nocalhost/nocalhost/bookinfo.git"
);

private static final List<String> BOOKINFO_APP_NAME = Lists.newArrayList(
"bookinfo"
);

private final Project project;

private final Application application;
private final NhctlInstallOptions opts;

private final OutputCapturedNhctlCommand outputCapturedNhctlCommand;

private String productPagePort;

public InstallApplicationTask(@Nullable Project project, Application application, NhctlInstallOptions opts) {
super(project, "Deploying application: " + application.getContext().getApplicationName(), true);
this.project = project;
Expand All @@ -53,20 +50,15 @@ public InstallApplicationTask(@Nullable Project project, Application application
@Override
public void onSuccess() {
super.onSuccess();
bookinfo();
ApplicationManager.getApplication().getMessageBus().syncPublisher(
NocalhostTreeUpdateNotifier.NOCALHOST_TREE_UPDATE_NOTIFIER_TOPIC).action();

NocalhostNotifier.getInstance(project).notifySuccess(
"Application " + application.getContext().getApplicationName() + " Deployed",
"");
}

private void bookinfo() {
if (BOOKINFO_APP_NAME.contains(application.getContext().getApplicationName())
&& BOOKINFO_URLS.contains(application.getContext().getApplicationUrl())
&& StringUtils.isNotBlank(productPagePort)) {
BrowserUtil.browse("http://127.0.0.1:" + productPagePort + "/productpage");
if (StringUtils.equals(Constants.DEMO_NAME, application.getContext().getApplicationName()) && BOOKINFO_GITS.contains(application.getContext().getApplicationUrl())) {
ProgressManager.getInstance().run(new BrowseDemoTask(project, Paths.get(opts.getKubeconfig()), opts.getNamespace()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void onThrowable(@NotNull Throwable e) {
@Override
public void runTask(@NotNull ProgressIndicator indicator) {
NhctlGetOptions opts = new NhctlGetOptions(kubeConfigPath, namespace, this);
String output = nhctlCommand.get(node.getKubeResource().getKind(), opts);
String output = nhctlCommand.get(node.controllerType(), opts);
JsonElement resources = DataUtils.GSON.fromJson(output, JsonElement.class);
JsonObject selectedResource = null;
for (JsonElement resource : resources.getAsJsonArray()) {
Expand Down
Loading

0 comments on commit f717d51

Please sign in to comment.