Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public void run(String[] argv) {
int exitCode = execute(argv);

if (exitCode != ExitCode.OK) {
ExitUtils.terminate(exitCode, null, null);
if (cmd.getOut() != null) {
cmd.getOut().println("Command executed with exit code: " + exitCode);
}
Comment on lines +81 to +83
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like extra output, why is this added?

if (System.getProperty("ozone.interactive.shell") == null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define a constant for "ozone.interactive.shell" and use in both places.

ExitUtils.terminate(exitCode, null, null);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ozone.shell;

import org.apache.hadoop.hdds.cli.GenericCli;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.shell.jline3.PicocliCommands.PicocliCommandsFactory;

/**
* Interactive Shell for all Ozone commands.
*/
public final class OzoneInteractiveShell {

private static final Logger LOG = LoggerFactory.getLogger(OzoneInteractiveShell.class);

private OzoneInteractiveShell() {
}

public static void main(String[] argv) throws Exception {
PicocliCommandsFactory factory = new PicocliCommandsFactory();
CommandLine topCmd = new CommandLine(new TopCommand(), factory);

// Add known subcommands statically if they are in the same module.
topCmd.addSubcommand("sh", new OzoneShell().getCmd());
topCmd.addSubcommand("tenant", new org.apache.hadoop.ozone.shell.tenant.TenantShell().getCmd());
topCmd.addSubcommand("s3", new org.apache.hadoop.ozone.shell.s3.S3Shell().getCmd());

// Dynamically add subcommands from other modules to avoid circular dependencies.
addDynamicSubcommand(topCmd, "admin", "org.apache.hadoop.ozone.admin.OzoneAdmin");
addDynamicSubcommand(topCmd, "debug", "org.apache.hadoop.ozone.debug.OzoneDebug");
addDynamicSubcommand(topCmd, "fs", "org.apache.hadoop.fs.ozone.OzoneFsShell");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs does not appear in help, probably because it's not implemented as GenericCli:

bash-5.1$ ozone interactive
ozone> help
 -  ozone registry
Summary: admin  Developer tools for Ozone Admin operations
         debug  Developer tools for Ozone Debug operations
         repair Advanced tool to repair Ozone. Check the --help output of the subcommand for
         s3     Shell for S3 specific operations
         sh     Shell for Ozone object store
         tenant Shell for multi-tenant specific operations

I think we can omit it for now, and maybe refactor OzoneFsShell later.

addDynamicSubcommand(topCmd, "repair", "org.apache.hadoop.ozone.repair.OzoneRepair");

Shell dummyShell = new Shell() {
@Override
public String name() {
return "ozone";
}

@Override
public String prompt() {
return "ozone";
}
};

new REPL(dummyShell, topCmd, factory, null);
}

private static void addDynamicSubcommand(CommandLine topCmd, String name, String className) {
try {
Class<?> clazz = Class.forName(className);
GenericCli instance = (GenericCli) clazz.getDeclaredConstructor().newInstance();
topCmd.addSubcommand(name, instance.getCmd());
} catch (Exception e) {
LOG.debug("Subcommand {} not loaded: class {} not found or could not be instantiated",
name, className, e);
}
}

@Command(name = "ozone", description = "Interactive Shell for all Ozone commands", mixinStandardHelpOptions = true)
private static class TopCommand implements Runnable {
@Override
public void run() {
// The top-level command is only used to group subcommands and has no execution logic itself.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class REPL {
}

String prompt = shell.prompt() + "> ";
System.setProperty("ozone.interactive.shell", "true");

final int batchSize = lines == null ? 0 : lines.size();
if (batchSize > 0) {
Expand Down
15 changes: 15 additions & 0 deletions hadoop-ozone/dist/src/shell/ozone/ozone
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ function ozonecmd_case
OZONE_CLASSNAME=org.apache.hadoop.security.token.DtUtilShell
OZONE_RUN_ARTIFACT_NAME="ozone-tools"
;;
interactive | --interactive)
OZONE_CLASSNAME=org.apache.hadoop.ozone.shell.OzoneInteractiveShell
OZONE_RUN_ARTIFACT_NAME="ozone-cli-shell"
OZONE_OPTS="${OZONE_OPTS} ${RATIS_OPTS} ${OZONE_MODULE_ACCESS_ARGS}"
# Add all CLI classpaths to support all subcommands dynamically
for cp_file in "ozone-cli-admin" "ozone-cli-debug" "ozone-cli-repair" "ozone-tools"; do
if [[ -f "${OZONE_HOME}/share/ozone/classpath/${cp_file}.classpath" ]]; then
ozone_add_classpath_from_file "${OZONE_HOME}/share/ozone/classpath/${cp_file}.classpath"
fi
done
Comment on lines +226 to +233
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create a new submodule to depend on all other CLI modules in the POM? Benefits:

  • can add all subcommands statically in OzoneInteractiveShell, without reflection
  • avoid the need for custom classpath management

;;
admin)
OZONE_CLASSNAME=org.apache.hadoop.ozone.admin.OzoneAdmin
OZONE_ADMIN_OPTS="${OZONE_ADMIN_OPTS} ${RATIS_OPTS} ${OZONE_MODULE_ACCESS_ARGS}"
Expand Down Expand Up @@ -318,6 +329,10 @@ fi

OZONE_SUBCMD=$1

if [[ "$OZONE_SUBCMD" == "--interactive" ]]; then
OZONE_SUBCMD="interactive"
fi

Comment on lines +332 to +335
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add --interactive for simplicity.

if [[ "$OZONE_SUBCMD" == "auditparser" ]] || [[ "$OZONE_SUBCMD" == "checknative" ]]; then
echo "warning: 'ozone $OZONE_SUBCMD' is deprecated, use 'ozone debug $OZONE_SUBCMD' instead."
OZONE_SUBCMD="debug"
Expand Down