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

Last change on this file since 946 was 946, checked in by price, 16 years ago

port sipb-xen-database to sqlalchemy-0.4

Apparently they like breaking old interfaces (e.g. DynamicMetaData?,
sqlalchemy.create_session, sqlalchemy.relation) when they add new ones.
Also letting other old interfaces (e.g. meta.connect) bitrot even
while still present.

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