1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.codehaus.plexus.util.cli.CommandLineException;
28 import org.codehaus.plexus.util.cli.CommandLineUtils;
29 import org.codehaus.plexus.util.cli.Commandline;
30 import org.codehaus.plexus.util.cli.StreamConsumer;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37 public class UnixUtils {
38
39 private static final Logger logger = LoggerFactory.getLogger(UnixUtils.class);
40
41 private static final String SCP = "scp";
42 private static final String SSH = "ssh";
43 private static final String RSYNC = "rsync";
44 private static final String FORWARD_SLASH = "/";
45 public static final int SUCCESS = 0;
46
47 private static final UnixCmds cmds = new UnixCmds();
48
49
50
51
52
53
54
55
56
57 public static final int rsyncdirs(File source, File destination) {
58 String sourcePath = validateRsyncSourceDir(source);
59 String destinationPath = validateRsyncDestinationDir(destination);
60
61
62 boolean different = !source.equals(destination);
63 Assert.isTrue(different);
64
65 return rsyncdirs(null, sourcePath, destinationPath);
66 }
67
68
69
70
71
72
73
74
75 public static final int rsyncdirs(File source, String destination) {
76 String sourcePath = validateRsyncSourceDir(source);
77 return rsyncdirs(null, sourcePath, destination);
78 }
79
80
81
82
83
84
85
86
87 public static final int rsyncdirs(String source, File destination) {
88 String destinationPath = validateRsyncDestinationDir(destination);
89 return rsyncdirs(null, source, destinationPath);
90 }
91
92
93
94
95
96
97
98
99
100 public static final int rsyncdirs(List<String> options, File source, File destination) {
101 String sourcePath = validateRsyncSourceDir(source);
102 String destinationPath = validateRsyncDestinationDir(destination);
103 return rsyncdirs(options, sourcePath, destinationPath);
104 }
105
106
107
108
109
110
111
112
113 public static final int rsync(List<String> options, File source, String destination) {
114 String sourcePath = validateRsyncSourceDir(source);
115 return rsyncdirs(options, sourcePath, destination);
116 }
117
118
119
120
121
122
123
124
125 public static final int rsyncdirs(List<String> options, String source, File destination) {
126 String destinationPath = validateRsyncDestinationDir(destination);
127 return rsyncdirs(options, source, destinationPath);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public static final int rsyncdirs(List<String> options, String source, String destination) {
148 List<String> rsyncDirOptions = getRsyncDirOptions(options);
149
150 String trailingSlashSource = StringUtils.endsWith(source, FORWARD_SLASH) ? source : source + FORWARD_SLASH;
151 return rsync(rsyncDirOptions, trailingSlashSource, destination);
152 }
153
154
155
156
157
158
159
160
161 public static final int rsyncdirs(String source, String destination) {
162 return rsyncdirs(null, source, destination);
163 }
164
165
166
167
168
169
170
171
172 public static final int rsync(String source, String destination) {
173 return rsync(null, source, destination);
174 }
175
176
177
178
179
180
181
182
183 public static final int rsync(List<String> options, String source, String destination) {
184 Assert.notNull(source);
185 Assert.notNull(destination);
186 List<String> arguments = new ArrayList<String>();
187 arguments.addAll(CollectionUtils.toEmptyList(options));
188 arguments.add(source);
189 arguments.add(destination);
190 Commandline cl = new Commandline();
191 cl.setExecutable(RSYNC);
192 cl.addArguments(CollectionUtils.toStringArray(arguments));
193 return execute(cl);
194 }
195
196
197
198
199
200
201 public static final int sshchown(List<String> args, String user, String hostname, List<String> options, String owner, String group, String file) {
202 Assert.notNull(owner);
203 Assert.notNull(group);
204 Assert.notNull(file);
205 String command = cmds.chown(options, owner, group, file);
206 return ssh(args, user, hostname, command);
207 }
208
209
210
211
212
213
214 public static final int sshchownr(String hostname, String owner, String group, String file) {
215 return sshchownr(null, null, hostname, owner, group, file);
216 }
217
218
219
220
221
222
223 public static final int sshchownr(String user, String hostname, String owner, String group, String file) {
224 return sshchownr(null, user, hostname, owner, group, file);
225 }
226
227
228
229
230
231
232 public static final int sshchownr(List<String> args, String hostname, String owner, String group, String file) {
233 return sshchownr(args, null, hostname, owner, group, file);
234 }
235
236
237
238
239
240
241 public static final int sshchownr(List<String> args, String user, String hostname, String owner, String group, String file) {
242 return sshchown(args, user, hostname, Arrays.asList("-R"), owner, group, file);
243 }
244
245
246
247
248
249
250 public static final int sshchown(List<String> args, String hostname, String owner, String group, String file) {
251 return sshchown(args, null, hostname, null, owner, group, file);
252 }
253
254
255
256
257
258
259 public static final int sshchown(List<String> args, String user, String hostname, String owner, String group, String file) {
260 return sshchown(args, user, hostname, null, owner, group, file);
261 }
262
263
264
265
266
267
268 public static final int sshchown(String user, String hostname, String owner, String group, String file) {
269 return sshchown(null, user, hostname, null, owner, group, file);
270 }
271
272
273
274
275
276
277 public static final int sshchown(String hostname, String owner, String group, String file) {
278 return sshchown(null, null, hostname, owner, group, file);
279 }
280
281
282
283
284
285
286 public static final int sshrm(String hostname, String file) {
287 return sshrm(null, null, hostname, file);
288 }
289
290
291
292
293
294
295 public static final int sshrm(String user, String hostname, String file) {
296 return sshrm(null, user, hostname, file);
297 }
298
299
300
301
302
303
304 public static final int sshrm(List<String> args, String hostname, String file) {
305 return sshrm(args, null, hostname, file);
306 }
307
308
309
310
311
312
313 public static final int sshrm(List<String> args, String user, String hostname, String file) {
314 Assert.notNull(file);
315 return sshrm(args, user, hostname, Collections.singletonList(file));
316 }
317
318
319
320
321
322
323 public static final int sshrm(String hostname, List<String> paths) {
324 return sshrm(null, null, hostname, paths);
325 }
326
327
328
329
330
331
332 public static final int sshrm(String user, String hostname, List<String> paths) {
333 return sshrm(null, user, hostname, paths);
334 }
335
336
337
338
339
340
341 public static final int sshrm(List<String> args, String hostname, List<String> paths) {
342 return sshrm(args, null, hostname, paths);
343 }
344
345
346
347
348
349
350 public static final int sshrm(List<String> args, String user, String hostname, List<String> paths) {
351 return sshrm(args, user, hostname, Arrays.asList("-r", "-f"), paths);
352 }
353
354
355
356
357
358
359 public static final int sshrm(List<String> args, String user, String hostname, List<String> options, List<String> paths) {
360 Assert.notEmpty(paths);
361 String command = cmds.rm(options, paths);
362 return ssh(args, user, hostname, command);
363 }
364
365
366
367
368
369
370 public static final int sshchmod(List<String> args, String user, String hostname, String mode, String path) {
371 Assert.hasLength(mode);
372 Assert.notNull(path);
373 return ssh(args, user, hostname, cmds.chmod(mode, path));
374 }
375
376
377
378
379
380
381 public static final int sshchmod(String user, String hostname, String mode, String file) {
382 return sshchmod(null, user, hostname, mode, file);
383 }
384
385
386
387
388
389
390 public static final int sshmkdir(String user, String hostname, String path) {
391 return sshmkdir(null, user, hostname, path);
392 }
393
394
395
396
397
398
399 public static final int sshmkdir(List<String> args, String user, String hostname, String path) {
400 Assert.notBlank(path);
401 return ssh(args, user, hostname, cmds.mkdirp(path));
402 }
403
404
405
406
407
408
409 public static final int sshmkdir(String hostname, String path) {
410 return sshmkdir(null, null, hostname, path);
411 }
412
413
414
415
416
417
418 public static final int sshmkdir(List<String> args, String hostname, String path) {
419 return sshmkdir(args, null, hostname, path);
420 }
421
422
423
424
425
426
427 public static final int sshsu(String hostname, String login, String command) {
428 return sshsu(null, null, hostname, login, command);
429 }
430
431
432
433
434
435
436 public static final int sshsu(List<String> args, String hostname, String login, String command) {
437 return sshsu(args, null, hostname, login, command);
438 }
439
440
441
442
443
444
445 public static final int sshsu(String user, String hostname, String login, String command) {
446 return sshsu(null, user, hostname, login, command);
447 }
448
449
450
451
452
453
454 public static final int sshsu(List<String> args, String user, String hostname, String login, String command) {
455 Assert.notNull(login);
456 Assert.notNull(command);
457 return ssh(user, hostname, cmds.su(login, command));
458 }
459
460
461
462
463
464
465 public static final int ssh(String hostname, String command) {
466 return ssh(null, null, hostname, command);
467 }
468
469
470
471
472
473
474 public static final int ssh(String user, String hostname, String command) {
475 return ssh(null, user, hostname, command);
476 }
477
478
479
480
481
482
483 public static final int ssh(List<String> args, String hostname, String command) {
484 return ssh(args, null, hostname, command);
485 }
486
487
488
489
490
491
492 public static final int ssh(List<String> args, String user, String hostname, String command) {
493 Assert.notNull(hostname);
494 Assert.notNull(command);
495 List<String> arguments = new ArrayList<String>();
496 arguments.addAll(CollectionUtils.toEmptyList(args));
497 if (!StringUtils.isBlank(user)) {
498 arguments.add(user + "@" + hostname);
499 } else {
500 arguments.add(hostname);
501 }
502 arguments.add(command);
503 Commandline cl = new Commandline();
504 cl.setExecutable(SSH);
505 cl.addArguments(CollectionUtils.toStringArray(arguments));
506 return execute(cl);
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520 public static final int scp(String source, String destination) {
521 return scp(null, source, destination);
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535 public static final int scp(List<String> args, String source, String destination) {
536 Assert.notNull(source);
537 Assert.notNull(destination);
538 List<String> arguments = new ArrayList<String>();
539 arguments.addAll(CollectionUtils.toEmptyList(args));
540 arguments.add(source);
541 arguments.add(destination);
542 Commandline cl = new Commandline();
543 cl.setExecutable(SCP);
544 cl.addArguments(CollectionUtils.toStringArray(arguments));
545 return execute(cl);
546 }
547
548
549
550
551
552
553
554
555
556
557
558
559 public static final int scp(List<String> args, File source, String destination) {
560 Assert.notNull(source);
561 String sourcePath = LocationUtils.getCanonicalPath(source);
562 if (!source.exists()) {
563 throw new IllegalArgumentException(sourcePath + " does not exist");
564 }
565 return scp(args, sourcePath, destination);
566 }
567
568
569
570
571
572
573
574
575
576
577
578
579 public static final int scp(List<String> args, String source, File destination) {
580 try {
581 FileUtils.touch(destination);
582 } catch (IOException e) {
583 throw new IllegalStateException("Unexpected IO error", e);
584 }
585 String localPath = LocationUtils.getCanonicalPath(destination);
586 return scp(args, source, localPath);
587 }
588
589
590
591
592
593
594
595
596
597
598
599
600 public static final int scp(File source, String destination) {
601 return scp(null, source, destination);
602 }
603
604
605
606
607
608
609
610
611
612
613
614
615 public static final int scp(String source, File destination) {
616 return scp(null, source, destination);
617 }
618
619 public static final void validate(int exitValue, String message, Mode mode) {
620 if (exitValue != UnixUtils.SUCCESS) {
621 ModeUtils.validate(mode, message + " Exit value=[" + exitValue + "]");
622 }
623 }
624
625 public static final void validate(int exitValue, String message) {
626 validate(exitValue, message, Mode.ERROR);
627 }
628
629 public static final int execute(Commandline cl) {
630 try {
631 StreamConsumer stdout = new LoggingStreamConsumer(logger, LoggerLevel.INFO);
632 StreamConsumer stderr = new LoggingStreamConsumer(logger, LoggerLevel.WARN);
633 logger.info(cl.toString());
634 return CommandLineUtils.executeCommandLine(cl, stdout, stderr);
635 } catch (CommandLineException e) {
636 throw new IllegalStateException(e);
637 }
638 }
639
640 public static final String getLocation(String user, String hostname, String filename) {
641 Assert.notNull(user);
642 Assert.notNull(filename);
643 StringBuilder sb = new StringBuilder();
644 if (!StringUtils.isBlank(user)) {
645 sb.append(user + "@");
646 }
647 sb.append(hostname);
648 sb.append(":");
649 sb.append(filename);
650 return sb.toString();
651 }
652
653 protected static final String validateRsyncSourceDir(File dir) {
654 String path = LocationUtils.getCanonicalPath(dir);
655 if (!dir.exists()) {
656 throw new IllegalArgumentException(path + " does not exist");
657 }
658 if (!dir.isDirectory()) {
659 throw new IllegalArgumentException(path + " is not a directory");
660 }
661 if (!StringUtils.endsWith(path, FORWARD_SLASH)) {
662 return path + FORWARD_SLASH;
663 } else {
664 return path;
665 }
666 }
667
668 protected static final String validateRsyncDestinationDir(File dir) {
669 try {
670 FileUtils.forceMkdir(dir);
671 return dir.getCanonicalPath();
672 } catch (IOException e) {
673 throw new IllegalArgumentException("Unexpected IO error", e);
674 }
675 }
676
677
678
679
680
681 protected static final List<String> getRsyncDirOptions(List<String> options) {
682 List<String> rsyncDirOptions = new ArrayList<String>();
683 rsyncDirOptions.add("--recursive");
684 rsyncDirOptions.add("--archive");
685 rsyncDirOptions.add("--delete");
686 for (String option : CollectionUtils.toEmptyList(options)) {
687 if (!rsyncDirOptions.contains(option)) {
688 rsyncDirOptions.add(option);
689 }
690 }
691 return rsyncDirOptions;
692 }
693
694 }