Skip to content

Commit 6f88c66

Browse files
NananasFizzadar
authored andcommitted
operations/crontab: fix replace containing newline, add test
This replace is triggered e.g. when replacing an (pre-)existing crontab line without name to one maintained by pyinfra, this time with a pyinfra-name. The sed replace command contains a newline character that should stay as a '\n' and not an actual newline, as sed does not support a multi-line script.
1 parent 6fa182d commit 6f88c66

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pyinfra/operations/crontab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def comma_sep(value):
157157
),
158158
):
159159
if not exists_name and cron_name:
160-
new_crontab_line = f"{name_comment}\n{new_crontab_line}"
160+
new_crontab_line = f"{name_comment}\\n{new_crontab_line}"
161161
edit_commands.append(
162162
sed_replace(
163163
temp_filename,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"args": ["command_to_be"],
3+
"kwargs": {
4+
"month": 1,
5+
"cron_name": "name of cron"
6+
},
7+
"facts": {
8+
"crontab.Crontab": {
9+
"user=None": {
10+
"command_to_be": {
11+
"minute": "*",
12+
"hour": "*",
13+
"month": "*",
14+
"day_of_week": "*",
15+
"day_of_month": "*",
16+
"comments": []
17+
}
18+
}
19+
}
20+
},
21+
"commands": [
22+
"crontab -l > _tempfile_",
23+
"sed -i.a-timestamp 's/.*command_to_be.*/# pyinfra-name=name of cron\\n* * * 1 * command_to_be/' _tempfile_ && rm -f _tempfile_.a-timestamp",
24+
"crontab _tempfile_"
25+
]
26+
}

0 commit comments

Comments
 (0)