Search in sources :

Example 1 with Config

use of com.torodb.standalone.config.model.Config in project torodb by torodb.

the class ConfigTest method testParseWithXML.

@Test
public void testParseWithXML() throws Exception {
    CliConfig cliConfig = new CliConfig() {

        @Override
        public boolean hasXmlConfFile() {
            return true;
        }

        @Override
        public InputStream getXmlConfInputStream() {
            return ConfigTest.class.getResourceAsStream("/test-parse-with-xml.xml");
        }
    };
    Config config = CliConfigUtils.readConfig(cliConfig);
    Assert.assertTrue("/generic not defined", config.getGeneric() != null);
    Assert.assertTrue("/generic/logPackages not defined", config.getGeneric().getLogPackages() != null);
    Assert.assertTrue("/generic/logPackages/com.torodb not defined", config.getGeneric().getLogPackages().get("com.torodb") != null);
    Assert.assertEquals("/generic/logLevel has different value than that specified", LogLevel.NONE, config.getGeneric().getLogLevel());
    Assert.assertEquals("/generic/logPackages has not 1 entry", 1, config.getGeneric().getLogPackages().size());
    Assert.assertEquals("/generic/logPackages/com.torodb has different value than that specified", LogLevel.DEBUG, config.getGeneric().getLogPackages().get("com.torodb"));
    Assert.assertTrue("/protocol not defined", config.getProtocol() != null);
    Assert.assertTrue("/protocol/mongo not defined", config.getProtocol().getMongo() != null);
    Assert.assertTrue("/protocol/mongo/net not defined", config.getProtocol().getMongo().getNet() != null);
    Assert.assertTrue("/protocol/mongo/replication not defined", config.getProtocol().getMongo().getReplication() != null);
    Assert.assertEquals("/protocol/mongo/net/port has different value than that specified", Integer.valueOf(27019), config.getProtocol().getMongo().getNet().getPort());
    Assert.assertEquals("/protocol/mongo/replication has not 1 element", 1, config.getProtocol().getMongo().getReplication().size());
    Assert.assertEquals("/protocol/mongo/replication/0/replSetName has different value than that specified", "rs1", config.getProtocol().getMongo().getReplication().get(0).getReplSetName());
    Assert.assertEquals("/protocol/mongo/replication/0/role has different value than that specified", Role.HIDDEN_SLAVE, config.getProtocol().getMongo().getReplication().get(0).getRole());
    Assert.assertEquals("/protocol/mongo/replication/0/syncSource has different value than that specified", "localhost:27017", config.getProtocol().getMongo().getReplication().get(0).getSyncSource());
    Assert.assertTrue("/backend not defined", config.getBackend() != null);
    Assert.assertEquals("/backend/postgres not defined", Postgres.class, config.getBackend().getBackendImplementation().getClass());
    Assert.assertTrue("/backend/postgres not identified as AbstractPostgres", config.getBackend().is(Postgres.class));
    Assert.assertTrue("/backend/postgres not identified as AbstractPostgres Like", config.getBackend().isLike(Postgres.class));
    Assert.assertEquals("/backend/postgres/host has different value than that specified", "localhost", config.getBackend().as(Postgres.class).getHost());
    Assert.assertEquals("/backend/postgres/port has different value than that specified", Integer.valueOf(5432), config.getBackend().as(Postgres.class).getPort());
    Assert.assertEquals("/backend/postgres/user has different value than that specified", "root", config.getBackend().as(Postgres.class).getUser());
    Assert.assertEquals("/backend/postgres/password has different value than that specified", null, config.getBackend().as(Postgres.class).getPassword());
}
Also used : Config(com.torodb.standalone.config.model.Config) CliConfig(com.torodb.standalone.CliConfig) CliConfig(com.torodb.standalone.CliConfig) Postgres(com.torodb.standalone.config.model.backend.postgres.Postgres) Test(org.junit.Test)

Example 2 with Config

use of com.torodb.standalone.config.model.Config in project torodb by torodb.

the class BootstrapModule method configure.

