Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changelog/67956.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add deb822 fromat support to aptpkg module
135 changes: 117 additions & 18 deletions salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
SaltInvocationError,
)
from salt.modules.cmdmod import _parse_env
from salt.utils.pkg.deb import SourceEntry, SourcesList
from salt.utils.pkg.deb import (
Deb822SourceEntry,
Section,
SourceEntry,
SourcesList,
_invalid,
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -1639,21 +1645,41 @@ def list_repos(**kwargs):
"""
repos = {}
sources = SourcesList()
for source in sources.list:
for source in sources:
if _skip_source(source):
continue
signedby = source.signedby
repo = {}
repo["file"] = source.file
repo["comps"] = getattr(source, "comps", [])
repo_comps = getattr(source, "comps", [])
repo_dists = source.dist.split(" ")
repo["comps"] = repo_comps
repo["disabled"] = source.disabled
repo["dist"] = source.dist
repo["enabled"] = not repo[
"disabled"
] # This is for compatibility with the other modules
repo["dist"] = repo_dists.pop(0)
repo["suites"] = list(source.suites)
repo["type"] = source.type
repo["uri"] = source.uri
repo["line"] = source.line.strip()
if "Types: " in source.line and "\n" in source.line:
repo["line"] = (
f"{source.type} {source.uri} {repo['dist']} {' '.join(repo_comps)}"
)
else:
repo["line"] = source.line.strip()
repo["architectures"] = getattr(source, "architectures", [])
repo["signedby"] = signedby
repos.setdefault(source.uri, []).append(repo)
if len(repo_dists):
for dist in repo_dists:
repo_copy = repo.copy()
repo_copy["dist"] = dist
if "Types: " in source.line and "\n" in source.line:
repo_copy["line"] = (
f"{source.type} {source.uri} {repo_copy['dist']} {' '.join(repo_comps)}"
)
repos[source.uri].append(repo_copy)
return repos


Expand All @@ -1662,12 +1688,17 @@ def get_repo(repo, **kwargs):
Display a repo from the sources.list / sources.list.d

The repo passed in needs to be a complete repo entry.
When system uses repository in the deb822 format,
get_repo uses a partial match of distributions.

In that case, include any distribution of the deb822
repository in the repo name to match that repo.

CLI Examples:

.. code-block:: bash

salt '*' pkg.get_repo "myrepo definition"
salt '*' pkg.get_repo "deb URL noble main"
"""
ppa_auth = kwargs.get("ppa_auth", None)
# we have to be clever about this since the repo definition formats
Expand Down Expand Up @@ -1726,11 +1757,17 @@ def del_repo(repo, **kwargs):
The repo passed in must be a fully formed repository definition
string.

When system uses repository in the deb822 format,
del_repo uses a partial match of distributions.

In that case, include any distribution of the deb822
repository in the repo name to match that repo.

CLI Examples:

.. code-block:: bash

salt '*' pkg.del_repo "myrepo definition"
salt '*' pkg.del_repo "deb URL noble main"
"""
is_ppa = False
if repo.startswith("ppa:") and __grains__["os"] in ("Ubuntu", "Mint", "neon"):
Expand Down Expand Up @@ -1761,11 +1798,22 @@ def del_repo(repo, **kwargs):
source.type == repo_entry["type"]
and source.architectures == repo_entry["architectures"]
and source.uri.rstrip("/") == repo_entry["uri"].rstrip("/")
and source.dist == repo_entry["dist"]
and repo_entry["dist"] in source.suites
):

s_comps = set(source.comps)
r_comps = set(repo_entry["comps"])
if s_comps == r_comps:
r_suites = list(source.suites)
r_suites.remove(repo_entry["dist"])
source.suites = r_suites
deleted_from[source.file] = 0
if not source.suites:
try:
sources.remove(source)
except ValueError:
pass
sources.save()
continue
if s_comps.intersection(r_comps) or (not s_comps and not r_comps):
deleted_from[source.file] = 0
source.comps = list(s_comps.difference(r_comps))
Expand All @@ -1782,11 +1830,23 @@ def del_repo(repo, **kwargs):
and repo_entry["type"] == "deb"
and source.type == "deb-src"
and source.uri == repo_entry["uri"]
and source.dist == repo_entry["dist"]
and repo_entry["dist"] in source.suites
):

s_comps = set(source.comps)
r_comps = set(repo_entry["comps"])
if s_comps == r_comps:
r_suites = list(source.suites)
r_suites.remove(repo_entry["dist"])
source.suites = r_suites
deleted_from[source.file] = 0
if not source.suites:
try:
sources.remove(source)
except ValueError:
pass
sources.save()
continue
if s_comps.intersection(r_comps) or (not s_comps and not r_comps):
deleted_from[source.file] = 0
source.comps = list(s_comps.difference(r_comps))
Expand All @@ -1799,6 +1859,8 @@ def del_repo(repo, **kwargs):
if deleted_from:
ret = ""
for source in sources:
if source.invalid:
continue
if source.file in deleted_from:
deleted_from[source.file] += 1
for repo_file, count in deleted_from.items():
Expand Down Expand Up @@ -2238,6 +2300,12 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
``ppa:<project>/repo`` format is acceptable. ``ppa:`` format can only be
used to create a new repository.

When system uses repository in the deb822 format, mod_repo uses a partial
match of distributions.

In that case, include any distribution of the deb822 repository in the
repo definition to match that repo.

The following options are available to modify a repo definition:

architectures
Expand Down Expand Up @@ -2292,8 +2360,8 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

.. code-block:: bash

salt '*' pkg.mod_repo 'myrepo definition' uri=http://new/uri
salt '*' pkg.mod_repo 'myrepo definition' comps=main,universe
salt '*' pkg.mod_repo 'deb URL noble main' uri=http://new/uri
salt '*' pkg.mod_repo 'deb URL noble main' comps=main,universe
"""
if "refresh_db" in kwargs:
refresh = kwargs["refresh_db"]
Expand Down Expand Up @@ -2413,6 +2481,13 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

repos = []
for source in sources:
if isinstance(source, Deb822SourceEntry):
if source.types == [""] or not bool(source.types) or not source.type:
continue
else:
_, invalid, _, _ = _invalid(source.line)
if invalid:
continue
repos.append(source)

mod_source = None
Expand Down Expand Up @@ -2569,9 +2644,9 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
repo_matches = (
apt_source.type == repo_entry["type"]
and apt_source.uri.rstrip("/") == repo_entry["uri"].rstrip("/")
and apt_source.dist == repo_entry["dist"]
and repo_entry["dist"] in apt_source.suites
)
kw_matches = apt_source.dist == kw_dist and apt_source.type == kw_type
kw_matches = kw_dist in apt_source.suites and apt_source.type == kw_type

if repo_matches or kw_matches:
for comp in full_comp_list:
Expand All @@ -2589,17 +2664,35 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

repo_source_entry = SourceEntry(repo)
if not mod_source:
mod_source = SourceEntry(repo)
apt_source_file = kwargs.get("file")
if not apt_source_file:
raise SaltInvocationError(
"missing 'file' argument when defining a new repository"
)

if not apt_source_file.endswith(".list"):
section = Section("")
section["Types"] = repo_entry["type"]
section["URIs"] = repo_entry["uri"]
section["Suites"] = repo_entry["dist"]
section["Components"] = " ".join(repo_entry["comps"])
if kwargs.get("trusted") is True or kwargs.get("Trusted") is True:
section["Trusted"] = "yes"
mod_source = Deb822SourceEntry(section, apt_source_file)
else:
mod_source = SourceEntry(repo)
if "comments" in kwargs:
mod_source.comment = kwargs["comments"]
sources.list.append(mod_source)
elif "comments" in kwargs:
mod_source.comment = kwargs["comments"]

mod_source.line = repo_source_entry.line
if not mod_source.line.endswith("\n"):
mod_source.line = mod_source.line + "\n"

if not kwargs["architectures"] and not mod_source.architectures:
kwargs.pop("architectures")

for key in kwargs:
if key in _MODIFY_OK and hasattr(mod_source, key):
setattr(mod_source, key, kwargs[key])
Expand All @@ -2615,15 +2708,21 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

signedby = mod_source.signedby

repo_source_line = mod_source.line
if "Types: " in repo_source_line and "\n" in repo_source_line:
repo_source_line = f"{mod_source.type} {mod_source.uri} {repo_entry['dist']} {' '.join(mod_source.comps)}"

return {
repo: {
"architectures": getattr(mod_source, "architectures", []),
"dist": mod_source.dist,
"suites": mod_source.suites,
"comps": mod_source.comps,
"disabled": mod_source.disabled,
"file": mod_source.file,
"type": mod_source.type,
"uri": mod_source.uri,
"line": mod_source.line,
"line": repo_source_line,
"signedby": signedby,
}
}
Expand Down Expand Up @@ -2726,7 +2825,7 @@ def _expand_repo_def(os_name, os_codename=None, **kwargs):
sanitized["dist"] = _source_entry.dist
sanitized["type"] = _source_entry.type
sanitized["uri"] = _source_entry.uri
sanitized["line"] = _source_entry.line.strip()
sanitized["line"] = getattr(_source_entry, "line", "").strip()
sanitized["architectures"] = getattr(_source_entry, "architectures", [])
sanitized["signedby"] = signedby

Expand Down
Loading
Loading