Search in sources :

Example 1 with DomainCommandBuilder

use of org.wildfly.core.launcher.DomainCommandBuilder in project wildfly-core by wildfly.

the class LauncherTestCase method testDomainServerWithAgent.

@Test
public void testDomainServerWithAgent() throws Exception {
    final DomainCommandBuilder builder = DomainCommandBuilder.of(ServerHelper.JBOSS_HOME).addHostControllerJavaOptions(ServerHelper.DEFAULT_SERVER_JAVA_OPTS).setUseSecurityManager(parseProperty("security.manager"));
    final String localRepo = System.getProperty("maven.repo.local");
    if (localRepo != null) {
        builder.addHostControllerJavaOption("-Dmaven.repo.local=" + localRepo).addProcessControllerJavaOption("-Dmaven.repo.local=" + localRepo);
    }
    Process process = null;
    try {
        process = Launcher.of(builder).setRedirectErrorStream(true).redirectOutput(STDOUT_PATH).launch();
        ServerHelper.waitForDomain(process, LauncherTestCase::readStdout);
        // Start server-three, configure it, then stop it
        try (DomainClient client = DomainClient.Factory.create(TestSuiteEnvironment.getModelControllerClient())) {
            final String hostName = ServerHelper.determineHostName(client);
            client.startServer(hostName, "server-three");
            ServerHelper.waitForManagedServer(client, "server-three", LauncherTestCase::readStdout);
            final List<JsonObject> lines = ServerHelper.readLogFileFromModel("json.log", "host", hostName, "server", "server-three");
            Assert.assertEquals("Expected 2 lines found " + lines.size(), 2, lines.size());
            JsonObject msg = lines.get(0);
            Assert.assertEquals(LoggingAgent.MSG, msg.getString("message"));
            Assert.assertEquals("FINE", msg.getString("level"));
            msg = lines.get(1);
            Assert.assertEquals(LoggingAgent.MSG, msg.getString("message"));
            Assert.assertEquals("INFO", msg.getString("level"));
            ServerHelper.shutdownDomain(client);
        }
        process.waitFor(ServerHelper.TIMEOUT, TimeUnit.SECONDS);
        if (process.exitValue() != 0) {
            Assert.fail(readStdout());
        }
    } finally {
        ProcessHelper.destroyProcess(process);
    }
}
Also used : DomainClient(org.jboss.as.controller.client.helpers.domain.DomainClient) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 2 with DomainCommandBuilder

use of org.wildfly.core.launcher.DomainCommandBuilder in project wildfly-maven-plugin by wildfly.

the class DomainDeploymentManagerIT method startServer.

@BeforeClass
public static void startServer() throws Exception {
    boolean ok = false;
    try {
        client = DomainClient.Factory.create(Environment.createClient());
        if (ServerHelper.isDomainRunning(client) || ServerHelper.isStandaloneRunning(client)) {
            Assert.fail("A WildFly server is already running: " + ServerHelper.getContainerDescription(client));
        }
        final DomainCommandBuilder commandBuilder = DomainCommandBuilder.of(Environment.WILDFLY_HOME);
        if (IS_MODULAR_JDK) {
            commandBuilder.addHostControllerJavaOptions(MODULAR_JDK_ARGUMENTS);
        }
        process = Launcher.of(commandBuilder).launch();
        consoleConsomer = ConsoleConsumer.start(process, System.out);
        ServerHelper.waitForDomain(client, Environment.TIMEOUT);
        ok = true;
    } finally {
        if (!ok) {
            final Process p = process;
            final ModelControllerClient c = client;
            process = null;
            client = null;
            try {
                ProcessHelper.destroyProcess(p);
            } finally {
                safeClose(c);
            }
        }
    }
}
Also used : DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) BeforeClass(org.junit.BeforeClass)

Example 3 with DomainCommandBuilder

use of org.wildfly.core.launcher.DomainCommandBuilder in project wildfly-core by wildfly.

the class ServerConfigurator method configureDomain.