@Override
protected void configure() {
    binder().requireExplicitBindings();
    install(new PackagingModule(clock));
    install(new CoreModule());
    install(new ExecutorServicesModule());
    install(new ConcurrentModule());
    install(new MetainfModule());
    install(new MetricsModule(config.getGeneric()));
    install(new BackendMultiImplementationModule(config.getProtocol().getMongo(), config.getGeneric(), config.getBackend().getBackendImplementation(), new BackendPostgresImplementationModule(), new BackendDerbyImplementationModule()));
    bind(Config.class).toInstance(config);
    bind(MongodServerConfig.class).toInstance(new MongodServerConfig(HostAndPort.fromParts("localhost", 27017)));
    bind(BuildProperties.class).to(DefaultBuildProperties.class).asEagerSingleton();
}
Also used : PackagingModule(com.torodb.packaging.guice.PackagingModule) ExecutorServicesModule(com.torodb.packaging.guice.ExecutorServicesModule) Config(com.torodb.standalone.config.model.Config) MongodServerConfig(com.torodb.mongodb.core.MongodServerConfig) BackendMultiImplementationModule(com.torodb.packaging.guice.BackendMultiImplementationModule) MetricsModule(com.torodb.core.metrics.guice.MetricsModule) BackendDerbyImplementationModule(com.torodb.packaging.guice.BackendDerbyImplementationModule) ConcurrentModule(com.torodb.concurrent.guice.ConcurrentModule) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) CoreModule(com.torodb.core.guice.CoreModule) MetainfModule(com.torodb.metainfo.guice.MetainfModule) BackendPostgresImplementationModule(com.torodb.packaging.guice.BackendPostgresImplementationModule) MongodServerConfig(com.torodb.mongodb.core.MongodServerConfig)

Example 3 with Config

use of com.torodb.standalone.config.model.Config in project torodb by torodb.

the class Main method main.

public static void main(String[] args) throws Exception {
    Console console = JCommander.getConsole();
    ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
    final CliConfig cliConfig = new CliConfig();
    JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
    jCommander.setColumnSize(Integer.MAX_VALUE);
    if (cliConfig.isHelp()) {
        jCommander.usage();
        System.exit(0);
    }
    if (cliConfig.isHelpParam()) {
        console.println(cliBundle.getString("help-param-header"));
        ResourceBundle configBundle = PropertyResourceBundle.getBundle("ConfigMessages");
        ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, configBundle, console, 0);
        System.exit(0);
    }
    final Config config = CliConfigUtils.readConfig(cliConfig);
    if (cliConfig.isPrintConfig()) {
        ConfigUtils.printYamlConfig(config, console);
        System.exit(0);
    }
    if (cliConfig.isPrintXmlConfig()) {
        ConfigUtils.printXmlConfig(config, console);
        System.exit(0);
    }
    configureLogger(cliConfig, config);
    config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {

        @Override
        public void visit(AbstractDerby value) {
            parseToropassFile(value);
        }

        @Override
        public void visit(AbstractPostgres value) {
            parseToropassFile(value);
        }

        public void parseToropassFile(BackendPasswordConfig value) {
            try {
                ConfigUtils.parseToropassFile(value);
            } catch (Exception ex) {
                throw new SystemException(ex);
            }
        }
    });
    if (config.getProtocol().getMongo().getReplication() != null) {
        for (AbstractReplication replication : config.getProtocol().getMongo().getReplication()) {
            if (replication.getAuth().getUser() != null) {
                HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
                ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                    @Override
                    public void setPassword(String password) {
                        replication.getAuth().setPassword(password);
                    }

                    @Override
                    public String getUser() {
                        return replication.getAuth().getUser();
                    }

                    @Override
                    public Integer getPort() {
                        return syncSource.getPort();
                    }

                    @Override
                    public String getPassword() {
                        return replication.getAuth().getPassword();
                    }

                    @Override
                    public String getMongopassFile() {
                        return config.getProtocol().getMongo().getMongopassFile();
                    }

                    @Override
                    public String getHost() {
                        return syncSource.getHostText();
                    }

                    @Override
                    public String getDatabase() {
                        return replication.getAuth().getSource();
                    }
                });
            }
        }
    }
    if (config.getBackend().isLike(AbstractPostgres.class)) {
        AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + postgres.getUser() + " password:");
            postgres.setPassword(readPwd());
        }
    } else if (config.getBackend().isLike(AbstractDerby.class)) {
        AbstractDerby derby = config.getBackend().as(AbstractDerby.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + derby.getUser() + " password:");
            derby.setPassword(readPwd());
        }
    }
    try {
        Clock clock = Clock.systemDefaultZone();
        Service server;
        if (config.getProtocol().getMongo().getReplication() == null || config.getProtocol().getMongo().getReplication().isEmpty()) {
            Service toroDbServer = ToroDbBootstrap.createStandaloneService(config, clock);
            toroDbServer.startAsync();
            toroDbServer.awaitRunning();
            server = toroDbServer;
        } else {
            throw new UnsupportedOperationException("Replication not supported yet!");
        }
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            server.stopAsync();
            server.awaitTerminated();
        }));
    } catch (CreationException ex) {
        ex.getErrorMessages().stream().forEach(m -> {
            if (m.getCause() != null) {
                LOGGER.error(m.getCause().getMessage());
            } else {
                LOGGER.error(m.getMessage());
            }
        });
        System.exit(1);
    } catch (Throwable ex) {
        LOGGER.error("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}
Also used : Charsets(com.google.common.base.Charsets) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) JCommander(com.beust.jcommander.JCommander) Throwables(com.google.common.base.Throwables) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) Console(com.beust.jcommander.internal.Console) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) HostAndPort(com.google.common.net.HostAndPort) Service(com.google.common.util.concurrent.Service) SystemException(com.torodb.core.exceptions.SystemException) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) ConfigUtils(com.torodb.packaging.config.util.ConfigUtils) CreationException(com.google.inject.CreationException) Logger(org.apache.logging.log4j.Logger) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) Clock(java.time.Clock) Log4jUtils(com.torodb.packaging.util.Log4jUtils) LogManager(org.apache.logging.log4j.LogManager) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) InputStream(java.io.InputStream) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) Service(com.google.common.util.concurrent.Service) CreationException(com.google.inject.CreationException) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) Clock(java.time.Clock) IOException(java.io.IOException) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) HostAndPort(com.google.common.net.HostAndPort) SystemException(com.torodb.core.exceptions.SystemException) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig)

