Skip to content

Commit

Permalink
Merge pull request #363 from phillipthelen/highlighting
Browse files Browse the repository at this point in the history
Highlight when hostname or ipaddress are different
  • Loading branch information
phillipthelen authored Apr 22, 2020
2 parents 8cdb2fb + 32b0a55 commit b5334ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Lagerregal/template_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@
USE_OPSI = True
OPSI_SETTINGS = {
"host": "https://opsi.mpib-berlin.mpg.de",
"port": "4447"
"port": "4447",
}

HOST_BASE_DOMAIN = "mpib-berlin.mpg.de"

# sample logger
# import logging, logging.handlers
# logger = logging.getLogger('django_auth_ldap')
Expand Down
14 changes: 13 additions & 1 deletion devicedata/providers/base_provider.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from Lagerregal import settings
from devices.models import Device
from abc import ABC, abstractmethod


class BaseDeviceInfo(ABC):
device = None
formatted_entries = []
raw_entries = []

def find_entries(self, entry_type):
return [entry for entry in self.raw_entries if entry.type == entry_type]

def __init__(self, raw_entries):
def __init__(self, device, raw_entries):
self.device = device
self.formatted_entries = []
self.raw_entries = raw_entries
self.format_entries()
Expand Down Expand Up @@ -61,3 +64,12 @@ def get_software_info(self, device):
@abstractmethod
def has_device(self, device):
pass


def build_full_hostname(device):
hostname = device.hostname.lower()
if len(hostname) > 0:
if hasattr(settings, "HOST_BASE_DOMAIN") and not hostname.endswith(
settings.HOST_BASE_DOMAIN):
hostname += "." + settings.HOST_BASE_DOMAIN
return hostname
35 changes: 25 additions & 10 deletions devicedata/providers/opsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from Lagerregal import settings
from devicedata.providers.base_provider import BaseProvider, SoftwareEntry, DeviceInfoEntry, BaseDeviceInfo, \
FormattedDeviceInfoEntry
FormattedDeviceInfoEntry, build_full_hostname
from devicedata.providers.helpers import format_bytes
from devicedata.providers.opsirpc import OpsiConnection
from django.utils.translation import ugettext_lazy as _
Expand All @@ -21,7 +21,12 @@ def format_system(self):
entries = self.find_entries("COMPUTER_SYSTEM")
if len(entries) > 0:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Manufacturer"), entries[0].raw_value["vendor"]))
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), entries[0].raw_value["hostId"]))
hostname = build_full_hostname(self.device)
if entries[0].raw_value["hostId"] != hostname:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), "<span class='text-warning'>" +
entries[0].raw_value["hostId"] + "</span>"))
else:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), entries[0].raw_value["hostId"]))
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Last Seen"), entries[0].raw_value["lastseen"]))

def format_processor(self):
Expand Down Expand Up @@ -55,9 +60,16 @@ def format_network(self):
for entry in entries:
if entry.raw_value["ipAddress"] is not None:
controllers.append(entry.raw_value)
formatted_controllers = "<br />".join(
["{0} {1}".format(controller["description"], controller["ipAddress"]) for controller in controllers])
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Network"), formatted_controllers))
formatted_controllers = []
device_addresses = self.device.ipaddress_set.all()
for controller in controllers:
if any(elem.address in controller["ipAddress"] for elem in device_addresses):
formatted_controllers.append("{0} {1}".format(controller["description"], controller["ipAddress"]))
else:
formatted_controllers.append(
"{0} <span class='text-warning'>{1}<span>".format(controller["description"],
controller["ipAddress"]))
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Network"), "<br />".join(formatted_controllers)))

def format_graphics(self):
entries = self.find_entries("VIDEO_CONTROLLER")
Expand All @@ -82,13 +94,16 @@ class OpsiProvider(BaseProvider):

def __get_host(self, device):
host = None
hostname = build_full_hostname(device)
if len(hostname) > 0:
response = self.__connection.host_getObjects(id=hostname)
if len(response) == 1:
return response[0]
for ip in device.ipaddress_set.all():
response = self.__connection.host_getObjects(ipAddress=ip.address)
for h in response:
if str(device.id) in h['id']:
host = response[0]
break

