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

Last change on this file since 2200 was 2200, checked in by price, 15 years ago

fix more imports EIBTI-style

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