source: trunk/packages/sipb-xen-database/python/database/models.py @ 1136

Last change on this file since 1136 was 1136, checked in by broder, 16 years ago

invirt.database.clear_cache should actually do something

File size: 4.9 KB
RevLine 
[13]1from sqlalchemy import *
[991]2from sqlalchemy import orm
[946]3from sqlalchemy.orm import create_session, relation
[13]4
[18]5from sqlalchemy.ext.sessioncontext import SessionContext
6from sqlalchemy.ext.assignmapper import assign_mapper
7
[77]8__all__ = ['meta',
[991]9           'session',
[296]10           'clear_cache',
[77]11           'machine_table',
[243]12           'machine_access_table',
[77]13           'nic_table',
14           'disk_table',
15           'types_table',
[108]16           'cdroms_table',
[443]17           'autoinstalls_table',
[77]18           'Machine',
[243]19           'MachineAccess',
[77]20           'NIC',
21           'Disk',
[108]22           'Type',
[443]23           'CDROM',
24           'Autoinstall',
[874]25           'or_',
[443]26           ]
[77]27
[946]28meta = ThreadLocalMetaData()
[1014]29session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False))
[13]30
31machine_table = Table('machines', meta,
32       Column('machine_id', Integer, primary_key=True, nullable=False),
33       Column('name', String, nullable=False),
[609]34       Column('description', String, nullable=False),
[13]35       Column('memory', Integer, nullable=False),
36       Column('owner', String, nullable=False),
37       Column('contact', String, nullable=False),
38       Column('uuid', String, nullable=False),
[168]39       Column('administrator', String, nullable=False, default=False),
[13]40       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
41       Column('autorestart', Boolean, nullable=False, default=False),
[874]42       Column('cpus', Integer, nullable=False, default=1),
43       Column('adminable', Boolean, nullable=False, default=False))
[13]44
45nic_table = Table('nics', meta,
46       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
[106]47       Column('mac_addr', String, nullable=False, primary_key=True),
[107]48       Column('ip', String, nullable=False, unique=True),
[106]49       Column('hostname', String, nullable=True))
[13]50
51disk_table = Table('disks', meta,
52       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False),
53       Column('guest_device_name', String, nullable=False),
54       Column('size', Integer, nullable=False),
55       PrimaryKeyConstraint('machine_id', 'guest_device_name'))
56
57types_table = Table('types', meta,
58       Column('type_id', String, primary_key=True, nullable=False),
59       Column('description', String, nullable=False),
[64]60       Column('hvm', Boolean, nullable=False),
[13]61       Column('apic', Boolean, nullable=False),
62       Column('acpi', Boolean, nullable=False),
63       Column('pae', Boolean, nullable=False))
64
[108]65cdroms_table = Table('cdroms', meta,
66       Column('cdrom_id', String, primary_key=True, nullable=False),
67       Column('description', String, nullable=False))
[18]68
[443]69autoinstalls_table = Table('autoinstalls', meta,
70       Column('autoinstall_id', String, primary_key=True, nullable=False),
71       Column('description', String, nullable=False),
[630]72       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
73       Column('distribution', String, nullable=False),
74       Column('mirror', String, nullable=False))
[443]75
[238]76machine_access_table = Table('machine_access', meta,
77       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False, index=True),
78       Column('user', String, nullable=False, index=True),
[592]79       PrimaryKeyConstraint('machine_id', 'user', ondelete='CASCADE'))
[108]80
[18]81class Machine(object):
[204]82    def __repr__(self):
83        return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
[18]84
[238]85class MachineAccess(object):
86    def __repr__(self):
87        return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
88
[18]89class NIC(object):
[204]90    def __repr__(self):
91        return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
[18]92
93class Disk(object):
[204]94    def __repr__(self):
95        return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
[18]96
97class Type(object):
[204]98    def __repr__(self):
99        return "<Type %s: %s>" % (self.type_id, self.description)
[18]100
[108]101class CDROM(object):
[204]102    def __repr__(self):
103        return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
[18]104
[443]105class Autoinstall(object):
106    def __repr__(self):
107        return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
108
[991]109session.mapper(Machine, machine_table,
[555]110              properties={'nics': relation(NIC, backref="machine", lazy=False),
111                          'disks': relation(Disk, backref="machine", lazy=False),
112                          'type': relation(Type, lazy=False),
[592]113                          'acl': relation(MachineAccess, backref="machine", lazy=False, passive_deletes=True, cascade="all, delete-orphan")});
[991]114session.mapper(MachineAccess, machine_access_table)
115session.mapper(NIC, nic_table)
116session.mapper(Disk, disk_table)
117session.mapper(Type, types_table)
118session.mapper(CDROM, cdroms_table)
119session.mapper(Autoinstall, autoinstalls_table)
[287]120
121def clear_cache():
[1136]122    """Clear sqlalchemy's cache
123    """
[287]124
[1136]125    session.clear()
Note: See TracBrowser for help on using the repository browser.