Skip to content

Commit

Permalink
[INLONG-4972][TubeMQ] Add a command-line tool for TubeMQ (#8835)
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycoderzf authored Sep 6, 2023
1 parent e3b4cc4 commit 4ddf19c
Show file tree
Hide file tree
Showing 16 changed files with 1,850 additions and 0 deletions.
40 changes: 40 additions & 0 deletions inlong-tubemq/bin/tubectl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

#
# 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.
#

if [ -z "$BASE_DIR" ] ; then
PRG="$0"

# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
BASE_DIR=`dirname "$PRG"`/..

# make it fully qualified
BASE_DIR=`cd "$BASE_DIR" && pwd`
#echo "TubeMQ master is at $BASE_DIR"
fi
source $BASE_DIR/bin/env.sh
$JAVA $TOOLS_ARGS org.apache.inlong.tubemq.server.tools.cli.CommandToolMain $@
31 changes: 31 additions & 0 deletions inlong-tubemq/bin/tubectl.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@rem
@rem Licensed to the Apache Software Foundation (ASF) under one
@rem or more contributor license agreements. See the NOTICE file
@rem distributed with this work for additional information
@rem regarding copyright ownership. The ASF licenses this file
@rem to you under the Apache License, Version 2.0 (the
@rem "License"); you may not use this file except in compliance
@rem with the License. You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing,
@rem software distributed under the License is distributed on an
@rem "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@rem KIND, either express or implied. See the License for the
@rem specific language governing permissions and limitations
@rem under the License.
@rem

REM Windows Command-line Tool for TubeMQ
REM please do not change any command or variable in this script, check out
REM env.cmd for details.

setlocal
call "%~dp0env.cmd"

set TUBECTLMAIN=org.apache.inlong.tubemq.server.tools.cli.CommandToolMain

echo on
call %JAVA% %MASTER_JVM_OPTS% %GENERIC_ARGS% "%TUBECTLMAIN%" %@
endlocal
4 changes: 4 additions & 0 deletions inlong-tubemq/tubemq-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.inlong.tubemq.server.tools.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;

/**
* Class for parse command.
*/
public abstract class AbstractCommand {

protected final JCommander jcommander;

@Parameter(names = {"-h", "--help"}, help = true, hidden = true)
private boolean help;

public AbstractCommand(String cmdName) {
jcommander = new JCommander();
jcommander.setProgramName("tubectl " + cmdName);
}

public boolean run(String[] args) {

if (help) {
jcommander.usage();
return true;
}

try {
jcommander.parse(args);
} catch (Exception e) {
System.err.println(e.getMessage());
jcommander.usage();
return false;
}

String cmd = jcommander.getParsedCommand();
if (cmd == null) {
jcommander.usage();
return false;
} else {
JCommander obj = jcommander.getCommands().get(cmd);
AbstractCommandRunner commandRunner = (AbstractCommandRunner) obj.getObjects().get(0);
try {
commandRunner.run();
return true;
} catch (ParameterException e) {
System.err.println(e.getMessage() + System.lineSeparator());
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.inlong.tubemq.server.tools.cli;

/**
* The runner of command.
*/
public abstract class AbstractCommandRunner {

/**
* Execute the specified command.
*/
abstract void run() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.inlong.tubemq.server.tools.cli;

import org.apache.inlong.tubemq.corebase.utils.TStringUtils;
import org.apache.inlong.tubemq.server.common.fielddef.CliArgDef;
import org.apache.inlong.tubemq.server.common.utils.HttpUtils;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* This class is use to process Web Api Request process.
*/
public class CliWebapiAdmin extends CliAbstractBase {

private static final Logger logger =
LoggerFactory.getLogger(CliBrokerAdmin.class);

private static final String defMasterPortal = "127.0.0.1:8080";

private Map<String, Object> requestParams;

public CliWebapiAdmin() {
super(null);
initCommandOptions();
}

/**
* Construct CliWebapiAdmin with request parameters
*
* @param requestParams Request parameters map
*/
public CliWebapiAdmin(Map<String, Object> requestParams) {
this();
this.requestParams = requestParams;
}

/**
* Init command options
*/
@Override
protected void initCommandOptions() {
// add the cli required parameters
addCommandOption(CliArgDef.MASTERPORTAL);
addCommandOption(CliArgDef.ADMINMETHOD);
addCommandOption(CliArgDef.METHOD);
}

/**
* Call the Web API
*
* @param args Request parameters of method name,
* {"--method", "admin_query_topic_info"} as an example
*/
@Override
public boolean processParams(String[] args) throws Exception {
CommandLine cli = parser.parse(options, args);
if (cli == null) {
throw new ParseException("Parse args failure");
}
if (cli.hasOption(CliArgDef.VERSION.longOpt)) {
version();
}
if (cli.hasOption(CliArgDef.HELP.longOpt)) {
help();
}
String masterAddr = defMasterPortal;
if (cli.hasOption(CliArgDef.MASTERPORTAL.longOpt)) {
masterAddr = cli.getOptionValue(CliArgDef.MASTERPORTAL.longOpt);
if (TStringUtils.isBlank(masterAddr)) {
throw new Exception(CliArgDef.MASTERPORTAL.longOpt + " is required!");
}
}
JsonObject result = null;
String masterUrl = "http://" + masterAddr + "/webapi.htm";
if (cli.hasOption(CliArgDef.ADMINMETHOD.longOpt)) {
Map<String, String> inParamMap = new HashMap<>();
inParamMap.put(CliArgDef.METHOD.longOpt, "admin_get_methods");
result = HttpUtils.requestWebService(masterUrl, inParamMap);
System.out.println(formatResult(result));
System.exit(0);
}
String methodStr = cli.getOptionValue(CliArgDef.METHOD.longOpt);
if (TStringUtils.isBlank(methodStr)) {
throw new Exception(CliArgDef.METHOD.longOpt + " is required!");
}
requestParams.put(CliArgDef.METHOD.longOpt, methodStr);
Map<String, String> convertedRequestParams = convertRequestParams(requestParams);
result = HttpUtils.requestWebService(masterUrl, convertedRequestParams);
String formattedResult = formatResult(result);
System.out.println(formattedResult);
return true;
}

/**
* Convert request paramters map
*
* @param requestParamsMap Map
* @return a converted map
*/
private Map<String, String> convertRequestParams(Map<String, Object> requestParamsMap) {
// convert object values to string ones
Map<String, String> converttedrequestParamsMap = new HashMap<>();
for (String k : requestParamsMap.keySet()) {
converttedrequestParamsMap.put(k, String.valueOf(requestParamsMap.get(k)));
}
return converttedrequestParamsMap;
}

/**
* Convert json content to specific output format
*
* @param result JsonObject
* @return formatted results
*/
private String formatResult(JsonObject result) {
// format output results
return new GsonBuilder().setPrettyPrinting().create().toJson(result);
}

public static void main(String[] args) {
CliWebapiAdmin cliWebapiAdmin = new CliWebapiAdmin();
try {
cliWebapiAdmin.processParams(args);
} catch (Throwable ex) {
ex.printStackTrace();
logger.error(ex.getMessage());
cliWebapiAdmin.help();
}
}
}
Loading

0 comments on commit 4ddf19c

Please sign in to comment.