public static int processCommandLineArgs(String[] cliArgs) throws Exception
{
CommandLineParser cliParser = new GnuParser();
Options cliOptions = constructCommandLineOptions();
CommandLine cmd = null;
try
{
cmd = cliParser.parse(cliOptions, cliArgs);
}
catch (ParseException pe)
{
System.err.println("CommandLineClient: failed to parse command-line options: "
+ pe.toString());
printUsage(cliOptions);
System.exit(1);
}
ClusterSetup setupTool = new ClusterSetup(cmd.getOptionValue(zkServerAddress));
if (cmd.hasOption(addCluster))
{
String clusterName = cmd.getOptionValue(addCluster);
setupTool.addCluster(clusterName, false);
return 0;
}
if (cmd.hasOption(activateCluster))
{
String clusterName = cmd.getOptionValues(activateCluster)[0];
String grandCluster = cmd.getOptionValues(activateCluster)[1];
boolean enable = Boolean.parseBoolean(cmd.getOptionValues(activateCluster)[2]);
setupTool.activateCluster(clusterName, grandCluster, enable);
return 0;
}
if (cmd.hasOption(dropCluster))
{
String clusterName = cmd.getOptionValue(dropCluster);
setupTool.deleteCluster(clusterName);
return 0;
}
if (cmd.hasOption(addInstance))
{
String clusterName = cmd.getOptionValues(addInstance)[0];
String InstanceAddressInfo = cmd.getOptionValues(addInstance)[1];
String[] InstanceAddresses = InstanceAddressInfo.split(";");
setupTool.addInstancesToCluster(clusterName, InstanceAddresses);
return 0;
}
if (cmd.hasOption(addResource))
{
String clusterName = cmd.getOptionValues(addResource)[0];
String resourceName = cmd.getOptionValues(addResource)[1];
int partitions = Integer.parseInt(cmd.getOptionValues(addResource)[2]);
String stateModelRef = cmd.getOptionValues(addResource)[3];
String modeValue = IdealStateModeProperty.AUTO.toString();
if (cmd.hasOption(mode))
{
modeValue = cmd.getOptionValues(mode)[0];
}
int bucketSizeVal = 0;
if (cmd.hasOption(bucketSize))
{
bucketSizeVal = Integer.parseInt(cmd.getOptionValues(bucketSize)[0]);
}
setupTool.addResourceToCluster(clusterName,
resourceName,
partitions,
stateModelRef,
modeValue,
bucketSizeVal);
return 0;
}
if (cmd.hasOption(rebalance))
{
String clusterName = cmd.getOptionValues(rebalance)[0];
String resourceName = cmd.getOptionValues(rebalance)[1];
int replicas = Integer.parseInt(cmd.getOptionValues(rebalance)[2]);
if (cmd.hasOption(resourceKeyPrefix))
{
setupTool.rebalanceStorageCluster(clusterName,
resourceName,
replicas,
cmd.getOptionValue(resourceKeyPrefix));
return 0;
}
setupTool.rebalanceStorageCluster(clusterName, resourceName, replicas);
return 0;
}
if (cmd.hasOption(expandCluster))
{
String clusterName = cmd.getOptionValues(expandCluster)[0];
setupTool.expandCluster(clusterName);
return 0;
}
if (cmd.hasOption(expandResource))
{
String clusterName = cmd.getOptionValues(expandResource)[0];
String resourceName = cmd.getOptionValues(expandResource)[1];
setupTool.expandResource(clusterName, resourceName);
return 0;
}
if (cmd.hasOption(dropInstance))
{
String clusterName = cmd.getOptionValues(dropInstance)[0];
String InstanceAddressInfo = cmd.getOptionValues(dropInstance)[1];
String[] InstanceAddresses = InstanceAddressInfo.split(";");
setupTool.dropInstancesFromCluster(clusterName, InstanceAddresses);
return 0;
}
if (cmd.hasOption(listClusters))
{
List<String> clusters = setupTool.getClusterManagementTool().getClusters();
System.out.println("Existing clusters:");
for (String cluster : clusters)
{
System.out.println(cluster);
}
return 0;
}
if (cmd.hasOption(listResources))
{
String clusterName = cmd.getOptionValue(listResources);
List<String> resourceNames =
setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
System.out.println("Existing resources in cluster " + clusterName + ":");
for (String resourceName : resourceNames)
{
System.out.println(resourceName);
}
return 0;
}
else if (cmd.hasOption(listClusterInfo))
{
String clusterName = cmd.getOptionValue(listClusterInfo);
List<String> resourceNames =
setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
List<String> Instances =
setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
System.out.println("Existing resources in cluster " + clusterName + ":");
for (String resourceName : resourceNames)
{
System.out.println(resourceName);
}
System.out.println("Instances in cluster " + clusterName + ":");
for (String InstanceName : Instances)
{
System.out.println(InstanceName);
}
return 0;
}
else if (cmd.hasOption(listInstances))
{
String clusterName = cmd.getOptionValue(listInstances);
List<String> Instances =
setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
System.out.println("Instances in cluster " + clusterName + ":");
for (String InstanceName : Instances)
{
System.out.println(InstanceName);
}
return 0;
}
else if (cmd.hasOption(listInstanceInfo))
{
String clusterName = cmd.getOptionValues(listInstanceInfo)[0];
String instanceName = cmd.getOptionValues(listInstanceInfo)[1];
InstanceConfig config =
setupTool.getClusterManagementTool().getInstanceConfig(clusterName,
instanceName);
String result = new String(new ZNRecordSerializer().serialize(config.getRecord()));
System.out.println("InstanceConfig: " + result);
return 0;
}
else if (cmd.hasOption(listResourceInfo))
{
// print out partition number, db name and replication number
// Also the ideal states and current states
String clusterName = cmd.getOptionValues(listResourceInfo)[0];
String resourceName = cmd.getOptionValues(listResourceInfo)[1];
IdealState idealState =
setupTool.getClusterManagementTool().getResourceIdealState(clusterName,
resourceName);
ExternalView externalView =
setupTool.getClusterManagementTool().getResourceExternalView(clusterName,
resourceName);
if (idealState != null)
{
System.out.println("IdealState for " + resourceName + ":");
System.out.println(new String(new ZNRecordSerializer().serialize(idealState.getRecord())));
}
else
{
System.out.println("No idealState for " + resourceName);
}
System.out.println();
if (externalView != null)
{
System.out.println("ExternalView for " + resourceName + ":");
System.out.println(new String(new ZNRecordSerializer().serialize(externalView.getRecord())));
}
else
{
System.out.println("No externalView for " + resourceName);
}
return 0;
}
else if (cmd.hasOption(listPartitionInfo))
{
// print out where the partition master / slaves locates
String clusterName = cmd.getOptionValues(listPartitionInfo)[0];
String resourceName = cmd.getOptionValues(listPartitionInfo)[1];
String partitionName = cmd.getOptionValues(listPartitionInfo)[2];
IdealState idealState =
setupTool.getClusterManagementTool().getResourceIdealState(clusterName,
resourceName);
ExternalView externalView =
setupTool.getClusterManagementTool().getResourceExternalView(clusterName,
resourceName);
if (idealState != null)
{
ZNRecord partInfo = new ZNRecord(resourceName + "/" + partitionName);
ZNRecord idealStateRec = idealState.getRecord();
partInfo.setSimpleFields(idealStateRec.getSimpleFields());
if (idealStateRec.getMapField(partitionName) != null)
{
partInfo.setMapField(partitionName, idealStateRec.getMapField(partitionName));
}
if (idealStateRec.getListField(partitionName) != null)
{
partInfo.setListField(partitionName, idealStateRec.getListField(partitionName));
}
System.out.println("IdealState for " + resourceName + "/" + partitionName + ":");
System.out.println(new String(new ZNRecordSerializer().serialize(partInfo)));
}
else
{
System.out.println("No idealState for " + resourceName + "/" + partitionName);
}
System.out.println();
if (externalView != null)
{
ZNRecord partInfo = new ZNRecord(resourceName + "/" + partitionName);
ZNRecord extViewRec = externalView.getRecord();
partInfo.setSimpleFields(extViewRec.getSimpleFields());
if (extViewRec.getMapField(partitionName) != null)
{
partInfo.setMapField(partitionName, extViewRec.getMapField(partitionName));
}
if (extViewRec.getListField(partitionName) != null)
{
partInfo.setListField(partitionName, extViewRec.getListField(partitionName));
}
System.out.println("ExternalView for " + resourceName + "/" + partitionName + ":");
System.out.println(new String(new ZNRecordSerializer().serialize(partInfo)));
}
else
{
System.out.println("No externalView for " + resourceName + "/" + partitionName);
}
return 0;
}
else if (cmd.hasOption(enableInstance))
{
String clusterName = cmd.getOptionValues(enableInstance)[0];
String instanceName = cmd.getOptionValues(enableInstance)[1];
if (instanceName.contains(":"))
{
instanceName = instanceName.replaceAll(":", "_");
}
boolean enabled =
Boolean.parseBoolean(cmd.getOptionValues(enableInstance)[2].toLowerCase());
setupTool.getClusterManagementTool().enableInstance(clusterName,
instanceName,
enabled);
return 0;
}
else if (cmd.hasOption(enablePartition))
{
String[] args = cmd.getOptionValues(enablePartition);
boolean enabled = Boolean.parseBoolean(args[0].toLowerCase());
String clusterName = args[1];
String instanceName = args[2];
String resourceName = args[3];
List<String> partitionNames =
Arrays.asList(Arrays.copyOfRange(args, 4, args.length));
setupTool.getClusterManagementTool().enablePartition(enabled,
clusterName,
instanceName,
resourceName,
partitionNames);
return 0;
}
else if (cmd.hasOption(resetPartition))
{
String[] args = cmd.getOptionValues(resetPartition);
String clusterName = args[0];
String instanceName = args[1];
String resourceName = args[2];
List<String> partitionNames =
Arrays.asList(Arrays.copyOfRange(args, 3, args.length));
setupTool.getClusterManagementTool().resetPartition(clusterName,
instanceName,
resourceName,
partitionNames);
return 0;
}
else if (cmd.hasOption(resetInstance))
{
String[] args = cmd.getOptionValues(resetInstance);
String clusterName = args[0];
List<String> instanceNames =
Arrays.asList(Arrays.copyOfRange(args, 1, args.length));
setupTool.getClusterManagementTool().resetInstance(clusterName, instanceNames);
return 0;
}
else if (cmd.hasOption(resetResource))
{
String[] args = cmd.getOptionValues(resetResource);
String clusterName = args[0];
List<String> resourceNames =
Arrays.asList(Arrays.copyOfRange(args, 1, args.length));
setupTool.getClusterManagementTool().resetResource(clusterName, resourceNames);
return 0;
}
else if (cmd.hasOption(enableCluster))
{
String[] params = cmd.getOptionValues(enableCluster);
String clusterName = params[0];
boolean enabled = Boolean.parseBoolean(params[1].toLowerCase());
setupTool.getClusterManagementTool().enableCluster(clusterName, enabled);
return 0;
}
else if (cmd.hasOption(listStateModels))
{
String clusterName = cmd.getOptionValues(listStateModels)[0];
List<String> stateModels =
setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
System.out.println("Existing state models:");
for (String stateModel : stateModels)
{
System.out.println(stateModel);
}
return 0;
}
else if (cmd.hasOption(listStateModel))
{
String clusterName = cmd.getOptionValues(listStateModel)[0];
String stateModel = cmd.getOptionValues(listStateModel)[1];
StateModelDefinition stateModelDef =
setupTool.getClusterManagementTool().getStateModelDef(clusterName, stateModel);
String result =
new String(new ZNRecordSerializer().serialize(stateModelDef.getRecord()));
System.out.println("StateModelDefinition: " + result);
return 0;
}
else if (cmd.hasOption(addStateModelDef))
{
String clusterName = cmd.getOptionValues(addStateModelDef)[0];
String stateModelFile = cmd.getOptionValues(addStateModelDef)[1];
ZNRecord stateModelRecord =
(ZNRecord) (new ZNRecordSerializer().deserialize(readFile(stateModelFile)));
if (stateModelRecord.getId() == null || stateModelRecord.getId().length() == 0)
{
throw new IllegalArgumentException("ZNRecord for state model definition must have an id");
}
setupTool.getClusterManagementTool()
.addStateModelDef(clusterName,
stateModelRecord.getId(),
new StateModelDefinition(stateModelRecord));
return 0;
}
else if (cmd.hasOption(addIdealState))
{
String clusterName = cmd.getOptionValues(addIdealState)[0];
String resourceName = cmd.getOptionValues(addIdealState)[1];
String idealStateFile = cmd.getOptionValues(addIdealState)[2];
setupTool.addIdealState(clusterName, resourceName, idealStateFile);
return 0;
}
else if (cmd.hasOption(addStat))
{
String clusterName = cmd.getOptionValues(addStat)[0];
String statName = cmd.getOptionValues(addStat)[1];
setupTool.getClusterManagementTool().addStat(clusterName, statName);
}
else if (cmd.hasOption(addAlert))
{
String clusterName = cmd.getOptionValues(addAlert)[0];
String alertName = cmd.getOptionValues(addAlert)[1];
setupTool.getClusterManagementTool().addAlert(clusterName, alertName);
}
else if (cmd.hasOption(dropStat))
{
String clusterName = cmd.getOptionValues(dropStat)[0];
String statName = cmd.getOptionValues(dropStat)[1];
setupTool.getClusterManagementTool().dropStat(clusterName, statName);
}
else if (cmd.hasOption(dropAlert))
{
String clusterName = cmd.getOptionValues(dropAlert)[0];
String alertName = cmd.getOptionValues(dropAlert)[1];
setupTool.getClusterManagementTool().dropAlert(clusterName, alertName);
}
else if (cmd.hasOption(dropResource))
{
String clusterName = cmd.getOptionValues(dropResource)[0];
String resourceName = cmd.getOptionValues(dropResource)[1];
setupTool.getClusterManagementTool().dropResource(clusterName, resourceName);
}
else if (cmd.hasOption(swapInstance))
{
String clusterName = cmd.getOptionValues(swapInstance)[0];
String oldInstanceName = cmd.getOptionValues(swapInstance)[1];
String newInstanceName = cmd.getOptionValues(swapInstance)[2];
setupTool.swapInstance(clusterName, oldInstanceName, newInstanceName);
}
else if (cmd.hasOption(setConfig))
{
String scopeStr = cmd.getOptionValues(setConfig)[0];
String propertiesStr = cmd.getOptionValues(setConfig)[1];
setupTool.setConfig(scopeStr, propertiesStr);
}
else if (cmd.hasOption(getConfig))
{
String scopeStr = cmd.getOptionValues(getConfig)[0];
String keysStr = cmd.getOptionValues(getConfig)[1];
setupTool.getConfig(scopeStr, keysStr);
}
else if (cmd.hasOption(help))
{
printUsage(cliOptions);
return 0;
}
else if (cmd.hasOption(addResourceProperty))
{
String clusterName = cmd.getOptionValues(addResourceProperty)[0];
String resourceName = cmd.getOptionValues(addResourceProperty)[1];
String propertyKey = cmd.getOptionValues(addResourceProperty)[2];
String propertyVal = cmd.getOptionValues(addResourceProperty)[3];
setupTool.addResourceProperty(clusterName, resourceName, propertyKey, propertyVal);
return 0;
}
else if (cmd.hasOption(removeResourceProperty))
{
String clusterName = cmd.getOptionValues(removeResourceProperty)[0];
String resourceName = cmd.getOptionValues(removeResourceProperty)[1];
String propertyKey = cmd.getOptionValues(removeResourceProperty)[2];
setupTool.removeResourceProperty(clusterName, resourceName, propertyKey);
return 0;
}
return 0;