Skip to content

Commit 5af6c9b

Browse files
committed
issue #615: use FileService for target->controll file transfers
1 parent 8bac1cf commit 5af6c9b

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

ansible_mitogen/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,12 @@ def fetch_file(self, in_path, out_path):
950950
:param str out_path:
951951
Local filesystem path to write.
952952
"""
953-
output = self.get_chain().call(
954-
ansible_mitogen.target.read_path,
955-
mitogen.utils.cast(in_path),
953+
self._connect()
954+
ansible_mitogen.target.transfer_file(
955+
context=self.context,
956+
in_path=in_path,
957+
out_path=out_path
956958
)
957-
ansible_mitogen.target.write_path(out_path, output)
958959

959960
def put_data(self, out_path, data, mode=None, utimes=None):
960961
"""

mitogen/core.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ def bytes_partition(s, sep):
395395
return _partition(s, sep, s.find) or (s, '', '')
396396

397397

398+
def _has_parent_authority(context_id):
399+
return (
400+
(context_id == mitogen.context_id) or
401+
(context_id in mitogen.parent_ids)
402+
)
403+
398404
def has_parent_authority(msg, _stream=None):
399405
"""
400406
Policy function for use with :class:`Receiver` and
@@ -403,8 +409,7 @@ def has_parent_authority(msg, _stream=None):
403409
<Stream.auth_id>` has been set to that of a parent context or the current
404410
context.
405411
"""
406-
return (msg.auth_id == mitogen.context_id or
407-
msg.auth_id in mitogen.parent_ids)
412+
return _has_parent_authority(msg.auth_id)
408413

409414

410415
def _signals(obj, signal):

mitogen/service.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,11 @@ def fetch(self, path, sender, msg):
10231023
:raises Error:
10241024
Unregistered path, or Sender did not match requestee context.
10251025
"""
1026-
if path not in self._paths and not self._prefix_is_authorized(path):
1026+
if (
1027+
(path not in self._paths) and
1028+
(not self._prefix_is_authorized(path)) and
1029+
(not mitogen.core._has_parent_authority(msg.auth_id))
1030+
):
10271031
msg.reply(mitogen.core.CallError(
10281032
Error(self.unregistered_msg % (path,))
10291033
))

tests/ansible/tests/connection_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ def test_is_junk(self):
9999
self.assertEquals(None, self.func({1:2}))
100100

101101

102+
class FetchFileTest(ConnectionMixin, testlib.TestCase):
103+
def test_success(self):
104+
with tempfile.NamedTemporaryFile(prefix='mitotest') as ifp, \
105+
tempfile.NamedTemporaryFile(prefix='mitotest') as ofp:
106+
ifp.write('x' * (1048576 * 4))
107+
ifp.flush()
108+
ifp.seek(0)
109+
110+
self.conn.fetch_file(ifp.name, ofp.name)
111+
# transfer_file() uses os.rename rather than direct data overwrite,
112+
# so we must reopen.
113+
with open(ofp.name, 'rb') as fp:
114+
self.assertEquals(ifp.read(), fp.read())
115+
116+
102117
class PutDataTest(ConnectionMixin, testlib.TestCase):
103118
def test_out_path(self):
104119
path = tempfile.mktemp(prefix='mitotest')

0 commit comments

Comments
 (0)