From 9f1c8a5ba49e7cad67b7ebf6e3e119eeb32a7ff0 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Wed, 22 Feb 2017 14:49:55 +0530 Subject: cli: change in synopsis for gluster-block commands from now we will have fixed formats for commands. The new outfit will look like: $ gluster-block help gluster-block (3ba7ec5) usage: gluster-block [] commands: create [ha ] create block device. list list available block devices. info details about block device. delete delete block device. help show this message and exit. version show version info and exit. Example usage: $ gluster-block create volume/blockname 192.168.0.1 1GiB $ gluster-block create volume/blockname ha 2 192.168.0.1,192.168.0.2 1GiB $ gluster-block list volume $ gluster-block info volume/blockname $ gluster-block delete volume/blockname Change-Id: Idc6b55c26432ed1ac3f002c2a2b3dbb81b180ec2 Signed-off-by: Prasanna Kumar Kalever --- cli/gluster-block.c | 257 ++++++++++++++++++++++++++-------------------------- 1 file changed, 131 insertions(+), 126 deletions(-) (limited to 'cli') diff --git a/cli/gluster-block.c b/cli/gluster-block.c index 28ce99b..304a116 100644 --- a/cli/gluster-block.c +++ b/cli/gluster-block.c @@ -32,6 +32,7 @@ const char *argp_program_version = "" \ "or later), or the GNU General Public License, version 2 (GPLv2),\n"\ "in all cases as published by the Free Software Foundation."; + static int glusterBlockCliRPC_1(void *cobj, clioperations opt, char **out) { @@ -151,20 +152,26 @@ glusterBlockHelp(void) MSG("%s", PACKAGE_NAME" ("PACKAGE_VERSION")\n" "usage:\n" - " gluster-block [] \n" + " gluster-block []\n" + "\n" + "commands:\n" + " create [ha ] \n" + " create block device.\n" + "\n" + " list \n" + " list available block devices.\n" + "\n" + " info \n" + " details about block device.\n" "\n" - "commands and arguments:\n" - " create create block device\n" - " size size in KiB|MiB|GiB|TiB..\n" - " [mpath ] multipath requirement for high availability(default: 1)\n" - " servers servers in the pool where targets are exported\n" - " list list available block devices\n" - " info details about block device\n" - " modify modify metadata\n" - " delete delete block device\n" - " volume volume that hosts the block device\n" - " help show this message and exit\n" - " version show version info and exit\n" + " delete \n" + " delete block device.\n" + "\n" + " help\n" + " show this message and exit.\n" + "\n" + " version\n" + " show version info and exit.\n" ); } @@ -172,88 +179,82 @@ glusterBlockHelp(void) static int glusterBlockCreate(int argcount, char **options) { - size_t opt; size_t optind = 2; - int ret = 0; + int ret = -1; + ssize_t sparse_ret; char *out = NULL; - static blockCreateCli cobj = {0, }; + blockCreateCli cobj = {0, }; + char *argcopy; + char *sep; - if(argcount <= optind) { + if (argcount <= optind) { MSG("%s\n", "Insufficient arguments for create:"); - MSG("%s\n", "gluster-block create volume " - "size [mpath ] servers "); + MSG("%s\n", "gluster-block create [ha ]" + " "); return -1; } - /* name of block */ - strcpy(cobj.block_name, options[optind++]); - /* default mpath */ cobj.mpath = 1; - while (1) { - if(argcount <= optind) { - break; - } - - opt = glusterBlockCLICreateOptEnumParse(options[optind++]); - if (opt == GB_CLI_CREATE_OPT_MAX) { - MSG("unrecognized option '%s'\n", options[optind-1]); - return -1; - } else if (opt && !options[optind]) { - MSG("%s: require argument\n", options[optind-1]); - return -1; - } + if (GB_STRDUP (argcopy, options[optind++]) < 0) { + goto out; + } + /* part before '/' is the volume name */ + sep = strchr(argcopy, '/'); + if (!sep) { + MSG("%s\n", + "first argument '' doesn't seems to be right"); + MSG("%s\n", "gluster-block create [ha ] " + " "); + LOG("cli", GB_LOG_ERROR, "%s", + "create failed while parsing "); + goto out; + } + *sep = '\0'; + strcpy(cobj.volume, argcopy); - switch (opt) { - case GB_CLI_CREATE_VOLUME: - strcpy(cobj.volume, options[optind++]); - ret++; - break; + /* part after / is blockname */ + strcpy(cobj.block_name, sep + 1); - case GB_CLI_CREATE_MULTIPATH: + if (argcount - optind >= 2) { /* atleast 2 needed */ + /* if ha given then collect count which is next by 'ha' arg */ + if (!strcmp(options[optind], "ha")) { + optind++; sscanf(options[optind++], "%u", &cobj.mpath); - break; - - case GB_CLI_CREATE_SIZE: - cobj.size = glusterBlockCreateParseSize("cli", options[optind++]); - if (cobj.size < 0) { - LOG("cli", GB_LOG_ERROR, "failed while parsing size for block %s", - cobj.block_name); - ret = -1; - goto out; - } - ret++; - break; - - case GB_CLI_CREATE_BACKEND_SERVESRS: - if (GB_STRDUP(cobj.block_hosts, options[optind++]) < 0) { - LOG("cli", GB_LOG_ERROR, "failed while parsing servers for block %s", - cobj.block_name); - ret = -1; - goto out; - } - ret++; - break; - - default: - MSG("unrecognized option '%s'\n", options[optind-1]); - MSG("%s", "Hint: gluster-block help\n"); - ret = -1; - goto out; } } - /* check all options required by create command are specified */ - if(ret < 3) { - MSG("%s\n", "Insufficient arguments for create:"); - MSG("%s\n", "gluster-block create volume " - "size [mpath ] servers "); - ret = -1; + if (argcount - optind < 2) { /* left with servers and size so 2 */ + MSG("%s\n", "Insufficient arguments for create"); + MSG("%s\n", "gluster-block create [ha ]" + " "); + LOG("cli", GB_LOG_ERROR, + "failed creating block %s on volume %s with hosts %s", + cobj.block_name, cobj.volume, cobj.block_hosts); goto out; } + /* next arg to 'ha count' will be servers */ + if (GB_STRDUP(cobj.block_hosts, options[optind++]) < 0) { + LOG("cli", GB_LOG_ERROR, "failed while parsing servers for block %s", + cobj.block_name); + goto out; + } + + /* last arg will be size */ + sparse_ret = glusterBlockCreateParseSize("cli", options[optind]); + if (sparse_ret < 0) { + MSG("%s\n", "last argument '' doesn't seems to be right"); + MSG("%s\n", "gluster-block create [ha ] " + " "); + LOG("cli", GB_LOG_ERROR, "failed while parsing size for block %s", + cobj.block_name); + goto out; + } + cobj.size = sparse_ret; /* size is unsigned long long */ + ret = glusterBlockCliRPC_1(&cobj, CREATE_CLI, &out); if (ret) { LOG("cli", GB_LOG_ERROR, @@ -261,11 +262,12 @@ glusterBlockCreate(int argcount, char **options) cobj.block_name, cobj.volume, cobj.block_hosts); } - if(out) { + if (out) { MSG("%s", out); } out: + GB_FREE(argcopy); GB_FREE(cobj.block_hosts); GB_FREE(out); @@ -276,37 +278,26 @@ glusterBlockCreate(int argcount, char **options) static int glusterBlockList(int argcount, char **options) { - size_t opt; - size_t optind = 2; - static blockListCli cobj; + blockListCli cobj; char *out = NULL; int ret = -1; - if(argcount <= optind) { + if (argcount != 3) { MSG("%s\n", "Insufficient arguments for list:"); - MSG("%s\n", "gluster-block list volume "); + MSG("%s\n", "gluster-block list "); return -1; } - opt = glusterBlockCLICommonOptEnumParse(options[optind++]); - if (opt == GB_CLI_COMMON_OPT_MAX) { - MSG("unrecognized option '%s'\n", options[optind-1]); - MSG("%s\n", "List needs 'volume' option"); - return -1; - } else if (!options[optind]) { - MSG("%s: require argument\n", options[optind-1]); - return -1; - } + strcpy(cobj.volume, options[2]); - strcpy(cobj.volume, options[optind]); ret = glusterBlockCliRPC_1(&cobj, LIST_CLI, &out); if (ret) { LOG("cli", GB_LOG_ERROR, "failed listing blocks from volume %s", cobj.volume); } - if(out) { + if (out) { MSG("%s", out); } @@ -319,43 +310,50 @@ glusterBlockList(int argcount, char **options) static int glusterBlockDelete(int argcount, char **options) { - size_t opt; - size_t optind = 2; - static blockDeleteCli cobj; + blockDeleteCli cobj; char *out = NULL; + char *argcopy; + char *sep; int ret = -1; - if(argcount <= optind) { + if (argcount != 3) { MSG("%s\n", "Insufficient arguments for delete:"); - MSG("%s\n", "gluster-block delete volume "); + MSG("%s\n", "gluster-block delete "); return -1; } - /* name of block */ - strcpy(cobj.block_name, options[optind++]); - opt = glusterBlockCLICommonOptEnumParse(options[optind++]); - if (opt == GB_CLI_COMMON_OPT_MAX) { - MSG("unrecognized option '%s'\n", options[optind-1]); - MSG("%s\n", "Delete needs 'volume' option"); - return -1; - } else if (!options[optind]) { - MSG("%s: require argument\n", options[optind-1]); - return -1; + if (GB_STRDUP (argcopy, options[2]) < 0) { + goto out; + } + /* part before '/' is the volume name */ + sep = strchr(argcopy, '/'); + if (!sep) { + MSG("%s\n", "argument '' doesn't seems to be right"); + MSG("%s\n", "gluster-block delete "); + LOG("cli", GB_LOG_ERROR, "%s", + "delete failed while parsing "); + goto out; } + *sep = '\0'; + strcpy(cobj.volume, argcopy); + + /* part after / is blockname */ + strcpy(cobj.block_name, sep + 1); - strcpy(cobj.volume, options[optind]); ret = glusterBlockCliRPC_1(&cobj, DELETE_CLI, &out); if (ret) { LOG("cli", GB_LOG_ERROR, "failed deleting block %s on volume %s", cobj.block_name, cobj.volume); } - if(out) { + if (out) { MSG("%s", out); } + out: + GB_FREE(argcopy); GB_FREE(out); return ret; @@ -365,33 +363,38 @@ glusterBlockDelete(int argcount, char **options) static int glusterBlockInfo(int argcount, char **options) { - size_t opt; - size_t optind = 2; - static blockInfoCli cobj; + blockInfoCli cobj; char *out = NULL; + char *argcopy; + char *sep; int ret = -1; - if(argcount <= optind) { + if (argcount != 3) { MSG("%s\n", "Insufficient arguments for info:"); - MSG("%s\n", "gluster-block info volume "); + MSG("%s\n", "gluster-block info "); return -1; } - /* name of block */ - strcpy(cobj.block_name, options[optind++]); - opt = glusterBlockCLICommonOptEnumParse(options[optind++]); - if (opt == GB_CLI_COMMON_OPT_MAX) { - MSG("unrecognized option '%s'\n", options[optind-1]); - MSG("%s\n", "Info needs 'volume' option"); - return -1; - } else if (!options[optind]) { - MSG("%s: require argument\n", options[optind-1]); - return -1; + if (GB_STRDUP (argcopy, options[2]) < 0) { + goto out; + } + /* part before '/' is the volume name */ + sep = strchr(argcopy, '/'); + if (!sep) { + MSG("%s\n", "argument '' doesn't seems to be right"); + MSG("%s\n", "gluster-block info "); + LOG("cli", GB_LOG_ERROR, "%s", + "info failed while parsing "); + goto out; } + *sep = '\0'; + strcpy(cobj.volume, argcopy); + + /* part after / is blockname */ + strcpy(cobj.block_name, sep + 1); - strcpy(cobj.volume, options[optind]); ret = glusterBlockCliRPC_1(&cobj, INFO_CLI, &out); if (ret) { LOG("cli", GB_LOG_ERROR, @@ -399,10 +402,12 @@ glusterBlockInfo(int argcount, char **options) cobj.block_name, cobj.volume); } - if(out) { + if (out) { MSG("%s", out); } + out: + GB_FREE(argcopy); GB_FREE(out); return ret; -- cgit