summaryrefslogtreecommitdiffstats
path: root/gluster-block.c
blob: b8e64b90b0a08411f52803c504227082b2cc77f7 (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
/*
  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  <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 opterations {
  CREATE_CLI = 1,
  LIST_CLI   = 2,
  INFO_CLI   = 3,
  DELETE_CLI = 4
} opterations;


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

  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("client: socket");
    exit(1);
  }

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

  len = sizeof(saun.sun_family) + strlen(saun.sun_path);

  if (connect(sockfd, (struct sockaddr *) &saun, len) < 0) {
    perror("client: connect");
    exit(1);
  }

  clnt = clntunix_create ((struct sockaddr_un *) &saun, GLUSTER_BLOCK_CLI, GLUSTER_BLOCK_CLI_VERS, &sockfd, 0, 0);
  if (clnt == NULL) {
    clnt_pcreateerror ("localhost");
    exit (1);
  }
  switch(opt) {
  case CREATE_CLI:
    reply = block_create_cli_1((blockCreateCli *)cobj, clnt);
    if (reply == NULL) {
      clnt_perror (clnt, "call failed gluster-block");
    }
    break;
  case DELETE_CLI:
    reply = block_delete_cli_1((blockDeleteCli *)cobj, clnt);
    if (reply == NULL) {
      clnt_perror (clnt, "call failed gluster-block");
    }
    break;
  case INFO_CLI:
    reply = block_info_cli_1((blockInfoCli *)cobj, clnt);
    if (reply == NULL) {
      clnt_perror (clnt, "call failed gluster-block");
    }
    break;
  case LIST_CLI:
    reply = block_list_cli_1((blockListCli *)cobj, clnt);
    if (reply == NULL) {
      clnt_perror (clnt, "call failed gluster-block");
    }
    break;
  }

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

out:
  if (!clnt_freeres(clnt, (xdrproc_t) xdr_blockResponse, (char *) reply))
    clnt_perror (clnt, "clnt_freeres failed");

  clnt_destroy (clnt);

  return ret;
}


static void
glusterBlockHelp(void)
{
  MSG("%s",
      "gluster-block (Version 0.1) \n"
      " -c, --create      <name>          Create the gluster block\n"
      "     -h, --host         <gluster-node>   node addr from gluster pool\n"
      "     -s, --size         <size>           block storage size in KiB|MiB|GiB|TiB..\n"
      "\n"
      " -l, --list                        List available gluster blocks\n"
      "\n"
      " -i, --info        <name>          Details about gluster block\n"
      "\n"
      " -m, --modify      <resize|auth>   Modify the metadata\n"
      "\n"
      " -d, --delete      <name>          Delete the gluster block\n"
      "\n"
      "     -v, --volume       <vol>            gluster volume name\n"
      "    [-b, --block-host   <IP1,IP2,IP3...>]  block servers, clubbed with any option\n");
}


static int
glusterBlockCreate(int count, char **options, char *name)
{
  int c;
  int ret = 0;
  char *out = NULL;
  static blockCreateCli cobj;

  if (!name) {
    ERROR("%s", "Insufficient arguments supplied for"
                "'gluster-block create'");
    ret = -1;
    goto out;
  }

  strcpy(cobj.block_name, name);

  while (1) {
    static const struct option long_options[] = {
      {"volume",     required_argument, 0, 'v'},
      {"host",       required_argument, 0, 'h'},
      {"size",       required_argument, 0, 's'},
      {"block-host", required_argument, 0, 'b'},
      {0, 0, 0, 0}
    };

    /* getopt_long stores the option index here. */
    int option_index = 0;

    c = getopt_long(count, options, "b:v:h:s:",
                    long_options, &option_index);

    if (c == -1)
      break;

    switch (c) {
    case 'b':
      if (GB_STRDUP(cobj.block_hosts, optarg) < 0)
        return -1;
      break;

    case 'v':
      strcpy(cobj.volume, optarg);
      ret++;
      break;

    case 'h':
      strcpy(cobj.volfileserver, optarg);
      ret++;
      break;

    case 's':
      cobj.size = glusterBlockCreateParseSize(optarg);
      if (cobj.size < 0) {
        ERROR("%s", "failed while parsing size");
        ret = -1;
        goto out;
      }
      ret++;
      break;

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

    default:
      break;
    }
  }

  /* Print any remaining command line arguments (not options). */
  if (optind < count) {
    ERROR("%s", "non-option ARGV-elements: ");
    while (optind < count)
      printf("%s ", options[optind++]);
    putchar('\n');

    ret = -1;
    goto out;
  }

  if (ret != 3) {
    ERROR("%s", "Insufficient arguments supplied for"
                "'gluster-block create'\n");
    ret = -1;
    goto out;
  }

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

  MSG("%s", out);

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

  return ret;
}


