/* Copyright (c) 2010 Gluster, Inc. This file is part of GlusterFS. GlusterFS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. GlusterFS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #ifndef _CONFIG_H #define _CONFIG_H #include "config.h" #endif #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" extern struct rpc_clnt *global_rpc; extern rpc_clnt_prog_t *cli_rpc_prog; void cli_cmd_volume_start_usage () { cli_out ("Usage: volume start "); } int cli_cmd_volume_info_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; proc = &cli_rpc_prog->proctable[GF1_CLI_GET_VOLUME]; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; if (proc->fn) { ret = proc->fn (frame, THIS, NULL); } out: if (ret) cli_out ("Getting Volume information failed!"); return ret; } int cli_cmd_volume_create_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *options = NULL; proc = &cli_rpc_prog->proctable[GF1_CLI_CREATE_VOLUME]; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; ret = cli_cmd_volume_create_parse (words, wordcount, &options); if (ret) goto out; if (proc->fn) { ret = proc->fn (frame, THIS, options); } out: if (ret) { char *volname = (char *) words[2]; cli_out ("Creating Volume %s failed",volname ); } if (options) dict_destroy (options); return ret; } int cli_cmd_volume_delete_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; char *volname = NULL; proc = &cli_rpc_prog->proctable[GF1_CLI_DELETE_VOLUME]; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; //TODO: Build validation here volname = (char *)words[2]; GF_ASSERT (volname); if (proc->fn) { ret = proc->fn (frame, THIS, volname); } out: if (ret) cli_out ("Deleting Volume %s failed", volname); return ret; } int cli_cmd_volume_start_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; char *volname = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; //TODO: Build validation here if (wordcount < 3) { cli_cmd_volume_start_usage (); goto out; } volname = (char *)words[2]; GF_ASSERT (volname); proc = &cli_rpc_prog->proctable[GF1_CLI_START_VOLUME]; if (proc->fn) { ret = proc->fn (frame, THIS, volname); } out: if (!proc && ret && volname) cli_out ("Starting Volume %s failed", volname); return ret; } int cli_cmd_volume_stop_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; char *volname = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; //TODO: Build validation here volname = (char *)words[2]; GF_ASSERT (volname); proc = &cli_rpc_prog->proctable[GF1_CLI_STOP_VOLUME]; if (proc->fn) { ret = proc->fn (frame, THIS, volname); } out: if (!proc && ret) cli_out ("Stopping Volume %s failed", volname); return ret; } int cli_cmd_volume_rename_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *dict = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; dict = dict_new (); if (!dict) goto out; GF_ASSERT (words[2]); GF_ASSERT (words[3]); //TODO: Build validation here ret = dict_set_str (dict, "old-volname", (char *)words[2]); if (ret) goto out; ret = dict_set_str (dict, "new-volname", (char *)words[3]); if (ret) goto out; proc = &cli_rpc_prog->proctable[GF1_CLI_RENAME_VOLUME]; if (proc->fn) { ret = proc->fn (frame, THIS, dict); } out: if (!proc && ret) { char *volname = (char *) words[2]; if (dict) dict_destroy (dict); cli_out ("Renaming Volume %s failed", volname ); } return ret; } void cli_cmd_volume_defrag_usage () { cli_out ("Usage: volume rebalance "); } int cli_cmd_volume_defrag_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *dict = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; dict = dict_new (); if (!dict) goto out; GF_ASSERT (words[2]); if (!(words[3])) { cli_cmd_volume_defrag_usage(); goto out; } //TODO: Build validation here ret = dict_set_str (dict, "volname", (char *)words[2]); if (ret) goto out; ret = dict_set_str (dict, "command", (char *)words[3]); if (ret) goto out; proc = &cli_rpc_prog->proctable[GF1_CLI_DEFRAG_VOLUME]; if (proc->fn) { ret = proc->fn (frame, THIS, dict); } out: if (!proc && ret) { if (dict) dict_destroy (dict); cli_out ("Defrag of Volume %s failed", (char *)words[2]); } return 0; } int cli_cmd_volume_set_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { cli_cmd_broadcast_response (0); return 0; } int cli_cmd_volume_add_brick_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *options = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; ret = cli_cmd_volume_add_brick_parse (words, wordcount, &options); if (ret) goto out; proc = &cli_rpc_prog->proctable[GF1_CLI_ADD_BRICK]; if (proc->fn) { ret = proc->fn (frame, THIS, options); } out: if (!proc && ret) { char *volname = (char *) words[2]; cli_out ("Adding brick to Volume %s failed",volname ); } return ret; } int cli_cmd_volume_remove_brick_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *options = NULL; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; ret = cli_cmd_volume_remove_brick_parse (words, wordcount, &options); if (ret) goto out; proc = &cli_rpc_prog->proctable[GF1_CLI_REMOVE_BRICK]; if (proc->fn) { ret = proc->fn (frame, THIS, options); } out: if (!proc && ret) { char *volname = (char *) words[2]; cli_out ("Removing brick from Volume %s failed",volname ); } return ret; } int cli_cmd_volume_replace_brick_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { int ret = -1; rpc_clnt_procedure_t *proc = NULL; call_frame_t *frame = NULL; dict_t *options = NULL; proc = &cli_rpc_prog->proctable[GF1_CLI_REPLACE_BRICK]; frame = create_frame (THIS, THIS->ctx->pool); if (!frame) goto out; ret = cli_cmd_volume_replace_brick_parse (words, wordcount, &options); if (ret) goto out; if (proc->fn) { ret = proc->fn (frame, THIS, options); } out: if (ret) { char *volname = (char *) words[2]; cli_out ("Replacing brick from Volume %s failed",volname ); } return ret; } int cli_cmd_volume_set_transport_cbk (struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { cli_cmd_broadcast_response (0); return 0; } struct cli_cmd volume_cmds[] = { { "volume info [all|]", cli_cmd_volume_info_cbk }, { "volume create [stripe ] [replicate ] ...", cli_cmd_volume_create_cbk }, { "volume delete ", cli_cmd_volume_delete_cbk }, { "volume start ", cli_cmd_volume_start_cbk }, { "volume stop ", cli_cmd_volume_stop_cbk }, { "volume rename ", cli_cmd_volume_rename_cbk }, { "volume add-brick [(replica )|(stripe )] ...", cli_cmd_volume_add_brick_cbk }, { "volume remove-brick [(replica )|(stripe )] ...", cli_cmd_volume_remove_brick_cbk }, { "volume rebalance start", cli_cmd_volume_defrag_cbk }, { "volume rebalance stop", cli_cmd_volume_defrag_cbk }, { "volume rebalance status", cli_cmd_volume_defrag_cbk }, { "volume replace-brick ( )|pause|abort|start|status", cli_cmd_volume_replace_brick_cbk }, { "volume set-transport [] ...", cli_cmd_volume_set_transport_cbk }, { "volume set ", cli_cmd_volume_set_cbk }, { NULL, NULL } }; int cli_cmd_volume_register (struct cli_state *state) { int ret = 0; struct cli_cmd *cmd = NULL; for (cmd = volume_cmds; cmd->pattern; cmd++) { ret = cli_cmd_register (&state->tree, cmd->pattern, cmd->cbk); if (ret) goto out; } out: return ret; }