Private
Server IP : 195.201.23.43  /  Your IP : 3.142.243.141
Web Server : Apache
System : Linux webserver2.vercom.be 5.4.0-192-generic #212-Ubuntu SMP Fri Jul 5 09:47:39 UTC 2024 x86_64
User : kdecoratie ( 1041)
PHP Version : 7.1.33-63+ubuntu20.04.1+deb.sury.org+1
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /lib/ubuntu-advantage/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /lib/ubuntu-advantage/auto_attach.py
#!/usr/bin/env python3

"""
Perform auto-attach operation

On Ubuntu Pro machines, we try to perform an auto-attach operation
on first boot. This happens through a systemd unit that executes
this script on every boot. However, if we detect
that cloud-init has user data related to ua, we don't run
auto-attach here, since cloud-init will drive this operation on
their side.
"""
import logging
import sys

from uaclient import http, log, messages, system
from uaclient.api.exceptions import (
    AlreadyAttachedError,
    AutoAttachDisabledError,
    EntitlementsNotEnabledError,
)
from uaclient.api.u.pro.attach.auto.full_auto_attach.v1 import (
    FullAutoAttachOptions,
    full_auto_attach,
)
from uaclient.config import UAConfig
from uaclient.daemon import AUTO_ATTACH_STATUS_MOTD_FILE, retry_auto_attach
from uaclient.files import state_files

LOG = logging.getLogger("ubuntupro.lib.auto_attach")

# All known cloud-config keys which provide ubuntu pro configuration directives
CLOUD_INIT_UA_KEYS = set(
    ["ubuntu-advantage", "ubuntu_advantage", "ubuntu_pro"]
)

try:
    import cloudinit.stages as ci_stages  # type: ignore
except ImportError:
    pass


def get_cloudinit_init_stage():
    if "cloudinit.stages" in sys.modules:
        return ci_stages.Init()

    return None


def check_cloudinit_userdata_for_ua_info():
    init = get_cloudinit_init_stage()

    # if init is None, this means we were not able to import the cloud-init
    # module.
    if init is None:
        return False

    if init.cfg and CLOUD_INIT_UA_KEYS.intersection(init.cfg):
        return True

    return False


def main(cfg: UAConfig):
    if check_cloudinit_userdata_for_ua_info():
        LOG.info("cloud-init userdata has ubuntu-advantage key.")
        LOG.info(
            "Skipping auto-attach and deferring to cloud-init "
            "to setup and configure auto-attach"
        )
        return

    system.write_file(
        AUTO_ATTACH_STATUS_MOTD_FILE, messages.AUTO_ATTACH_RUNNING
    )
    try:
        full_auto_attach(FullAutoAttachOptions())
    except AlreadyAttachedError as e:
        LOG.info(e.msg)
    except AutoAttachDisabledError:
        LOG.debug("Skipping auto-attach. Config disable_auto_attach is set.")
    except EntitlementsNotEnabledError as e:
        LOG.warning(e.msg)
    except Exception as e:
        LOG.error(e)
        system.ensure_file_absent(AUTO_ATTACH_STATUS_MOTD_FILE)
        LOG.info("creating flag file to trigger retries")
        system.create_file(retry_auto_attach.FLAG_FILE_PATH)
        failure_reason = (
            retry_auto_attach.full_auto_attach_exception_to_failure_reason(e)
        )
        state_files.retry_auto_attach_state_file.write(
            state_files.RetryAutoAttachState(
                interval_index=0, failure_reason=failure_reason
            )
        )
        return 1

    system.ensure_file_absent(AUTO_ATTACH_STATUS_MOTD_FILE)
    return 0


if __name__ == "__main__":
    log.setup_journald_logging()
    cfg = UAConfig()
    http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)
    sys.exit(main(cfg))
Private