Skip to content

Commit

Permalink
Support different kernel repo (BugFix) (#978)
Browse files Browse the repository at this point in the history
* Refactor and support different kernel repo

* Separate the lowlatncey kernel checking and add the docstring

* Land the unittest for check_prerelease.py

* Update providers/base/bin/check_prerelease.py

Co-authored-by: Pierre Equoy <[email protected]>

---------

Co-authored-by: Pierre Equoy <[email protected]>
  • Loading branch information
baconYao and pieqq authored Feb 26, 2024
1 parent 0bc7bfd commit bfecdb0
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 33 deletions.
130 changes: 97 additions & 33 deletions providers/base/bin/check_prerelease.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,136 @@
Usage:
check-prerelease.py
"""

import logging
import os
import platform
import shlex
import sys

from subprocess import CalledProcessError, check_output


def check_kernel_status():
def get_apt_cache_information(command: str):
""" Execute the given apt-cache command and return the information.
This function runs the specified apt-cache command using the `check_output`
function, which returns the information about the Linux kernel package
queried by the command.
:param command: A string representing the apt-cache command to be executed.
:return:
A string containing the information retrieved from the apt-cache
command.
:raises CalledProcessError:
If the apt-cache command returns an empty string with exit code 0,
indicating a non-existent package.
:raises SystemExit:
If the apt-cache command returns an error status, indicating that
the kernel does not match any installed package.
"""
try:
aptinfo = check_output(shlex.split(command), universal_newlines=True)
# "apt-cache showpkg" returns an empty string with exit code 0 if
# called on a non-existent package.
if not aptinfo:
raise CalledProcessError(returncode=1, cmd=command)
return aptinfo
except CalledProcessError as e:
# "apt-cache show" returns an error status if called on a
# non-existent package.
logging.error(e)
logging.error(
"* Kernel does not match any installed package!")
raise SystemExit(1)


def verify_apt_cache_showpkg(kernel_release: str):
"""Check kernel to see if it's supported for certification
by "apt-cache showpkg linux-image-<kernel_release>"
:returns:
True if OK, False if not
"""
kernel_release = platform.release()

retval = True
command = "apt-cache showpkg linux-image-{}".format(kernel_release)
aptinfo = check_output(shlex.split(command), universal_newlines=True)

aptinfo = get_apt_cache_information(command)
# Exclude kernels that come from obvious PPAs....
retval = True
if "ppa.launchpad.net" in aptinfo:
print("* Kernel appears to have come from a PPA!")
logging.error("* Kernel appears to have come from a PPA!")
retval = False

# Exclude kernels that don't come from the main repo
if "main_binary" not in aptinfo:
print("* Kernel does not come from the main Ubuntu repository!")
# Exclude kernels that don't come from the specific Ubuntu repository
target_repo = os.environ.get("KERNEL_REPO", "main")
if "{}_binary".format(target_repo) not in aptinfo:
logging.error(
"* Kernel does not come from the {} Ubuntu repository!".format(
target_repo))
retval = False
return retval

try:
command = "apt-cache show linux-image-{}".format(kernel_release)
aptinfo = check_output(shlex.split(command), universal_newlines=True)
except CalledProcessError:
# "apt-cache show" returns an error status if called on a
# non-existent package.
print("* Kernel does not match any installed package!")
aptinfo = ""
retval = False

def verify_apt_cache_show(kernel_release: str):
"""Check kernel to see if it's supported for certification
by "apt-cache show linux-image-<kernel_release>"
:returns:
True if OK, False if not
"""
command = "apt-cache show linux-image-{}".format(kernel_release)
aptinfo = get_apt_cache_information(command)
retval = True

# Exclude 'edge' kernels, which are identified via the 'Source:' line
# in the apt-cache show output....
if "Source: linux-signed-hwe-edge" in aptinfo:
print("* Kernel is an 'edge' kernel!")
retval = False
if "Source: linux-hwe-edge" in aptinfo:
print("* Kernel is an 'edge' kernel!")
retval = False
for source in ["Source: linux-signed-hwe-edge", "Source: linux-hwe-edge"]:
if source in aptinfo:
logging.error("* Kernel is an 'edge' kernel!, found '{}'")
retval = False

# Exclude kernels that aren't from the "linux" (or variant, like
# "linux-hwe" or "linux-signed") source....
if "Source: linux" not in aptinfo:
print("* Kernel is not a Canonical kernel!")
logging.error("* Kernel is not a Canonical kernel!")
retval = False

return retval


def verify_not_lowlatency_kernel(kernel_release: str):
"""Check kernel to see if it's supported for certification
by verifying the "lowlatency" term not in kernel string
:returns:
True if OK, False if not
"""
# Exclude low-latency kernels, which are identified via the kernel name
# string itself....
if "lowlatency" in kernel_release:
print("* Kernel is a low-latency kernel!")
retval = False
logging.error("* Kernel is a low-latency kernel!")
return False
return True

if (not retval):
print("* Kernel release is {}".format(kernel_release))
print("* Kernel is ineligible for certification!")

return retval
def check_kernel_status():
"""Check kernel to see if it's supported for certification
:returns:
True if OK, False if not
"""
kernel_release = platform.release()
logging.info("* Kernel release is {}".format(kernel_release))

is_valid_kernel = True
is_valid_kernel &= verify_apt_cache_showpkg(kernel_release)
is_valid_kernel &= verify_apt_cache_show(kernel_release)
is_valid_kernel &= verify_not_lowlatency_kernel(kernel_release)

if not is_valid_kernel:
logging.error("* Kernel is ineligible for certification!")

return is_valid_kernel


def check_os_status():
Expand Down
Loading

0 comments on commit bfecdb0

Please sign in to comment.