From c9cf67d3fdd7bf2349c7ca6e80b20fbd059935c3 Mon Sep 17 00:00:00 2001 From: ChristinaDsl Date: Mon, 15 Jul 2024 16:58:11 +0300 Subject: [PATCH] [WFCORE-6788] Display in cli when server is in read-only mode --- cli/src/main/java/org/jboss/as/cli/Util.java | 22 +++++++++++++++++++ .../as/cli/impl/CLIModelControllerClient.java | 4 ++-- .../jboss/as/cli/impl/CommandContextImpl.java | 4 ++++ .../ServerEnvironmentResourceDescription.java | 6 ++++- .../descriptions/LocalDescriptions.properties | 1 + .../management/cli/ColorOutputTestCase.java | 19 +++++++++++----- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cli/src/main/java/org/jboss/as/cli/Util.java b/cli/src/main/java/org/jboss/as/cli/Util.java index 024098a0b58..38ac63af698 100644 --- a/cli/src/main/java/org/jboss/as/cli/Util.java +++ b/cli/src/main/java/org/jboss/as/cli/Util.java @@ -1864,4 +1864,26 @@ public static String compactToString(ModelNode node) { return stringWriter.toString(); } + public static boolean isServerInReadOnlyMode(CommandContext ctx) { + final ModelControllerClient client = ctx.getModelControllerClient(); + final ModelNode operation = new ModelNode(); + ModelNode result; + ModelNode address = operation.get("address"); + + address.add("core-service", "server-environment"); + operation.get(Util.OPERATION).set(Util.READ_RESOURCE); + operation.get(Util.INCLUDE_RUNTIME).set(true); + + try { + result = client.execute(operation); + if (isSuccess(result)) { + ModelNode serverEnv = result.get("result"); + return serverEnv.get("read-only").asBoolean(); + } + } catch (Exception e) { + } + return false; + + } + } diff --git a/cli/src/main/java/org/jboss/as/cli/impl/CLIModelControllerClient.java b/cli/src/main/java/org/jboss/as/cli/impl/CLIModelControllerClient.java index 4fd45fbf121..4e80910cf21 100644 --- a/cli/src/main/java/org/jboss/as/cli/impl/CLIModelControllerClient.java +++ b/cli/src/main/java/org/jboss/as/cli/impl/CLIModelControllerClient.java @@ -98,8 +98,8 @@ public void shutdown() { private final AtomicInteger state = new AtomicInteger(CLOSED); CLIModelControllerClient(final ControllerAddress address, CallbackHandler handler, int connectionTimeout, - final ConnectionCloseHandler closeHandler, Map saslOptions, SecurityFactory sslContextFactory, - boolean fallbackSslContext, ProtocolTimeoutHandler timeoutHandler, String clientBindAddress) throws IOException { + final ConnectionCloseHandler closeHandler, Map saslOptions, SecurityFactory sslContextFactory, + boolean fallbackSslContext, ProtocolTimeoutHandler timeoutHandler, String clientBindAddress) throws IOException { this.handler = handler; this.closeHandler = closeHandler; diff --git a/cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java b/cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java index 37155a1fcb1..b0f52808fa5 100644 --- a/cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java +++ b/cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java @@ -1598,6 +1598,10 @@ String getPrompt() { } else { buffer.append("standalone@"); } + if (Util.isServerInReadOnlyMode(this)){ + buffer.append("(read-only)"); + printLine("Server is in read-only mode", "warning"); + } if (controllerHost != null) { buffer.append(controllerHost).append(':').append(getControllerPort()).append(' '); } else { diff --git a/server/src/main/java/org/jboss/as/server/ServerEnvironmentResourceDescription.java b/server/src/main/java/org/jboss/as/server/ServerEnvironmentResourceDescription.java index c05490447f8..ad8281d03ca 100644 --- a/server/src/main/java/org/jboss/as/server/ServerEnvironmentResourceDescription.java +++ b/server/src/main/java/org/jboss/as/server/ServerEnvironmentResourceDescription.java @@ -60,10 +60,11 @@ public class ServerEnvironmentResourceDescription extends SimpleResourceDefiniti static final AttributeDefinition PERMISSIBLE_STABILITY_LEVELS = new SimpleListAttributeDefinition.Builder("permissible-stability-levels", STABILITY) .setFlags(AttributeAccess.Flag.STORAGE_RUNTIME) .build(); + public static final AttributeDefinition READ_ONLY = SimpleAttributeDefinitionBuilder.create("read-only", ModelType.BOOLEAN).setFlags(AttributeAccess.Flag.STORAGE_RUNTIME).build(); private static final AttributeDefinition[] SERVER_ENV_ATTRIBUTES = { BASE_DIR, CONFIG_DIR, CONFIG_FILE, CONTENT_DIR, DATA_DIR, DEPLOY_DIR, EXT_DIRS, HOME_DIR, HOST_NAME, INITIAL_RUNNING_MODE, LAUNCH_TYPE, LOG_DIR, NODE_NAME, - QUALIFIED_HOST_NAME, SERVER_NAME, TEMP_DIR, START_SUSPENDED, GRACEFUL_STARTUP, STABILITY, PERMISSIBLE_STABILITY_LEVELS }; + QUALIFIED_HOST_NAME, SERVER_NAME, TEMP_DIR, START_SUSPENDED, GRACEFUL_STARTUP, READ_ONLY, STABILITY, PERMISSIBLE_STABILITY_LEVELS }; private final ServerEnvironmentReadHandler osh; @@ -176,6 +177,9 @@ public void execute(final OperationContext context, final ModelNode operation) t if (equals(name, START_SUSPENDED)) { result.set(environment.isStartSuspended()); } + if (equals(name, READ_ONLY)) { + result.set(environment.getServerConfigurationFile().getInteractionPolicy().isReadOnly()); + } if (equals(name, GRACEFUL_STARTUP)) { result.set(environment.isStartGracefully()); } else if (equals(name, STABILITY)) { diff --git a/server/src/main/resources/org/jboss/as/server/controller/descriptions/LocalDescriptions.properties b/server/src/main/resources/org/jboss/as/server/controller/descriptions/LocalDescriptions.properties index a5e83cebfe6..78736dab76a 100644 --- a/server/src/main/resources/org/jboss/as/server/controller/descriptions/LocalDescriptions.properties +++ b/server/src/main/resources/org/jboss/as/server/controller/descriptions/LocalDescriptions.properties @@ -57,6 +57,7 @@ server.env.server-name=The name of the server. server.env.temp-dir=The temporary directory. server.suspend-state=The suspend state of the server server.env.start-suspended=Start the server suspended. +server.env.read-only=Start the server in read-only mode. server.env.start-gracefully=Start the server gracefully. server.env.stability=The stability level of the server. server.env.permissible-stability-levels=A list of all the stability levels supported by this server. diff --git a/testsuite/standalone/src/test/java/org/jboss/as/test/integration/management/cli/ColorOutputTestCase.java b/testsuite/standalone/src/test/java/org/jboss/as/test/integration/management/cli/ColorOutputTestCase.java index 61e8e80a050..a0fa4af5070 100644 --- a/testsuite/standalone/src/test/java/org/jboss/as/test/integration/management/cli/ColorOutputTestCase.java +++ b/testsuite/standalone/src/test/java/org/jboss/as/test/integration/management/cli/ColorOutputTestCase.java @@ -21,6 +21,7 @@ public class ColorOutputTestCase { private static CliProcessWrapper cli; private static String hostAndPort = TestSuiteEnvironment.getServerAddress() + ":" + TestSuiteEnvironment.getServerPort(); + private static String serverMode; /** * Initialize CommandContext before all tests @@ -31,6 +32,12 @@ public static void init() { .addCliArgument("--connect") .addCliArgument("--controller=" + hostAndPort); cli.executeInteractive(); + + if (cli.getCurrentPrompt().trim().contains("read-only")) { + serverMode = "standalone\u001B[0m@\u001B[;94;109m(read-only)"; + } else { + serverMode = "standalone\u001B[0m@\u001B[;94;109m"; + } } /** @@ -54,7 +61,7 @@ public void requiredArguments() throws Exception { @Test public void cliPrompt() { - Assert.assertEquals("[\u001B[;94;109mstandalone\u001B[0m@\u001B[;94;109m" + hostAndPort + " \u001B[0m/]", + Assert.assertEquals("[\u001B[;94;109m" + serverMode + hostAndPort + " \u001B[0m/]", cli.getCurrentPrompt().trim()); } @@ -63,7 +70,7 @@ public void batchPrompt() throws Exception { cli.pushLineAndWaitForResults("batch"); try { - Assert.assertEquals("[\u001B[;94;109mstandalone\u001B[0m@\u001B[;94;109m" + hostAndPort + " \u001B[0m/\u001B[;92;109m #\u001B[0m]", + Assert.assertEquals("[\u001B[;94;109m" + serverMode + hostAndPort + " \u001B[0m/\u001B[;92;109m #\u001B[0m]", cli.getCurrentPrompt().trim()); } finally { cli.pushLineAndWaitForResults("discard-batch"); @@ -76,7 +83,7 @@ public void ifPrompt() throws Exception { cli.pushLineAndWaitForResults("if outcome==failed of /system-property=test:read-resource"); try { cli.pushLineAndWaitForResults("echo \"Not Exists\""); - Assert.assertEquals("[\u001B[;94;109mstandalone\u001B[0m@\u001B[;94;109m" + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", + Assert.assertEquals("[\u001B[;94;109m" + serverMode + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", cli.getCurrentPrompt().trim()); } finally { cli.pushLineAndWaitForResults("end-if"); @@ -89,7 +96,7 @@ public void forPrompt() throws Exception { cli.pushLineAndWaitForResults("for PROP in :read-children-names(child-type=system-property)"); try { cli.pushLineAndWaitForResults("echo $PROP"); - Assert.assertEquals("[\u001B[;94;109mstandalone\u001B[0m@\u001B[;94;109m" + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", + Assert.assertEquals("[\u001B[;94;109m" + serverMode + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", cli.getCurrentPrompt().trim()); } finally { cli.pushLineAndWaitForResults("done"); @@ -102,7 +109,7 @@ public void tryPrompt() throws Exception { cli.pushLineAndWaitForResults("try"); try { cli.pushLineAndWaitForResults("echo \"Trying\""); - Assert.assertEquals("[\u001B[;94;109mstandalone\u001B[0m@\u001B[;94;109m" + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", + Assert.assertEquals("[\u001B[;94;109m" + serverMode + hostAndPort + " \u001B[0m/\u001B[;92;109m ...\u001B[0m]", cli.getCurrentPrompt().trim()); } finally { cli.pushLineAndWaitForResults("finally"); @@ -133,4 +140,6 @@ public void longCommand() throws Exception { } } + + }