From 8e1540557072938d9f62dccd33159d41f9791f04 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Fri, 18 Aug 2023 17:22:54 +0200 Subject: [PATCH] feat: Init logger, add initial MemDB plugin --- lib/bin/main/io/cloudquery/glob/LICENSE | 21 +++++ lib/bin/main/io/cloudquery/glob/README.md | 3 + lib/build.gradle | 5 +- lib/src/main/java/io/cloudquery/Main.java | 12 +++ .../main/java/io/cloudquery/memdb/MemDB.java | 9 +++ .../java/io/cloudquery/plugin/Plugin.java | 14 ++-- .../io/cloudquery/scheduler/Scheduler.java | 6 ++ .../java/io/cloudquery/server/DocCommand.java | 7 -- .../io/cloudquery/server/PluginServe.java | 20 +---- .../io/cloudquery/server/RootCommand.java | 2 +- .../io/cloudquery/server/ServeCommand.java | 80 ++++++++++++------- .../io/cloudquery/server/ServerException.java | 7 -- .../io/cloudquery/server/PluginServeTest.java | 41 ++++------ 13 files changed, 131 insertions(+), 96 deletions(-) create mode 100644 lib/bin/main/io/cloudquery/glob/LICENSE create mode 100644 lib/bin/main/io/cloudquery/glob/README.md create mode 100644 lib/src/main/java/io/cloudquery/Main.java create mode 100644 lib/src/main/java/io/cloudquery/memdb/MemDB.java create mode 100644 lib/src/main/java/io/cloudquery/scheduler/Scheduler.java delete mode 100644 lib/src/main/java/io/cloudquery/server/DocCommand.java delete mode 100644 lib/src/main/java/io/cloudquery/server/ServerException.java diff --git a/lib/bin/main/io/cloudquery/glob/LICENSE b/lib/bin/main/io/cloudquery/glob/LICENSE new file mode 100644 index 0000000..a9f1724 --- /dev/null +++ b/lib/bin/main/io/cloudquery/glob/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Ryan Uber + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/lib/bin/main/io/cloudquery/glob/README.md b/lib/bin/main/io/cloudquery/glob/README.md new file mode 100644 index 0000000..380f61d --- /dev/null +++ b/lib/bin/main/io/cloudquery/glob/README.md @@ -0,0 +1,3 @@ +# Glob Matching Library + +This glob-matching library was copied from [ryanuber/go-glob](https://github.com/ryanuber/go-glob) and therefore falls under its [license](LICENSE). diff --git a/lib/build.gradle b/lib/build.gradle index df13885..9fb4ca4 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java-library' - id "io.freefair.lombok" version "8.1.0" + id "io.freefair.lombok" version "8.2.2" id "maven-publish" } @@ -36,6 +36,9 @@ dependencies { implementation "io.cloudquery:plugin-pb-java:0.0.5" implementation "org.apache.arrow:arrow-vector:12.0.1" + implementation 'org.apache.logging.log4j:log4j-api:2.20.0' + implementation 'org.apache.logging.log4j:log4j-core:2.20.0' + testImplementation(platform('org.junit:junit-bom:5.10.0')) testImplementation('org.junit.jupiter:junit-jupiter:5.10.0') testImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0') diff --git a/lib/src/main/java/io/cloudquery/Main.java b/lib/src/main/java/io/cloudquery/Main.java new file mode 100644 index 0000000..c4dfbac --- /dev/null +++ b/lib/src/main/java/io/cloudquery/Main.java @@ -0,0 +1,12 @@ +package io.cloudquery; + +import io.cloudquery.memdb.MemDB; +import io.cloudquery.server.PluginServe; + +public class Main { + public static void main(String[] args) { + PluginServe serve = PluginServe.builder().plugin(new MemDB()).args(args).build(); + int exitCode = serve.Serve(); + System.exit(exitCode); + } +} diff --git a/lib/src/main/java/io/cloudquery/memdb/MemDB.java b/lib/src/main/java/io/cloudquery/memdb/MemDB.java new file mode 100644 index 0000000..a0c8f92 --- /dev/null +++ b/lib/src/main/java/io/cloudquery/memdb/MemDB.java @@ -0,0 +1,9 @@ +package io.cloudquery.memdb; + +import io.cloudquery.plugin.Plugin; + +public class MemDB extends Plugin { + public MemDB() { + super("memdb", "0.0.1"); + } +} diff --git a/lib/src/main/java/io/cloudquery/plugin/Plugin.java b/lib/src/main/java/io/cloudquery/plugin/Plugin.java index 96d977f..918981e 100644 --- a/lib/src/main/java/io/cloudquery/plugin/Plugin.java +++ b/lib/src/main/java/io/cloudquery/plugin/Plugin.java @@ -1,18 +1,14 @@ package io.cloudquery.plugin; -import lombok.Builder; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; -@Builder(builderMethodName = "innerBuilder") @Getter -public class Plugin { - public static PluginBuilder builder(String name, String version) { - return innerBuilder().name(name).verion(version); - } - +@AllArgsConstructor +public abstract class Plugin { @NonNull - private final String name; + protected final String name; @NonNull - private final String verion; + protected final String version; } diff --git a/lib/src/main/java/io/cloudquery/scheduler/Scheduler.java b/lib/src/main/java/io/cloudquery/scheduler/Scheduler.java new file mode 100644 index 0000000..8a99093 --- /dev/null +++ b/lib/src/main/java/io/cloudquery/scheduler/Scheduler.java @@ -0,0 +1,6 @@ +package io.cloudquery.scheduler; + +public class Scheduler { + public Scheduler() { + } +} diff --git a/lib/src/main/java/io/cloudquery/server/DocCommand.java b/lib/src/main/java/io/cloudquery/server/DocCommand.java deleted file mode 100644 index ab143c1..0000000 --- a/lib/src/main/java/io/cloudquery/server/DocCommand.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.cloudquery.server; - -import picocli.CommandLine; - -@CommandLine.Command -public class DocCommand { -} diff --git a/lib/src/main/java/io/cloudquery/server/PluginServe.java b/lib/src/main/java/io/cloudquery/server/PluginServe.java index 7a02fc0..7fffc5a 100644 --- a/lib/src/main/java/io/cloudquery/server/PluginServe.java +++ b/lib/src/main/java/io/cloudquery/server/PluginServe.java @@ -6,28 +6,14 @@ import lombok.NonNull; import picocli.CommandLine; -import java.util.ArrayList; -import java.util.List; - @Builder(access = AccessLevel.PUBLIC) public class PluginServe { @NonNull private final Plugin plugin; @Builder.Default - private List args = new ArrayList<>(); - private boolean destinationV0V1Server; - private String sentryDSN; - private boolean testListener; - //TODO: Allow a test listener to be passed in - // testListenerConn *bufconn.Listener + private String[] args = new String[] {}; - public void Serve() throws ServerException { - int exitStatus = new CommandLine(new RootCommand()). - addSubcommand("serve", new ServeCommand(plugin)). - addSubcommand("doc", new DocCommand()). - execute(args.toArray(new String[]{})); - if (exitStatus != 0) { - throw new ServerException("error processing command line exit status = "+exitStatus); - } + public int Serve() { + return new CommandLine(new RootCommand()).addSubcommand(new ServeCommand(plugin)).execute(args); } } diff --git a/lib/src/main/java/io/cloudquery/server/RootCommand.java b/lib/src/main/java/io/cloudquery/server/RootCommand.java index 336a89c..aa8bbd9 100644 --- a/lib/src/main/java/io/cloudquery/server/RootCommand.java +++ b/lib/src/main/java/io/cloudquery/server/RootCommand.java @@ -4,4 +4,4 @@ @CommandLine.Command public class RootCommand { -} +} \ No newline at end of file diff --git a/lib/src/main/java/io/cloudquery/server/ServeCommand.java b/lib/src/main/java/io/cloudquery/server/ServeCommand.java index 2ea55c4..b4eaccb 100644 --- a/lib/src/main/java/io/cloudquery/server/ServeCommand.java +++ b/lib/src/main/java/io/cloudquery/server/ServeCommand.java @@ -9,20 +9,28 @@ import io.grpc.Server; import io.grpc.protobuf.services.ProtoReflectionService; import lombok.ToString; - +import picocli.CommandLine.Command; +import java.io.IOException; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; -import static picocli.CommandLine.Command; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.layout.JsonLayout; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.LoggerConfig; + import static picocli.CommandLine.Option; -@Command +@Command(name = "serve", description = "start plugin gRPC server") @ToString public class ServeCommand implements Callable { - private static final Logger logger = Logger.getLogger(ServeCommand.class.getName()); + private static Logger logger; public static final List DISCOVERY_VERSIONS = List.of(3); @Option(names = "--address", converter = AddressConverter.class, description = "address to serve on. can be tcp: localhost:7777 or unix socket: `/tmp/plugin.rpc.sock` (default \"${DEFAULT-VALUE}\")") @@ -52,31 +60,47 @@ public ServeCommand(Plugin plugin) { this.plugin = plugin; } - @Override - public Integer call() throws Exception { - // Initialize a logger - - // Configure open telemetry + private LoggerContext initLogger() { + ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout( + this.logFormat == "text" ? PatternLayout.createDefaultLayout() : JsonLayout.createDefaultLayout()); - // Configure test listener + Configuration configuration = ConfigurationFactory.newConfigurationBuilder().build(); + configuration.addAppender(appender); + LoggerConfig loggerConfig = new LoggerConfig("io.cloudquery", Level.getLevel(logLevel), false); + loggerConfig.addAppender(appender, null, null); + configuration.addLogger("io.cloudquery", loggerConfig); + LoggerContext context = new LoggerContext(ServeCommand.class.getName() + "Context"); + context.start(configuration); - // Configure gRPC server - Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create()). - addService(new DiscoverServer(DISCOVERY_VERSIONS)). - addService(new PluginServer(plugin)). - addService(ProtoReflectionService.newInstance()). - executor(Executors.newFixedThreadPool(10)). - build(); - - // Configure sentry - - // Log we are listening on address and port + logger = context.getLogger(ServeCommand.class.getName()); + return context; + } - // Run gRPC server and block - server.start(); - logger.log(Level.INFO, "Started server on {0}", address); - server.awaitTermination(); - return 0; + @Override + public Integer call() { + LoggerContext context = this.initLogger(); + + try { + // Configure open telemetry + // Configure test listener + // Configure gRPC server + Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create()) + .addService(new DiscoverServer(DISCOVERY_VERSIONS)).addService(new PluginServer(plugin)) + .addService(ProtoReflectionService.newInstance()).executor(Executors.newFixedThreadPool(10)) + .build(); + // Configure sentry + // Log we are listening on address and port + // Run gRPC server and block + server.start(); + logger.info("Started server on {}:{}", address.host(), address.port()); + server.awaitTermination(); + return 0; + } catch (IOException | InterruptedException e) { + logger.error("Failed to start server", e); + return 1; + } finally { + context.close(); + } } } diff --git a/lib/src/main/java/io/cloudquery/server/ServerException.java b/lib/src/main/java/io/cloudquery/server/ServerException.java deleted file mode 100644 index 6483e35..0000000 --- a/lib/src/main/java/io/cloudquery/server/ServerException.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.cloudquery.server; - -public class ServerException extends Exception { - public ServerException(String message) { - super(message); - } -} diff --git a/lib/src/test/java/io/cloudquery/server/PluginServeTest.java b/lib/src/test/java/io/cloudquery/server/PluginServeTest.java index 76acb2e..bf922e0 100644 --- a/lib/src/test/java/io/cloudquery/server/PluginServeTest.java +++ b/lib/src/test/java/io/cloudquery/server/PluginServeTest.java @@ -1,13 +1,12 @@ package io.cloudquery.server; +import io.cloudquery.memdb.MemDB; import io.cloudquery.plugin.Plugin; import io.cloudquery.server.PluginServe.PluginServeBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import java.util.List; - @Disabled(value = "blocking tests - only used manually to test the gRPC runs correctly") public class PluginServeTest { public static final String URL = "https://sentry.url"; @@ -16,41 +15,31 @@ public class PluginServeTest { @BeforeEach public void setUp() { - plugin = Plugin.builder("test-plugin", "0.1.0").build(); + plugin = new MemDB(); } @Test - public void simpleCallToServe() throws ServerException { - PluginServe pluginServe = new PluginServeBuilder(). - plugin(plugin). - sentryDSN(URL). - args(List.of("serve")). - build(); + public void simpleCallToServe() { + PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(new String[] { "serve" }).build(); pluginServe.Serve(); } @Test - public void simpleCallToServeHelp() throws ServerException { - PluginServe pluginServe = new PluginServeBuilder(). - plugin(plugin). - sentryDSN(URL). - args(List.of("serve", "--help")). - build(); + public void simpleCallToServeHelp() { + PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(new String[] { "serve", "--help" }) + .build(); pluginServe.Serve(); } @Test - public void simpleOverrideCommandLineArguments() throws ServerException { - PluginServe pluginServe = new PluginServeBuilder(). - plugin(plugin). - sentryDSN(URL). - args(List.of( - "serve", - "--address", "foo.bar.com:7777", - "--disable-sentry", - "--otel-endpoint", "some-endpoint" - )). - build(); + public void simpleOverrideCommandLineArguments() { + String[] args = new String[] { + "serve", + "--address", "foo.bar.com:7777", + "--disable-sentry", + "--otel-endpoint", "some-endpoint" + }; + PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(args).build(); pluginServe.Serve(); } } \ No newline at end of file