Browse code

ver0.9.3

Masanobu Yasui authored on 2008/10/26 19:06:05
Showing 6 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 0.9.3:
2
- -
2
+ - change msync command design.
3
+ - fixed daemonize error message.
3 4
 
4 5
 0.9.2: 
5 6
  - block device transfer support
... ...
@@ -42,19 +42,19 @@ The makuosan must be invoked with -b option specifying the base directory. The t
42 42
 
43 43
 Makuosan is terminated by sending SIGTERM
44 44
 
45
-   # killall makuosan
45
+   # pkill makuosan
46 46
 
47 47
 [How to transfer files]
48 48
 The msync command line is used to transfer files.
49 49
 
50 50
 Example 1) To send index.html in the base directory.(/var/www/index.html)
51
-   $ msync send index.html
51
+   $ msync index.html
52 52
 
53 53
 Example 2) To recursively send file system tree in the base directory.
54
-   $ msync send -r
54
+   $ msync -r
55 55
 
56 56
 Example 3) To show what will be transfered.(dry run)
57
-   $ msync send -r -n
57
+   $ msync -r -n
58 58
 
59 59
 
60 60
 [Advantages of Makuosan]
... ...
@@ -69,14 +69,14 @@ Example;
69 69
 In the case of MAKUO, file transfer is done between makuosan daemon. 
70 70
 Accordingly, you need to make makuosan up and running on every server, where you want to transfer the files to. As a result, the command line only requires the target files or the target directory.
71 71
 
72
- $ msync send -r hogehoge/
72
+ $ msync -r hogehoge/
73 73
 
74 74
 This is it. This will have a makuosan send hogehoge/ to all other makuosan daemons in the cluster. This is much simpler and easier than the situation where rsync or scp is used.
75 75
 
76 76
 What if a user want to send files only to specified servers?
77 77
 There is -t option to specify the receiver.
78 78
 
79
- $ msync send -t host3 -r hogehoge/
79
+ $ msync -t host3 -r hogehoge/
80 80
 
81 81
 [A case study]
82 82
 
... ...
@@ -137,7 +137,7 @@ There is -t option to specify the receiver.
137 137
 
138 138
  == Issue file transferring instruction ==
139 139
 
140
- host1:~$ msync send -r
140
+ host1:~$ msync -r
141 141
 
142 142
 
143 143
  == Confirm the result ==
... ...
@@ -51,20 +51,20 @@
51 51
 
52 52
 まくおを停止するには、killコマンドなどでSIGTERMを送ります。
53 53
 
54
-   # killall makuosan
54
+   # pkill makuosan
55 55
 
56 56
 
57 57
 【使用方法】
58 58
  ファイルを転送するには「msyncコマンド」を使用します。
59 59
 
60 60
   例1)ベースディレクトリ直下のindex.htmlを転送する(/var/www/index.html)
61
-   $ msync send index.html
61
+   $ msync index.html
62 62
 
63 63
   例2)ベースディレクトリ以下のディレクトリツリーを全部転送する
64
-   $ msync send -r
64
+   $ msync -r
65 65
 
66 66
   例3)転送されるファイルを確認する(dry run)
67
-   $ msync send -r -n
67
+   $ msync -r -n
68 68
 
69 69
 
70 70
 【利用するメリット】
... ...
@@ -82,7 +82,7 @@
82 82
 そのかわり、ファイル転送は以下の例のように転送したいファイルやディレクトリ
83 83
 を伝えるだけで済みます。
84 84
 
85
- $ msync send -r hogehoge/
85
+ $ msync -r hogehoge/
86 86
 
87 87
 このコマンド一発で、「まくお」が稼働している全てのサーバに hogehoge/を転送す
88 88
 ることができます。rsyncやscpを利用する場合と比べて、遥かにシンプルで簡単に転
