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
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())
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)
def interface_edit(interface_id): interface = Interface.q.get(interface_id) if interface is None: flash(u"Interface existiert nicht.", 'error') abort(404) subnets = get_subnets_for_room(interface.host.room) current_ips = list(ip.address for ip in interface.ips) form = InterfaceForm(obj=interface) unused_ips = [ip for ips in get_unused_ips(subnets).values() for ip in ips] form.ips.choices = [(str(ip), str(ip)) for ip in current_ips + unused_ips] unique_mac_error = None if not form.is_submitted(): form.ips.process_data(ip for ip in current_ips) else: if form.mac.data != interface.mac: unique_mac_error = validate_unique_mac(form, form.mac) if unique_mac_error: form.validate() form.mac.errors.append(unique_mac_error) if not unique_mac_error and form.validate_on_submit(): ips = set([IPv4Address(ip) for ip in form.ips.data]) _, success = web_execute(lib_host.interface_edit, "Interface erfolgreich bearbeitet.", interface, form.name.data, form.mac.data, ips, current_user) if success: session.session.commit() return redirect( url_for('user.user_show', user_id=interface.host.owner_id, _anchor='hosts')) form_args = { 'form': form, 'cancel_to': url_for('user.user_show', user_id=interface.host.owner_id) } return render_template('generic_form.html', page_title="Interface editieren", form_args=form_args)
def interface_create(host_id): host = Host.q.get(host_id) if host is None: flash(u"Host existiert nicht.", 'error') abort(404) subnets = get_subnets_for_room(host.room) form = InterfaceForm() unused_ips = [ip for ips in get_unused_ips(subnets).values() for ip in ips] form.ips.choices = [(str(ip), str(ip)) for ip in unused_ips] unique_mac_error = None if not form.is_submitted(): form.ips.process_data([next(iter(unused_ips), None)]) else: unique_mac_error = validate_unique_mac(form, form.mac) if unique_mac_error: form.validate() form.mac.errors.append(unique_mac_error) if not unique_mac_error and form.validate_on_submit(): ips = set([IPv4Address(ip) for ip in form.ips.data]) _, success = web_execute(lib_host.interface_create, "Interface erfolgreich erstellt.", host, form.name.data, form.mac.data, ips, current_user) if success: session.session.commit() return redirect( url_for('user.user_show', user_id=host.owner.id, _anchor='hosts')) form_args = { 'form': form, 'cancel_to': url_for('user.user_show', user_id=host.owner.id) } return render_template('generic_form.html', page_title="Interface erstellen", form_args=form_args)
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())
flash("Interface erfolgreich gelöscht.", 'success') return redirect( url_for('user.user_show', user_id=interface.host.owner_id, _anchor='hosts')) @bp.route('/interface/<int:interface_id>/edit', methods=['GET', 'POST']) @access.require('hosts_change') def interface_edit(interface_id): if (interface := Interface.get(interface_id)) is None: flash("Interface existiert nicht.", 'error') abort(404) subnets = get_subnets_for_room(interface.host.room) current_ips = [ip.address for ip in interface.ips] form = InterfaceForm(obj=interface) form.meta.current_mac = interface.mac unused_ips = [ip for ips in get_unused_ips(subnets).values() for ip in ips] form.ips.choices = [(str(ip), str(ip)) for ip in current_ips + unused_ips] def default_response(): form_args = { 'form': form, 'cancel_to': url_for('user.user_show', user_id=interface.host.owner_id) } return render_template('generic_form.html',