Skip to content

feat: Init logger, add initial MemDB plugin #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/bin/main/io/cloudquery/glob/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions lib/bin/main/io/cloudquery/glob/README.md
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 4 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}

Expand Down Expand Up @@ -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')
Expand Down
12 changes: 12 additions & 0 deletions lib/src/main/java/io/cloudquery/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions lib/src/main/java/io/cloudquery/memdb/MemDB.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
14 changes: 5 additions & 9 deletions lib/src/main/java/io/cloudquery/plugin/Plugin.java
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions lib/src/main/java/io/cloudquery/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.cloudquery.scheduler;

public class Scheduler {
public Scheduler() {
}
}
7 changes: 0 additions & 7 deletions lib/src/main/java/io/cloudquery/server/DocCommand.java

This file was deleted.

20 changes: 3 additions & 17 deletions lib/src/main/java/io/cloudquery/server/PluginServe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
}
2 changes: 1 addition & 1 deletion lib/src/main/java/io/cloudquery/server/RootCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

@CommandLine.Command
public class RootCommand {
}
}
80 changes: 52 additions & 28 deletions lib/src/main/java/io/cloudquery/server/ServeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> {
private static final Logger logger = Logger.getLogger(ServeCommand.class.getName());
private static Logger logger;
public static final List<Integer> 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}\")")
Expand Down Expand Up @@ -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();
}
}

}
7 changes: 0 additions & 7 deletions lib/src/main/java/io/cloudquery/server/ServerException.java

This file was deleted.

41 changes: 15 additions & 26 deletions lib/src/test/java/io/cloudquery/server/PluginServeTest.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
}
}