... ...
@@ -91,12 +91,12 @@
91 91
 また「指定したサーバにだけ転送したい」というニーズのために、特定のサーバにだ
92 92
 け転送するオプション(-t)も用意しています。
93 93
 
94
- $ msync send -t host3 -r hogehoge/
94
+ $ msync -t host3 -r hogehoge/
95 95
 
96 96
 ただし、この場合でも、host3では makuosanが動いている必要があります。尚、転送
97 97
 可能なサーバの一覧は、以下のコマンドで確認することができます。
98 98
 
99
- $ msync members
99
+ $ msync --members
100 100
 
101 101
 
102 102
 【簡単な使用例】
... ...
@@ -158,7 +158,7 @@
158 158
 
159 159
  == ファイルを転送する ==
160 160
 
161
- host1:~$ msync send -r
161
+ host1:~$ msync -r
162 162
 
163 163
 
164 164
  == 転送されたファイルを確認 ==
... ...
@@ -176,10 +176,7 @@ void lprintf(int l, char *fmt, ...)
176 176
     va_start(arg, fmt);
177 177
     vsprintf(msg, fmt, arg);
178 178
     va_end(arg);
179
-    if(moption.dontfork){
180
-      fprintf(stderr, "%d:",l);
181
-      fprintf(stderr, msg);
182
-    }
179
+    fprintf(stderr, "%d: %s", l, msg);
183 180
     syslog(LOG_ERR, "%s: %d: %s", moption.user_name, l, msg);
184 181
   }
185 182
 }
... ...
@@ -13,6 +13,7 @@ char *command_list[]={"quit",     /*  */
13 13
                       "members",  /*  */
14 14
                       "status",   /*  */
15 15
                       "md5",      /*  */
16
+                      "check",    /*  */
16 17
                       "echo",     /*  */
17 18
                       "exclude",  /*  */
18 19
                       "loglevel", /*  */
... ...
@@ -84,7 +85,7 @@ int mexec_scan_send(int fd, char *path, char *sendhost, int mode)
84 84
       if(!is_reg(path)){
85 85
         return(0);
86 86
       }
87
-      sprintf(comm, "md5 %s%s\r\n",     buff, path);
87
+      sprintf(comm, "check %s%s\r\n",     buff, path);
88 88
       break;
89 89
   }
90 90
   mexec_scan_cmd(fd, comm);
... ...
@@ -237,7 +238,7 @@ int mexec_help(mcomm *c, int n)
237 237
   cprintf(0, c, "  exclude list\n");
238 238
   cprintf(0, c, "  exclude clear\n");
239 239
   cprintf(0, c, "  send [-n] [-r] [-t host] [filename]\n");
240
-  cprintf(0, c, "  md5 [-r] [-t host] [filename]\n");
240
+  cprintf(0, c, "  check [-r] [-t host] [filename]\n");
241 241
   cprintf(0, c, "  syncdir [-r] [-t host] [dirname]\n");
242 242
   cprintf(0, c, "  loglevel num (0-9)\n");
243 243
   cprintf(0, c, "  members\n");
... ...
@@ -370,7 +371,7 @@ int mexec_send(mcomm *c, int n)
370 370
   return(0);
371 371
 }
372 372
 
