From 4b269fba0ca273dfa3acf44c9f5490f01e0c3d87 Mon Sep 17 00:00:00 2001 From: Trevor Bramwell Date: Fri, 22 Sep 2017 12:23:36 -0700 Subject: Rename pharos-dashboard and pharos-validator As subdirectories of the pharos-tools repo, there is little need to keep the pharos prefix. Change-Id: Ica3d79411f409df638647300036c0664183c2725 Signed-off-by: Trevor Bramwell --- validator/src/validation_tool/src/dhcp.py | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 validator/src/validation_tool/src/dhcp.py (limited to 'validator/src/validation_tool/src/dhcp.py') diff --git a/validator/src/validation_tool/src/dhcp.py b/validator/src/validation_tool/src/dhcp.py new file mode 100644 index 0000000..26c42f8 --- /dev/null +++ b/validator/src/validation_tool/src/dhcp.py @@ -0,0 +1,102 @@ +############################################################################## +# Copyright (c) 2015 Todd Gaunt and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import yaml +import netifaces +import subprocess +import copy +import re +import os +import logging + +from pharosvalidator.specification import * +from pharosvalidator import util + +init_cmd = ["systemctl", "start", "dhcpd.service"] + +def gen_dhcpd_file(dhcpdfile, nodes, network): + """Generates and associates incremental ip addresses to + MAC addresses according to restrictions placed by network + configuration file. Writes all of this out in dhcpd.conf format""" + logger = logging.getLogger(__name__) + logger.info("Generating dhcpfile...") + + header = "default-lease-time 86400;\n\ + max-lease-time 604800;\n\ + max-lease-time 604800;\n\ + \n\ + allow booting;\n\ + authoritative;\n\ + \n" + + # Skip this network if it is disabled + if network.enabled == False: + logger.info("Admin network is disabled, please change the configuration to \"enabled\" if you would like this test to run") + quit() + + # Not explicitly in the cofiguration file + broadcastaddr = "0.0.0.0" + next_server = "0.0.0.0" + + ip_range = util.gen_ip_range(network.cidr, [network.installer_ip], network.usable_ip_range.minimum, \ + network.usable_ip_range.maximum) + + tab = ' ' + subnetconf = "subnet {} netmask {} {{\n".format(network.subnet, network.netmask)\ + + tab+"range {} {};\n".format(network.usable_ip_range.minimum, network.usable_ip_range.maximum)\ + + tab+"option broadcast-address {};\n".format(broadcastaddr)\ + + tab+'filename "pxelinux.0";\n'\ + + tab+"next-server {};\n".format(next_server) + + # For now no static addresses are assigned + """ + static_addrs = [] + for node in nodes: + # Skip the node if it doesn't have a name or mac address specified + if not node.name or not node.mac_address: + continue + + if node.ipmi_addr in ip_range: + ip_range.remove(node.ipmi_addr) + + static_line = "host {node} {{ hardware ethernet {ipmi_mac}; fixed-address {ip_addr}; }}\n".format\ + (node=node.name, ipmi_mac=node.mac_address, ip_addr=ip_range[0]) + ip_range = ip_range[1::] # Remove the assigned ip address + static_addrs.append(static_line) + + # Now add all statically assigned ip addresses + for addr in static_addrs: + subnetconf += tab+addr + """ + + subnetconf += "}\n" # Just the closing bracket + + # The final text to be written out to a file + dhcpdtext = header + subnetconf + + with open(dhcpdfile, "w+") as fd: + logger.info("Writing out dhcpd file to {}".format(dhcpdfile)) + fd.write(dhcpdtext) + + return dhcpdtext + +def start_server(): + logger = logging.getLogger(__name__) + global init_cmd + cmd = init_cmd + with open(os.devnull, 'w') as fn: + status = subprocess.Popen(cmd, stdout=fn, stderr=fn).wait() + if int(status) != 0: + logger.error("Could not bring up dhcpd server") + else: + logger.info("Dhcp server brought up") + return status + +if __name__ == "__main__": + split("inventory.yaml", "eth0") -- cgit 1.2.3-korg