static int
glusterBlockList(char *volume, char *blkServers)
{
  static blockListCli cobj;
  char *out = NULL;
  int ret = -1;

  strcpy(cobj.volume, volume);
  if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) {
    return -1;
  }

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

  MSG("%s", out);
  GB_FREE(cobj.block_hosts);
  GB_FREE(out);

  return ret;
}


static int
glusterBlockDelete(char* name, char* volume, char *blkServers)
{
  static blockDeleteCli cobj;
  char *out = NULL;
  int ret = -1;

  strcpy(cobj.block_name, name);
  strcpy(cobj.volume, volume);
  if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) {
    return -1;
  }

  ret = gluster_block_cli_1(&cobj, DELETE_CLI, &out);

  MSG("%s", out);
  GB_FREE(cobj.block_hosts);
  GB_FREE(out);

  return ret;
}


static int
glusterBlockInfo(char* name, char* volume, char *blkServers)
{
  static blockInfoCli cobj;
  char *out = NULL;
  int ret = -1;

  strcpy(cobj.block_name, name);
  strcpy(cobj.volume, volume);
  if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) {
    return -1;
  }

  ret = gluster_block_cli_1(&cobj, INFO_CLI, &out);

  MSG("%s", out);
  GB_FREE(cobj.block_hosts);
  GB_FREE(out);

  return ret;
}


static int
glusterBlockParseArgs(int count, char **options)
{
  int c;
  int ret = 0;
  int optFlag = 0;
  char *block = NULL;
  char *blkServers = NULL;
  char *volume = NULL;

  while (1) {
    static const struct option long_options[] = {
      {HELP,      no_argument,       0, 'h'},
      {CREATE,    required_argument, 0, 'c'},
      {DELETE,    required_argument, 0, 'd'},
      {LIST,      no_argument,       0, 'l'},
      {INFO,      required_argument, 0, 'i'},
      {MODIFY,    required_argument, 0, 'm'},
      {VOLUME,    required_argument, 0, 'v'},
      {BLOCKHOST, required_argument, 0, 'b'},
      {0, 0, 0, 0}
    };

    /* getopt_long stores the option index here. */
    int option_index = 0;

    c = getopt_long(count, options, "hc:b:d:lim:",
                    long_options, &option_index);

    /* Detect the end of the options. */
    if (c == -1)
      break;

    switch (c) {
    case 'b':
      blkServers = optarg;
      if (optFlag)
        goto opt;
      break;

    case 'v':
      volume = optarg;
      break;

    case 'c':
      ret = glusterBlockCreate(count, options, optarg);
      if (ret && ret != EEXIST) {
        ERROR("%s", FAILED_CREATE);
        goto out;
      }
      break;

    case 'l':
    case 'd':
    case 'i':
      if (optFlag) /* more than one main opterations ?*/
        goto out;
      optFlag = c;
      block = optarg;
      if (blkServers)
        goto opt;
      break;

    case 'm':
      MSG("option --modify yet TODO '%s'", optarg);
      break;

    case 'h':
      glusterBlockHelp();
      break;

    case '?':
      /* getopt_long already printed an error message. */
      break;
    }
  }

 opt:
  switch (optFlag) {
  case 'l':
    ret = glusterBlockList(volume, blkServers);
    if (ret)
        ERROR("%s", FAILED_LIST);
    break;
  case 'i':
    ret = glusterBlockInfo(block, volume, blkServers);
    if (ret)
        ERROR("%s", FAILED_INFO);
    break;
  case 'd':
    ret = glusterBlockDelete(block, volume, blkServers);
    if (ret)
        ERROR("%s", FAILED_DELETE);
    break;
  }

 out:
  if (ret == 0 && optind < count) {
    ERROR("%s", "Unable to parse elements: ");
    while (optind < count)
      printf("%s ", options[optind++]);
    putchar('\n');
    MSG("Hint: %s --help", options[0]);
  }

  return ret;
}


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

  ret = glusterBlockParseArgs(argc, argv);

  return ret;
}