#!/usr/bin/env python
"""dnsmasq DHCP support"""
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

import sys
import logging
import datetime
import phoenix
from phoenix.system import System
from phoenix.node import Node

class DnsmasqDhcp(object):
    dhcptype = "dnsmasq"

    @classmethod
    def update_dhcp_reservations(cls):
        curtime = datetime.datetime.now()
        timestamp = curtime.strftime('%Y-%m-%d %H:%M:%S')
        output = [ '# this file was generated by Phoenix at %s' % timestamp ]
        for nodename,node in sorted(Node.nodes.items()):
            if 'interfaces' in node:
                for ifacename, iface in node['interfaces'].items():
                    try:
                        usedhcp = iface['dhcp']
                    except KeyError:
                        usedhcp = False
                    if not usedhcp:
                        continue
                    if 'mac' not in iface:
                        logging.warn("No MAC address for %s", nodename)
                        continue

                    hostname = iface['hostname'] if 'hostname' in iface else nodename
                    output.append("%s\t%s" % (iface['mac'], hostname))
        with open('/etc/ethers', 'w') as ethersfile:
            ethersfile.write('\n'.join(output))
            ethersfile.write('\n')

    @classmethod
    def get_dhcp_conf(cls):
        return "# not yet implemented"

#!/usr/bin/env python
"""dnsmasq DHCP support"""
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

import sys
import logging
import datetime
import phoenix
from phoenix.system import System
from phoenix.node import Node

class DnsmasqDhcp(object):
    dhcptype = "dnsmasq"

    @classmethod
    def update_dhcp_reservations(cls):
        curtime = datetime.datetime.now()
        timestamp = curtime.strftime('%Y-%m-%d %H:%M:%S')
        output = [ '# this file was generated by Phoenix at %s' % timestamp ]
        for nodename,node in sorted(Node.nodes.items()):
            if 'interfaces' in node:
                for ifacename, iface in node['interfaces'].items():
                    try:
                        usedhcp = iface['dhcp']
                    except KeyError:
                        usedhcp = False
                    if not usedhcp:
                        continue
                    if 'mac' not in iface:
                        logging.warn("No MAC address for %s", nodename)
                        continue

                    hostname = iface['hostname'] if 'hostname' in iface else nodename
                    output.append("%s\t%s" % (iface['mac'], hostname))
        with open('/etc/ethers', 'w') as ethersfile:
            ethersfile.write('\n'.join(output))
            ethersfile.write('\n')

    @classmethod
    def get_dhcp_conf(cls):
        return "# not yet implemented"

#!/usr/bin/env python dnsmasq DHCP support # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4