-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add script-based custom checks in addition to the predefined ch…
…ecks
- Loading branch information
1 parent
6e69105
commit 2079eb5
Showing
4 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import psutil | ||
import subprocess | ||
from typing import List, Optional | ||
from logging import getLogger | ||
from ublue_update.config import cfg | ||
|
||
"""Setup logging""" | ||
log = getLogger(__name__) | ||
|
||
|
||
def run_custom_check_script(script) -> dict: | ||
if script.get('shell') != 'bash': | ||
raise Exception('checks.scripts.*.shell must be set to \'bash\'') | ||
|
||
log.debug(f"Running script {script}") | ||
|
||
# Run the specified custom script via bash | ||
script_result = subprocess.run( | ||
['bash', '-c', script['run']], | ||
capture_output=True, | ||
check=False, | ||
) | ||
|
||
# An exit code of 0 means "OK", a non-zero exit code | ||
# means "Do not download or perform updates right now" | ||
script_pass: bool = script_result.returncode == 0 | ||
|
||
# Use either the message specified in the config, | ||
# the output of the script (if not empty), or a fallback | ||
script_output: Optional[str] = script_result.stdout.strip() | ||
if len(script_output) == 0: | ||
script_output = None | ||
|
||
# Write error messages to our log in case of failure | ||
# to catch any interpreter errors etc. | ||
script_stderr = script_result.stderr.strip() | ||
if not script_pass and len(script_stderr) > 0: | ||
log.warning(f"A custom check script failed and wrote the following to STDERR:\n====\n{script_stderr}\n====") | ||
|
||
fallback_message = "A custom check script returned a non-0 exit code" | ||
script_message = script.get('message') or script_output or fallback_message | ||
|
||
return { | ||
"passed": script_pass, | ||
"message": script_message, | ||
} | ||
|
||
|
||
def run_custom_check_scripts() -> List[dict]: | ||
results = [] | ||
for script in (cfg.custom_check_scripts or []): | ||
results.append(run_custom_check_script(script)) | ||
return results | ||
|
||
|
||
def check_custom_inhibitors() -> bool: | ||
|
||
custom_inhibitors = run_custom_check_scripts() | ||
|
||
failures = [] | ||
custom_checks_failed = False | ||
for inhibitor_result in custom_inhibitors: | ||
if not inhibitor_result["passed"]: | ||
custom_checks_failed = True | ||
failures.append(inhibitor_result["message"]) | ||
if not custom_checks_failed: | ||
log.info("System passed custom checks") | ||
return custom_checks_failed, failures |