Ejemplo n.º 1
0
def machine_detail(request, **kwargs):
    machine = kwargs['instance']
    machine_group = machine.machine_group
    business_unit = machine_group.business_unit

    try:
        ip_address_fact = machine.facts.get(fact_name='ipv4_address')
        ip_address = ip_address_fact.fact_data
    except (Fact.MultipleObjectsReturned, Fact.DoesNotExist):
        ip_address = None

    if Plugin.objects.filter(name='Uptime'):
        try:
            plugin_script_submission = PluginScriptSubmission.objects.get(
                machine=machine, plugin='Uptime')
            uptime_seconds = PluginScriptRow.objects.get(
                submission=plugin_script_submission,
                pluginscript_name='UptimeSeconds').pluginscript_data
        except Exception:
            uptime_seconds = '0'

        uptime = utils.display_time(int(uptime_seconds))
    else:
        uptime = None

    output = utils.get_machine_detail_placeholder_markup(machine)

    # Process machine's managed items for display in the template.
    managed_items_queryset = ManagedItem.objects.filter(machine=machine)
    managed_items = defaultdict(dict)
    for item in managed_items_queryset:
        if item.data:
            try:
                data = json.loads(item.data)
            except json.decoder.JSONDecodeError:
                logger.error('Managed item JSON is invalid: %s', item.data)
                data = {}
        else:
            data = {}
        # Structure is a dict with keys for each source; each source
        # will then have as many sub types as ManagedItem.data "types".
        # The final structure is a list of ManagedItems.
        data_type = data.get('type')
        if data_type not in managed_items[item.management_source.name]:
            managed_items[item.management_source.name][data.get('type')] = []
        managed_items[item.management_source.name][data.get('type')].append(
            item)

    # Process histories for use in item modals.
    history_queryset = machine.manageditemhistory_set.all()
    histories = {}
    for item in history_queryset:
        histories.setdefault(f'{item.management_source.name}||{item.name}',
                             []).append(item)

    # Determine pending updates
    pending = managed_items_queryset.filter(status="PENDING")

    # Determine which tab to display first.
    # TODO: Do we just use the first, or configure for each OS / source?
    if machine.os_family == 'Darwin':
        initial_source = 'munki'
        active_table = 'ManagedInstalls'
    else:
        sources = sorted([s for s in managed_items])
        initial_source = sources[0] if sources else ''
        active_table = list(
            managed_items[initial_source].keys())[0] if initial_source else ''

    messages = {}
    for message in machine.messages.all():
        messages.setdefault(message.message_type, []).append(message)

    context = {
        'user': request.user,
        'machine_group': machine_group,
        'business_unit': business_unit,
        'messages': messages,
        'managed_items': dict(managed_items),
        'histories': histories,
        'management_tools': _get_management_tools(managed_items.keys(),
                                                  machine),
        'pending': pending,
        'fact_sources': get_fact_sources(machine),
        'initial_source': initial_source,
        'active_table': active_table,
        'statuses': STATUSES,
        'machine': machine,
        'ip_address': ip_address,
        'uptime': uptime,
        'output': output
    }
    return render(request, 'server/machine_detail.html', context)
Ejemplo n.º 2
0
def machine_detail(request, **kwargs):
    machine = kwargs['instance']
    machine_group = machine.machine_group
    business_unit = machine_group.business_unit

    report = machine.get_report()

    try:
        ip_address_condition = machine.conditions.get(condition_name='ipv4_address')
        ip_address = ip_address_condition.condition_data
    except (Condition.MultipleObjectsReturned, Condition.DoesNotExist):
        ip_address = None

    install_results = {}
    for result in report.get('InstallResults', []):
        nameAndVers = result['name'] + '-' + result['version']
        if result['status'] == 0:
            install_results[nameAndVers] = "installed"
        else:
            install_results[nameAndVers] = 'error'

    # if install_results:
    for item in report.get('ItemsToInstall', []):
        name = item.get('display_name', item['name'])
        nameAndVers = ('%s-%s'
                       % (name, item['version_to_install']))
        item['install_result'] = install_results.get(
            nameAndVers, 'pending')

        # Get the update history
        try:
            item['update_history'] = UpdateHistory.objects.get(
                machine=machine, version=item['version_to_install'], name=item['name'],
                update_type='third_party').updatehistoryitem_set.all()
        except (IndexError, UpdateHistory.DoesNotExist):
            pass

    for item in report.get('ManagedInstalls', []):
        if 'version_to_install' in item:
            name = item.get('display_name', item['name'])
            nameAndVers = ('%s-%s'
                           % (name, item['version_to_install']))
            if install_results.get(nameAndVers) == 'installed':
                item['installed'] = True

        if 'version_to_install' in item or 'installed_version' in item:
            if 'version_to_install' in item:
                version = item['version_to_install']
            else:
                version = item['installed_version']
            item['version'] = version
            # Get the update history
            try:
                item['update_history'] = UpdateHistory.objects.get(
                    machine=machine, version=version, name=item['name'],
                    update_type='third_party').updatehistoryitem_set.all()
            except Exception:
                pass

    # handle items that were removed during the most recent run
    # this is crappy. We should fix it in Munki.
    removal_results = {}
    for result in report.get('RemovalResults', []):
        try:
            m = re.search('^Removal of (.+): (.+)$', result)
        except Exception:
            m = None
        if m:
            try:
                if m.group(2) == 'SUCCESSFUL':
                    removal_results[m.group(1)] = 'removed'
                else:
                    removal_results[m.group(1)] = m.group(2)
            except IndexError:
                pass

    if removal_results:
        for item in report.get('ItemsToRemove', []):
            name = item.get('display_name', item['name'])
            item['install_result'] = removal_results.get(
                name, 'pending')
            if item['install_result'] == 'removed':
                if 'RemovedItems' not in report:
                    report['RemovedItems'] = [item['name']]
                elif name not in report['RemovedItems']:
                    report['RemovedItems'].append(item['name'])

    if Plugin.objects.filter(name='Uptime'):
        try:
            plugin_script_submission = PluginScriptSubmission.objects.get(
                machine=machine, plugin='Uptime')
            uptime_seconds = PluginScriptRow.objects.get(
                submission=plugin_script_submission,
                pluginscript_name='UptimeSeconds').pluginscript_data
        except Exception:
            uptime_seconds = '0'

        uptime = utils.display_time(int(uptime_seconds))
    else:
        uptime = None

    if 'managed_uninstalls_list' in report:
        report['managed_uninstalls_list'].sort()

    output = utils.get_machine_detail_placeholder_markup(machine)

    context = {
        'user': request.user,
        'machine_group': machine_group,
        'business_unit': business_unit,
        'report': report,
        'install_results': install_results,
        'removal_results': removal_results,
        'machine': machine,
        'ip_address': ip_address,
        'uptime': uptime,
        'output': output}
    return render(request, 'server/machine_detail.html', context)