private static void configureDomain() throws IOException, InterruptedException {
    final Path stdout = Paths.get(TestSuiteEnvironment.getTmpDir(), "config-domain-stdout.txt");
    final DomainCommandBuilder builder = DomainCommandBuilder.of(ServerHelper.JBOSS_HOME).addHostControllerJavaOptions(ServerHelper.DEFAULT_SERVER_JAVA_OPTS);
    final String localRepo = System.getProperty("maven.repo.local");
    if (localRepo != null) {
        builder.addHostControllerJavaOption("-Dmaven.repo.local=" + localRepo).addProcessControllerJavaOption("-Dmaven.repo.local=" + localRepo);
    }
    Process process = null;
    try {
        process = Launcher.of(builder).setRedirectErrorStream(true).redirectOutput(stdout).launch();
        ServerHelper.waitForDomain(process, () -> readStdout(stdout));
        // Start server-three, configure it, then stop it
        try (DomainClient client = DomainClient.Factory.create(TestSuiteEnvironment.getModelControllerClient())) {
            final String hostName = ServerHelper.determineHostName(client);
            // Configure server-three to launch with an agent
            final CompositeOperationBuilder opBuilder = CompositeOperationBuilder.create();
            ModelNode address = Operations.createAddress("profile", "default");
            ModelNode op = Operations.createOperation("clone", address);
            op.get("to-profile").set("test");
            opBuilder.addStep(op);
            address = Operations.createAddress("server-group", "other-server-group");
            op = Operations.createWriteAttributeOperation(address, "profile", "test");
            opBuilder.addStep(op);
            address = Operations.createAddress("host", hostName, "server-config", "server-three", "jvm", "default");
            op = Operations.createAddOperation(address);
            op.get("module-options").setEmptyList().add("-javaagent:" + ServerHelper.JBOSS_HOME.resolve("logging-agent-tests.jar") + "=" + LoggingAgent.DEBUG_ARG);
            opBuilder.addStep(op);
            address = Operations.createAddress("profile", "test", "subsystem", "logging", "custom-formatter", "json");
            op = Operations.createAddOperation(address);
            op.get("class").set("org.jboss.logmanager.formatters.JsonFormatter");
            op.get("module").set("org.jboss.logmanager");
            final ModelNode properties = op.get("properties").setEmptyObject();
            properties.get("exceptionOutputType").set("FORMATTED");
            properties.get("prettyPrint").set(false);
            opBuilder.addStep(op);
            address = Operations.createAddress("profile", "test", "subsystem", "logging", "file-handler", "json");
            op = Operations.createAddOperation(address);
            op.get("append").set(false);
            op.get("level").set("DEBUG");
            op.get("autoflush").set(true);
            op.get("named-formatter").set("json");
            final ModelNode file = op.get("file");
            file.get("relative-to").set("jboss.server.log.dir");
            file.get("path").set("json.log");
            opBuilder.addStep(op);
            address = Operations.createAddress("profile", "test", "subsystem", "logging", "logger", "org.wildfly.common.test.LoggingAgent");
            op = Operations.createAddOperation(address);
            op.get("handlers").setEmptyList().add("json");
            op.get("level").set("DEBUG");
            opBuilder.addStep(op);
            executeOperation(client, opBuilder.build());
            client.startServer(hostName, "server-three");
            ServerHelper.waitForManagedServer(client, "server-three", () -> readStdout(stdout));
            ServerHelper.shutdownDomain(client);
        }
        if (!process.waitFor(ServerHelper.TIMEOUT, TimeUnit.SECONDS)) {
            Assert.fail(readStdout(stdout));
        }
        if (process.exitValue() != 0) {
            Assert.fail(readStdout(stdout));
        }
    } finally {
        ProcessHelper.destroyProcess(process);
    }
}
Also used : Path(java.nio.file.Path) DomainClient(org.jboss.as.controller.client.helpers.domain.DomainClient) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) CompositeOperationBuilder(org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder) ModelNode(org.jboss.dmr.ModelNode)

Example 4 with DomainCommandBuilder

use of org.wildfly.core.launcher.DomainCommandBuilder in project wildfly-core by wildfly.

the class DomainLifecycleUtil method start.

/**
 * Starts a {@code ProcessController} process configured according to the provided {@link #getConfiguration() configuration}.
 * The {@code ProcessController} will start a {@code HostController} process, which, if it is so configured, will
 * start managed servers. This method will block until any such servers are started.
 *
 * @throws IllegalStateException if {@link #close()} has previously been invoked on this instance.
 */
