Skip to content
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

[WFCORE-6788] Display in cli when server is in read-only mode #6139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions cli/src/main/java/org/jboss/as/cli/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> saslOptions, SecurityFactory<SSLContext> sslContextFactory,
boolean fallbackSslContext, ProtocolTimeoutHandler timeoutHandler, String clientBindAddress) throws IOException {
final ConnectionCloseHandler closeHandler, Map<String, String> saslOptions, SecurityFactory<SSLContext> sslContextFactory,
boolean fallbackSslContext, ProtocolTimeoutHandler timeoutHandler, String clientBindAddress) throws IOException {
this.handler = handler;
this.closeHandler = closeHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
}
}

/**
Expand All @@ -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());
}

Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -133,4 +140,6 @@ public void longCommand() throws Exception {
}
}



}