Skip to content

Commit

Permalink
feat(cli): add a server local command to start in h2 local database
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Jun 17, 2022
1 parent dd0127c commit 55e5768
Show file tree
Hide file tree
Showing 28 changed files with 135 additions and 155 deletions.
4 changes: 0 additions & 4 deletions cli/src/main/java/io/kestra/cli/AbstractApiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public abstract class AbstractApiCommand extends AbstractCommand {
@CommandLine.Option(names = {"--user"}, description = "<user:password> Server user and password")
protected String user;

public AbstractApiCommand(boolean withServer) {
super(withServer);
}

protected DefaultHttpClient client() throws URISyntaxException {
return new DefaultHttpClient(server.toURI());
}
Expand Down
13 changes: 5 additions & 8 deletions cli/src/main/java/io/kestra/cli/AbstractCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import ch.qos.logback.classic.LoggerContext;
import com.google.common.collect.ImmutableMap;
import io.kestra.cli.commands.servers.ServerCommandInterface;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.yaml.YamlPropertySourceLoader;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.management.endpoint.EndpointDefaultConfiguration;
import io.micronaut.runtime.server.EmbeddedServer;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -31,9 +33,8 @@
mixinStandardHelpOptions = true
)
@Slf4j
@Introspected
abstract public class AbstractCommand implements Callable<Integer> {
private final boolean withServer;

@Inject
private ApplicationContext applicationContext;

Expand Down Expand Up @@ -63,10 +64,6 @@ public enum LogLevel {
ERROR
}

public AbstractCommand(boolean withServer) {
this.withServer = withServer;
}

@Override
public Integer call() throws Exception {
Thread.currentThread().setName(this.getClass().getDeclaredAnnotation(CommandLine.Command.class).name());
Expand Down Expand Up @@ -98,7 +95,7 @@ private void startLogger() {
this.logLevel = LogLevel.TRACE;
}

if (this.withServer) {
if (this instanceof ServerCommandInterface) {
log.info("Starting Kestra with environments {}", applicationContext.getEnvironment().getActiveNames());
}

Expand Down Expand Up @@ -128,7 +125,7 @@ private void sendServerLog() {
}

private void startWebserver() {
if (!this.withServer) {
if (!(this instanceof ServerCommandInterface)) {
return;
}

Expand Down
25 changes: 24 additions & 1 deletion cli/src/main/java/io/kestra/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import io.kestra.cli.commands.sys.SysCommand;
import io.kestra.core.contexts.KestraApplicationContextBuilder;
import io.kestra.core.contexts.KestraClassLoader;
import io.micronaut.core.annotation.Introspected;
import org.slf4j.bridge.SLF4JBridgeHandler;
import picocli.CommandLine;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;

Expand All @@ -37,6 +41,7 @@
ConfigCommand.class,
}
)
@Introspected
public class App implements Callable<Integer> {
public static void main(String[] args) {
execute(App.class, args);
Expand Down Expand Up @@ -92,8 +97,26 @@ protected static ApplicationContext applicationContext(Class<?> mainClass, Strin
// if class have propertiesFromConfig, add configuration files
builder.properties(getPropertiesFromMethod(cls, "propertiesFromConfig", commandLine.getCommandSpec().userObject()));

Map<String, Object> properties = new HashMap<>();

// if class have propertiesOverrides, add force properties for this class
builder.properties(getPropertiesFromMethod(cls, "propertiesOverrides", null));
Map<String, Object> propertiesOverrides = getPropertiesFromMethod(cls, "propertiesOverrides", null);
if (propertiesOverrides != null) {
properties.putAll(propertiesOverrides);
}

// custom server configuration
commandLine
.getParseResult()
.matchedArgs()
.stream()
.filter(argSpec -> ((Field) argSpec.userObject()).getName().equals("serverPort"))
.findFirst()
.ifPresent(argSpec -> {
properties.put("micronaut.server.port", argSpec.getValue());
});

builder.properties(properties);

// add plugins registry if plugin path defined
builder.pluginRegistry(getPropertiesFromMethod(cls, "initPluginRegistry", commandLine.getCommandSpec().userObject()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
)
@Slf4j
public class ConfigCommand extends AbstractCommand {
public ConfigCommand() {
super(false);
}

@Override
public Integer call() throws Exception {
super.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class ConfigPropertiesCommand extends AbstractCommand {
@Inject
private ApplicationContext applicationContext;

public ConfigPropertiesCommand() {
super(false);
}

@Override
public Integer call() throws Exception {
super.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
)
@Slf4j
public class FlowCommand extends AbstractCommand {
public FlowCommand() {
super(false);
}

@SneakyThrows
@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@
import io.kestra.core.services.Graph2DotService;
import io.kestra.core.services.GraphService;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

import java.nio.file.Path;
import jakarta.inject.Inject;

@CommandLine.Command(
name = "dot",
description = "generate a dot graph from a file"
)
@Slf4j
public class FlowDotCommand extends AbstractCommand {
@CommandLine.Parameters(index = "0", description = "the flow file to display")
private Path file;

@Inject
private ApplicationContext applicationContext;

public FlowDotCommand() {
super(false);
}
@CommandLine.Parameters(index = "0", description = "the flow file to display")
private Path file;

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.kestra.cli.commands.flows;

import com.google.common.collect.ImmutableMap;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Prototype;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import io.kestra.cli.AbstractCommand;
import io.kestra.core.exceptions.MissingRequiredInput;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.repositories.LocalFlowRepositoryLoader;
import io.kestra.core.runners.RunnerUtils;
import io.kestra.runner.memory.MemoryRunner;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import picocli.CommandLine;

import java.io.IOException;
Expand All @@ -23,14 +23,16 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import jakarta.inject.Inject;

@CommandLine.Command(
name = "test",
description = "test a flow"
)
@Slf4j
public class FlowTestCommand extends AbstractCommand {
@Inject
private ApplicationContext applicationContext;

@CommandLine.Parameters(index = "0", description = "the flow file to test")
private Path file;

Expand All @@ -44,15 +46,8 @@ public class FlowTestCommand extends AbstractCommand {
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

@Inject
private ApplicationContext applicationContext;

private static final SecureRandom random = new SecureRandom();

public FlowTestCommand() {
super(false);
}

@SuppressWarnings("unused")
public static Map<String, Object> propertiesOverrides() {
return ImmutableMap.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public class ValidateCommand extends AbstractCommand {
@CommandLine.Parameters(index = "0", description = "the flow file to test")
private Path file;

public ValidateCommand() {
super(false);
}

@Override
public Integer call() throws Exception {
super.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
)
@Slf4j
public class FlowNamespaceCommand extends AbstractCommand {
public FlowNamespaceCommand() {
super(false);
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package io.kestra.cli.commands.flows.namespaces;

import io.kestra.cli.AbstractApiCommand;
import io.kestra.cli.commands.flows.ValidateCommand;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.serializers.YamlFlowParser;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.client.netty.DefaultHttpClient;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import io.kestra.cli.AbstractApiCommand;
import io.kestra.cli.commands.flows.ValidateCommand;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.serializers.YamlFlowParser;
import picocli.CommandLine;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import javax.validation.ConstraintViolationException;

import static io.kestra.core.utils.Rethrow.throwFunction;

@CommandLine.Command(
name = "update",
description = "handle namespace flows",
Expand All @@ -37,10 +34,6 @@ public class FlowNamespaceUpdateCommand extends AbstractApiCommand {
@CommandLine.Parameters(index = "1", description = "the directory containing flows to from current namespace")
private Path directory;

public FlowNamespaceUpdateCommand() {
super(false);
}

@Override
public Integer call() throws Exception {
super.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
)
@Slf4j
public class PluginCommand extends AbstractCommand {
public PluginCommand() {
super(false);
}

@SneakyThrows
@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public class PluginDocCommand extends AbstractCommand {
@CommandLine.Parameters(index = "0", description = "Path to write documentations files")
private Path output = Paths.get(System.getProperty("user.dir"), "docs");

public PluginDocCommand() {
super(false);
}

@CommandLine.Option(names = {"--core"}, description = "Also write core tasks docs files")
private boolean core = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public class PluginInstallCommand extends AbstractCommand {
CommandLine.Model.CommandSpec spec;

@Inject
PluginDownloader pluginDownloader;

public PluginInstallCommand() {
super(false);
}
private PluginDownloader pluginDownloader;

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public class PluginListCommand extends AbstractCommand {
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

public PluginListCommand() {
super(false);
}

@CommandLine.Option(names = {"--core"}, description = "Also write core tasks plugins")
private boolean core = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.kestra.cli.commands.servers;

import io.kestra.cli.AbstractCommand;
import picocli.CommandLine;

abstract public class AbstractServerCommand extends AbstractCommand implements ServerCommandInterface {
@CommandLine.Option(names = {"--port"}, description = "the port to bind")
Integer serverPort;
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package io.kestra.cli.commands.servers;

import com.google.common.collect.ImmutableMap;
import io.kestra.core.models.ServerType;
import io.kestra.core.runners.ExecutorInterface;
import io.kestra.core.utils.Await;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import io.kestra.cli.AbstractCommand;
import io.kestra.core.models.ServerType;
import io.kestra.core.utils.Await;
import picocli.CommandLine;

import java.util.Map;
import jakarta.inject.Inject;

@CommandLine.Command(
name = "executor",
description = "start an executor"
)
@Slf4j
public class ExecutorCommand extends AbstractCommand {
public class ExecutorCommand extends AbstractServerCommand {
@Inject
private ApplicationContext applicationContext;

public ExecutorCommand() {
super(true);
}

@SuppressWarnings("unused")
public static Map<String, Object> propertiesOverrides() {
return ImmutableMap.of(
Expand Down
Loading

0 comments on commit 55e5768

Please sign in to comment.