1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{copyto [-rpC] FILE ... REMOTE_DIR}
19
20 Copies C{FILE}s to C{REMOTE_DIR} on each node of a cluster.
21 The cluster is identified using remote execution syntax, for example::
22
23 osh @fred [ copyto log.properties /opt/foobar/config ]
24
25 C{REMOTE_DIR} must be an absolute path, (i.e. must begin with C{'/'}).
26
27 C{copyto} is implemented using C{scp}, and the following C{scp} flags
28 are supported:
29 - C{-C}: enable compression.
30 - C{-r}: recursive copy
31 - C{-p}: preserve modification times, access times, and modes.
32 """
33
34 import osh.core
35 import osh.spawn
36
37 Spawn = osh.spawn.Spawn
38
39
42
43
44 -def copyto(files, compress = False, recursive = False, preserve = False):
45 """Copies files to each node of the specified C{cluster}. The last elements
46 of C{files} is the target directory on each node. The preceding elements are
47 the local files to be copied. Compression is used for copying if C{compress}
48 is True. Directories are copied recursively if C{recursive} is
49 True. File attributes are preserved if C{preserve} is True.
50 """
51 args = []
52 if compress:
53 args.append(Option('-C'))
54 if recursive:
55 args.append(Option('-r'))
56 if preserve:
57 args.append(Option('-p'))
58 return _CopyTo().process_args(*args)
59
61
62
63
64 _remote_dir = None
65 _files = None
66 _scp_options = None
67
68
69
70
73
74
75
76
79
81 args = self.args()
82 scp_options = ''
83 for option in 'Crp':
84 if args.flag('-' + option):
85 scp_options += option
86 if scp_options:
87 self._scp_options = '-' + scp_options
88 else:
89 self._scp_options = ''
90 files = args.remaining()
91 if len(files) < 2:
92 self.usage()
93 self._remote_dir = files[-1]
94 if not self._remote_dir.startswith('/'):
95 self.usage()
96 self._files = files[:-1]
97
98
99
101 host = self.thread_state
102 _copyup(self._files,
103 host.user,
104 host.identity,
105 host.address,
106 self._remote_dir,
107 self._scp_options)
108
109 -def _copyup(files, user, identity, host, remote_dir, options):
110 if isinstance(files, list) or isinstance(files, tuple):
111 files = ' '.join(files)
112 if identity:
113 scp_command = 'scp %s -i %s %s %s@%s:%s' % (options,
114 identity,
115 files,
116 user,
117 host,
118 remote_dir)
119 else:
120 scp_command = 'scp %s %s %s@%s:%s' % (options, files, user, host, remote_dir)
121 Spawn(scp_command, None, None, None).run()
122