373
-int mexec_md5(mcomm *c, int n)
373
+int mexec_check(mcomm *c, int n)
374 374
 {
375 375
   int i;
376 376
   int r;
... ...
@@ -420,7 +421,7 @@ int mexec_md5(mcomm *c, int n)
420 420
 
421 421
   /*----- help -----*/
422 422
   if(!fn){
423
-    cprintf(0, c,"usage: md5 [-t host] [-r] [path]\r\n");
423
+    cprintf(0, c,"usage: check [-t host] [-r] [path]\r\n");
424 424
     cprintf(0, c, "  -r  # dir recursive\r\n");
425 425
     cprintf(0, c, "  -t  # target host\r\n");
426 426
     return(0);
... ...
@@ -798,7 +799,10 @@ int mexec(mcomm *c, int n)
798 798
       return(mexec_send(c,n));
799 799
 
800 800
     if(!strcmp("md5",command_list[r]))
801
-      return(mexec_md5(c,n));
801
+      return(mexec_check(c,n));
802
+
803
+    if(!strcmp("check",command_list[r]))
804
+      return(mexec_check(c,n));
802 805
 
803 806
     if(!strcmp("syncdir",command_list[r]))
804 807
       return(mexec_syncdir(c,n));
... ...
@@ -1,10 +1,12 @@
1 1
 /*
2 2
  * msync.c
3
- * MAKUOSAN CLI
4 3
  * Copyright (C) 2008 KLab Inc. 
5 4
  */
6 5
 #define _GNU_SOURCE
7 6
 #define _FILE_OFFSET_BITS 64
7
+#ifdef HAVE_CONFIG_H
8
+#include "config.h"
9
+#endif
8 10
 #include <stdio.h>
9 11
 #include <unistd.h>
10 12
 #include <stdlib.h>
... ...
@@ -16,21 +18,52 @@
16 16
 #include <sys/types.h>
17 17
 #include <sys/socket.h>
18 18
 #include <sys/un.h>
19
+#include "makuosan.h"
20
+
21
+excludeitem *exclude = NULL;
19 22
 
20 23
 void usage()
21 24
 {
22
-  printf("usage: msync [-l loglevel] [-c TARGET] [-K PASSWORDFILE] COMMAND [OPT] \n");
23
-  printf("       msync [-l loglevel] [-c TARGET] [-K PASSWORDFILE] -f SCRIPT_FILE\n");
25
+  printf("msync version %s (CLI for makuosan)\n", PACKAGE_VERSION);
26
+  printf("usage: msync [OPTION] [FILENAME]\n");
27
+  printf("\n");
28
+  printf("  OPTION\n");
29
+  printf("    --status            # show makuosan status\n");
30
+  printf("    --members           # show makuosan members\n");
31
+  printf("    --check             # file check use md5\n");
32
+  printf("    --exclude=PATTERN   # \n"); 
33
+  printf("    --exclude-from=FILE # \n");
34
+  printf("\n");
35
+  printf("    -l LOGLEVEL(0-9)    # log level select. default=0\n");
36
+  printf("    -c MSYNC_TARGET     # \n");
37
+  printf("    -f SCRIPT_FILE      # \n");
38
+  printf("    -t HOSTNAME         # distnation hostname\n");
39
+  printf("    -v                  # log level increment\n");
40
+  printf("    -n                  # dry run\n");
41
+  printf("    -r                  # recurse into directories\n");
24 42
   printf("\n");
25
-  printf("  TARGET\n");
26
-  printf("    tcp:HOST:PORT     ex) tcp:127.0.0.1:5000\n");
27
-  printf("    unix:SOCKET       ex) unix:makuosan.sock\n");
43
+  printf("  MSYNC_TARGET\n");
44
+  printf("    tcp:HOST:PORT  ex) tcp:127.0.0.1:5000\n");
45
+  printf("    unix:SOCKET    ex) unix:makuosan.sock\n");
46
+  printf("\n");
47
+  printf("  SCRIPT_FILE\n");
48
+  printf("    (It writes later)\n");
28 49
   printf("\n");
29
-  printf("  COMMAND\n");
30
-  printf("    send [-n] [-r] [-t HOST] [FILENAME]\n");
31
-  printf("    md5  [-r] [FILENAME]\n");
32
-  printf("    status\n");
33
-  printf("    members\n");
50
+}
51
+
52
+excludeitem *add_exclude(char *pattern)
53
+{
54
+  excludeitem *item = malloc(sizeof(excludeitem));
55
+  item->pattern = malloc(strlen(pattern) + 1);
56
+  strcpy(item->pattern, pattern);
57
+  item->prev    = NULL;
58
+  item->next    = NULL;
59
+  if(exclude){
60
+    exclude->prev = item;
61
+    item->next = exclude;
62
+  }
63
+  exclude = item;
64
+  return(item);
34 65
 }
35 66
 
36 67
 int connect_socket_tcp(char *host, char *port)
... ...
@@ -216,7 +249,7 @@ int makuo(int s, char *c)
216 216
   return(r);
217 217
 }
218 218
 
219
-int makuolog(int s, int l)
219
+int makuo_log(int s, int l)
220 220
 {
221 221
   int r;
222 222
   char cmd[256];
... ...
@@ -232,9 +265,45 @@ int makuolog(int s, int l)
232 232
   return(0);
233 233
 }
234 234
 
235
-int makuoquit(int s)
235
+int makuo_exclude(int s)
236
+{
237
+  int r;
238
+  char cmd[1024];
239
+  excludeitem *item;
240
+  for(item=exclude;item;item=item->next){
241
+    sprintf(cmd, "exclude add %s", item->pattern);
242
+    r = makuo(s, cmd);
243
+    if(r == 0){
244
+      fprintf(stderr, "error: makuosan remote close. (%s)\n", cmd);
245
+      return(1);
246
+    }
247
+    if(r == -1){
248
+      fprintf(stderr, "error: makuosan socket error. (%s)\n", cmd);
249
+      return(1);
250
+    }
251
+  }
252
+  return(0);
253
+}
254
+
255
+
256
+int makuo_exec(int s, char *cmd)
257
+{ 
258
+  int r = makuo(s, cmd);
259
+  if(r == 0){
260
+    fprintf(stderr, "error: makuosan remote close. (%s)\n", cmd);
261
+    return(1);
262
+  }
263
+  if(r == -1){
264
+    fprintf(stderr, "error: makuosan socket error. (%s)\n", cmd);
265
+    return(1);
266
+  }
267
+  return(0);
268
+}
269
+
270
+int makuo_quit(int s)
236 271
 { 
237 272
   int r = makuo(s, "quit");
273
+  close(s);
238 274
   if(r == 0){
239 275
     return(0); /* success */
240 276
   }
... ...
@@ -245,14 +314,13 @@ int makuoquit(int s)
245 245
   return(1);
246 246
 }
247 247
 
248
-int fromfile(int s, char *filename)
248
+int exclude_from(char *filename)
249 249
 {
250 250
   int  f;
251 251
   int  r;
252 252
   char line[256];
253 253
 
254 254
   if(!strcmp(filename, "-")){
255
-    /* f = stdin */
256 255
     f = dup(0);
257 256
   }else{
258 257
     f = open(filename, O_RDONLY);
... ...
@@ -261,60 +329,49 @@ int fromfile(int s, char *filename)
261 261
     fprintf(stderr,"can't open: %s\n", filename);
262 262
     return(1);
263 263
   }
264
-
265
-  /* command read loop */
266 264
   while(r = readline(f, line, sizeof(line), 0, NULL)){
267 265
     if(r == -1){
268 266
       fprintf(stderr, "file read error: %s\n", filename);
269 267
       close(f);
270 268
       return(1);
271 269
     }
272
-    r = makuo(s, line);
273
-    if(r == 0){
274
-      fprintf(stderr, "error: makuosan remote close\n");
275
-      close(f);
276
-      return(1);
277
-    }
278
-    if(r == -1){
279
-      close(f);
280
-      return(1); /* socket error */
281
-    }
270
+    add_exclude(line);
282 271
   }
283
-
284
-  /* quit */
285
-  return(makuoquit(s));
272
+  close(f);
273
+  return(0);
286 274
 }
287 275
 
288
-int fromargv(int s, int argc, char *argv[], int start)
276
+int fromfile(int s, char *filename)
289 277
 {
290
-  int i;
291
-  int r;
292
-  char cmd[256];
278
+  int  f;
279
+  int  r;
280
+  char line[256];
293 281
 
294
-  cmd[0] = 0;
295
-  for(i=start;i<argc;i++){
296
-    if(strlen(cmd) + strlen(argv[i]) + 2 > sizeof(cmd)){
297
-      fprintf(stderr, "error: command too long\n");
298
-      return(1);
299
-    }
300
-    if(cmd[0]){
301
-      strcat(cmd, " ");
302
-    }
303
-    strcat(cmd, argv[i]);
282
+  if(!strcmp(filename, "-")){
283
+    /* f = stdin */
284
+    f = dup(0);
285
+  }else{
286
+    f = open(filename, O_RDONLY);
304 287
   }
305
-
306
-  /* command execute */
307
-  r = makuo(s, cmd);
308
-  if(r == 0){
309
-    fprintf(stderr, "error: makuosan remote close\n");
288
+  if(f == -1){
289
+    fprintf(stderr,"can't open: %s\n", filename);
310 290
     return(1);
311 291
   }
312
-  if(r == -1){
313
-    return(1); /* socket error */
314
-  }
315 292
 
316
-  /* quit */
317
-  return(makuoquit(s));
293
+  /* command read loop */
294
+  while(r = readline(f, line, sizeof(line), 0, NULL)){
295
+    if(r == -1){
296
+      fprintf(stderr, "file read error: %s\n", filename);
297
+      close(f);
298
+      return(1);
299
+    }
300
+    if(makuo_exec(s, line)){
301
+      close(f);
302
+      return(1);
303
+    }
304
+  }
305
+  close(f);
306
+  return(makuo_quit(s));
318 307
 }
319 308
 
320 309
 int loadpass(char *filename, char *passwd, int size)
... ...
@@ -346,26 +403,113 @@ void defaulttarget(char *target, int size)
346 346
 
347 347
 int main(int argc, char *argv[])
348 348
 {
349
+  int i;
349 350
   int r;
350 351
   int s;
351 352
 
353
+  if(argc == 1){
354
+    usage();
355
+    return(1);
356
+  }
357
+
358
+  /* makuo command */
359
+  char cmd[1024];
360
+  char mcmd[256];
361
+  char mopt[256];
362
+  strcpy(mcmd,"send");
363
+  strcpy(mopt,"");
364
+
352 365
   /* option */
366
+  int loopflag = 1;
353 367
   int loglevel = 0;
354 368
   char scfile[256];
355 369
   char passwd[256];
356 370
   char target[256];
357 371
 
372
+  /* long option */
373
+  struct option longopt[7];
374
+  memset(longopt, 0, sizeof(longopt));
375
+  longopt[0].name    = "help";
376
+  longopt[0].has_arg = 0;
377
+  longopt[0].flag    = NULL;
378
+  longopt[0].val     = 'h';
379
+  longopt[1].name    = "status";
380
+  longopt[1].has_arg = 0;
381
+  longopt[1].flag    = NULL;
382
+  longopt[1].val     = 'S';
383
+  longopt[2].name    = "members";
384
+  longopt[2].has_arg = 0;
385
+  longopt[2].flag    = NULL;
386
+  longopt[2].val     = 'M';
387
+  longopt[3].name    = "check";
388
+  longopt[3].has_arg = 0;
389
+  longopt[3].flag    = NULL;
390
+  longopt[3].val     = 'C';
391
+  longopt[4].name    = "exclude";
392
+  longopt[4].has_arg = 1;
393
+  longopt[4].flag    = NULL;
394
+  longopt[4].val     = 'E';
395
+  longopt[5].name    = "exclude-from";
396
+  longopt[5].has_arg = 1;
397
+  longopt[5].flag    = NULL;
398
+  longopt[5].val     = 'F';
399
+  longopt[6].name    = "delete";
400
+  longopt[6].has_arg = 0;
401
+  longopt[6].flag    = NULL;
402
+  longopt[6].val     = 'D';
403
+
358 404
   /* default */
359 405
   scfile[0] = 0;
360 406
   passwd[0] = 0;
361 407
   defaulttarget(target, sizeof(target));
362 408
 
363
-  while((r = getopt(argc, argv, "+c:f:K:l:h")) != -1){
409
+  while((r = getopt_long(argc, argv, "c:f:t:K:l:hvrn", longopt, NULL)) != -1){
364 410
     switch(r){
365 411
       case 'h':
366 412
         usage();
367 413
         return(0);
368 414
 
415
+      case 'S':
416
+        strcpy(mcmd, "status");
417
+        loopflag = 0;
418
+        break;
419
+
420
+      case 'M':
421
+        strcpy(mcmd, "members");
422
+        loopflag = 0;
423
+        break;
424
+
425
+      case 'C':
426
+        strcpy(mcmd, "check");
427
+        break;
428
+
429
+      case 'E':
430
+        add_exclude(optarg);
431
+        break;
432
+
433
+      case 'F':
434
+        if(exclude_from(optarg)){
435
+          return(1);
436
+        }
437
+        break;
438
+
439
+      case 'r':
440
+        strcat(mopt," -r");
441
+        break;
442
+
443
+      case 'n':
444
+        strcat(mopt," -n");
445
+        break;
446
+
447
+      case 't':
448
+        strcat(mopt," -t ");
449
+        strcat(mopt,optarg);
450
+        break;
451
+
452
+      case 'v':
453
+        loglevel++;
454
+        break;
455
+
369 456
       case 'l':
370 457
         loglevel = atoi(optarg);
371 458
         break;
... ...
@@ -393,12 +537,12 @@ int main(int argc, char *argv[])
393 393
           return(1);
394 394
         }
395 395
         break;
396
-    }
397
-  }
398 396
 
399
-  if(!scfile[0] && optind == argc){
400
-    usage();
401
-    return(1);
397
+      case '?':
398
+        usage();
399
+        return(1);
400
+        break;
401
+    }
402 402
   }
403 403
 
404 404
   s = connect_socket(target);
... ...
@@ -413,23 +557,46 @@ int main(int argc, char *argv[])
413 413
     close(s);
414 414
     return(1);
415 415
   }
416
+
416 417
   if(r == -1){
417 418
     fprintf(stderr, "socket read error\n");
418 419
     close(s);
419 420
     return(1);
420 421
   }
421 422
 
422
-  if(makuolog(s, loglevel)){
423
+  if(makuo_log(s, loglevel)){
424
+    close(s);
425
+    return(1);
426
+  }
427
+
428
+  if(makuo_exclude(s)){
423 429
     close(s);
424 430
     return(1);
425 431
   }
426 432
 
427 433
   if(scfile[0]){
428
-    r = fromfile(s, scfile);
434
+    return(fromfile(s, scfile));
435
+  }
436
+
437
+  if(loopflag && (optind < argc)){
438
+    for(i=optind;i<argc;i++){
439
+      sprintf(cmd, "%s%s %s", mcmd, mopt, argv[i]);
440
+      if(makuo_exec(s, cmd)){
441
+        close(s);
442
+        return(1);
443
+      }
444
+    }
429 445
   }else{
430
-    r = fromargv(s, argc, argv, optind);
446
+    sprintf(cmd, "%s%s", mcmd, mopt);
447
+    for(i=optind;i<argc;i++){
448
+      strcat(cmd, " ");
449
+      strcat(cmd, argv[i]);
450
+    }
451
+    if(makuo_exec(s, cmd)){
452
+      close(s);
453
+      return(1);
454
+    }
431 455
   }
432
-  close(s);
433
-  return(r);
456
+  return(makuo_quit(s));
434 457
 }
435 458