Skip to content

Commit

Permalink
Remove at-execution script in files.line operation
Browse files Browse the repository at this point in the history
This is no longer required with the v3 eval-facts-at-runtime changes!
  • Loading branch information
Fizzadar committed Jan 14, 2024
1 parent 8c5bb73 commit 7ccce9c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 70 deletions.
57 changes: 18 additions & 39 deletions pyinfra/operations/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,48 +381,27 @@ def line(

# No line and we want it, append it
if not present_lines and present:
# If the file does not exist - it *might* be created, so we handle it
# dynamically with a little script.
if present_lines is None:
yield make_formatted_string_command(
"""
if [ -f '{target}' ]; then
( grep {match_line} '{target}' && \
{sed_replace_command}) 2> /dev/null || \
{echo_command} ;
else
{echo_command} ;
fi
""",
target=QuoteString(path),
match_line=QuoteString(match_line),
echo_command=echo_command,
sed_replace_command=sed_replace_command,
# If we're doing replacement, only append if the *replacement* line
# does not exist (as we are appending the replacement).
if replace:
# Ensure replace explicitly matches a whole line
replace_line = replace
if not replace_line.startswith("^"):
replace_line = f"^{replace_line}"
if not replace_line.endswith("$"):
replace_line = f"{replace_line}$"

present_lines = host.get_fact(
FindInFile,
path=path,
pattern=replace_line,
interpolate_variables=interpolate_variables,
)

# File exists but has no matching lines - append it.
if not present_lines:
yield echo_command
else:
# If we're doing replacement, only append if the *replacement* line
# does not exist (as we are appending the replacement).
if replace:
# Ensure replace explicitly matches a whole line
replace_line = replace
if not replace_line.startswith("^"):
replace_line = f"^{replace_line}"
if not replace_line.endswith("$"):
replace_line = f"{replace_line}$"

present_lines = host.get_fact(
FindInFile,
path=path,
pattern=replace_line,
interpolate_variables=interpolate_variables,
)

if not present_lines:
yield echo_command
else:
host.noop('line "{0}" exists in {1}'.format(replace or line, path))
host.noop('line "{0}" exists in {1}'.format(replace or line, path))

# Line(s) exists and we want to remove them, replace with nothing
elif present_lines and not present:
Expand Down
15 changes: 0 additions & 15 deletions tests/operations/files.line/add_no_exist.json

This file was deleted.

15 changes: 0 additions & 15 deletions tests/operations/files.line/add_no_exist_with_quote.json

This file was deleted.

2 changes: 1 addition & 1 deletion tests/operations/server.security_limit/set.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"facts": {"files.FindInFile": {}},
"commands": [
"if [ -f /etc/security/limits.conf ]; then ( grep '^root[[:space:]]+hard[[:space:]]+memlock.*$' /etc/security/limits.conf && sed -i.a-timestamp 's/^root[[:space:]]+hard[[:space:]]+memlock.*$/root\thard\tmemlock\tunlimited/' /etc/security/limits.conf && rm -f /etc/security/limits.conf.a-timestamp ) 2> /dev/null || echo 'root\thard\tmemlock\tunlimited' >> /etc/security/limits.conf ; else echo 'root\thard\tmemlock\tunlimited' >> /etc/security/limits.conf ; fi"
"echo 'root\thard\tmemlock\tunlimited' >> /etc/security/limits.conf"
]
}

0 comments on commit 7ccce9c

Please sign in to comment.