source: trunk/packages/invirt-database/python/database/models.py @ 2132

Last change on this file since 2132 was 2132, checked in by iannucci, 15 years ago

RAM quotas at remctl; RAM quota exception script, table, and usage in -web and -remote-create; /etc/nocreate support

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