Skip to content

Commit

Permalink
Small enhancements following review
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Mar 26, 2024
1 parent fc34a50 commit d212c5d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/offspot_demo/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def setup_check_systemd_service(
) -> None:
"""Check status of the systemd unit
By default, check at least that the unit is loaded properly (i.e. parsing is ok)
If check_running is True, it also checks that the unit is running
If check_enabled is True, it also checks that the unit is enabled
If check_waiting is True, it also checks that the unit is waiting
The minimal check consists in ensuring that the systemd unit is properly loaded (no
parsing issue, no unknown parameter, ...)
Parameters:
unit_fullname: full name of the systemd unit to check (e.g. my-timer.timer)
check_running: also check that systemd unit is running
check_waiting: also check that systemd unit is waiting (for timers typically)
check_enabled: also check that systemd unit is enabled
"""

try:
Expand All @@ -87,18 +91,14 @@ def setup_check_systemd_service(
check_enabled=check_enabled,
check_waiting=check_waiting,
)
except SystemdNotLoadedError as exc:
logger.error(f"systemd unit not loaded properly:\n{exc.stdout}")
sys.exit(2)
except SystemdNotRunningError as exc:
logger.error(f"systemd unit not running:\n{exc.stdout}")
sys.exit(3)
except SystemdNotWaitingError as exc:
logger.error(f"systemd unit not waiting:\n{exc.stdout}")
sys.exit(4)
except SystemdNotEnabledError as exc:
logger.error(f"systemd unit not enabled:\n{exc.stdout}")
sys.exit(5)
except (

Check warning on line 94 in src/offspot_demo/setup.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/setup.py#L94

Added line #L94 was not covered by tests
SystemdNotLoadedError,
SystemdNotRunningError,
SystemdNotWaitingError,
SystemdNotEnabledError,
) as exc:
logger.error(f"systemd check failed for {unit_fullname}:\n{exc.stdout}")
sys.exit(exc.return_code)

Check warning on line 101 in src/offspot_demo/setup.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/setup.py#L100-L101

Added lines #L100 - L101 were not covered by tests

logger.info("\tsystemd unit is properly loaded")

Check warning on line 103 in src/offspot_demo/setup.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/setup.py#L103

Added line #L103 was not covered by tests
if check_enabled:
Expand Down
30 changes: 18 additions & 12 deletions src/offspot_demo/utils/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ def __init__(self, stdout: str, *args: object) -> None:


class SystemdNotRunningError(SystemdError):
pass
return_code = 3

Check warning on line 14 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L13-L14

Added lines #L13 - L14 were not covered by tests


class SystemdNotWaitingError(SystemdError):
pass
return_code = 4

Check warning on line 18 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L17-L18

Added lines #L17 - L18 were not covered by tests


class SystemdNotEnabledError(SystemdError):
pass
return_code = 5

Check warning on line 22 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L21-L22

Added lines #L21 - L22 were not covered by tests


class SystemdNotLoadedError(SystemdError):
pass
return_code = 2

Check warning on line 26 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L25-L26

Added lines #L25 - L26 were not covered by tests


def check_systemd_service(

Check warning on line 29 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L29

Added line #L29 was not covered by tests
Expand All @@ -35,14 +35,20 @@ def check_systemd_service(
) -> subprocess.CompletedProcess[str]:
"""Check status of the systemd unit
By default, check at least that the unit is loaded properly (i.e. parsing is ok),
otherwise a SystemdNotLoadedError is raised.
If check_running is True, it also checks that the unit is running, otherwise a
SystemdNotRunningError is raiase
If check_enabled is True, it also checks that the unit is enabled, otherwise a
SystemdNotEnabledError is raiase
If check_waiting is True, it also checks that the unit is waiting, otherwise a
SystemdNotWaitingError is raiase
The minimal check consists in ensuring that the systemd unit is properly loaded (no
parsing issue, no unknown parameter, ...)
Parameters:
unit_fullname: full name of the systemd unit to check (e.g. my-timer.timer)
check_running: also check that systemd unit is running
check_waiting: also check that systemd unit is waiting (for timers typically)
check_enabled: also check that systemd unit is enabled
Raises:
SystemdNotLoadedError: if unit is not properly loaded
SystemdNotRunningError: if unit is not running ; when using check_running
SystemdNotEnabledError: if unit is not enabled ; when using check_enabled
SystemdNotWaitingError: if unit is not waiting ; when using check_waiting
"""
process = run_command(

Check warning on line 53 in src/offspot_demo/utils/systemd.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/utils/systemd.py#L53

Added line #L53 was not covered by tests
[
Expand Down
2 changes: 1 addition & 1 deletion src/offspot_demo/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_new_deploy_url() -> str | None:
Returns None otherwise.
"""
if not (LAST_IMAGE_DEPLOYED_PATH).exists():
if not LAST_IMAGE_DEPLOYED_PATH.exists():
last_image_fetched = None

Check warning on line 26 in src/offspot_demo/watcher.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/watcher.py#L26

Added line #L26 was not covered by tests
else:
last_image_fetched = LAST_IMAGE_DEPLOYED_PATH.read_text()

Check warning on line 28 in src/offspot_demo/watcher.py

View check run for this annotation

Codecov / codecov/patch

src/offspot_demo/watcher.py#L28

Added line #L28 was not covered by tests
Expand Down

0 comments on commit d212c5d

Please sign in to comment.