Example #1
0
File: app.py Project: rmoe/illmatic
import flask
import flask.ext.restless
from illmatic.db import db
from illmatic.db import init_db, drop_db
from illmatic.db.models import Interface, IPAddress, IPRange, Network

app = flask.Flask(__name__)
app.config['DEBUG'] = True

drop_db()
init_db()

manager = flask.ext.restless.APIManager(app, session=db())

manager.create_api(Interface, methods=['GET', 'POST', 'PUT', 'DELETE'])
manager.create_api(IPAddress, methods=['GET', 'POST', 'PUT', 'DELETE'])
manager.create_api(IPRange, methods=['GET', 'POST', 'PUT', 'DELETE'])
manager.create_api(Network, methods=['GET', 'POST', 'PUT', 'DELETE'])

app.run()
Example #2
0
def import_stuff():
    ranges = {}
    for network in ndb().query(nmodels.NetworkGroup).all():
        inet = models.Network(
            name=network.name,
            cidr=network.cidr,
            gateway=network.gateway,
            vlan=network.vlan_start,
            meta=json.dumps(network.meta)
        )
        db().add(inet)
        db().commit()

        for ipr in network.ip_ranges:
            new_ipr = models.IPRange(
                network_id=inet.id,
                first=ipr.first,
                last=ipr.last
            )
            db().add(new_ipr)
            db().commit()

            ranges[str(new_ipr.id)] = IPRange(ipr.first, ipr.last)


    slaves = {}
    for iface in ndb().query(nmodels.NodeNICInterface).all():
        new_iface = models.Interface(
            name=iface.name,
            mac=iface.mac,
            node_id=iface.node_id,
            if_type='ether',
            interface_properties=json.dumps(iface.interface_properties),
            driver=iface.driver,
            bus_info=iface.bus_info,
            offloading_modes=json.dumps(iface.offloading_modes),
            provider='linux'
        )
        db().add(new_iface)
        db().commit()
        if iface.parent_id:
            slaves.setdefault(iface.parent_id, [])
            slaves[iface.parent_id].append(new_iface)

    for iface in ndb().query(nmodels.NodeBondInterface).all():
        slave_ifs = slaves[iface.id]
        new_iface = models.Interface(
            name=iface.name,
            mac=rand_mac(),
            node_id=iface.node_id,
            if_type='bond',
            interface_properties=json.dumps(iface.bond_properties),
            provider='linux',
            slaves=slave_ifs
        )

    for ip in ndb().query(nmodels.IPAddr).all():
        iface = None
        meta = ''
        ipr_id = None

        interface_id = None
        if ip.node:
            for i in ip.node_data.interfaces + ip.node_data.bond_interfaces:
                if ip.network in [n['id'] for n in i.assigned_networks]:
                    interface_id = \
                    db().query(models.Interface).filter_by(node_id=str(ip.node),
                            name=i.name).first().id

        if ip.vip_type:
            meta = 'vip_type={0}'.format(ip.vip_type)

        for range_id, ipr in ranges.items():
            if ip.ip_addr in ipr:
                ipr_id=range_id
                break

        new_ip = models.IPAddress(
            interface_id=interface_id,
            ip_range_id=ipr_id,
            address=ip.ip_addr,
            meta=meta
        )
        db().add(new_ip)
    db().commit()