def handle(self, *args, **options): if not args: raise CommandError('What do you want? fill or dump or dhcpconf?') if args[0] == 'fill': addresses = Address.objects.fill_pool() for addr in addresses: print('{0}={1}'.format(addr.mac, addr.ip)) print('\nInserted {0} addresses.'.format(len(addresses))) elif args[0] == 'dump': for addr in Address.objects.all(): print('{0}\t{1}'.format(addr.mac, addr.ip)) elif args[0] == 'dhcpconf': subnet_ip, cidr = settings.VM_IP_BLOCK.split('/') netmask = int_to_ip(cidr_to_netmask(int(cidr))) print('subnet {0} netmask {1} {{'.format(subnet_ip, netmask)) for addr in Address.objects.all(): print('\thost vm{0} {{'.format(addr.pk)) print('\t\thardware ethernet {0};'.format(addr.mac)) print('\t\tfixed-address {0};'.format(addr.ip)) subnet_mask = int_to_ip(cidr_to_netmask(settings.VM_NET_SIZE)) print('\t\toption subnet-mask {0};'.format(subnet_mask)) router_ip = int_to_ip(ip_to_int(addr.ip) - 1) print('\t\toption routers {0};'.format(router_ip)) print('\t}') print('}')
def handle(self, *args, **options): if not args: raise CommandError('What do you want? fill or dump or dhcpconf?') if args[0] == 'fill': addresses = Address.objects.fill_pool() for addr in addresses: print('{0}={1}'.format(addr.mac, addr.ip)) print('\nInserted {0} addresses.'.format(len(addresses))) elif args[0] == 'dump': for addr in Address.objects.all(): print('{0}\t{1}'.format(addr.mac, addr.ip)) elif args[0] == 'dhcpconf': subnet_ip, cidr = settings.VM_IP_BLOCK.split('/') netmask = int_to_ip(cidr_to_netmask(int(cidr))) print('subnet {0} netmask {1} {{'.format(subnet_ip, netmask)) for addr in Address.objects.all(): print('\thost vm{0} {{'.format(addr.pk)) print('\t\thardware ethernet {0};'.format(addr.mac)) print('\t\tfixed-address {0};'.format(addr.ip)) subnet_mask = int_to_ip(cidr_to_netmask(settings.VM_NET_SIZE)) print('\t\toption subnet-mask {0};'.format(subnet_mask)) router_ip = int_to_ip(ip_to_int(addr.ip) - 1) print('\t\toption routers {0};'.format(router_ip)) print('\t}') print('}')
def fill_pool(self): """Insert all addresses into the pool.""" def random_16(): return ''.join(random.choice('0123456789abcdef') for _i in (0, 1)) new_addresses = [] oui = getattr(settings, 'VM_MAC_OUI', '52:54:00') if not hasattr(settings, 'VM_IP_BLOCK'): raise NetworkError('Please set VM_IP_BLOCK in settings.py') if not hasattr(settings, 'VM_NET_SIZE'): raise NetworkError('Please set VM_NET_SIZE in settings.py') vm_nets = iterate_nets(settings.VM_IP_BLOCK, settings.VM_NET_SIZE) for net_ip_int in vm_nets: mac = ':'.join((oui, random_16(), random_16(), random_16())) ip = int_to_ip(net_ip_int + 2) try: new_addresses.append(Address.objects.create(mac=mac, ip=ip)) except IntegrityError: transaction.rollback() else: transaction.commit() return new_addresses
def fill_pool(self): """Insert all addresses into the pool.""" def random_16(): return ''.join(random.choice('0123456789abcdef') for _i in (0, 1)) new_addresses = [] oui = getattr(settings, 'VM_MAC_OUI', '52:54:00') if not hasattr(settings, 'VM_IP_BLOCK'): raise NetworkError('Please set VM_IP_BLOCK in settings.py') if not hasattr(settings, 'VM_NET_SIZE'): raise NetworkError('Please set VM_NET_SIZE in settings.py') vm_nets = iterate_nets(settings.VM_IP_BLOCK, settings.VM_NET_SIZE) for net_ip_int in vm_nets: mac = ':'.join((oui, random_16(), random_16(), random_16())) ip = int_to_ip(net_ip_int + 2) try: new_addresses.append(Address.objects.create(mac=mac, ip=ip)) except IntegrityError: transaction.rollback() else: transaction.commit() return new_addresses