Example #1
0
def _is_cloudinit_disabled(disable_file, paths):
    """Report whether cloud-init is disabled.

    @param disable_file: The path to the cloud-init disable file.
    @param paths: An initialized cloudinit.helpers.Paths object.
    @returns: A tuple containing (bool, reason) about cloud-init's status and
    why.
    """
    is_disabled = False
    cmdline_parts = get_cmdline().split()
    if not uses_systemd():
        reason = 'Cloud-init enabled on sysvinit'
    elif 'cloud-init=enabled' in cmdline_parts:
        reason = 'Cloud-init enabled by kernel command line cloud-init=enabled'
    elif os.path.exists(disable_file):
        is_disabled = True
        reason = 'Cloud-init disabled by {0}'.format(disable_file)
    elif 'cloud-init=disabled' in cmdline_parts:
        is_disabled = True
        reason = 'Cloud-init disabled by kernel parameter cloud-init=disabled'
    elif not os.path.exists(os.path.join(paths.run_dir, 'enabled')):
        is_disabled = True
        reason = 'Cloud-init disabled by cloud-init-generator'
    else:
        reason = 'Cloud-init enabled by systemd cloud-init-generator'
    return (is_disabled, reason)
Example #2
0
def _is_cloudinit_disabled(disable_file, paths):
    """Report whether cloud-init is disabled.

    @param disable_file: The path to the cloud-init disable file.
    @param paths: An initialized cloudinit.helpers.Paths object.
    @returns: A tuple containing (bool, reason) about cloud-init's status and
    why.
    """
    is_disabled = False
    cmdline_parts = get_cmdline().split()
    if not uses_systemd():
        reason = 'Cloud-init enabled on sysvinit'
    elif 'cloud-init=enabled' in cmdline_parts:
        reason = 'Cloud-init enabled by kernel command line cloud-init=enabled'
    elif os.path.exists(disable_file):
        is_disabled = True
        reason = 'Cloud-init disabled by {0}'.format(disable_file)
    elif 'cloud-init=disabled' in cmdline_parts:
        is_disabled = True
        reason = 'Cloud-init disabled by kernel parameter cloud-init=disabled'
    elif not os.path.exists(os.path.join(paths.run_dir, 'enabled')):
        is_disabled = True
        reason = 'Cloud-init disabled by cloud-init-generator'
    else:
        reason = 'Cloud-init enabled by systemd cloud-init-generator'
    return (is_disabled, reason)
Example #3
0
 def __init__(self, name, cfg, paths):
     distros.Distro.__init__(self, name, cfg, paths)
     # This will be used to restrict certain
     # calls from repeatly happening (when they
     # should only happen say once per instance...)
     self._runner = helpers.Runners(paths)
     self.osfamily = "gentoo"
     # Fix sshd restarts
     cfg["ssh_svcname"] = "/etc/init.d/sshd"
     if distros.uses_systemd():
         LOG.error("Cloud-init does not support systemd with gentoo")
Example #4
0
def available(target=None):
    # TODO: Move `uses_systemd` to a more appropriate location
    # It is imported here to avoid circular import
    from cloudinit.distros import uses_systemd

    config_present = os.path.isfile(subp.target_path(target, path=NM_CFG_FILE))
    nmcli_present = subp.which("nmcli", target=target)
    service_active = True
    if uses_systemd():
        try:
            subp.subp(["systemctl", "is-enabled", "NetworkManager.service"])
        except subp.ProcessExecutionError:
            service_active = False

    return config_present and bool(nmcli_present) and service_active
Example #5
0
def dist_check_timestamp():
    """
    Determine which init system a particular linux distro is using.
    Each init system (systemd, etc) has a different way of
    providing timestamps.

    :return: timestamps of kernelboot, kernelendboot, and cloud-initstart
    or TIMESTAMP_UNKNOWN if the timestamps cannot be retrieved.
    """

    if uses_systemd():
        return gather_timestamps_using_systemd()

    # Use dmesg to get timestamps if the distro does not have systemd
    if util.is_FreeBSD() or "gentoo" in util.system_info()["system"].lower():
        return gather_timestamps_using_dmesg()

    # this distro doesn't fit anything that is supported by cloud-init. just
    # return error codes
    return TIMESTAMP_UNKNOWN
Example #6
0
def get_boot_telemetry():
    """Report timestamps related to kernel initialization and systemd
       activation of cloud-init"""
    if not distros.uses_systemd():
        raise RuntimeError("distro not using systemd, skipping boot telemetry")

    LOG.debug("Collecting boot telemetry")
    try:
        kernel_start = float(time.time()) - float(util.uptime())
    except ValueError:
        raise RuntimeError("Failed to determine kernel start timestamp")

    try:
        out, _ = util.subp(
            ['/bin/systemctl', 'show', '-p', 'UserspaceTimestampMonotonic'],
            capture=True)
        tsm = None
        if out and '=' in out:
            tsm = out.split("=")[1]

        if not tsm:
            raise RuntimeError("Failed to parse "
                               "UserspaceTimestampMonotonic from systemd")

        user_start = kernel_start + (float(tsm) / 1000000)
    except util.ProcessExecutionError as e:
        raise RuntimeError("Failed to get UserspaceTimestampMonotonic: %s" % e)
    except ValueError as e:
        raise RuntimeError("Failed to parse "
                           "UserspaceTimestampMonotonic from systemd: %s" % e)

    try:
        out, _ = util.subp([
            '/bin/systemctl', 'show', 'cloud-init-local', '-p',
            'InactiveExitTimestampMonotonic'
        ],
                           capture=True)
        tsm = None
        if out and '=' in out:
            tsm = out.split("=")[1]
        if not tsm:
            raise RuntimeError("Failed to parse "
                               "InactiveExitTimestampMonotonic from systemd")

        cloudinit_activation = kernel_start + (float(tsm) / 1000000)
    except util.ProcessExecutionError as e:
        raise RuntimeError("Failed to get InactiveExitTimestampMonotonic: %s" %
                           e)
    except ValueError as e:
        raise RuntimeError("Failed to parse "
                           "InactiveExitTimestampMonotonic from systemd: %s" %
                           e)

    evt = events.ReportingEvent(
        BOOT_EVENT_TYPE, 'boot-telemetry',
        "kernel_start=%s user_start=%s cloudinit_activation=%s" %
        (datetime.utcfromtimestamp(kernel_start).isoformat() + 'Z',
         datetime.utcfromtimestamp(user_start).isoformat() + 'Z',
         datetime.utcfromtimestamp(cloudinit_activation).isoformat() + 'Z'),
        events.DEFAULT_EVENT_ORIGIN)
    events.report_event(evt)

    # return the event for unit testing purpose
    return evt