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

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

add and respect 'adminable' column to machines

This is for selectively, temporarily, manually enabling admin mode
for a VM. Where it's not set, admin mode now bypasses quotas and does
nothing else.

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