-
-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crontab rewrite #652
base: 1.x
Are you sure you want to change the base?
Crontab rewrite #652
Conversation
- Now returns a list of dicts that represent "chunks" of the crontab - All elements of the crontab are now captured in the correct order
- Use new crontab fact format to allow full reproduction of crontab in proper order with all chunks (whitespace, comments, etc.) - Removed commented out breakpoint in crontab fact
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a huge improvement - thank you so much for taking the time to rewrite this!
The only change required is the fact class name to avoid breaking existing implementations, once that's swapped out (and tests/linting pass) I think this is good to go 👍
pyinfra/facts/server.py
Outdated
@@ -344,7 +344,7 @@ def process(output): | |||
|
|||
class Crontab(FactBase): | |||
''' | |||
Returns a dictionary of cron command -> execution time. | |||
Returns a list of dictionaries where each entry corresponds to a single cron command. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a much cleaner approach - I always regretted the command -> details dict choice here!
One problem here is that this is a breaking change - could we make this a new class (maybe just Crons
? Or CronEntries
?) and leave the old one as-is, I can then add a deprecation notice when used pending removal in v2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah, I can make that change. Will get to that when I have some more time to work on this PR.
@Fizzadar Ah, good to hear! I may not be able to get to this for a little while, but I do plan to continue on this when I have time (will be out on leave for about a month). In the meantime, these are a couple of questions that I have:
I have a few more questions, but I'll get to those when I get back to this PR to tidy it up and get it ready for merging. |
Hey @taranlu-houzz! No worries at all hugely appreciate you taking the time to work on this; answers below:
The JSON files should roughly define, for a given operation, the inputs (args/kwargs) and the commands output as a result. There's also ways to define facts/filesystem objects/etc which have been tacked on over time. I need to do a proper write up of the format of these because I'm sure this will help many contributors - it's certainly quite confusing at the moment (#656).
There's an
The idea here is that it will double-quote any user provided strings in the output shell - that should then enable environment variable substitution in those strings.
👍 |
@Fizzadar I'm back from leave and will hopefully be able to get this wrapped up soon. I do have a further question about the In some basic test using the below deploy, it doesn't seem to make any difference (I think I am probably still just misunderstanding what it does): #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test crontab interpolate_variables param."""
####################################################################################################
# Imports
####################################################################################################
# ==Site-Packages==
from pyinfra.operations import server
####################################################################################################
# Main
####################################################################################################
server.crontab(
name=f"Are my variables interpolated yet?",
command='echo "$USER" "toot" "oop"',
special_time="@reboot",
interpolate_variables=True,
) I did run several variations with |
@taranlu-houzz the variables will only render if not quoted at all, for example: from pyinfra.operations import files
files.line(
path='somefile',
line='this is $USER data',
interpolate_variables=True,
)
files.line(
path='somefile',
line='this is $USER data',
) Results in a file:
This has uncovered an unrelated bug (fixed by adaf051). |
I made this PR in order to deal with some of the various issues that I have run into when using the crontab operation (recently #533).
Currently, this patch resolves all issues that I have run into personally (I managed to track down the cause of #533 to be an issue with the way
sed_replace
works at least in my specific edge case). I feel likesed
is pretty tricky to work with when it comes to catching all of the possible edge cases...@Fizzadar I was hoping that I could get some feedback to see if this is something that you would consider integrating into
pyinfra
.Advantages:
Crontab
fact is more robust, and the entirety of the crontab is gathered, in order, so that any part could be modified (I feel like it could be nice to add anormalize_whitespace
param that would turn all whitespace chunks into a single blank line, for example).Crontab
operation add/remove/update logic is a bit more straightforward and clear in my opinion.sed
issues with edge cases are avoided.Remaining issues:
pyinfra
style spec (single quotes mainly).interporlate_variables
is supposed to work. I assumed it means that you could have something like$USER
in the command argument, and it would resolve that before actually writing to the crontab, but that did not seem to work when I tested oncurrent
.special_time
param). Not sure how that is handled elsewhere inpyinfra
.