summaryrefslogtreecommitdiffstats
path: root/pharos-validator/src/validation_tool/src/dhcp.py
blob: 26c42f84c297ce6e349e4392212c78ee21c3d923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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")