Ejemplo n.º 3
0
def machine_detail(request, **kwargs):
    machine = kwargs['instance']
    machine_group = machine.machine_group
    business_unit = machine_group.business_unit

    report = machine.get_report()

    try:
        ip_address_condition = machine.conditions.get(
            condition_name='ipv4_address')
        ip_address = ip_address_condition.condition_data
    except (Condition.MultipleObjectsReturned, Condition.DoesNotExist):
        ip_address = None

    install_results = {}
    for result in report.get('InstallResults', []):
        nameAndVers = result['name'] + '-' + result['version']
        if result['status'] == 0:
            install_results[nameAndVers] = "installed"
        else:
            install_results[nameAndVers] = 'error'

    # if install_results:
    for item in report.get('ItemsToInstall', []):
        name = item.get('display_name', item['name'])
        nameAndVers = ('%s-%s' % (name, item['version_to_install']))
        item['install_result'] = install_results.get(nameAndVers, 'pending')

        # Get the update history
        try:
            item['update_history'] = UpdateHistory.objects.get(
                machine=machine,
                version=item['version_to_install'],
                name=item['name'],
                update_type='third_party').updatehistoryitem_set.all()
        except (IndexError, UpdateHistory.DoesNotExist):
            pass

    for item in report.get('ManagedInstalls', []):
        if 'version_to_install' in item:
            name = item.get('display_name', item['name'])
            nameAndVers = ('%s-%s' % (name, item['version_to_install']))
            if install_results.get(nameAndVers) == 'installed':
                item['installed'] = True

        if 'version_to_install' in item or 'installed_version' in item:
            if 'version_to_install' in item:
                version = item['version_to_install']
            else:
                version = item['installed_version']
            item['version'] = version
            # Get the update history
            try:
                item['update_history'] = UpdateHistory.objects.get(
                    machine=machine,
                    version=version,
                    name=item['name'],
                    update_type='third_party').updatehistoryitem_set.all()
            except Exception:
                pass

    # handle items that were removed during the most recent run
    # this is crappy. We should fix it in Munki.
    removal_results = {}
    for result in report.get('RemovalResults', []):
        try:
            m = re.search('^Removal of (.+): (.+)$', result)
        except Exception:
            m = None
        if m:
            try:
                if m.group(2) == 'SUCCESSFUL':
                    removal_results[m.group(1)] = 'removed'
                else:
                    removal_results[m.group(1)] = m.group(2)
            except IndexError:
                pass

    if removal_results:
        for item in report.get('ItemsToRemove', []):
            name = item.get('display_name', item['name'])
            item['install_result'] = removal_results.get(name, 'pending')
            if item['install_result'] == 'removed':
                if 'RemovedItems' not in report:
                    report['RemovedItems'] = [item['name']]
                elif name not in report['RemovedItems']:
                    report['RemovedItems'].append(item['name'])

    if Plugin.objects.filter(name='Uptime'):
        try:
            plugin_script_submission = PluginScriptSubmission.objects.get(
                machine=machine, plugin='Uptime')
            uptime_seconds = PluginScriptRow.objects.get(
                submission=plugin_script_submission,
                pluginscript_name='UptimeSeconds').pluginscript_data
        except Exception:
            uptime_seconds = '0'

        uptime = utils.display_time(int(uptime_seconds))
    else:
        uptime = None

    if 'managed_uninstalls_list' in report:
        report['managed_uninstalls_list'].sort()

    output = utils.get_machine_detail_placeholder_markup(machine)

    context = {
        'user': request.user,
        'machine_group': machine_group,
        'business_unit': business_unit,
        'report': report,
        'install_results': install_results,
        'removal_results': removal_results,
        'machine': machine,
        'ip_address': ip_address,
        'uptime': uptime,
        'output': output
    }
    return render(request, 'server/machine_detail.html', context)