Pyinfra: server.crontab breaks if the tab already contains an entry with a "special" time

Created on 3 Feb 2021  ยท  2Comments  ยท  Source: Fizzadar/pyinfra

Describe the bug
If crontab contains an entry like: @daily echo hello, server.crontab fails because it tries to parse the results of ! command -v crontab > /dev/null || crontab -l and is explicitly expecting individual entries for each time parameter.

To Reproduce
Try an operation like this on an empty crontab (if cron hasn't been setup or was removed with crontab -r, server.crontab always fails, maybe should add an issue for that too?):

server.crontab(
    name="Install test cron job",
    command="echo hello",
    cron_name="TL test again",
    special_time="@daily",
)

And then attempt to run the operation again:

[----] >>> sh -c '! command -v crontab > /dev/null || (crontab -l)'
[----] # pyinfra-name=TL test
[----] @daily echo hello
    [pyinfra.api.connectors.ssh] Waiting for exit status...
    [pyinfra.api.connectors.ssh] Command exit status: 0
--> An unexpected exception occurred in: deploys/tasks/----/install_cron.py:

  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra_cli/util.py", line 52, in exec_file
    exec(PYTHON_CODES[filename], data)
  File "deploys/tasks/----/install_cron.py", line 29, in <module>
    special_time="@daily",
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/api/operation.py", line 347, in decorated_func
    commands = unroll_generators(func(*actual_args, **actual_kwargs))
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/api/util.py", line 192, in unroll_generators
    for item in generator:
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/operations/server.py", line 519, in crontab
    crontab = host.fact.crontab(user)
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/api/facts.py", line 321, in wrapper
    fact_data = get_facts(state, name, args=args, kwargs=kwargs, ensure_hosts=(host,))
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/api/facts.py", line 267, in get_facts
    data = fact.process(stdout)
  File "/Users/----/.local/pipx/venvs/pyinfra/lib/python3.7/site-packages/pyinfra/facts/server.py", line 343, in process
    minute, hour, day_of_month, month, day_of_week, command = line.split(' ', 5)
ValueError: not enough values to unpack (expected 6, got 3)

It looks like it is just a simple issue where it is expecting to always get a "standard" cron time entry, as opposed to a "special" one.

Expected behavior
Handle existing special time entries.

Meta

  • Include output of pyinfra --support.
โฏ pyinfra --support
--> Support information:

    If you are having issues with pyinfra or wish to make feature requests, please
    check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
    When adding an issue, be sure to include the following:

    System: Darwin
      Platform: Darwin-18.7.0-x86_64-i386-64bit
      Release: 18.7.0
      Machine: x86_64
    pyinfra: v1.3.4
    Executable: /Users/----/.local/bin/pyinfra
    Python: 3.7.8 (CPython, Clang 10.0.1 (clang-1001.0.46.4))
  • How was pyinfra installed (source/pip)? pipx
Bug

All 2 comments

Thanks for spotting this @taranlu-houzz; I've pushed fixes for both the fact & operation when using special times:

Aiming to release these in v1.3.5 this weekend!

Now released in v1.3.5!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bystroushaak picture Bystroushaak  ยท  4Comments

taranlu-houzz picture taranlu-houzz  ยท  5Comments

Senhaji-Rhazi-Hamza picture Senhaji-Rhazi-Hamza  ยท  3Comments

meantheory picture meantheory  ยท  3Comments

mkinney picture mkinney  ยท  5Comments