Skip to content

Commit 084c0ac

Browse files
committed
ansible: avoid roundtrip in copy action due to fixup_perms2().
On top of existing temporary files work, this reduces the number of roundtrips required for "copy" and "template" actions from 6 to 3.
1 parent e18396d commit 084c0ac

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

ansible_mitogen/mixins.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ def _transfer_data(self, remote_path, data):
216216
self._connection.put_data(remote_path, data)
217217
return remote_path
218218

219+
#: Actions listed here cause :func:`_fixup_perms2` to avoid a needless
220+
#: roundtrip, as they modify file modes separately afterwards. This is due
221+
#: to the method prototype having a default of `execute=True`.
222+
FIXUP_PERMS_RED_HERRING = set(['copy'])
223+
219224
def _fixup_perms2(self, remote_paths, remote_user=None, execute=True):
220225
"""
221226
Mitogen always executes ActionBase helper methods in the context of the
@@ -224,7 +229,7 @@ def _fixup_perms2(self, remote_paths, remote_user=None, execute=True):
224229
"""
225230
LOG.debug('_fixup_perms2(%r, remote_user=%r, execute=%r)',
226231
remote_paths, remote_user, execute)
227-
if execute:
232+
if execute and self._load_name not in self.FIXUP_PERMS_RED_HERRING:
228233
return self._remote_chmod(remote_paths, mode='u+x')
229234
return self.COMMAND_RESULT.copy()
230235

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
- import_playbook: remote_file_exists.yml
2-
- import_playbook: remote_expand_user.yml
1+
- import_playbook: fixup_perms2__copy.yml
32
- import_playbook: low_level_execute_command.yml
43
- import_playbook: make_tmp_path.yml
4+
- import_playbook: remote_expand_user.yml
5+
- import_playbook: remote_file_exists.yml
56
- import_playbook: transfer_data.yml
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Verify action plugins still set file modes correctly even though
2+
# fixup_perms2() avoids setting execute bit despite being asked to.
3+
4+
- name: integration/action/fixup_perms2__copy.yml
5+
hosts: test-targets
6+
any_errors_fatal: true
7+
tasks:
8+
- name: Get default remote file mode
9+
shell: python -c 'import os; print("%04o" % (int("0666", 8) & ~os.umask(0)))'
10+
register: py_umask
11+
12+
- name: Set default file mode
13+
set_fact:
14+
mode: "{{py_umask.stdout}}"
15+
16+
#
17+
# copy module (no mode).
18+
#
19+
20+
- name: "Copy files (no mode)"
21+
copy:
22+
content: ""
23+
dest: /tmp/copy-no-mode
24+
25+
- stat: path=/tmp/copy-no-mode
26+
register: out
27+
- assert:
28+
that:
29+
- out.stat.mode == mode
30+
31+
#
32+
# copy module (explicit mode).
33+
#
34+
35+
- name: "Copy files from content: arg"
36+
copy:
37+
content: ""
38+
mode: 0400
39+
dest: /tmp/copy-with-mode
40+
41+
- stat: path=/tmp/copy-with-mode
42+
register: out
43+
- assert:
44+
that:
45+
- out.stat.mode == "0400"
46+
47+
#
48+
# copy module (existing disk files, no mode).
49+
#
50+
51+
- file:
52+
path: /tmp/weird-mode
53+
state: absent
54+
55+
- name: Create local test file.
56+
connection: local
57+
copy:
58+
content: "weird mode"
59+
dest: "/tmp/weird-mode"
60+
mode: "1462"
61+
62+
- copy:
63+
src: "/tmp/weird-mode"
64+
dest: "/tmp/weird-mode"
65+
66+
- stat:
67+
path: "/tmp/weird-mode"
68+
register: out
69+
- assert:
70+
that:
71+
- out.stat.mode == mode
72+
73+
#
74+
# copy module (existing disk files, preserve mode).
75+
#
76+
77+
- copy:
78+
src: "/tmp/weird-mode"
79+
dest: "/tmp/weird-mode"
80+
mode: preserve
81+
82+
- stat:
83+
path: "/tmp/weird-mode"
84+
register: out
85+
- assert:
86+
that:
87+
- out.stat.mode == "1462"
88+
89+
#
90+
# copy module (existing disk files, explicit mode).
91+
#
92+
93+
- copy:
94+
src: "/tmp/weird-mode"
95+
dest: "/tmp/weird-mode"
96+
mode: "1461"
97+
98+
- stat:
99+
path: "/tmp/weird-mode"
100+
register: out
101+
102+
- assert:
103+
that:
104+
- out.stat.mode == "1461"

0 commit comments

Comments
 (0)