Blame cloudinit/apport.py

Packit Service a04d08
# Copyright (C) 2017 Canonical Ltd.
Packit Service a04d08
#
Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
'''Cloud-init apport interface'''
Packit Service a04d08
Packit Service a04d08
try:
Packit Service a04d08
    from apport.hookutils import (
Packit Service a04d08
        attach_file, attach_root_command_outputs, root_command_output)
Packit Service a04d08
    has_apport = True
Packit Service a04d08
except ImportError:
Packit Service a04d08
    has_apport = False
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
KNOWN_CLOUD_NAMES = [
Packit Service a04d08
    'AliYun',
Packit Service a04d08
    'AltCloud',
Packit Service a04d08
    'Amazon - Ec2',
Packit Service a04d08
    'Azure',
Packit Service a04d08
    'Bigstep',
Packit Service a04d08
    'Brightbox',
Packit Service a04d08
    'CloudSigma',
Packit Service a04d08
    'CloudStack',
Packit Service a04d08
    'DigitalOcean',
Packit Service a04d08
    'E24Cloud',
Packit Service a04d08
    'GCE - Google Compute Engine',
Packit Service a04d08
    'Exoscale',
Packit Service a04d08
    'Hetzner Cloud',
Packit Service a04d08
    'IBM - (aka SoftLayer or BlueMix)',
Packit Service a04d08
    'LXD',
Packit Service a04d08
    'MAAS',
Packit Service a04d08
    'NoCloud',
Packit Service a04d08
    'OpenNebula',
Packit Service a04d08
    'OpenStack',
Packit Service a04d08
    'Oracle',
Packit Service a04d08
    'OVF',
Packit Service a04d08
    'RbxCloud - (HyperOne, Rootbox, Rubikon)',
Packit Service a04d08
    'OpenTelekomCloud',
Packit Service 9bfd13
    'SAP Converged Cloud',
Packit Service a04d08
    'Scaleway',
Packit Service a04d08
    'SmartOS',
Packit Service a04d08
    'VMware',
Packit Service a04d08
    'ZStack',
Packit Service a04d08
    'Other'
Packit Service a04d08
]
Packit Service a04d08
Packit Service a04d08
# Potentially clear text collected logs
Packit Service a04d08
CLOUDINIT_LOG = '/var/log/cloud-init.log'
Packit Service a04d08
CLOUDINIT_OUTPUT_LOG = '/var/log/cloud-init-output.log'
Packit Service a04d08
USER_DATA_FILE = '/var/lib/cloud/instance/user-data.txt'  # Optional
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def attach_cloud_init_logs(report, ui=None):
Packit Service a04d08
    '''Attach cloud-init logs and tarfile from 'cloud-init collect-logs'.'''
Packit Service a04d08
    attach_root_command_outputs(report, {
Packit Service a04d08
        'cloud-init-log-warnings':
Packit Service a04d08
            'egrep -i "warn|error" /var/log/cloud-init.log',
Packit Service a04d08
        'cloud-init-output.log.txt': 'cat /var/log/cloud-init-output.log'})
Packit Service a04d08
    root_command_output(
Packit Service a04d08
        ['cloud-init', 'collect-logs', '-t', '/tmp/cloud-init-logs.tgz'])
Packit Service a04d08
    attach_file(report, '/tmp/cloud-init-logs.tgz', 'logs.tgz')
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def attach_hwinfo(report, ui=None):
Packit Service a04d08
    '''Optionally attach hardware info from lshw.'''
Packit Service a04d08
    prompt = (
Packit Service a04d08
        'Your device details (lshw) may be useful to developers when'
Packit Service a04d08
        ' addressing this bug, but gathering it requires admin privileges.'
Packit Service a04d08
        ' Would you like to include this info?')
Packit Service a04d08
    if ui and ui.yesno(prompt):
Packit Service a04d08
        attach_root_command_outputs(report, {'lshw.txt': 'lshw'})
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def attach_cloud_info(report, ui=None):
Packit Service a04d08
    '''Prompt for cloud details if available.'''
Packit Service a04d08
    if ui:
Packit Service a04d08
        prompt = 'Is this machine running in a cloud environment?'
Packit Service a04d08
        response = ui.yesno(prompt)
Packit Service a04d08
        if response is None:
Packit Service a04d08
            raise StopIteration  # User cancelled
Packit Service a04d08
        if response:
Packit Service a04d08
            prompt = ('Please select the cloud vendor or environment in which'
Packit Service a04d08
                      ' this instance is running')
Packit Service a04d08
            response = ui.choice(prompt, KNOWN_CLOUD_NAMES)
Packit Service a04d08
            if response:
Packit Service a04d08
                report['CloudName'] = KNOWN_CLOUD_NAMES[response[0]]
Packit Service a04d08
            else:
Packit Service a04d08
                report['CloudName'] = 'None'
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def attach_user_data(report, ui=None):
Packit Service a04d08
    '''Optionally provide user-data if desired.'''
Packit Service a04d08
    if ui:
Packit Service a04d08
        prompt = (
Packit Service a04d08
            'Your user-data or cloud-config file can optionally be provided'
Packit Service a04d08
            ' from {0} and could be useful to developers when addressing this'
Packit Service a04d08
            ' bug. Do you wish to attach user-data to this bug?'.format(
Packit Service a04d08
                USER_DATA_FILE))
Packit Service a04d08
        response = ui.yesno(prompt)
Packit Service a04d08
        if response is None:
Packit Service a04d08
            raise StopIteration  # User cancelled
Packit Service a04d08
        if response:
Packit Service a04d08
            attach_file(report, USER_DATA_FILE, 'user_data.txt')
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def add_bug_tags(report):
Packit Service a04d08
    '''Add any appropriate tags to the bug.'''
Packit Service a04d08
    if 'JournalErrors' in report.keys():
Packit Service a04d08
        errors = report['JournalErrors']
Packit Service a04d08
        if 'Breaking ordering cycle' in errors:
Packit Service a04d08
            report['Tags'] = 'systemd-ordering'
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def add_info(report, ui):
Packit Service a04d08
    '''This is an entry point to run cloud-init's apport functionality.
Packit Service a04d08
Packit Service a04d08
    Distros which want apport support will have a cloud-init package-hook at
Packit Service a04d08
    /usr/share/apport/package-hooks/cloud-init.py which defines an add_info
Packit Service a04d08
    function and returns the result of cloudinit.apport.add_info(report, ui).
Packit Service a04d08
    '''
Packit Service a04d08
    if not has_apport:
Packit Service a04d08
        raise RuntimeError(
Packit Service a04d08
            'No apport imports discovered. Apport functionality disabled')
Packit Service a04d08
    attach_cloud_init_logs(report, ui)
Packit Service a04d08
    attach_hwinfo(report, ui)
Packit Service a04d08
    attach_cloud_info(report, ui)
Packit Service a04d08
    attach_user_data(report, ui)
Packit Service a04d08
    add_bug_tags(report)
Packit Service a04d08
    return True
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab