Skip to content

Commit

Permalink
lib: add post-dist-upgrade script to convert files to deb822
Browse files Browse the repository at this point in the history
The script noops in all releases but noble.
On noble, it will convert valid entitlements repofiles to the new format.

Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo authored and orndorffgrant committed Jan 9, 2024
1 parent 4c39b48 commit 29cac6d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
71 changes: 71 additions & 0 deletions lib/convert_list_to_deb822.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3

"""
This script is called after running do-release-upgrade in a machine.
This converts list files to deb822 files when upgrading to Noble.
"""

import logging
import os
import sys

from aptsources.sourceslist import SourceEntry # type: ignore

from uaclient import entitlements
from uaclient.apt import _get_sources_file_content
from uaclient.cli import setup_logging
from uaclient.config import UAConfig
from uaclient.system import (
ensure_file_absent,
get_release_info,
load_file,
write_file,
)
from uaclient.util import set_filename_extension

if __name__ == "__main__":
series = get_release_info().series
if series != "noble":
sys.exit(0)

setup_logging(logging.DEBUG)
cfg = UAConfig()

for entitlement_class in entitlements.ENTITLEMENT_CLASSES:
if not issubclass(
entitlement_class, entitlements.repo.RepoEntitlement
):
continue

entitlement = entitlement_class(cfg)

filename = set_filename_extension(entitlement.repo_file, "list")
if os.path.exists(filename):
# If do-release-upgrade commented out the file, whether the
# repository is not reachable or is considered a third party, then
# it will be handled in upgrade_lts_contract. This script only
# changes services which are enabled, active and reachable.
valid_sources = [
SourceEntry(line)
for line in load_file(filename).strip().split("\n")
if line.strip().startswith("deb")
]
if valid_sources:
# get this information from the file, to avoid interacting with
# the entitlement_config
suites = list(set(source.dist for source in valid_sources))
repo_url = valid_sources[0].uri
include_deb_src = any(
source.type == "deb-src" for source in valid_sources
)
content = _get_sources_file_content(
suites,
series,
True,
repo_url,
entitlement.repo_key_file,
include_deb_src,
)
write_file(entitlement.repo_file, content)

ensure_file_absent(filename)
2 changes: 1 addition & 1 deletion release-upgrades.d/ubuntu-advantage-upgrades.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Sources]
Pockets=security,updates,proposed,backports,infra-security,infra-updates,apps-security,apps-updates
[Distro]
PostInstallScripts=./xorg_fix_proprietary.py, /usr/lib/ubuntu-advantage/upgrade_lts_contract.py
PostInstallScripts=./xorg_fix_proprietary.py, /usr/lib/ubuntu-advantage/convert_list_to_deb822.py, /usr/lib/ubuntu-advantage/upgrade_lts_contract.py
6 changes: 5 additions & 1 deletion uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
DEB822_REPO_FILE_CONTENT = """\
# Written by ubuntu-advantage-tools
Types: deb
Types: deb{deb_src}
URIs: {url}
Suites: {suites}
Components: main
Expand Down Expand Up @@ -506,6 +506,7 @@ def _get_sources_file_content(
updates_enabled: bool,
repo_url: str,
keyring_file: str,
include_deb_src: bool = False,
) -> str:
appliable_suites = [suite for suite in suites if series in suite]
if not updates_enabled:
Expand All @@ -518,11 +519,14 @@ def _get_sources_file_content(
suite for suite in appliable_suites if "-updates" not in suite
]

deb_src = " deb-src" if include_deb_src else ""

content = DEB822_REPO_FILE_CONTENT.format(
url=repo_url,
suites=" ".join(appliable_suites),
keyrings_dir=KEYRINGS_DIR,
keyring_file=keyring_file,
deb_src=deb_src,
)

return content
Expand Down
1 change: 1 addition & 0 deletions uaclient/entitlements/esm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def setup_local_esm_repo(self) -> None:
suites=suites,
keyrings_dir=KEYRINGS_DIR,
keyring_file=self.repo_key_file,
deb_src="",
),
)

Expand Down
1 change: 1 addition & 0 deletions uaclient/entitlements/tests/test_esm.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_setup_local_esm_repo(
suites=suites,
keyrings_dir=apt.KEYRINGS_DIR,
keyring_file=entitlement.repo_key_file,
deb_src="",
),
)
]
Expand Down

0 comments on commit 29cac6d

Please sign in to comment.