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

[INLONG-4972][TubeMQ] Add a command-line tool for TubeMQ #8835

Merged
merged 27 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9427304
Add command line tool: tubectl, and its documents.
fancycoderzf Sep 4, 2023
6dc2efd
Add unit tests and update documents.
fancycoderzf Sep 4, 2023
4072df9
Update documents.
fancycoderzf Sep 4, 2023
656de97
Add cmd script.
fancycoderzf Sep 4, 2023
109acf6
[INLONG-4972][TubeMQ] Add command line tool: tubectl, and its documents.
fancycoderzf Sep 4, 2023
fc649ac
[INLONG-4972][TubeMQ] Add unit tests and update documents.
fancycoderzf Sep 4, 2023
8cc4f4e
[INLONG-4972][TubeMQ] Update documents.
fancycoderzf Sep 4, 2023
bf3fdcf
[INLONG-4972][TubeMQ] Add cmd script.
fancycoderzf Sep 4, 2023
8a811bb
Merge branch 'tubectl' of github.com:fancycoderzf/inlong into tubectl
fancycoderzf Sep 4, 2023
5592037
[INLONG-4972][TubeMQ] Add command line tool: tubectl, and its documents.
fancycoderzf Sep 4, 2023
d9ce8a5
[INLONG-4972][TubeMQ] Add unit tests and update documents.
fancycoderzf Sep 4, 2023
2fcb4e7
[INLONG-4972][TubeMQ] Update documents.
fancycoderzf Sep 4, 2023
dea0675
[INLONG-4972][TubeMQ] Add cmd script.
fancycoderzf Sep 4, 2023
7df1cfc
Add command line tool: tubectl, and its documents.
fancycoderzf Sep 4, 2023
0c5802e
Add unit tests and update documents.
fancycoderzf Sep 4, 2023
5273b49
Update documents.
fancycoderzf Sep 4, 2023
4daef89
[INLONG-4972][TubeMQ] Split test files, update poms, and optimize codes.
fancycoderzf Sep 5, 2023
edbc510
Merge branch 'apache:master' into tubectl
fancycoderzf Sep 5, 2023
d169381
conflict fixed
fancycoderzf Sep 5, 2023
b411443
conflict fixed
fancycoderzf Sep 5, 2023
27a7936
conflict fixed
fancycoderzf Sep 5, 2023
7951491
[INLONG-4972][TubeMQ] Rename unit test files.
fancycoderzf Sep 5, 2023
a9be8db
[INLONG-4972][TubeMQ] Modify pom.xml and tubectl.cmd.
fancycoderzf Sep 5, 2023
75285ac
[INLONG-4972][TubeMQ] Standardize naming and add comments
fancycoderzf Sep 5, 2023
6b8432f
[INLONG-4972][TubeMQ] Update pom.xml, delete version
fancycoderzf Sep 5, 2023
7609ee9
[INLONG-4972][TubeMQ] Register in the LICENSE index, and add NOTICE
fancycoderzf Sep 5, 2023
d43c9bc
[INLONG-4972][TubeMQ] Update commands
fancycoderzf Sep 5, 2023
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
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