Skip to content
Merged
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
@@ -0,0 +1,84 @@
/*
* 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, "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.
}
}
}
12 changes: 12 additions & 0 deletions hadoop-ozone/dist/src/shell/ozone/ozone
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function ozone_usage
ozone_add_subcommand "insight" client "tool to get runtime operation information"
ozone_add_subcommand "version" client "print the version"
ozone_add_subcommand "dtutil" client "operations related to delegation tokens"
ozone_add_subcommand "interactive" client "interactive shell for ozone commands"
ozone_add_subcommand "admin" client "Ozone admin tool"
ozone_add_subcommand "debug" client "Ozone debug tool"
ozone_add_subcommand "repair" client "Ozone repair tool"
Expand Down Expand Up @@ -221,6 +222,17 @@ function ozonecmd_case
OZONE_CLASSNAME=org.apache.hadoop.security.token.DtUtilShell
OZONE_RUN_ARTIFACT_NAME="ozone-tools"
;;
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 +227 to +234
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, it sounds better and requires some refactoring. Would you suggest doing that in this PR, or should we land the current implementation first and refactor it in a separate PR?

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.

Created HDDS-15185 for follow-up.

;;
admin)
OZONE_CLASSNAME=org.apache.hadoop.ozone.admin.OzoneAdmin
OZONE_ADMIN_OPTS="${OZONE_ADMIN_OPTS} ${RATIS_OPTS} ${OZONE_MODULE_ACCESS_ARGS}"
Expand Down
Loading