| Server IP : 167.99.224.18 / Your IP : 216.73.216.136 Web Server : Apache/2.4.41 (Ubuntu) System : Linux wordpress-ubuntu-s-1vcpu-1gb-nyc1-01 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.0.25 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,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /lib/python3/dist-packages/uaclient/ |
Upload File : |
import logging
import time
from subprocess import TimeoutExpired
from uaclient import actions, exceptions, lock, messages, system, util
from uaclient.clouds import AutoAttachCloudInstance
from uaclient.clouds.gcp import UAAutoAttachGCPInstance
from uaclient.clouds.identity import cloud_instance_factory
from uaclient.config import UAConfig
LOG = logging.getLogger("pro.daemon")
def start():
try:
system.subp(
["systemctl", "start", "ubuntu-advantage.service"], timeout=2.0
)
except (exceptions.ProcessExecutionError, TimeoutExpired) as e:
LOG.warning(e)
def stop():
try:
system.subp(
["systemctl", "stop", "ubuntu-advantage.service"], timeout=2.0
)
except (exceptions.ProcessExecutionError, TimeoutExpired) as e:
LOG.warning(e)
def attempt_auto_attach(cfg: UAConfig, cloud: AutoAttachCloudInstance):
try:
with lock.SpinLock(
cfg=cfg, lock_holder="pro.daemon.attempt_auto_attach"
):
actions.auto_attach(cfg, cloud)
except exceptions.LockHeldError as e:
LOG.error(e)
cfg.notice_file.add(
"",
messages.NOTICE_DAEMON_AUTO_ATTACH_LOCK_HELD.format(
operation=e.lock_holder
),
)
LOG.debug("Failed to auto attach")
return
except Exception as e:
LOG.exception(e)
cfg.notice_file.add("", messages.NOTICE_DAEMON_AUTO_ATTACH_FAILED)
lock.clear_lock_file_if_present()
LOG.debug("Failed to auto attach")
return
LOG.debug("Successful auto attach")
def poll_for_pro_license(cfg: UAConfig):
if util.is_config_value_true(
config=cfg.cfg, path_to_value="features.disable_auto_attach"
):
LOG.debug("Configured to not auto attach, shutting down")
return
if cfg.is_attached:
LOG.debug("Already attached, shutting down")
return
if not system.is_current_series_lts():
LOG.debug("Not on LTS, shutting down")
return
try:
cloud = cloud_instance_factory()
except exceptions.CloudFactoryError:
LOG.debug("Not on cloud, shutting down")
return
if not isinstance(cloud, UAAutoAttachGCPInstance):
LOG.debug("Not on gcp, shutting down")
return
if not cloud.should_poll_for_pro_license():
LOG.debug("Not on supported instance, shutting down")
return
try:
pro_license_present = cloud.is_pro_license_present(
wait_for_change=False
)
except exceptions.CancelProLicensePolling:
LOG.debug("Cancelling polling")
return
except exceptions.DelayProLicensePolling:
# Continue to polling loop anyway and handle error there if it occurs
# again
pass
else:
if pro_license_present:
attempt_auto_attach(cfg, cloud)
return
if not cfg.poll_for_pro_license:
LOG.debug("Configured to not poll for pro license, shutting down")
return
while True:
try:
start = time.time()
pro_license_present = cloud.is_pro_license_present(
wait_for_change=True
)
end = time.time()
except exceptions.CancelProLicensePolling:
LOG.debug("Cancelling polling")
return
except exceptions.DelayProLicensePolling:
time.sleep(cfg.polling_error_retry_delay)
continue
else:
if cfg.is_attached:
# This could have changed during the long poll or sleep
LOG.debug("Already attached, shutting down")
return
if pro_license_present:
attempt_auto_attach(cfg, cloud)
return
if end - start < 10:
LOG.debug(
"wait_for_change returned quickly and no pro license"
" present. Waiting {} seconds before polling again".format(
cfg.polling_error_retry_delay
)
)
time.sleep(cfg.polling_error_retry_delay)
continue