summaryrefslogtreecommitdiffstats
path: root/gluster-block.c
blob: b9d21c2f22f178e8ab7b004099028b19afbe33f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
  This file is part of gluster-block.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/


# define   _GNU_SOURCE         /* See feature_test_macros(7) */

# include  <unistd.h>
# include  <getopt.h>

# include  "common.h"
# include  "rpc/block.h"


# define  LIST      "list"
# define  CREATE    "create"
# define  DELETE    "delete"
# define  INFO      "info"
# define  MODIFY    "modify"
# define  BLOCKHOST "block-host"
# define  VOLUME    "volume"
# define  HELP      "help"



typedef enum operations {
  CREATE_CLI = 1,
  LIST_CLI   = 2,
  INFO_CLI   = 3,
  DELETE_CLI = 4
} operations;


static int
glusterBlockCliRPC_1(void *cobj, operations opt, char **out)
{
  CLIENT *clnt = NULL;
  int ret = -1;
  int sockfd;
  struct sockaddr_un saun;
  blockResponse *reply;


  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    LOG("cli", GB_LOG_ERROR, "socket creation failed (%s)", strerror (errno));
    goto out;
  }

  saun.sun_family = AF_UNIX;
  strcpy(saun.sun_path, ADDRESS);

  if (connect(sockfd, (struct sockaddr *) &saun,
              sizeof(struct sockaddr_un)) < 0) {
    LOG("cli", GB_LOG_ERROR, "connect failed (%s)", strerror (errno));
    goto out;
  }

  clnt = clntunix_create ((struct sockaddr_un *) &saun,
                          GLUSTER_BLOCK_CLI, GLUSTER_BLOCK_CLI_VERS,
                          &sockfd, 0, 0);
  if (!clnt) {
    LOG("cli", GB_LOG_ERROR, "%s, unix addr %s",
        clnt_spcreateerror("client create failed"), ADDRESS);
    goto out;
  }

  switch(opt) {
  case CREATE_CLI:
    reply = block_create_cli_1((blockCreateCli *)cobj, clnt);
    if (!reply) {
      LOG("cli", GB_LOG_ERROR, "%s", clnt_sperror(clnt, "block create failed"));
      goto out;
    }
    break;
  case DELETE_CLI:
    reply = block_delete_cli_1((blockDeleteCli *)cobj, clnt);
    if (!reply) {
      LOG("cli", GB_LOG_ERROR, "%s", clnt_sperror(clnt, "block delete failed"));
      goto out;
    }
    break;
  case INFO_CLI:
    reply = block_info_cli_1((blockInfoCli *)cobj, clnt);
    if (!reply) {
      LOG("cli", GB_LOG_ERROR, "%s", clnt_sperror(clnt, "block info failed"));
      goto out;
    }
    break;
  case LIST_CLI:
    reply = block_list_cli_1((blockListCli *)cobj, clnt);
    if (!reply) {
      LOG("cli", GB_LOG_ERROR, "%s", clnt_sperror(clnt, "block list failed"));
      goto out;
    }
    break;
  }

  if (GB_STRDUP(*out, reply->out) < 0)
    goto out;
  ret = reply->exit;

 out:
  if (clnt) {
    if (!reply || !clnt_freeres(clnt, (xdrproc_t)xdr_blockResponse, (char *)reply))
      LOG("cli", GB_LOG_ERROR, "%s", clnt_sperror(clnt, "clnt_freeres failed"));

    clnt_destroy (clnt);
  }

  return ret;
}


static void
glusterBlockHelp(void)
{
  MSG("%s",
      "gluster-block (Version 0.1) \n"
      " create         <name>          Create the gluster block\n"
      "    volserver      [gluster-node]     node addr from gluster pool(default: localhost)\n"
      "    size           <size>             block storage size in KiB|MiB|GiB|TiB..\n"
      "    mpath          <count>            multi path requirement for high availablity\n"
      "    servers        <IP1,IP2,IP3...>   block servers, clubbed with any option\n"
      "\n"
      " list                           List available gluster blocks\n"
      "\n"
      " info           <name>          Details about gluster block\n"
      "\n"
      " modify         <resize|auth>   Modify the metadata\n"
      "\n"
      " delete         <name>          Delete the gluster block\n"
      "\n"
      "    volume         <vol>              gluster volume name\n"
      );
}


