Ejemplo n.º 1
0
    def test_correct_subnet_and_ip(self, session, subnet, interface):
        ip_address, _ = get_free_ip((subnet, ))

        with session.begin_nested():
            session.add(IP(interface=interface, address=ip_address, subnet=subnet))

        ip_address, _ = get_free_ip((subnet, ))
        with session.begin_nested():
            session.add(IP(address=ip_address, subnet=subnet, interface=interface))

        with session.begin_nested():
            IP.q.filter(IP.interface == interface).delete()
Ejemplo n.º 2
0
def interface_create(host, name, mac, ips, processor):
    interface = Interface(host=host, mac=mac, name=name)

    session.add(interface)

    subnets = get_subnets_for_room(interface.host.room)

    if ips is None:
        ip, _ = get_free_ip(subnets)
        ips = {ip}

    # IP added
    for ip in ips:
        subnet = next(
            iter([subnet for subnet in subnets if (ip in subnet.address)]),
            None)

        if subnet is not None:
            session.add(IP(interface=interface, address=ip, subnet=subnet))

    message = deferred_gettext(
        u"Created interface ({}, {}) with name '{}' for host '{}'.".format(
            interface.mac, ', '.join(str(ip.address) for ip in interface.ips),
            interface.name, interface.host.name))
    log_user_event(author=processor,
                   user=host.owner,
                   message=message.to_json())

    return interface
Ejemplo n.º 3
0
Archivo: user.py Proyecto: JuKu/pycroft
def migrate_user_host(host, new_room, processor):
    """
    Migrate a UserHost to a new room and if necessary to a new subnet.
    If the host changes subnet, it will get a new IP address.
    :param Host host: Host to be migrated
    :param Room new_room: new room of the host
    :param User processor: User processing the migration
    :return:
    """
    old_room = host.room
    host.room = new_room
    subnets = [
        subnet for p in new_room.connected_patch_ports
        for vlan in p.switch_port.default_vlans for subnet in vlan.subnets
    ]
    if old_room.building_id == new_room.building_id:
        return
    for interface in host.interfaces:
        old_ips = tuple(ip for ip in interface.ips)
        for old_ip in old_ips:
            ip_address, subnet = get_free_ip(subnets)
            new_ip = IP(interface=interface, address=ip_address, subnet=subnet)
            session.session.add(new_ip)

            old_address = old_ip.address
            session.session.delete(old_ip)

            message = deferred_gettext(
                u"Changed IP from {old_ip} to {new_ip}.").format(
                    old_ip=str(old_address), new_ip=str(new_ip))
            log_user_event(author=processor,
                           user=host.owner,
                           message=message.to_json())
Ejemplo n.º 4
0
    def test_missing_subnet(self):
        ip_address, _ = get_free_ip((self.subnet, ))
        ip = host.IP(interface=self.interface, address=ip_address)

        with self.assertRaises(IntegrityError):
            self.session.add(ip)
            self.session.commit()
Ejemplo n.º 5
0
 def pick_ip(self):
     ip, _ = get_free_ip((self.subnet, ))
     addr = host.IP(interface=self.interface,
                    address=ip,
                    subnet=self.subnet)
     self.session.add(addr)
     self.session.commit()
     return addr
Ejemplo n.º 6
0
def setup_ipv4_networking(host):
    """Add suitable ips for every interface of a host"""
    subnets = get_subnets_for_room(host.room)

    for interface in host.interfaces:
        ip_address, subnet = get_free_ip(subnets)
        new_ip = IP(interface=interface, address=ip_address, subnet=subnet)
        session.session.add(new_ip)
Ejemplo n.º 7
0
    def test_correct_subnet_and_ip(self):
        ip_address, _ = get_free_ip((self.subnet, ))

        ip = host.IP(interface=self.interface,
                     address=ip_address,
                     subnet=self.subnet)
        self.session.add(ip)
        self.session.commit()

        ip_address, _ = get_free_ip((self.subnet, ))
        ip = host.IP(address=ip_address,
                     subnet=self.subnet,
                     interface=self.interface)
        self.session.add(ip)
        self.session.commit()

        host.IP.q.filter(host.IP.interface == self.interface).delete()
        self.session.commit()