Example 4 with Config

use of com.torodb.standalone.config.model.Config in project torodb by torodb.

the class ConfigTest method testReplicationFiltering.

@Test
public void testReplicationFiltering() throws Exception {
    CliConfig cliConfig = new CliConfig() {

        @Override
        public boolean hasConfFile() {
            return true;
        }

        @Override
        public InputStream getConfInputStream() {
            return ConfigTest.class.getResourceAsStream("/test-parse-with-yaml.yml");
        }

        @Override
        public List<String> getParams() {
            String[] params = new String[] { "/protocol/mongo/replication/0/include={torodb: [postgres, derby]}", "/protocol/mongo/replication/0/exclude={mongodb: {mmapv1, wiredtiger}}" };
            return Arrays.asList(params);
        }
    };
    Config config = CliConfigUtils.readConfig(cliConfig);
    Assert.assertTrue("/generic not defined", config.getGeneric() != null);
    Assert.assertTrue("/generic/logPackages not defined", config.getGeneric().getLogPackages() != null);
    Assert.assertTrue("/generic/logPackages/com.torodb not defined", config.getGeneric().getLogPackages().get("com.torodb") != null);
    Assert.assertEquals("/generic/logLevel has different value than that specified", LogLevel.NONE, config.getGeneric().getLogLevel());
    Assert.assertEquals("/generic/logPackages has not 1 entry", 1, config.getGeneric().getLogPackages().size());
    Assert.assertEquals("/generic/logPackages/com.torodb has different value than that specified", LogLevel.DEBUG, config.getGeneric().getLogPackages().get("com.torodb"));
    Assert.assertTrue("/protocol not defined", config.getProtocol() != null);
    Assert.assertTrue("/protocol/mongo not defined", config.getProtocol().getMongo() != null);
    Assert.assertTrue("/protocol/mongo/net not defined", config.getProtocol().getMongo().getNet() != null);
    Assert.assertTrue("/protocol/mongo/replication not defined", config.getProtocol().getMongo().getReplication() != null);
    Assert.assertEquals("/protocol/mongo/net/port has different value than that specified", Integer.valueOf(27019), config.getProtocol().getMongo().getNet().getPort());
    Assert.assertEquals("/protocol/mongo/replication has not 1 element", 1, config.getProtocol().getMongo().getReplication().size());
    Assert.assertEquals("/protocol/mongo/replication/0/replSetName has different value than that specified", "rs1", config.getProtocol().getMongo().getReplication().get(0).getReplSetName());
    Assert.assertEquals("/protocol/mongo/replication/0/role has different value than that specified", Role.HIDDEN_SLAVE, config.getProtocol().getMongo().getReplication().get(0).getRole());
    Assert.assertEquals("/protocol/mongo/replication/0/syncSource has different value than that specified", "localhost:27017", config.getProtocol().getMongo().getReplication().get(0).getSyncSource());
    Assert.assertTrue("/protocol/mongo/replication/0/include not defined", config.getProtocol().getMongo().getReplication().get(0).getInclude() != null);
    Assert.assertTrue("/protocol/mongo/replication/0/include/torodb not defined", config.getProtocol().getMongo().getReplication().get(0).getInclude().get("torodb") != null);
    Assert.assertEquals("/protocol/mongo/replication/0/include/torodb has different value than that specified", ImmutableMap.of("postgres", ImmutableList.of(), "derby", ImmutableList.of()), config.getProtocol().getMongo().getReplication().get(0).getInclude().get("torodb"));
    Assert.assertTrue("/protocol/mongo/replication/0/exclude not defined", config.getProtocol().getMongo().getReplication().get(0).getExclude() != null);
    Assert.assertTrue("/protocol/mongo/replication/0/exclude/mongodb not defined", config.getProtocol().getMongo().getReplication().get(0).getExclude().get("mongodb") != null);
    Assert.assertEquals("/protocol/mongo/replication/0/exclude/mongodb has different value than that specified", ImmutableMap.of("mmapv1", ImmutableList.of(), "wiredtiger", ImmutableList.of()), config.getProtocol().getMongo().getReplication().get(0).getExclude().get("mongodb"));
    Assert.assertTrue("/backend not defined", config.getBackend() != null);
    Assert.assertEquals("/backend/postgres not defined", Postgres.class, config.getBackend().getBackendImplementation().getClass());
    Assert.assertTrue("/backend/postgres not identified as AbstractPostgres", config.getBackend().is(Postgres.class));
    Assert.assertTrue("/backend/postgres not identified as AbstractPostgres Like", config.getBackend().isLike(Postgres.class));
    Assert.assertEquals("/backend/postgres/host has different value than that specified", "localhost", config.getBackend().as(Postgres.class).getHost());
    Assert.assertEquals("/backend/postgres/port has different value than that specified", Integer.valueOf(5432), config.getBackend().as(Postgres.class).getPort());
    Assert.assertEquals("/backend/postgres/user has different value than that specified", "root", config.getBackend().as(Postgres.class).getUser());
    Assert.assertEquals("/backend/postgres/password specified but should have not been read from parameters", null, config.getBackend().as(Postgres.class).getPassword());
}
Also used : Config(com.torodb.standalone.config.model.Config) CliConfig(com.torodb.standalone.CliConfig) CliConfig(com.torodb.standalone.CliConfig) Postgres(com.torodb.standalone.config.model.backend.postgres.Postgres) Test(org.junit.Test)