static int
glusterBlockCreate(int argcount, char **options)
{
  size_t opt;
  size_t optind = 2;
  int ret = 0;
  char *out = NULL;
  bool volserver = FALSE;
  static blockCreateCli cobj = {0, };


  if(argcount <= optind) {
    MSG("%s\n", "Insufficient options for create");
    return -1;
  }

  /* name of block */
  strcpy(cobj.block_name, options[optind++]);

  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;
    }


    switch (opt) {
    case GB_CLI_CREATE_VOLUME:
      strcpy(cobj.volume, options[optind++]);
      ret++;
      break;

    case GB_CLI_CREATE_VOLSERVER:
      strcpy(cobj.volfileserver, options[optind++]);
      volserver = TRUE;
      break;

    case GB_CLI_CREATE_MULTIPATH:
      sscanf(options[optind++], "%u", &cobj.mpath);
      ret++;
      break;

    case GB_CLI_CREATE_SIZE:
      cobj.size = glusterBlockCreateParseSize(options[optind++]);
      if (cobj.size < 0) {
        LOG("cli", GB_LOG_ERROR, "%s", "failed while parsing size");
        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, "%s", "failed while parsing size");
        ret = -1;
        goto out;
      }
      ret++;
      break;

    default:
      MSG("unrecognized option '%s'\n", options[optind-1]);
      MSG("%s", "Hint: gluster-block help\n");
      goto out;
    }
  }

  /* check all options required by create command are specified */
  if(ret < 4) {
    MSG("%s\n", "Insufficient options for create");
    ret = -1;
    goto out;
  }

  if(!volserver) {
    strcpy(cobj.volfileserver, "localhost");
  }

  ret = glusterBlockCliRPC_1(&cobj, CREATE_CLI, &out);

  if(out)
    MSG("%s", out);

 out:
  GB_FREE(cobj.block_hosts);
  GB_FREE(out);

  return ret;
}


static int
glusterBlockList(int argcount, char **options)
{
  size_t opt;
  size_t optind = 2;
  static blockListCli cobj;
  char *out = NULL;
  int ret = -1;


  if(argcount <= optind) {
    MSG("%s\n", "Insufficient options for 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;
  }

  if ((opt == GB_CLI_COMMON_VOLUME)) {
    strcpy(cobj.volume, options[optind]);

    ret = glusterBlockCliRPC_1(&cobj, LIST_CLI, &out);

    if(out)
      MSG("%s", out);

    GB_FREE(out);
  }

  return ret;
}


static int
glusterBlockDelete(int argcount, char **options)
{
  size_t opt;
  size_t optind = 2;
  static blockDeleteCli cobj;
  char *out = NULL;
  int ret = -1;

  if(argcount <= optind) {
    MSG("%s\n", "Insufficient options for 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 ((opt == GB_CLI_COMMON_VOLUME)) {
    strcpy(cobj.volume, options[optind]);
    ret = glusterBlockCliRPC_1(&cobj, DELETE_CLI, &out);

    if(out)
      MSG("%s", out);

    GB_FREE(out);
  }

  return ret;
}


static int
glusterBlockInfo(int argcount, char **options)
{
  size_t opt;
  size_t optind = 2;
  static blockInfoCli cobj;
  char *out = NULL;
  int ret = -1;

  if(argcount <= optind) {
    MSG("%s\n", "Insufficient options for 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 ((opt == GB_CLI_COMMON_VOLUME)) {
    strcpy(cobj.volume, options[optind]);
    ret = glusterBlockCliRPC_1(&cobj, INFO_CLI, &out);

    if(out)
      MSG("%s", out);

    GB_FREE(out);
  }

  return ret;
}


static int
glusterBlockParseArgs(int count, char **options)
{
  int ret = 0;
  size_t opt = 0;

  opt = glusterBlockCLIOptEnumParse(options[1]);
  if (!opt || opt >= GB_CLI_OPT_MAX) {
    MSG("unknow option: %s\n", options[1]);
    return -1;
  }

  while (1) {
    switch (opt) {
    case GB_CLI_CREATE:
      ret = glusterBlockCreate(count, options);
      if (ret && ret != EEXIST) {
        LOG("cli", GB_LOG_ERROR, "%s", FAILED_CREATE);
      }
      goto out;

    case GB_CLI_LIST:
      ret = glusterBlockList(count, options);
      if (ret) {
        LOG("cli", GB_LOG_ERROR, "%s", FAILED_LIST);
      }
      goto out;

    case GB_CLI_INFO:
      ret = glusterBlockInfo(count, options);
      if (ret) {
        LOG("cli", GB_LOG_ERROR, "%s", FAILED_INFO);
      }
      goto out;

    case GB_CLI_DELETE:
      ret = glusterBlockDelete(count, options);
      if (ret) {
        LOG("cli", GB_LOG_ERROR, "%s", FAILED_DELETE);
      }
      goto out;

    case GB_CLI_HELP:
      glusterBlockHelp();
      goto out;
    }
  }

 out:
  return ret;
}


int
main(int argc, char *argv[])
{
  if (argc <= 1)
    glusterBlockHelp();

  return glusterBlockParseArgs(argc, argv);
}