HDFS-9961. Ozone: Add buckets commands to CLI. Contributed by Anu Engineer.

This commit is contained in:
Chris Nauroth 2016-03-15 21:27:53 -07:00
parent 4683d68ee6
commit 4143f0362f
5 changed files with 446 additions and 4 deletions

View File

@ -31,6 +31,10 @@ 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.InfoVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.ListVolumeHandler; import org.apache.hadoop.ozone.web.ozShell.volume.ListVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.volume.UpdateVolumeHandler; import org.apache.hadoop.ozone.web.ozShell.volume.UpdateVolumeHandler;
import org.apache.hadoop.ozone.web.ozShell.bucket.CreateBucketHandler;
import org.apache.hadoop.ozone.web.ozShell.bucket.DeleteBucketHandler;
import org.apache.hadoop.ozone.web.ozShell.bucket.InfoBucketHandler;
import org.apache.hadoop.ozone.web.ozShell.bucket.ListBucketHandler;
import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.util.ToolRunner;
@ -60,6 +64,15 @@ public class Shell extends Configured implements Tool {
public static final String LIST_VOLUME = "listVolume"; public static final String LIST_VOLUME = "listVolume";
public static final String INFO_VOLUME = "infoVolume"; public static final String INFO_VOLUME = "infoVolume";
// bucket related command line arguments
public static final String CREATE_BUCKET = "createBucket";
public static final String UPDATE_BUCKET = "updateBucket";
public static final String DELETE_BUCKET = "deleteBucket";
public static final String LIST_BUCKET = "listBucket";
public static final String INFO_BUCKET = "infoBucket";
public static final String ADD_ACLS = "addAcl";
public static final String REMOVE_ACLS = "removeAcl";
/** /**
* Execute the command with the given arguments. * Execute the command with the given arguments.
@ -91,6 +104,7 @@ public class Shell extends Configured implements Tool {
private Options getOpts() { private Options getOpts() {
Options opts = new Options(); Options opts = new Options();
addVolumeCommands(opts); addVolumeCommands(opts);
addBucketCommands(opts);
return opts; return opts;
} }
@ -161,6 +175,34 @@ public class Shell extends Configured implements Tool {
options.addOption(infoVolume); options.addOption(infoVolume);
} }
/**
* All bucket related commands for ozone.
*
* @param opts - Options
*/
private void addBucketCommands(Options opts) {
Option createBucket = new Option(CREATE_BUCKET, true,
"creates a bucket in a given volume.\n" +
"\t For example : hdfs oz " +
"-createBucket " +
"<volumeName/bucketName>");
opts.addOption(createBucket);
Option infoBucket =
new Option(INFO_BUCKET, true, "returns information about a bucket.");
opts.addOption(infoBucket);
Option deleteBucket =
new Option(DELETE_BUCKET, true, "deletes an empty bucket.");
opts.addOption(deleteBucket);
Option listBucket =
new Option(LIST_BUCKET, true, "Lists the buckets in a volume.");
opts.addOption(listBucket);
}
/** /**
* Main for the ozShell Command handling. * Main for the ozShell Command handling.
* *
@ -194,6 +236,7 @@ public class Shell extends Configured implements Tool {
private int dispatch(CommandLine cmd, Options opts) private int dispatch(CommandLine cmd, Options opts)
throws IOException, OzoneException, URISyntaxException { throws IOException, OzoneException, URISyntaxException {
Handler handler = null; Handler handler = null;
final int eightyColumn = 80;
try { try {
@ -218,16 +261,33 @@ public class Shell extends Configured implements Tool {
handler = new InfoVolumeHandler(); handler = new InfoVolumeHandler();
} }
// bucket functions
if (cmd.hasOption(Shell.CREATE_BUCKET)) {
handler = new CreateBucketHandler();
}
if (cmd.hasOption(Shell.DELETE_BUCKET)) {
handler = new DeleteBucketHandler();
}
if (cmd.hasOption(Shell.INFO_BUCKET)) {
handler = new InfoBucketHandler();
}
if (cmd.hasOption(Shell.LIST_BUCKET)) {
handler = new ListBucketHandler();
}
if (handler != null) { if (handler != null) {
handler.execute(cmd); handler.execute(cmd);
return 0; return 0;
} else { } else {
HelpFormatter helpFormatter = new HelpFormatter(); HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter helpFormatter.printHelp(eightyColumn, "hdfs oz -command uri [args]",
.printHelp(80, "hdfs oz -command uri [args]", "Ozone Commands", "Ozone Commands",
opts, "Please correct your command and try again."); opts, "Please correct your command and try again.");
return 1; return 1;
} }
} catch (IOException | OzoneException | URISyntaxException ex) { } catch (IOException | OzoneException | URISyntaxException ex) {
System.err.printf("Command Failed : %s%n", ex.getMessage()); System.err.printf("Command Failed : %s%n", ex.getMessage());

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.bucket;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneBucket;
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.nio.file.Path;
import java.nio.file.Paths;
/**
* create bucket handler.
*/
public class CreateBucketHandler extends Handler {
private String volumeName;
private String bucketName;
private String rootName;
/**
* Executes create bucket.
*
* @param cmd - CommandLine
*
* @throws IOException
* @throws OzoneException
* @throws URISyntaxException
*/
@Override
protected void execute(CommandLine cmd)
throws IOException, OzoneException, URISyntaxException {
if (!cmd.hasOption(Shell.CREATE_BUCKET)) {
throw new OzoneClientException(
"Incorrect call : createBucket is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.CREATE_BUCKET);
URI ozoneURI = verifyURI(ozoneURIString);
Path path = Paths.get(ozoneURI.getPath());
if (path.getNameCount() < 2) {
throw new OzoneClientException(
"volume and bucket name required in createBucket");
}
volumeName = path.getName(0).toString();
bucketName = path.getName(1).toString();
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Volume Name : %s%n", volumeName);
System.out.printf("Bucket Name : %s%n", bucketName);
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
OzoneVolume vol = client.getVolume(volumeName);
OzoneBucket bucket = vol.createBucket(bucketName);
if (cmd.hasOption(Shell.VERBOSE)) {
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(bucket.getBucketInfo().toJsonString(),
Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.bucket;
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 java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Delete bucket Handler.
*/
public class DeleteBucketHandler extends Handler {
private String volumeName;
private String bucketName;
private String rootName;
/**
* 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.DELETE_BUCKET)) {
throw new OzoneClientException(
"Incorrect call : deleteBucket is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.DELETE_BUCKET);
URI ozoneURI = verifyURI(ozoneURIString);
Path path = Paths.get(ozoneURI.getPath());
if (path.getNameCount() < 2) {
throw new OzoneClientException(
"volume and bucket name required in delete Bucket");
}
volumeName = path.getName(0).toString();
bucketName = path.getName(1).toString();
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Volume Name : %s%n", volumeName);
System.out.printf("Bucket Name : %s%n", bucketName);
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
OzoneVolume vol = client.getVolume(volumeName);
vol.deleteBucket(bucketName);
}
}

View File

@ -0,0 +1,97 @@
/*
* 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.bucket;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneBucket;
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.nio.file.Path;
import java.nio.file.Paths;
/**
* Executes Info bucket.
*/
public class InfoBucketHandler extends Handler {
private String volumeName;
private String bucketName;
private String rootName;
/**
* 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.INFO_BUCKET)) {
throw new OzoneClientException(
"Incorrect call : infoBucket is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.INFO_BUCKET);
URI ozoneURI = verifyURI(ozoneURIString);
Path path = Paths.get(ozoneURI.getPath());
if (path.getNameCount() < 2) {
throw new OzoneClientException(
"volume and bucket name required in delete Bucket");
}
volumeName = path.getName(0).toString();
bucketName = path.getName(1).toString();
if (cmd.hasOption(Shell.VERBOSE)) {
System.out.printf("Volume Name : %s%n", volumeName);
System.out.printf("Bucket Name : %s%n", bucketName);
}
if (cmd.hasOption(Shell.RUNAS)) {
rootName = "hdfs";
} else {
rootName = System.getProperty("user.name");
}
client.setEndPointURI(ozoneURI);
client.setUserAuth(rootName);
OzoneVolume vol = client.getVolume(volumeName);
OzoneBucket bucket = vol.getBucket(bucketName);
ObjectMapper mapper = new ObjectMapper();
Object json =
mapper.readValue(bucket.getBucketInfo().toJsonString(), Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}

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.bucket;
import org.apache.commons.cli.CommandLine;
import org.apache.hadoop.ozone.web.client.OzoneBucket;
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.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
* Executes List Bucket.
*/
public class ListBucketHandler extends Handler {
private String volumeName;
private String rootName;
/**
* 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_BUCKET)) {
throw new OzoneClientException("Incorrect call : listBucket is missing");
}
String ozoneURIString = cmd.getOptionValue(Shell.LIST_BUCKET);
URI ozoneURI = verifyURI(ozoneURIString);
Path path = Paths.get(ozoneURI.getPath());
if (path.getNameCount() < 1) {
throw new OzoneClientException("volume is required in listBucket");
}
volumeName = path.getName(0).toString();
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);
OzoneVolume vol = client.getVolume(volumeName);
List<OzoneBucket> bucketList = vol.listBuckets();
ObjectMapper mapper = new ObjectMapper();
for (OzoneBucket bucket : bucketList) {
Object json =
mapper.readValue(bucket.getBucketInfo().toJsonString(), Object.class);
System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(json));
}
}
}