File: snap.py

package info (click to toggle)
cloud-init 25.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,412 kB
  • sloc: python: 135,894; sh: 3,883; makefile: 141; javascript: 30; xml: 22
file content (57 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This file is part of cloud-init. See LICENSE file for license information.
import logging
from typing import Iterable, List

from cloudinit import subp, util
from cloudinit.distros.package_management.package_manager import (
    PackageManager,
    UninstalledPackages,
)

LOG = logging.getLogger(__name__)


class Snap(PackageManager):
    name = "snap"

    def available(self) -> bool:
        return bool(subp.which("snap"))

    def update_package_sources(self, *, force=False):
        pass

    def install_packages(self, pkglist: Iterable) -> UninstalledPackages:
        # Snap doesn't provide us with a mechanism to know which packages
        # are available or have failed, so install one at a time
        pkglist = util.expand_package_list("%s=%s", list(pkglist))
        failed: List[str] = []
        for pkg in pkglist:
            try:
                subp.subp(["snap", "install"] + pkg.split("=", 1))
            except subp.ProcessExecutionError:
                failed.append(pkg)
                LOG.info("Failed to 'snap install %s'!", pkg)
        return failed

    @staticmethod
    def upgrade_packages():
        command = ["snap", "get", "system", "-d"]
        snap_hold = None
        try:
            result = subp.subp(command)
            snap_hold = (
                util.load_json(result.stdout).get("refresh", {}).get("hold")
            )
        except subp.ProcessExecutionError as e:
            LOG.info(
                "Continuing to snap refresh. Unable to run command: %s: %s",
                command,
                e,
            )
        if snap_hold == "forever":
            LOG.info(
                "Skipping snap refresh because refresh.hold is set to '%s'",
                snap_hold,
            )
        else:
            subp.subp(["snap", "refresh"])