404

[ Avaa Bypassed ]




Upload:

Command:

botdev@18.225.156.49: ~ $
import functools
import logging
import os
import time

from uaclient import config, exceptions, util
from uaclient.files import notices
from uaclient.files.notices import Notice

LOG = logging.getLogger(util.replace_top_level_logger_name(__name__))

# Set a module-level callable here so we don't have to reinstantiate
# UAConfig in order to determine dynamic data_path exception handling of
# main_error_handler
clear_lock_file = None


def clear_lock_file_if_present():
    global clear_lock_file
    if clear_lock_file:
        clear_lock_file()


class SingleAttemptLock:
    """
    Context manager for gaining exclusive access to the lock file.
    Create a lock file if absent. The lock file will contain a pid of the
    running process, and a customer-visible description of the lock holder.

    :param lock_holder: String with the service name or command which is
        holding the lock. This lock_holder string will be customer visible in
        status.json.
    :raises: LockHeldError if lock is held.
    """

    def __init__(self, *_args, cfg: config.UAConfig, lock_holder: str):
        self.cfg = cfg
        self.lock_holder = lock_holder

    def __enter__(self):
        global clear_lock_file
        (lock_pid, cur_lock_holder) = self.cfg.check_lock_info()
        if lock_pid > 0:
            raise exceptions.LockHeldError(
                lock_request=self.lock_holder,
                lock_holder=cur_lock_holder,
                pid=lock_pid,
            )
        self.cfg.write_cache(
            "lock", "{}:{}".format(os.getpid(), self.lock_holder)
        )
        notices.add(
            Notice.OPERATION_IN_PROGRESS,
            operation=self.lock_holder,
        )
        clear_lock_file = functools.partial(self.cfg.delete_cache_key, "lock")

    def __exit__(self, _exc_type, _exc_value, _traceback):
        global clear_lock_file
        self.cfg.delete_cache_key("lock")
        clear_lock_file = None  # Unset due to successful lock delete


class SpinLock(SingleAttemptLock):
    """
    Context manager for gaining exclusive access to the lock file. In contrast
    to the SingleAttemptLock, the SpinLock will try several times to acquire
    the lock before giving up. The number of times to try and how long to sleep
    in between tries is configurable.

    :param lock_holder: String with the service name or command which is
        holding the lock. This lock_holder string will be customer visible in
        status.json.
    :param sleep_time: Number of seconds to sleep before retrying if the lock
        is already held.
    :param max_retries: Maximum number of times to try to grab the lock before
        giving up and raising a LockHeldError.
    :raises: LockHeldError if lock is held after (sleep_time * max_retries)
    """

    def __init__(
        self,
        *_args,
        cfg: config.UAConfig,
        lock_holder: str,
        sleep_time: int = 10,
        max_retries: int = 12
    ):
        super().__init__(cfg=cfg, lock_holder=lock_holder)
        self.sleep_time = sleep_time
        self.max_retries = max_retries

    def __enter__(self):
        LOG.debug("spin lock starting for %s", self.lock_holder)
        tries = 0
        while True:
            try:
                super().__enter__()
                break
            except exceptions.LockHeldError as e:
                LOG.debug(
                    "SpinLock Attempt %d. %s. Spinning...", tries + 1, e.msg
                )
                tries += 1
                if tries >= self.max_retries:
                    raise e
                else:
                    time.sleep(self.sleep_time)

Filemanager

Name Type Size Permission Actions
__pycache__ Folder 0755
api Folder 0755
cli Folder 0755
clouds Folder 0755
daemon Folder 0755
entitlements Folder 0755
files Folder 0755
http Folder 0755
messages Folder 0755
timer Folder 0755
__init__.py File 0 B 0644
actions.py File 8.79 KB 0644
apt.py File 32.73 KB 0644
apt_news.py File 6.54 KB 0644
config.py File 23.1 KB 0644
contract.py File 30.3 KB 0644
contract_data_types.py File 9.89 KB 0644
data_types.py File 10.27 KB 0644
defaults.py File 2.1 KB 0644
event_logger.py File 8.06 KB 0644
exceptions.py File 14.62 KB 0644
gpg.py File 836 B 0644
livepatch.py File 12.51 KB 0644
lock.py File 3.56 KB 0644
log.py File 2.91 KB 0644
security.py File 56.78 KB 0644
security_status.py File 25.25 KB 0644
snap.py File 6.84 KB 0644
status.py File 29.69 KB 0644
system.py File 24.65 KB 0644
types.py File 308 B 0644
upgrade_lts_contract.py File 3.5 KB 0644
util.py File 15.31 KB 0644
version.py File 2.63 KB 0644
yaml.py File 840 B 0644