pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

host.get_fact(Crontab) is not able to handle crontab-level environment variables like CRON_TZ

Open JakkuSakura opened this issue 5 months ago • 1 comments

Describe the bug

I need to enforce CRON_TZ=UTC in my crontab. changing machine's timezone is not possible But pyinfra can't parse the line:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra_cli/util.py", line 65, in exec_file
    exec(PYTHON_CODES[filename], data)
  File "***.py", line 15, in <module>
    server.crontab(
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/operation.py", line 295, in decorated_func
    for _ in command_generator():
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/operation.py", line 282, in command_generator
    for command in func(*args, **kwargs):
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/operations/server.py", line 655, in crontab
    crontab = host.get_fact(Crontab, user=user)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/host.py", line 350, in get_fact
    return get_host_fact(self.state, self, name_or_cls, args=args, kwargs=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/facts.py", line 320, in get_host_fact
    return get_fact(state, host, cls, args=args, kwargs=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/facts.py", line 188, in get_fact
    return _get_fact(
           ^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/api/facts.py", line 272, in _get_fact
    data = fact.process(stdout_lines)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/pyinfra/facts/server.py", line 437, in process
    minute, hour, day_of_month, month, day_of_week, command = line.split(None, 5)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 6, got 1)

To Reproduce

Steps to reproduce the behavior, please include where possible:

  • Operation code & usage
CRON_TZ=UTC
host.get_fact(Crontab)
  • Target system information ubuntu

Expected behavior

pyinfra handles this CRON_TZ, or at least ignore it

Meta

  • Include output of pyinfra --support.

    System: Darwin
      Platform: macOS-14.5-arm64-arm-64bit
      Release: 23.5.0
      Machine: arm64
    pyinfra: v3.0.2
    Executable: /opt/homebrew/bin/pyinfra
    Python: 3.12.5 (CPython, Clang 15.0.0 (clang-1500.3.9.4))
  • How was pyinfra installed (source/pip)? source
  • Include pyinfra-debug.log (if one was created)
  • Consider including output with -vv and --debug.
[work-data] >>> sh -c '! command -v crontab >/dev/null || crontab -l || true'
[work-data] # Edit this file to introduce tasks to be run by cron.
[work-data] # 
[work-data] # Each task to run has to be defined through a single line
[work-data] # indicating with different fields when the task will be run
[work-data] # and what command to run for the task
[work-data] # 
[work-data] # To define the time you can provide concrete values for
[work-data] # minute (m), hour (h), day of month (dom), month (mon),
[work-data] # and day of week (dow) or use '*' in these fields (for 'any').
[work-data] # 
[work-data] # Notice that tasks will be started based on the cron's system
[work-data] # daemon's notion of time and timezones.
[work-data] # 
[work-data] # Output of the crontab jobs (including errors) is sent through
[work-data] # email to the user the crontab file belongs to (unless redirected).
[work-data] # 
[work-data] # For example, you can run a backup of all your user accounts
[work-data] # at 5 a.m every week with:
[work-data] # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
[work-data] # 
[work-data] # For more information see the manual pages of crontab(5) and cron(8)
[work-data] # 
[work-data] # m h  dom mon dow   command
[work-data] CRON_TZ=UTC

JakkuSakura avatar Aug 29 '24 18:08 JakkuSakura