host = h
break
if host is None:
raise ObjectDoesNotExist()
return host
Expand All @@ -99,7 +114,7 @@ def get_device_info(self, device):
device_entries = []
for entry in hardware:
device_entries.append(DeviceInfoEntry(entry["hardwareClass"], entry["name"], entry))
return OpsiDeviceInfo(device_entries)
return OpsiDeviceInfo(device, device_entries)

def get_software_info(self, device):
host = self.__get_host(device)
Expand Down
23 changes: 17 additions & 6 deletions devicedata/providers/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from Lagerregal import settings
from devicedata.providers.base_provider import BaseProvider, BaseDeviceInfo, DeviceInfoEntry, SoftwareEntry, \
FormattedDeviceInfoEntry
FormattedDeviceInfoEntry, build_full_hostname
from django.utils.translation import ugettext_lazy as _

from devicedata.providers.helpers import format_bytes
Expand All @@ -29,7 +29,12 @@ def format_type(self):
def format_hostname(self):
entries = self.find_entries("fqdn")
if len(entries) > 0:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), entries[0].raw_value))
hostname = build_full_hostname(self.device)
if entries[0].raw_value["hostId"] != hostname:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), "<span class='text-warning'>" +
entries[0].raw_value + "</span>"))
else:
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Hostname"), entries[0].raw_value))

def format_lastseen(self):
entries = self.find_entries("timestamp")
Expand Down Expand Up @@ -72,11 +77,17 @@ def format_network(self):
for entry in entries:
interfaces = entry.raw_value["interfaces"]
for interface in interfaces:
if "mac" in interfaces[interface] and interfaces[interface]["mac"] is not None:
if "mac" in interfaces[interface] and "ip" in interfaces[interface] and interfaces[interface]["mac"] is not None:
interfaces[interface]["identifier"] = interface
controllers.append(interfaces[interface])
formatted_controllers = "<br />".join(
["{0}: {1}".format(controller["identifier"], controller["ip"]) for controller in controllers])
formatted_controllers = []
device_addresses = self.device.ipaddress_set.all()
for controller in controllers:
if any(elem.address in controller["ipAddress"] for elem in device_addresses):
formatted_controllers.append("{0} {1}".format(controller["identifier"], controller["ip"]))
else:
formatted_controllers.append(
"{0} <span class='text-warning'>{1}<span>".format(controller["identifier"], controller["ip"]))
self.formatted_entries.append(FormattedDeviceInfoEntry(_("Network"), formatted_controllers))

def format_graphics(self):
Expand Down Expand Up @@ -140,7 +151,7 @@ def get_device_info(self, device):
device_entries = []
for entry in res:
device_entries.append(DeviceInfoEntry(entry["name"], None, entry["value"]))
return PuppetDeviceInfo(device_entries)
return PuppetDeviceInfo(device, device_entries)

def get_software_info(self, device):
software_fact = settings.PUPPETDB_SETTINGS['software_fact']
Expand Down
8 changes: 4 additions & 4 deletions templates/devices/detail/device_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@

<div class="row">
<div class="col-md-4">
<h5 id="details_header">{% trans "Manual Data" %}</h5>
<h5 id="manual_details_header">{% trans "Manual Data" %}</h5>
<table class="table table-bordered table-striped">
<tbody>
<tr>
Expand Down Expand Up @@ -311,7 +311,7 @@ <h5 id="details_header">{% trans "Manual Data" %}</h5>
{% include "devices/detail/device_lending_info.html" %}
</div>
<div class="col-md-5">
<h5 id="details_header">{% trans "Automatic Data" %}</h5>
<h5 id="automatic_details_header">{% trans "Automatic Data" %}</h5>
<table id="details_table" class="table table-bordered table-striped">
<tbody>
</tbody>
Expand Down Expand Up @@ -504,10 +504,10 @@ <h4>{% trans "Installed software from backend providers" %}</h4>
$('#details_table tbody').append(row);
}

var rowCount = $('#details_table tr').length;
var rowCount = $('#automatic_details_table tr').length;
console.log(rowCount);
if (rowCount == 0) {
$('#details_header').remove();
$('#automatic_details_header').remove();
$('#details_table').remove();
}
}
Expand Down

0 comments on commit b5334ef

Please sign in to comment.