Esempio n. 1
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
Esempio n. 2
0
File: user.py Progetto: 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())
Esempio n. 3
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)
Esempio n. 4
0
File: user.py Progetto: 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)
Esempio n. 5
0
def interface_edit(interface, name, mac, ips, processor):
    message = u"Edited interface ({}, {}) of host '{}'." \
        .format(interface.mac,
                ', '.join(str(ip.address) for ip in
                          interface.ips),
                interface.host.name)

    if interface.name != name:
        interface.name = name
        message += " New name: '{}'.".format(interface.name)

    if interface.mac != mac:
        interface.mac = mac
        message += " New MAC: {}.".format(interface.mac)

    ips_changed = False

    current_ips = list(ip.address for ip in interface.ips)
    subnets = get_subnets_for_room(interface.host.room)

    new_ips = set(current_ips)

    # IP removed
    for ip in current_ips:
        if ip not in ips:
            session.delete(IP.q.filter_by(address=ip).first())
            ips_changed = True
            new_ips.remove(ip)

    # IP added
    for ip in ips:
        if ip not in current_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))
                ips_changed = True
                new_ips.add(str(ip))

    if ips_changed:
        message += " New IPs: {}.".format(', '.join(str(ip) for ip in
                                                    new_ips))

    log_user_event(author=processor,
                   user=interface.host.owner,
                   message=deferred_gettext(message).to_json())
Esempio n. 6
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())
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
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
Esempio n. 10
0
 def test_missing_ip(self, session, interface, subnet):
     with pytest.raises(IntegrityError), session.begin_nested():
         session.add(IP(interface=interface, subnet=subnet))
Esempio n. 11
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()