Skip to content

Commit

Permalink
fix crontab tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JakkuSakura committed Sep 26, 2024
1 parent 082216d commit 936840a
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 32 deletions.
12 changes: 10 additions & 2 deletions pyinfra/facts/crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@ class CrontabDict(TypedDict):
class CrontabFile:
commands: List[CrontabDict]

def __init__(self):
def __init__(self, input: dict[str, CrontabDict] | None = None):
super().__init__()
self.commands = []
if input:
for command, others in input.items():
val = others.copy()
val['command'] = command
self.add_item(val)

def add_item(self, item: CrontabDict):
self.commands.append(item)

def __len__(self):
return len(self.commands)

def __bool__(self):
return len(self) > 0

def items(self):
return {
item.get("command") or item.get('env'): item for item in self.commands
}

def get_command(
self, command: Optional[str] = None, name: Optional[str] = None
self, command: Optional[str] = None, name: Optional[str] = None
) -> Optional[CrontabDict]:
assert command or name, "Either command or name must be provided"

Expand Down
35 changes: 15 additions & 20 deletions pyinfra/operations/crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from pyinfra import host
from pyinfra.api import StringCommand, operation
from pyinfra.api.util import try_int
from pyinfra.facts.crontab import CrontabFile
from pyinfra.facts.server import (
Crontab,
)
from pyinfra.facts.crontab import CrontabFile, Crontab
from pyinfra.operations.util.files import sed_replace


Expand Down Expand Up @@ -77,24 +74,21 @@ def comma_sep(value):
day_of_week = comma_sep(day_of_week)
day_of_month = comma_sep(day_of_month)

crontab: CrontabFile = host.get_fact(Crontab, user=user)
ctb0: CrontabFile | dict = host.get_fact(Crontab, user=user)
# facts from test are in dict
if isinstance(ctb0, dict):
ctb = CrontabFile(ctb0)
else:
ctb = ctb0
name_comment = "# pyinfra-name={0}".format(cron_name)

existing_crontab = crontab.get_command(command)
existing_crontab_command = command
existing_crontab_match = command

if not existing_crontab and cron_name: # find the crontab by name if provided
for cmd in crontab.commands:
if not cmd["comments"]:
continue
if name_comment in cmd["comments"]:
existing_crontab = cmd
existing_crontab_match = cmd["command"]
existing_crontab_command = cmd["command"]
existing_crontab = ctb.get_command(command=command, name=cron_name)
existing_crontab_command = existing_crontab['command'] if existing_crontab else command
existing_crontab_match = existing_crontab['command'] if existing_crontab else command

print('existing_crontab', existing_crontab)
exists = existing_crontab is not None
exists_name = existing_crontab is not None and name_comment in existing_crontab["comments"]
exists_name = existing_crontab is not None and name_comment in existing_crontab.get("comments", '')

edit_commands: list[str | StringCommand] = []
temp_filename = host.get_temp_filename()
Expand Down Expand Up @@ -126,7 +120,8 @@ def comma_sep(value):

# Want the cron but it doesn't exist? Append the line
elif present and not exists:
if crontab: # append a blank line if cron entries already exist
print('present', present, 'exists', exists)
if ctb: # append a blank line if cron entries already exist
edit_commands.append("echo '' >> {0}".format(temp_filename))
if cron_name:
edit_commands.append(
Expand Down Expand Up @@ -175,7 +170,7 @@ def comma_sep(value):
crontab_args.append("-u {0}".format(user))

# List the crontab into a temporary file if it exists
if crontab:
if ctb:
yield "crontab -l {0} > {1}".format(" ".join(crontab_args), temp_filename)

# Now yield any edits
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"args": ["this_is_a_command"],
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"args": ["this_is_a_command"],
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"this_is_another_command": {}
}
}
},
"commands": [
"crontab -l > _tempfile_",
"echo '' >> _tempfile_",
"echo '* * * * * this_is_a_command' >> _tempfile_",
"crontab _tempfile_"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"args": ["this_is_a_command"],
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"this_is_a_command": {
"minute": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cron_name": "a-name"
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"apt update": {
"minute": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"special_time": "@reboot"
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"user": "pyinfra"
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=pyinfra": {}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"day_of_week": [1, 2, "3"]
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"this_is_a_command": {
"minute": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"cron_name": "name of cron"
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"command_to_change": {
"minute": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"special_time": "@reboot"
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"this_is_a_command": {
"special_time": "@daily",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"present": false
},
"facts": {
"server.Crontab": {
"crontab.Crontab": {
"user=None": {
"this_is_a_command": {
"minute": "*",
Expand Down

0 comments on commit 936840a

Please sign in to comment.