From 3659215a2f6f085f558d117484cd1445fc681578 Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Fri, 25 Mar 2016 13:52:34 +0100 Subject: [PATCH] refactor(pxe): Move pxe functions to own file --- mdb/models.py | 2 +- mdb/pxe.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ mdb/utils.py | 49 +------------------------------------------------ 3 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 mdb/pxe.py diff --git a/mdb/models.py b/mdb/models.py index 8ac5bac..d988d4d 100644 --- a/mdb/models.py +++ b/mdb/models.py @@ -1,7 +1,7 @@ from django.db import models from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ -from mdb.utils import host_as_pxe_files +from mdb.pxe import host_as_pxe_files from mdb.validators import validate_hostname, validate_macaddr diff --git a/mdb/pxe.py b/mdb/pxe.py new file mode 100644 index 0000000..4f1b242 --- /dev/null +++ b/mdb/pxe.py @@ -0,0 +1,47 @@ +from django.conf import settings +from django.template.loader import render_to_string +import ipaddress + + +def render_pxelinux_cfg(_if, host): + # TODO add fields to os model and make dynamic per host + distro = 'ubuntu/14_04/amd64/alternate/' + kernel = distro + 'linux' + initrd = distro + 'initrd.gz' + preseed_config_url = settings.MDB_PXE_PRESEED_URL + context = { + 'kernel': kernel, + 'initrd': initrd, + 'preseed_config_url': preseed_config_url, + 'host': host, + 'interface': _if + } + return render_to_string('pxelinux.cfg', context=context) + + +def get_pxe_filename(interface, filename_format='mac_addr'): + """ + Mac or IP address based PXE filename + Ref: http://www.syslinux.org/wiki/index.php/PXELINUX#Examples + """ + ip_addr_in_hex = '{:02X}'.format(int(ipaddress.IPv4Address(interface.ip4address.address))) + formats = { + 'ip_addr': ip_addr_in_hex, + 'mac_addr': '01-{}'.format(interface.macaddr.lower().replace(':', '-')) + } + + return formats.get(filename_format, formats['mac_addr']) + + +def host_as_pxe_files(host): + """ Returns a list of filename,content for each host interface with an IPv4 address """ + from mdb.models import Host + assert isinstance(host, Host) + + pxe_files = [] + for _if in host.interface_set.exclude(ip4address__isnull=True): + filename = get_pxe_filename(_if) + content = render_pxelinux_cfg(_if, host) + pxe_files.append((filename, content)) + + return pxe_files diff --git a/mdb/utils.py b/mdb/utils.py index 766d5c1..7f16f3d 100644 --- a/mdb/utils.py +++ b/mdb/utils.py @@ -1,7 +1,4 @@ import datetime -from django.conf import settings -from django.template.loader import render_to_string -import ipaddress import re @@ -34,48 +31,4 @@ def format_domain_serial_and_add_one(serial): else: """ Just return the first serial for today. """ return "%4d%02d%02d%02d" % \ - (today.year, today.month, today.day, 1) - - -def render_pxelinux_cfg(_if, host): - # TODO add fields to os model and make dynamic per host - distro = 'ubuntu/14_04/amd64/alternate/' - kernel = distro + 'linux' - initrd = distro + 'initrd.gz' - preseed_config_url = settings.MDB_PXE_PRESEED_URL - context = { - 'kernel': kernel, - 'initrd': initrd, - 'preseed_config_url': preseed_config_url, - 'host': host, - 'interface': _if - } - return render_to_string('pxelinux.cfg', context=context) - - -def get_pxe_filename(interface, filename_format='mac_addr'): - """ - Mac or IP address based PXE filename - Ref: http://www.syslinux.org/wiki/index.php/PXELINUX#Examples - """ - ip_addr_in_hex = '{:02X}'.format(int(ipaddress.IPv4Address(interface.ip4address.address))) - formats = { - 'ip_addr': ip_addr_in_hex, - 'mac_addr': '01-{}'.format(interface.macaddr.lower().replace(':', '-')) - } - - return formats.get(filename_format, formats['mac_addr']) - - -def host_as_pxe_files(host): - """ Returns a list of filename,content for each host interface with an IPv4 address """ - from mdb.models import Host - assert isinstance(host, Host) - - pxe_files = [] - for _if in host.interface_set.exclude(ip4address__isnull=True): - filename = get_pxe_filename(_if) - content = render_pxelinux_cfg(_if, host) - pxe_files.append((filename, content)) - - return pxe_files + (today.year, today.month, today.day, 1) \ No newline at end of file