Example 5 with Config

use of com.torodb.standalone.config.model.Config in project torodb by torodb.

the class ConfigTest method testPrintConf.

@Test
public void testPrintConf() throws Exception {
    ByteArrayConsole byteArrayConsole = new ByteArrayConsole();
    ConfigUtils.printYamlConfig(new Config(), byteArrayConsole);
    ConfigUtils.readConfigFromYaml(Config.class, new String(byteArrayConsole.getByteArrayOutputStream().toByteArray()));
}
Also used : Config(com.torodb.standalone.config.model.Config) CliConfig(com.torodb.standalone.CliConfig) Test(org.junit.Test)

Aggregations

Config (com.torodb.standalone.config.model.Config)16 CliConfig (com.torodb.standalone.CliConfig)12 Test (org.junit.Test)12 Postgres (com.torodb.standalone.config.model.backend.postgres.Postgres)5 Derby (com.torodb.standalone.config.model.backend.derby.Derby)3 InputStream (java.io.InputStream)2 JCommander (com.beust.jcommander.JCommander)1 Console (com.beust.jcommander.internal.Console)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 Charsets (com.google.common.base.Charsets)1 Throwables (com.google.common.base.Throwables)1 HostAndPort (com.google.common.net.HostAndPort)1 Service (com.google.common.util.concurrent.Service)1 CreationException (com.google.inject.CreationException)1 ConcurrentModule (com.torodb.concurrent.guice.ConcurrentModule)1 SystemException (com.torodb.core.exceptions.SystemException)1 CoreModule (com.torodb.core.guice.CoreModule)1 MetricsModule (com.torodb.core.metrics.guice.MetricsModule)1