Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move system update script to python #81

Merged
merged 14 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions files/etc/ublue-update.d/system/00-system-update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

from subprocess import run
from json import loads, load
from json.decoder import JSONDecodeError
import os


def check_for_rebase():
# initialize variables
default_image_ref = []
current_image_ref = []
default_image_tag = ""
image_tag = ""

try:
with open("/usr/share/ublue-os/image-info.json") as f:
image_info = load(f)
default_image_ref = image_info["image-ref"].split(":")
default_image_tag = image_info["image-tag"]
except (FileNotFoundError, KeyError):
print("uBlue image info file does not exist")
return False, ""

status_cmd = [
"rpm-ostree",
"status",
"--pending-exit-77",
"-b",
"--json",
]
status_out = run(status_cmd, capture_output=True)
if status_out.returncode != 0:
print(status_out.stdout.decode("utf-8"))
return False, ""

try:
current_image_ref = (
loads(status_out.stdout)["deployments"][0]["container-image-reference"]
.replace(
"ostree-unverified-registry:", "ostree-unverified-image:docker://"
) # replace shorthand
.split(":")
)
if current_image_ref == default_image_ref:
return False, ""
gerblesh marked this conversation as resolved.
Show resolved Hide resolved
except (JSONDecodeError, KeyError):
print("unable to parse JSON output")
print(status_out.stdout.decode("utf-8"))
return False, ""

try: # preserve image tag when rebasing unsigned
if current_image_ref[2] == default_image_ref[2]:
image_tag = current_image_ref[3]
except KeyError:
gerblesh marked this conversation as resolved.
Show resolved Hide resolved
print("unable to get image tag from current deployment!")
image_tag = default_image_tag

return True, f"{default_image_ref[0]}:{default_image_ref[1]}:{default_image_ref[2]}:{image_tag}"


if __name__ == "__main__":
rpm_ostree = os.access("/usr/bin/rpm-ostree", os.X_OK)
if not rpm_ostree:
print("system isn't managed by rpm-ostree")
os._exit(1)
rebase, image_ref = check_for_rebase()
if rebase:
rebase_cmd = ["rpm-ostree", "rebase", image_ref]
rebase_out = run(rebase_cmd, capture_output=True)
if rebase_out.returncode == 0:
os._exit(0) # rebase sucessful
else:
print("rebase failed!, command output:")
print(rebase_out.stdout.decode("utf-8"))
update_cmd = ["rpm-ostree", "upgrade"]
update_out = run(update_cmd, capture_output=True)
if update_out.returncode != 0:
print(
f"rpm-ostree upgrade returned code {update_out.returncode}, program output:"
)
print(update_out.stdout.decode("utf-8"))
os._exit(update_out.returncode)
37 changes: 0 additions & 37 deletions files/etc/ublue-update.d/system/00-system-update.sh

This file was deleted.

6 changes: 3 additions & 3 deletions src/ublue_update/update_checks/system.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from json import loads
from json.decoder import JSONDecodeError
from logging import getLogger
from subprocess import PIPE, run
from subprocess import run


"""Setup logging"""
Expand All @@ -11,7 +11,7 @@
def skopeo_inspect(latest_image: str):
"""Inspect latest image with Skopeo"""
skopeo_inspect = ["skopeo", "inspect", latest_image]
inspect = run(skopeo_inspect, stdout=PIPE).stdout
inspect = run(skopeo_inspect, capture_output=True).stdout
"""Parse and return digest"""
digest = loads(inspect)["Digest"]
return digest
Expand All @@ -20,7 +20,7 @@ def skopeo_inspect(latest_image: str):
def system_update_check():
"""Pull deployment status via rpm-ostree"""
rpm_ostree_status = ["rpm-ostree", "status", "--json"]
status = run(rpm_ostree_status, stdout=PIPE).stdout
status = run(rpm_ostree_status, capture_output=True).stdout
"""Parse installation digest and image"""
try:
deployments = loads(status)["deployments"][0]
Expand Down
4 changes: 2 additions & 2 deletions src/ublue_update/update_checks/wait.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from json import loads
from json.decoder import JSONDecodeError
from subprocess import PIPE, run
from subprocess import run
from time import sleep
from logging import getLogger

Expand All @@ -12,7 +12,7 @@ def transaction():
try:
"""Pull deployment status via rpm-ostree"""
rpm_ostree_status = ["/usr/bin/rpm-ostree", "status", "--json"]
status = run(rpm_ostree_status, stdout=PIPE)
status = run(rpm_ostree_status, capture_output=True)
"""Parse transaction state"""
return loads(status.stdout)["transaction"]
except (JSONDecodeError, KeyError):
Expand Down
6 changes: 4 additions & 2 deletions ublue-update.spec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ black src
flake8 src
shellcheck files/etc/%{NAME}.d/user/*.sh
shellcheck files/etc/%{NAME}.d/system/*.sh
black files/etc/%{NAME}.d/system/*.py
flake8 files/etc/%{NAME}.d/system/*.py
%pyproject_wheel

%install
Expand All @@ -67,8 +69,8 @@ cp -rp files/etc files/usr %{buildroot}
%attr(0644,root,root) %{_exec_prefix}/lib/systemd/system/%{NAME}.timer
%attr(0644,root,root) %{_exec_prefix}/lib/systemd/system-preset/00-%{NAME}.preset
%attr(0644,root,root) %{_exec_prefix}/etc/%{NAME}/%{NAME}.toml
%attr(0755,root,root) %{_sysconfdir}/%{NAME}.d/user/*.sh
%attr(0755,root,root) %{_sysconfdir}/%{NAME}.d/system/*.sh
%attr(0755,root,root) %{_sysconfdir}/%{NAME}.d/user/*
%attr(0755,root,root) %{_sysconfdir}/%{NAME}.d/system/*
%attr(0644,root,root) %{_exec_prefix}/etc/polkit-1/rules.d/%{NAME}.rules

%changelog
Expand Down