def loadDhcpConf(args):
    #
    # Setup the DHCP conf helper and related variables
    #
    args.dhcpConf = DhcpConfHelper(Config.DHCP_CONF)
    args.dhcpSharedNet = args.dhcpConf.getRootEntry().getFirstChild(DhcpConfEntry.Type.Shared_Network)
    args.dhcpGroup = args.dhcpConf.getRootEntry().findChild(DhcpConfEntry.Type.Group)
    def handleInstallationCompleted(self, hostname):
        dhcpConf = DhcpConfHelper(self._dhcpConfFilename)
        dhcpGroup = dhcpConf.getGroup()
        success = False

        try:
            if dhcpGroup.removeChild(DhcpConfEntry.Type.Host, hostname):
                print("DHCP configuration for host '%s' removed." % hostname)
                if dhcpConf.save():
                    print("\nChanges saved in %s\n" % dhcpConf.getFilename())
                    restartDHCP()
            else:
                print("DHCP configuration for host '%s' not found." % hostname)
            success = True
            print("Processed notification from '%s' successfully" % hostname)
            self.returnAckJson()
        except:
            print("Failure to handle event from: %s" % hostname)
            traceback.print_exc(file=sys.stdout)
            self.send_error(400, "Invalid parameter")

        # Check if there are any more server to wait for.
        serversList = dhcpGroup.getChildren(DhcpConfEntry.Type.Host)

        # Check if there are any servers left to wait for.
        if serversList is None or len(serversList) == 0:
            print("No more hosts to wait for.  Stopping the listener.")
            self.shutdownHandler()

        # Return the status of the processing
        return success
def generateHostEntry(cfg, ip, device, machineConf):
    """
    Generate a host entry instance for the provided device.
    """
    vars = {
        'server_hostname': device.hostname,
        'server_mac_address': device.mac,
        'server_ip': ip,
        'server_image': machineConf['image']
    }

    hostText = cfg.generateDhcpHostEntryText(vars)
    return DhcpConfHelper().readText(hostText).getRootEntry().getFirstChild(DhcpConfEntry.Type.Host)
def generateSubnetEntry(cfg, bootServerIP, subnet):
    """
    Generate a subnet entry instance for the provided subnet.
    """
    vars = {
        'bootServerIP': bootServerIP,
        'subnet_ip': subnet.network,
        'subnet_netmask': subnet.netmask,
        'subnet_broadcast': subnet.broadcast,
        'subnet_gateway': subnet.gateway
    }
    subnetText = cfg.generateDhcpSubnetEntryText(vars)
    return DhcpConfHelper().readText(subnetText).getRootEntry().getFirstChild(DhcpConfEntry.Type.Subnet)
def preProcessArgs(args):
    #
    # First check for the boot server IP.  If the option not specified,
    # then get the current IP on first NIC of current OS
    #
    if 'bootServerIP' not in args or args.bootServerIP is None:
        args.bootServerIP = get_ip()

    #
    # Create the config instance which will be used to generate the SFTP and files.
    #
    args.bootServerListenPort = 8888
    args.cfg = Config(args.config,args.bootServerIP)
    args.valid_tags = args.cfg.getMachineTags()

    #
    # Setup tags param (if --tag used)
    #
    if 'tag' in args and args.tag: #args.tag:
        if ',' in args.tag:
            args.tags = args.tag.split(',')

        elif args.tag == 'all':
            args.tags = args.valid_tags
        else:
            args.tags = [args.tag]
        # Once tags are set, verify they are valid tags.
        for tag in args.tags:
            if tag not in args.valid_tags:
                print("\nERROR: invalid tag specified.  Valid values are: %s\n" % ', '.join(args.valid_tags))
                sys.exit(1)
    #
    # Setup hostnames param (if --hostname used)
    #
    if 'hostname' in args and args.hostname: #args.hostname:
        if ',' in args.hostname:
            args.hostnames = args.hostname.split(',')
        else:
            args.hostnames = [args.hostname]

    #
    # First retrieve the mentioned devices and also any existing host entries in the dhcp config
    #
    args.slHelper = SoftLayerHelper()
    args.vlan = args.slHelper.getVlan(args.cfg.vlanIdOrName, subnetType=Subnet.Type.Portable, addressSpace=Subnet.AddressSpace.Private)

    if not args.vlan:
        print("\nERROR: Cannot find matching VLAN based on the configuration.")
        sys.exit(1)

    #
    # Setup the DHCP conf helper and related variables
    #
    args.dhcpConf = DhcpConfHelper('/etc/dhcp/dhcpd.conf')
    args.dhcpSharedNet = args.dhcpConf.getRootEntry().getFirstChild(DhcpConfEntry.Type.Shared_Network)
    args.dhcpGroup = args.dhcpConf.getRootEntry().findChild(DhcpConfEntry.Type.Group)

    if args.dhcpSharedNet is None or args.dhcpGroup is None:
        print("\nERROR: The dhcpd.conf file does have the structure expected. Run the configuration on the bootserver again.")
        sys.exit(1)

    args.hosts = args.dhcpGroup.getChildren(DhcpConfEntry.Type.Host)
    return args