import logging import re from typing import List, Optional from uaclient import apt, event_logger, exceptions, messages, system SNAP_CMD = "/usr/bin/snap" SNAP_INSTALL_RETRIES = [0.5, 1.0, 5.0] HTTP_PROXY_OPTION = "proxy.http" HTTPS_PROXY_OPTION = "proxy.https" SNAP_CHANNEL_SHORTEN_VALUE = "…" event = event_logger.get_event_logger() def is_installed() -> bool: """Returns whether or not snap is installed""" return "snapd" in apt.get_installed_packages_names() def configure_snap_proxy( http_proxy: Optional[str] = None, https_proxy: Optional[str] = None, retry_sleeps: Optional[List[float]] = None, ) -> None: """ Configure snap to use http and https proxies. :param http_proxy: http proxy to be used by snap. If None, it will not be configured :param https_proxy: https proxy to be used by snap. If None, it will not be configured :param retry_sleeps: Optional list of sleep lengths to apply between retries. Specifying a list of [0.5, 1] tells subp to retry twice on failure; sleeping half a second before the first retry and 1 second before the second retry. """ if not system.which(SNAP_CMD): logging.debug( "Skipping configure snap proxy. {} does not exist.".format( SNAP_CMD ) ) return if http_proxy or https_proxy: event.info(messages.SETTING_SERVICE_PROXY.format(service="snap")) if http_proxy: system.subp( ["snap", "set", "system", "proxy.http={}".format(http_proxy)], retry_sleeps=retry_sleeps, ) if https_proxy: system.subp( ["snap", "set", "system", "proxy.https={}".format(https_proxy)], retry_sleeps=retry_sleeps, ) def unconfigure_snap_proxy( protocol_type: str, retry_sleeps: Optional[List[float]] = None ) -> None: """ Unset snap configuration settings for http and https proxies. :param protocol_type: String either http or https :param retry_sleeps: Optional list of sleep lengths to apply between retries. Specifying a list of [0.5, 1] tells subp to retry twice on failure; sleeping half a second before the first retry and 1 second before the second retry. """ if not system.which(SNAP_CMD): logging.debug( "Skipping unconfigure snap proxy. {} does not exist.".format( SNAP_CMD ) ) return system.subp( ["snap", "unset", "system", "proxy.{}".format(protocol_type)], retry_sleeps=retry_sleeps, ) def get_config_option_value(key: str) -> Optional[str]: """ Gets the config value from snap. :param protocol: can be any valid snap config option :return: the value of the snap config option, or None if not set """ try: out, _ = system.subp(["snap", "get", "system", key]) return out.strip() except exceptions.ProcessExecutionError: return None def get_snap_package_info_tracking(package: str) -> Optional[str]: out, _ = system.subp( ["snap", "info", package, "--color", "never", "--unicode", "never"] ) match = re.search(r"tracking:\s+(?P<tracking>.*)", out) if match: return match.group("tracking") return None class SnapPackage: def __init__(self, name, version, rev, tracking, publisher, notes): self.name = name self.version = version self.rev = rev self.tracking = tracking self.publisher = publisher self.notes = notes def get_installed_snaps() -> List[SnapPackage]: out, _ = system.subp( ["snap", "list", "--color", "never", "--unicode", "never"] ) apps = out.splitlines() apps = apps[1:] snaps = [] for line in apps: pkg = line.split() snap = SnapPackage(*pkg) if snap.tracking.endswith(SNAP_CHANNEL_SHORTEN_VALUE): channel = get_snap_package_info_tracking(snap.name) snap.tracking = channel if channel else snap.tracking snaps.append(snap) return snaps
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
api | Folder | 0755 |
|
|
clouds | Folder | 0755 |
|
|
daemon | Folder | 0755 |
|
|
entitlements | Folder | 0755 |
|
|
files | Folder | 0755 |
|
|
jobs | Folder | 0755 |
|
|
__init__.py | File | 0 B | 0644 |
|
actions.py | File | 8.19 KB | 0644 |
|
apt.py | File | 25.74 KB | 0644 |
|
apt_news.py | File | 6.33 KB | 0644 |
|
cli.py | File | 64.22 KB | 0644 |
|
config.py | File | 24.5 KB | 0644 |
|
contract.py | File | 27.47 KB | 0644 |
|
contract_data_types.py | File | 9.38 KB | 0644 |
|
data_types.py | File | 10.3 KB | 0644 |
|
defaults.py | File | 2.46 KB | 0644 |
|
event_logger.py | File | 7.75 KB | 0644 |
|
exceptions.py | File | 13.56 KB | 0644 |
|
gpg.py | File | 813 B | 0644 |
|
livepatch.py | File | 11.03 KB | 0644 |
|
lock.py | File | 3.58 KB | 0644 |
|
log.py | File | 1.89 KB | 0644 |
|
messages.py | File | 38.47 KB | 0644 |
|
pip.py | File | 756 B | 0644 |
|
security.py | File | 48.75 KB | 0644 |
|
security_status.py | File | 24.19 KB | 0644 |
|
serviceclient.py | File | 6.22 KB | 0644 |
|
snap.py | File | 4.06 KB | 0644 |
|
status.py | File | 25.73 KB | 0644 |
|
system.py | File | 17.09 KB | 0644 |
|
types.py | File | 308 B | 0644 |
|
util.py | File | 20.3 KB | 0644 |
|
version.py | File | 2.81 KB | 0644 |
|
yaml.py | File | 642 B | 0644 |
|