forked from apache/incubator-uniffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#768] feat(cli): Cli method for blacklist update
- Loading branch information
wangjunbo
committed
Jun 14, 2023
1 parent
c274b37
commit d26cca0
Showing
6 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
cli/src/main/java/org/apache/uniffle/cli/UniffleAdminCLI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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.uniffle.cli; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.uniffle.AbstractCustomCommandLine; | ||
import org.apache.uniffle.UniffleCliArgsException; | ||
import org.apache.uniffle.api.AdminRestApi; | ||
import org.apache.uniffle.client.UniffleRestClient; | ||
|
||
public class UniffleAdminCLI extends AbstractCustomCommandLine { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UniffleAdminCLI.class); | ||
|
||
private final Options allOptions; | ||
private final Option checkerClass; | ||
private final Option coordinatorHost; | ||
private final Option coordPort; | ||
private final Option refreshAccessCli; | ||
private final Option help; | ||
protected UniffleRestClient client; | ||
|
||
public UniffleAdminCLI(String shortPrefix, String longPrefix) { | ||
allOptions = new Options(); | ||
refreshAccessCli = new Option(shortPrefix + "r", longPrefix + "refreshChecker", | ||
false, "This is an admin command that will refresh access checker."); | ||
checkerClass = new Option(shortPrefix + "c", longPrefix + "checkerClass", | ||
true, "This is the checker class that will be refreshed."); | ||
help = new Option(shortPrefix + "h", longPrefix + "help", | ||
false, "Help for the Uniffle Admin CLI."); | ||
coordinatorHost = new Option(shortPrefix + "s", longPrefix + "coordinatorHost", | ||
true, "This is coordinator server host."); | ||
coordPort = new Option(shortPrefix + "p", longPrefix + "port", | ||
true, "This is coordinator server port."); | ||
allOptions.addOption(coordinatorHost); | ||
allOptions.addOption(coordPort); | ||
allOptions.addOption(refreshAccessCli); | ||
allOptions.addOption(help); | ||
} | ||
|
||
public UniffleAdminCLI(String shortPrefix, String longPrefix, UniffleRestClient client) { | ||
this(shortPrefix, longPrefix); | ||
this.client = client; | ||
} | ||
|
||
public int run(String[] args) throws UniffleCliArgsException { | ||
final CommandLine cmd = parseCommandLineOptions(args, true); | ||
|
||
if (cmd.hasOption(help.getOpt())) { | ||
printUsage(); | ||
return 0; | ||
} | ||
|
||
if (cmd.hasOption(coordinatorHost.getOpt()) && cmd.hasOption(coordPort.getOpt())) { | ||
String host = cmd.getOptionValue(coordinatorHost.getOpt()).trim(); | ||
int port = Integer.parseInt(cmd.getOptionValue(coordPort.getOpt()).trim()); | ||
String hostUrl = String.format("https://%s:%d", host, port); | ||
LOG.info("connected to coordinator: https://{}:{}.", host, port); | ||
client = UniffleRestClient.builder(hostUrl).build(); | ||
} | ||
|
||
if (cmd.hasOption(refreshAccessCli.getOpt())) { | ||
if (!cmd.hasOption(checkerClass.getOpt())) { | ||
throw new UniffleCliArgsException(""); | ||
} | ||
String checker = cmd.getOptionValue(checkerClass.getOpt()); | ||
if (checker == null) { | ||
throw new UniffleCliArgsException(""); | ||
} | ||
LOG.info("uniffle-admin-cli : refresh access checker {}!", checker); | ||
refreshAccessChecker(checker); | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
private String refreshAccessChecker(String checker) throws UniffleCliArgsException { | ||
if (client == null) { | ||
throw new UniffleCliArgsException("Missing Coordinator host address and grpc port parameters."); | ||
} | ||
AdminRestApi adminRestApi = new AdminRestApi(client); | ||
return adminRestApi.refreshAccessChecker(checker); | ||
} | ||
|
||
@Override | ||
public void addRunOptions(Options baseOptions) { | ||
baseOptions.addOption(refreshAccessCli); | ||
baseOptions.addOption(checkerClass); | ||
baseOptions.addOption(coordinatorHost); | ||
baseOptions.addOption(coordPort); | ||
} | ||
|
||
@Override | ||
public void addGeneralOptions(Options baseOptions) { | ||
baseOptions.addOption(help); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int retCode; | ||
try { | ||
final UniffleAdminCLI cli = new UniffleAdminCLI("", ""); | ||
retCode = cli.run(args); | ||
} catch (UniffleCliArgsException e) { | ||
retCode = AbstractCustomCommandLine.handleCliArgsException(e, LOG); | ||
} catch (Exception e) { | ||
retCode = AbstractCustomCommandLine.handleError(e, LOG); | ||
} | ||
System.exit(retCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters