summaryrefslogtreecommitdiffstats
path: root/validator/src/validation_tool/src/dhcp.py
diff options
context:
space:
mode:
authorTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
committerTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
commit4b269fba0ca273dfa3acf44c9f5490f01e0c3d87 (patch)
tree909b5262b46306eb78327e535f2e14120e45116a /validator/src/validation_tool/src/dhcp.py
parentd46ab54583a6c20bfa5bea581f512474f488e788 (diff)
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 <tbramwell@linuxfoundation.org>
Diffstat (limited to 'validator/src/validation_tool/src/dhcp.py')
-rw-r--r--validator/src/validation_tool/src/dhcp.py102
1 files changed, 102 insertions, 0 deletions
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")