public void start() {
    try {
        log.info("Starting HostController (host = " + configuration.getHostName() + ")");
        configuration.validate();
        final String address = NetworkUtils.formatPossibleIpv6Address(configuration.getHostControllerManagementAddress());
        final int port = configuration.getHostControllerManagementPort();
        final URI connectionURI = new URI(configuration.getHostControllerManagementProtocol(), null, address, port, null, null, null);
        // Create the connection - this will try to connect on the first request
        connection = clientConfiguration.createConnection(connectionURI, configuration.getCallbackHandler());
        final DomainCommandBuilder commandBuilder;
        final String jbossHome = configuration.getJbossHome();
        if (configuration.getControllerJavaHome() == null) {
            commandBuilder = DomainCommandBuilder.of(jbossHome);
        } else {
            commandBuilder = DomainCommandBuilder.of(jbossHome, configuration.getControllerJavaHome());
        }
        final String jbossOptions = System.getProperty("jboss.options");
        if (jbossOptions != null) {
            final String[] javaOpts = jbossOptions.split("\\s+");
            commandBuilder.setHostControllerJavaOptions(javaOpts).setProcessControllerJavaOptions(javaOpts);
        }
        if (configuration.getJavaVmArguments() != null) {
            final String[] javaOpts = configuration.getJavaVmArguments().split("\\s+");
            commandBuilder.addHostControllerJavaOptions(javaOpts).addProcessControllerJavaOptions(javaOpts);
        }
        if (configuration.isEnableAssertions()) {
            commandBuilder.addProcessControllerJavaOption("-ea");
            commandBuilder.addHostControllerJavaOption("-ea");
        // This doesn't work; the HC Main class treats this as an invalid param
        // commandBuilder.addServerArgument("-ea");
        }
        final String jbossArgs = System.getProperty("jboss.domain.server.args");
        if (jbossArgs != null && !jbossArgs.isEmpty()) {
            commandBuilder.addServerArguments(jbossArgs.split("\\s+"));
        }
        // Set the Java Home for the servers
        if (configuration.getJavaHome() != null) {
            commandBuilder.setServerJavaHome(configuration.getJavaHome());
        }
        if (configuration.getDomainDirectory() != null) {
            commandBuilder.setBaseDirectory(configuration.getDomainDirectory());
        }
        if (configuration.getModulePath() != null && !configuration.getModulePath().isEmpty()) {
            commandBuilder.setModuleDirs(configuration.getModulePath().split(Pattern.quote(File.pathSeparator)));
        }
        final Path domainDir = commandBuilder.getBaseDirectory();
        final Path configDir = commandBuilder.getConfigurationDirectory();
        if (configuration.getMgmtUsersFile() != null) {
            copyConfigFile(configuration.getMgmtUsersFile(), configDir, null);
        } else {
            // No point backing up the file in a test scenario, just write what we need.
            final String text = "testSuite=29a64f8524f32269aa9b681efc347f96\n" + "slave=" + new UsernamePasswordHashUtil().generateHashedHexURP(SLAVE_HOST_USERNAME, "ManagementRealm", SLAVE_HOST_PASSWORD.toCharArray());
            createFile(configDir.resolve("mgmt-users.properties"), text);
        }
        if (configuration.getMgmtGroupsFile() != null) {
            copyConfigFile(configuration.getMgmtGroupsFile(), configDir, null);
        } else {
            // Put out empty mgmt-groups.properties.
            createFile(configDir.resolve("mgmt-groups.properties"), "# Management groups");
        }
        // Put out empty application realm properties files so servers don't complain
        createFile(configDir.resolve("application-users.properties"), "# Application users");
        createFile(configDir.resolve("application-roles.properties"), "# Application roles");
        // Copy the logging.properties file
        copyConfigFile(Paths.get(jbossHome, "domain", "configuration", "logging.properties"), configDir, null);
        final List<String> ipv6Args = new ArrayList<>();
        TestSuiteEnvironment.getIpv6Args(ipv6Args);
        commandBuilder.addHostControllerJavaOptions(ipv6Args).addProcessControllerJavaOptions(ipv6Args);
        if (configuration.getHostCommandLineProperties() != null) {
            commandBuilder.addHostControllerJavaOptions(configuration.getHostCommandLineProperties().split("\\s+"));
        }
        if (configuration.isAdminOnly()) {
            commandBuilder.setAdminOnly();
        }
        if (configuration.isBackupDC()) {
            commandBuilder.setBackup();
        }
        if (configuration.isCachedDC()) {
            commandBuilder.setCachedDomainController();
        }
        if (configuration.getDomainConfigFile() != null) {
            final String prefix = configuration.isCachedDC() ? null : "testing-";
            String name;
            if (configuration.isRewriteConfigFiles()) {
                name = copyConfigFile(configuration.getDomainConfigFile(), configDir, prefix);
            } else {
                name = configDir.resolve(prefix + Paths.get(configuration.getDomainConfigFile()).getFileName()).getFileName().toString();
            }
            if (configuration.isReadOnlyDomain()) {
                commandBuilder.setReadOnlyDomainConfiguration(name);
            } else if (!configuration.isCachedDC()) {
                commandBuilder.setDomainConfiguration(name);
            }
        }
        if (configuration.getHostConfigFile() != null) {
            final String prefix = "testing-";
            String name;
            if (configuration.isRewriteConfigFiles()) {
                name = copyConfigFile(configuration.getHostConfigFile(), configDir, prefix);
            } else {
                name = configDir.resolve(prefix + Paths.get(configuration.getHostConfigFile()).getFileName()).getFileName().toString();
            }
            if (configuration.isReadOnlyHost()) {
                commandBuilder.setReadOnlyHostConfiguration(name);
            } else {
                commandBuilder.setHostConfiguration(name);
            }
        }
        if (configuration.getHostControllerManagementAddress() != null) {
            commandBuilder.setInterProcessHostControllerAddress(configuration.getHostControllerManagementAddress());
            commandBuilder.setProcessControllerAddress(configuration.getHostControllerManagementAddress());
        }
        // the process working dir
        final String workingDir = domainDir.toString();
        // Start the process
        final ProcessWrapper wrapper = new ProcessWrapper(configuration.getHostName(), commandBuilder, Collections.<String, String>emptyMap(), workingDir);
        wrapper.start();
        process = wrapper;
        long start = System.currentTimeMillis();
        if (!configuration.isAdminOnly()) {
            // Wait a bit to let HC get going
            TimeUnit.SECONDS.sleep(2);
            // Wait for the servers to be started
            awaitServers(start);
            log.info("All servers started in " + (System.currentTimeMillis() - start) + " ms");
        }
        // Wait for the HC to be in running state. Normally if all servers are started, this is redundant
        // but there may not be any servers or we may be in --admin-only mode
        awaitHostController(start);
        log.info("HostController (host = " + configuration.getHostName() + ") started in " + (System.currentTimeMillis() - start) + " ms");
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("Could not start container", e);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException) ConfigXMLParseException(org.wildfly.client.config.ConfigXMLParseException) GeneralSecurityException(java.security.GeneralSecurityException) CancellationException(java.util.concurrent.CancellationException) OperationFailedException(org.jboss.as.controller.OperationFailedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) UsernamePasswordHashUtil(org.wildfly.security.sasl.util.UsernamePasswordHashUtil)

Example 5 with DomainCommandBuilder

use of org.wildfly.core.launcher.DomainCommandBuilder in project wildfly-maven-plugin by wildfly.

the class StartMojo method createDomainCommandBuilder.

private CommandBuilder createDomainCommandBuilder(final Path jbossHome) throws MojoExecutionException {
    final Path javaHome = (this.javaHome == null ? Paths.get(System.getProperty("java.home")) : Paths.get(this.javaHome));
    final DomainCommandBuilder commandBuilder = DomainCommandBuilder.of(jbossHome, javaHome).addModuleDirs(modulesPath.getModulePaths());
    // Set the JVM options
    if (Utils.isNotNullOrEmpty(javaOpts)) {
        commandBuilder.setProcessControllerJavaOptions(javaOpts).setHostControllerJavaOptions(javaOpts);
    }
    if (domainConfig != null) {
        commandBuilder.setDomainConfiguration(domainConfig);
    }
    if (hostConfig != null) {
        commandBuilder.setHostConfiguration(hostConfig);
    }
    if (propertiesFile != null) {
        commandBuilder.setPropertiesFile(propertiesFile);
    }
    if (serverArgs != null) {
        commandBuilder.addServerArguments(serverArgs);
    }
    // Workaround for WFCORE-4121
    if (Environment.isModularJvm(javaHome)) {
        commandBuilder.addHostControllerJavaOptions(Environment.getModularJvmArguments());
        commandBuilder.addProcessControllerJavaOptions(Environment.getModularJvmArguments());
    }
    // Print some server information
    final Log log = getLog();
    log.info("JAVA_HOME : " + commandBuilder.getJavaHome());
    log.info("JBOSS_HOME: " + commandBuilder.getWildFlyHome());
    log.info("JAVA_OPTS : " + Utils.toString(commandBuilder.getHostControllerJavaOptions(), " "));
    try {
        addUsers(commandBuilder.getWildFlyHome(), commandBuilder.getJavaHome());
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to add users", e);
    }
    return commandBuilder;
}
Also used : Path(java.nio.file.Path) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Log(org.apache.maven.plugin.logging.Log) IOException(java.io.IOException)

Aggregations

DomainCommandBuilder (org.wildfly.core.launcher.DomainCommandBuilder)6 Path (java.nio.file.Path)3 IOException (java.io.IOException)2 DomainClient (org.jboss.as.controller.client.helpers.domain.DomainClient)2 URI (java.net.URI)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JsonObject (javax.json.JsonObject)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 Log (org.apache.maven.plugin.logging.Log)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 CompositeOperationBuilder (org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder)1 ModelNode (org.jboss.dmr.ModelNode)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 ConfigXMLParseException (org.wildfly.client.config.ConfigXMLParseException)1