HDFS-9926. ozone : Add volume commands to CLI. Contributed by Anu Engineer.

This commit is contained in:
Chris Nauroth 2016-03-14 10:50:54 -07:00 committed by Owen O'Malley
parent 60fbef08ec
commit b9d53ed836
8 changed files with 815 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* 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.web.ozShell;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClient;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Common interface for command handling.
*/
public abstract class Handler {
protected OzoneClient client;
/**
* Constructs a client object.
*/
public Handler() {
client = new OzoneClient();
}
/**
* Executes the Client command.
*
* @param cmd - CommandLine
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
protected abstract void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException;
/**
* verifies user provided URI.
*
* @param uri - UriString
* @return URI
* @throws URISyntaxException
* @throws OzoneException
*/
protected URI verifyURI(String uri) throws URISyntaxException,
OzoneException {
if ((uri == null) || uri.isEmpty()) {
throw new OzoneClientException(
"Ozone URI is needed to execute this command.");
}
URIBuilder ozoneURI = new URIBuilder(uri);
if (ozoneURI.getPort() == 0) {
ozoneURI.setPort(Shell.DEFAULT_OZONE_PORT);
}
return ozoneURI.build();
}
}

View File

@ -0,0 +1,238 @@
/*
* 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.web.ozShell;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.volume.CreateVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.DeleteVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.InfoVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.ListVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.UpdateVolumeHandler;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* Ozone user interface commands.
*
* This class uses dispatch method to make calls
* to appropriate handlers that execute the ozone functions.
*/
public class Shell extends Configured implements Tool {
// General options
public static final int DEFAULT_OZONE_PORT = 50070;
public static final String VERBOSE = "v";
// volume related command line arguments
public static final String RUNAS = "root";
public static final String USER = "user";
public static final String OWNER = "owner";
public static final String QUOTA = "quota";
public static final String CREATE_VOLUME = "createVolume";
public static final String UPDATE_VOLUME = "updateVolume";
public static final String DELETE_VOLUME = "deleteVolume";
public static final String LIST_VOLUME = "listVolume";
public static final String INFO_VOLUME = "infoVolume";
/**
* Execute the command with the given arguments.
*
* @param args command specific arguments.
*
* @return exit code.
*
* @throws Exception
*/
@Override
public int run(String[] args) throws Exception {
Options opts = getOpts();
CommandLine cmd = parseArgs(args, opts);
return dispatch(cmd, opts);
}
/**
* Construct an ozShell.
*/
public Shell() {
}
/**
* returns the Command Line Options.
*
* @return Options
*/
private Options getOpts() {
Options opts = new Options();
addVolumeCommands(opts);
return opts;
}
/**
* This function parses all command line arguments
* and returns the appropriate values.
*
* @param argv - Argv from main
*
* @return CommandLine
*/
private CommandLine parseArgs(String[] argv, Options opts)
throws org.apache.commons.cli.ParseException {
BasicParser parser = new BasicParser();
return parser.parse(opts, argv);
}
/**
* All volume related commands are added in this function for the command
* parser.
*
* @param options - Command Options class.
*/
private void addVolumeCommands(Options options) {
Option verbose = new Option(VERBOSE, false, "verbose information output.");
options.addOption(verbose);
Option runas = new Option(RUNAS, false, "Run the command as \"hdfs\" user");
options.addOption(runas);
Option userName = new Option(USER, true,
"Name of the user in volume management " +
"functions");
options.addOption(userName);
Option quota = new Option(QUOTA, true, "Quota for the volume. E.g. 10TB");
options.addOption(quota);
Option createVolume = new Option(CREATE_VOLUME, true, "creates a volume" +
"for the specified user.\n \t For example : hdfs oz -createVolume " +
"<volumeURI> -root -user <userName>\n");
options.addOption(createVolume);
Option deleteVolume = new Option(DELETE_VOLUME, true, "deletes a volume" +
"if it is empty.\n \t For example : hdfs oz -deleteVolume <volumeURI>" +
" -root \n");
options.addOption(deleteVolume);
Option listVolume =
new Option(LIST_VOLUME, true, "List the volumes of a given user.\n" +
"For example : hdfs oz -listVolume <ozoneURI>" +
"-user <username> -root or hdfs oz " +
"-listVolume");
options.addOption(listVolume);
Option updateVolume =
new Option(UPDATE_VOLUME, true, "updates an existing volume.\n" +
"\t For example : hdfs oz " +
"-updateVolume <volumeURI> -quota " +
"100TB\n");
options.addOption(updateVolume);
Option infoVolume = new Option(INFO_VOLUME, true,
"returns information about a specific " +
"volume.");
options.addOption(infoVolume);
}
/**
* Main for the ozShell Command handling.
*
* @param argv - System Args Strings[]
*
* @throws Exception
*/
public static void main(String[] argv) throws Exception {
Shell shell = new Shell();
Configuration conf = new Configuration();
conf.setQuietMode(false);
shell.setConf(conf);
int res = 0;
try {
res = ToolRunner.run(shell, argv);
} catch (Exception ex) {
System.exit(1);
}
System.exit(res);
}
/**
* Dispatches calls to the right command Handler classes.
*
* @param cmd - CommandLine
*
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
private int dispatch(CommandLine cmd, Options opts)
throws IOException, OzoneException, URISyntaxException {
Handler handler = null;
try {
// volume functions
if (cmd.hasOption(Shell.CREATE_VOLUME)) {
handler = new CreateVolumeHandler();
}
if (cmd.hasOption(Shell.DELETE_VOLUME)) {
handler = new DeleteVolumeHandler();
}
if (cmd.hasOption(Shell.LIST_VOLUME)) {
handler = new ListVolumeHandler();
}
if (cmd.hasOption(Shell.UPDATE_VOLUME)) {
handler = new UpdateVolumeHandler();
}
if (cmd.hasOption(Shell.INFO_VOLUME)) {
handler = new InfoVolumeHandler();
}
if (handler != null) {
handler.execute(cmd);
return 0;
} else {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter
.printHelp(80, "hdfs oz -command uri [args]", "Ozone Commands",
opts, "Please correct your command and try again.");
return 1;
}
} catch (IOException | OzoneException | URISyntaxException ex) {
System.err.printf("Command Failed : %s%n", ex.getMessage());
return 1;
}
}
}

View File

@ -0,0 +1,27 @@
/*
* 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.
*/
/**
* ozShell Class acts as the command line interface to
* the ozone Rest Client.
*/
package org.apache.hadoop.ozone.web.ozShell;
/**
A simple CLI to work against Ozone.
**/

View File

@ -0,0 +1,102 @@
/*
* 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.web.ozShell.volume;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.client.OzoneVolume;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.Handler;
import org.apache.hadoop.ozone.web.ozShell.Shell;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Executes the create volume call for the shell.
*/
public class CreateVolumeHandler extends Handler {
private String rootName;
private String userName;
private String volumeName;
private String quota;
/**
* Executes the Create Volume.
*
* @param cmd - CommandLine
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.CREATE_VOLUME)) {
throw new OzoneClientException(
"Incorrect call : createVolume is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.CREATE_VOLUME);
URI ozoneURI = verifyURI(ozoneURIString);
if (ozoneURI.getPath().isEmpty()) {
throw new OzoneClientException(
"Volume name is required to create a volume");
}
// we need to skip the slash in the URI path
// getPath returns /volumeName needs to remove the first slash.
volumeName = ozoneURI.getPath().substring(1);
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Volume name : %s%n", volumeName);
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
if (!cmd.hasOption(Shell.USER)) {
throw new OzoneClientException(
"User name is needed in createVolume call.");
}
if (cmd.hasOption(Shell.QUOTA)) {
quota = cmd.getOptionValue(Shell.QUOTA);
}
userName = cmd.getOptionValue(Shell.USER);
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
OzoneVolume vol = client.createVolume(volumeName, userName, quota);
if (cmd.hasOption(Shell.VERBOSE)) {
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(vol.getJsonString(), Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}
}

View File

@ -0,0 +1,81 @@
/*
* 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.web.ozShell.volume;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.Handler;
import org.apache.hadoop.ozone.web.ozShell.Shell;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Executes deleteVolume call for the shell.
*/
public class DeleteVolumeHandler extends Handler {
private String volumeName;
private String rootName;
/**
* Executes the delete volume call.
*
* @param cmd - CommandLine
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.DELETE_VOLUME)) {
throw new OzoneClientException(
"Incorrect call : deleteVolume call is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.DELETE_VOLUME);
URI ozoneURI = verifyURI(ozoneURIString);
if (ozoneURI.getPath().isEmpty()) {
throw new OzoneClientException(
"Volume name is required to delete a volume");
}
// we need to skip the slash in the URI path
volumeName = ozoneURI.getPath().substring(1);
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Volume name : %s%n", volumeName);
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
client.deleteVolume(volumeName);
}
}

View File

@ -0,0 +1,99 @@
/*
* 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.web.ozShell.volume;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.client.OzoneVolume;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.Handler;
import org.apache.hadoop.ozone.web.ozShell.Shell;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Executes volume Info calls.
*/
public class InfoVolumeHandler extends Handler{
private String rootName;
private String volumeName;
private String userName;
/**
* Executes volume Info.
*
* @param cmd - CommandLine
*
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.INFO_VOLUME)) {
throw new OzoneClientException(
"Incorrect call : infoVolume is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.INFO_VOLUME);
URI ozoneURI = verifyURI(ozoneURIString);
if (ozoneURI.getPath().isEmpty()) {
throw new OzoneClientException(
"Volume name is required to get info of a volume");
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
}
// we need to skip the slash in the URI path
volumeName = ozoneURI.getPath().substring(1);
if (cmd.hasOption(Shell.USER)) {
userName = cmd.getOptionValue(Shell.USER);
} else {
userName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
if (rootName != null) {
client.setUserAuth(rootName);
} else {
client.setUserAuth(userName);
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
OzoneVolume vol = client.getVolume(volumeName);
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(vol.getJsonString(), Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}

View File

@ -0,0 +1,100 @@
/*
* 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.web.ozShell.volume;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.client.OzoneVolume;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.Handler;
import org.apache.hadoop.ozone.web.ozShell.Shell;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
/**
* Executes List Volume call.
*/
public class ListVolumeHandler extends Handler {
private String rootName;
private String userName;
/**
* Executes the Client Calls.
*
* @param cmd - CommandLine
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.LIST_VOLUME)) {
throw new OzoneClientException(
"Incorrect call : listVolume is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.LIST_VOLUME);
URI ozoneURI = verifyURI(ozoneURIString);
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
}
if (!cmd.hasOption(Shell.USER)) {
throw new OzoneClientException(
"User name is needed in listVolume call.");
}
if (cmd.hasOption(Shell.USER)) {
userName = cmd.getOptionValue(Shell.USER);
} else {
userName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
if (rootName != null) {
client.setUserAuth(rootName);
} else {
client.setUserAuth(userName);
}
List<OzoneVolume> volumes = client.listVolumes(userName);
if (volumes != null) {
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Found : %d volumes for user : %s %n", volumes.size(),
userName);
}
ObjectMapper mapper = new ObjectMapper();
for (OzoneVolume vol : volumes) {
Object json = mapper.readValue(vol.getJsonString(), Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}
}
}

View File

@ -0,0 +1,92 @@
/*
* 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.web.ozShell.volume;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneClientException;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
import org.apache.hadoop.ozone.web.ozShell.Handler;
import org.apache.hadoop.ozone.web.ozShell.Shell;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Executes update volume calls.
*/
public class UpdateVolumeHandler extends Handler {
private String rootName;
private String ownerName;
private String volumeName;
private String quota;
/**
* Executes update volume calls.
*
* @param cmd - CommandLine
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.UPDATE_VOLUME)) {
throw new OzoneClientException(
"Incorrect call : updateVolume is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.UPDATE_VOLUME);
URI ozoneURI = verifyURI(ozoneURIString);
if (ozoneURI.getPath().isEmpty()) {
throw new OzoneClientException(
"Volume name is required to update a volume");
}
// we need to skip the slash in the URI path
volumeName = ozoneURI.getPath().substring(1);
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
if (cmd.hasOption(Shell.QUOTA)) {
quota = cmd.getOptionValue(Shell.QUOTA);
}
if (cmd.hasOption(Shell.OWNER)) {
ownerName = cmd.getOptionValue(Shell.OWNER);
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
if (quota != null && !quota.isEmpty()) {
client.setVolumeQuota(volumeName, quota);
}
if (ownerName != null && !ownerName.isEmpty()) {
client.setVolumeOwner(volumeName, ownerName);
}
}
}