Ejemplo n.º 8
0
    def test_get_free_ip_next_to_full(self):
        first_net = self.subnets[0]
        second_net = self.subnets[1]
        subnets = (first_net, second_net)
        host = self.host

        interface = host.interfaces[0]
        self.fill_net(first_net, interface)
        self.session.refresh(first_net)
        self.assertRaises(SubnetFullException, get_free_ip, (first_net,))
        try:
            get_free_ip(subnets)
        except SubnetFullException:
            self.fail("Subnets should have free IPs.")
        self.fill_net(subnets[1], interface)
        self.assertRaises(SubnetFullException, get_free_ip, subnets)

        self.session.delete(host)
        self.session.commit()
Ejemplo n.º 9
0
    def test_0010_correct_subnet_and_ip(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()

        ip_address, _ = get_free_ip((subnet, ))

        ip = host.IP(interface=interface)
        ip.address = ip_address
        ip.subnet = subnet
        session.session.add(ip)
        session.session.commit()

        interface = host.Interface.q.first()
        ip_address, _ = get_free_ip((subnet, ))
        ip = host.IP(address=ip_address, subnet=subnet, interface=interface)
        session.session.add(ip)
        session.session.commit()

        host.IP.q.filter(host.IP.interface == interface).delete()
        session.session.commit()
Ejemplo n.º 10
0
    def test_0010_correct_subnet_and_ip(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()

        ip_address, _ = get_free_ip((subnet, ))

        ip = host.IP(interface=interface)
        ip.address = ip_address
        ip.subnet = subnet
        session.session.add(ip)
        session.session.commit()

        interface = host.Interface.q.first()
        ip_address, _ = get_free_ip((subnet, ))
        ip = host.IP(address=ip_address, subnet=subnet, interface=interface)
        session.session.add(ip)
        session.session.commit()

        host.IP.q.filter(host.IP.interface == interface).delete()
        session.session.commit()
Ejemplo n.º 11
0
Archivo: user.py Proyecto: JuKu/pycroft
def setup_ipv4_networking(host):
    """Add suitable ips for every interface of a host"""
    subnets = [
        s for p in host.room.connected_patch_ports
        for v in p.switch_port.default_vlans for s in v.subnets
        if s.address.version == 4
    ]
    for interface in host.interfaces:
        ip_address, subnet = get_free_ip(subnets)
        new_ip = IP(interface=interface, address=ip_address, subnet=subnet)
        session.session.add(new_ip)
Ejemplo n.º 12
0
    def test_0020_missing_subnet(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()

        ip_address, _ = get_free_ip((subnet, ))
        ip = host.IP(interface=interface)
        ip.address = ip_address

        def commit():
            session.session.add(ip)
            session.session.commit()
        self.assertRaises(IntegrityError, commit)
Ejemplo n.º 13
0
    def test_0020_missing_subnet(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()

        ip_address, _ = get_free_ip((subnet, ))
        ip = host.IP(interface=interface)
        ip.address = ip_address

        def commit():
            session.session.add(ip)
            session.session.commit()
        self.assertRaises(IntegrityError, commit)
Ejemplo n.º 14
0
    def test_0040_delete_subnet(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()
        ip, _ = get_free_ip((subnet, ))
        ip_addr = host.IP(interface=interface, address=ip, subnet=subnet)

        session.session.add(ip_addr)
        session.session.commit()

        with self.assertRaises(IntegrityError):
            ip_addr.subnet = None
            self.assertIsNone(ip_addr.subnet)
            session.session.commit()
Ejemplo n.º 15
0
    def test_0030_get_free_ip_next_to_full(self):
        first_net = Subnet.q.filter_by(
            address=SubnetData.user_ipv4.address).one()
        second_net = Subnet.q.filter_by(
            address=SubnetData.dummy_subnet2.address).one()
        subnets = (first_net, second_net)
        owner = User.q.filter_by(login=HostData.dummy.owner.login).one()
        host = Host.q.filter_by(owner=owner).one()

        interface = host.interfaces[0]
        self.fill_net(first_net, interface)
        session.session.refresh(first_net)
        self.assertRaises(SubnetFullException, get_free_ip, (first_net,))
        try:
            get_free_ip(subnets)
        except SubnetFullException:
            self.fail("Subnets should have free IPs.")
        self.fill_net(subnets[1], interface)
        self.assertRaises(SubnetFullException, get_free_ip, subnets)

        session.session.delete(host)
        session.session.commit()
Ejemplo n.º 16
0
    def test_0040_delete_subnet(self):
        subnet = Subnet.q.first()
        interface = host.Interface.q.first()
        ip, _ = get_free_ip((subnet, ))
        ip_addr = host.IP(interface=interface, address=ip, subnet=subnet)

        session.session.add(ip_addr)
        session.session.commit()

        with self.assertRaises(IntegrityError):
            ip_addr.subnet = None
            self.assertIsNone(ip_addr.subnet)
            session.session.commit()
Ejemplo n.º 17
0
    def test_0030_get_free_ip_next_to_full(self):
        first_net = Subnet.q.filter_by(
            address=SubnetData.user_ipv4.address).one()
        second_net = Subnet.q.filter_by(
            address=SubnetData.dummy_subnet2.address).one()
        subnets = (first_net, second_net)
        owner = User.q.filter_by(login=HostData.dummy.owner.login).one()
        host = Host.q.filter_by(owner=owner).one()

        interface = host.interfaces[0]
        self.fill_net(first_net, interface)
        session.session.refresh(first_net)
        self.assertRaises(SubnetFullException, get_free_ip, (first_net, ))
        try:
            get_free_ip(subnets)
        except SubnetFullException:
            self.fail("Subnets should have free IPs.")
        self.fill_net(subnets[1], interface)
        self.assertRaises(SubnetFullException, get_free_ip, subnets)

        session.session.delete(host)
        session.session.commit()
Ejemplo n.º 18
0
    def test_wrong_subnet(self, interface, subnet, subnet2):
        ip_address, _ = get_free_ip((subnet, ))

        ip = IP(interface=interface, address=ip_address)

        with pytest.raises(ValueError):
            ip.subnet = subnet2

        ip = IP(interface=interface, subnet=subnet2)

        with pytest.raises(ValueError):
            ip.address = ip_address

        with pytest.raises(ValueError):
            IP(interface=interface, subnet=subnet2, address=ip_address)
Ejemplo n.º 19
0
    def test_0040_wrong_subnet(self):
        subnets = Subnet.q.all()
        interface = host.Interface.q.first()
        ip_address, _ = get_free_ip((subnets[0], ))

        ip = host.IP(interface=interface, address=ip_address)

        with self.assertRaises(ValueError):
            ip.subnet = subnets[1]

        ip = host.IP(interface=interface, subnet=subnets[1])

        with self.assertRaises(ValueError):
            ip.address = ip_address

        with self.assertRaises(ValueError):
            host.IP(interface=interface, subnet=subnets[1], address=ip_address)
Ejemplo n.º 20
0
    def test_0040_wrong_subnet(self):
        subnets = Subnet.q.all()
        interface = host.Interface.q.first()
        ip_address, _ = get_free_ip((subnets[0], ))

        ip = host.IP(interface=interface, address=ip_address)

        with self.assertRaises(ValueError):
            ip.subnet = subnets[1]

        ip = host.IP(interface=interface, subnet=subnets[1])

        with self.assertRaises(ValueError):
            ip.address = ip_address

        with self.assertRaises(ValueError):
            host.IP(interface=interface, subnet=subnets[1], address=ip_address)
Ejemplo n.º 21
0
def migrate_user_host(host, new_room, processor):
    """
    Migrate a UserHost to a new room and if necessary to a new subnet.
    If the host changes subnet, it will get a new IP address.
    :param Host host: Host to be migrated
    :param Room new_room: new room of the host
    :param User processor: User processing the migration
    :return:
    """
    old_room = host.room
    host.room = new_room

    subnets_old = get_subnets_for_room(old_room)
    subnets = get_subnets_for_room(new_room)

    if subnets_old != subnets:
        for interface in host.interfaces:
            old_ips = tuple(ip for ip in interface.ips)
            for old_ip in old_ips:
                ip_address, subnet = get_free_ip(subnets)
                new_ip = IP(interface=interface,
                            address=ip_address,
                            subnet=subnet)
                session.session.add(new_ip)

                old_address = old_ip.address
                session.session.delete(old_ip)

                message = deferred_gettext(
                    u"Changed IP of {mac} from {old_ip} to {new_ip}.").format(
                        old_ip=str(old_address),
                        new_ip=str(new_ip.address),
                        mac=interface.mac)
                log_user_event(author=processor,
                               user=host.owner,
                               message=message.to_json())

    message = deferred_gettext(
        u"Moved host '{name}' from {room_old} to {room_new}.".format(
            name=host.name,
            room_old=old_room.short_name,
            room_new=new_room.short_name))

    log_user_event(author=processor,
                   user=host.owner,
                   message=message.to_json())
Ejemplo n.º 22
0
    def test_wrong_subnet(self):
        ip_address, _ = get_free_ip((self.subnets[0], ))

        ip = host.IP(interface=self.interface, address=ip_address)

        with self.assertRaises(ValueError):
            ip.subnet = self.subnets[1]

        ip = host.IP(interface=self.interface, subnet=self.subnets[1])

        with self.assertRaises(ValueError):
            ip.address = ip_address

        with self.assertRaises(ValueError):
            host.IP(interface=self.interface,
                    subnet=self.subnets[1],
                    address=ip_address)
Ejemplo n.º 23
0
 def test_0010_get_free_ip_simple(self):
     subnets = Subnet.q.all()
     for subnet in subnets:
         ip, subnet = get_free_ip((subnet,))
         self.assertIn(ip, subnet.address)
Ejemplo n.º 24
0
 def test_get_free_ip_simple(self):
     for subnet in self.subnets:
         ip, subnet = get_free_ip((subnet,))
         self.assertIn(ip, subnet.address)
Ejemplo n.º 25
0
 def test_missing_subnet(self, session, interface, subnet):
     ip_address, _ = get_free_ip((subnet, ))
     ip = IP(interface=interface, address=ip_address)
     with pytest.raises(IntegrityError), session.begin_nested():
         session.add(ip)
Ejemplo n.º 26
0
 def ip_addr(self, class_session, subnet, interface):
     ip, _ = get_free_ip((subnet,))
     addr = IP(interface=interface, address=ip, subnet=subnet)
     class_session.add(addr)
     return addr
Ejemplo n.º 27
0
 def fill_net(self, net, interface):
     for num in range(0, self.calculate_usable_ips(net)):
         ip, _ = get_free_ip((net,))
         session.session.add(IP(address=ip, subnet=net,
                                interface=interface))
     session.session.commit()
Ejemplo n.º 28
0
 def fill_net(self, net, interface):
     for num in range(0, self.calculate_usable_ips(net)):
         ip, _ = get_free_ip((net, ))
         session.session.add(IP(address=ip, subnet=net,
                                interface=interface))
     session.session.commit()
Ejemplo n.º 29
0
 def test_0010_get_free_ip_simple(self):
     subnets = Subnet.q.all()
     for subnet in subnets:
         ip, subnet = get_free_ip((subnet, ))
         self.assertIn(ip, subnet.address)