1 from sqlalchemy import *
2 from sqlalchemy import orm
3 from sqlalchemy.orm import create_session, relation
5 from sqlalchemy.ext.sessioncontext import SessionContext
6 from sqlalchemy.ext.assignmapper import assign_mapper
8 from invirt.database import record
9 from invirt.database.owner import Owner
15 'machine_access_table',
35 meta = ThreadLocalMetaData()
36 session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False))
38 machine_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))
52 nic_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 Column('reusable', Boolean, nullable=False, default=True))
59 disk_table = Table('disks', meta,
60 Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False),
61 Column('guest_device_name', String, nullable=False),
62 Column('size', Integer, nullable=False),
63 PrimaryKeyConstraint('machine_id', 'guest_device_name'))
65 types_table = Table('types', meta,
66 Column('type_id', String, primary_key=True, nullable=False),
67 Column('description', String, nullable=False),
68 Column('hvm', Boolean, nullable=False),
69 Column('apic', Boolean, nullable=False),
70 Column('acpi', Boolean, nullable=False),
71 Column('pae', Boolean, nullable=False))
73 mirrors_table = Table('mirrors', meta,
74 Column('mirror_id', String, primary_key=True, nullable=False),
75 Column('uri_prefix', String, nullable=False))
77 cdroms_table = Table('cdroms', meta,
78 Column('cdrom_id', String, primary_key=True, nullable=False),
79 Column('description', String, nullable=False),
80 Column('mirror_id', String, ForeignKey('mirrors.mirror_id')),
81 Column('uri_suffix', String))
83 autoinstalls_table = Table('autoinstalls', meta,
84 Column('autoinstall_id', String, primary_key=True, nullable=False),
85 Column('description', String, nullable=False),
86 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
87 Column('distribution', String, nullable=False),
88 Column('mirror', String, nullable=False),
89 Column('arch', String, nullable=False))
91 owners_table = Table('owners', meta,
92 Column('owner_id', String, primary_key=True, nullable=False),
93 Column('ram_quota_total', Integer, nullable=True, default=None),
94 Column('ram_quota_single', Integer, nullable=True, default=None),
95 Column('disk_quota_total', Integer, nullable=True, default=None),
96 Column('disk_quota_single', Integer, nullable=True, default=None),
97 Column('vms_quota_total', Integer, nullable=True, default=None),
98 Column('vms_quota_active', Integer, nullable=True, default=None))
100 machine_access_table = Table('machine_access', meta,
101 Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True),
102 Column('user', String, nullable=False, index=True),
103 PrimaryKeyConstraint('machine_id', 'user'))
105 class Machine(record.Record):
106 _identity_field = 'name'
108 class MachineAccess(record.Record):
111 class NIC(record.Record):
114 class Disk(record.Record):
117 class Type(record.Record):
118 _identity_field = 'type_id'
120 class Mirror(record.Record):
121 _identity_field = 'mirror_id'
123 class CDROM(record.Record):
124 _identity_field = 'cdrom_id'
126 class Autoinstall(record.Record):
127 _identity_field = 'autoinstall_id'
129 session.mapper(Machine, machine_table,
130 properties={'nics': relation(NIC, backref="machine"),
131 'disks': relation(Disk, backref="machine"),
132 'type': relation(Type),
133 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")});
134 session.mapper(MachineAccess, machine_access_table)
135 session.mapper(NIC, nic_table)
136 session.mapper(Disk, disk_table)
137 session.mapper(Type, types_table)
138 session.mapper(Mirror, mirrors_table)
139 session.mapper(CDROM, cdroms_table,
140 properties={'mirror': relation(Mirror, backref="cdroms")})
141 session.mapper(Autoinstall, autoinstalls_table)
142 session.mapper(Owner, owners_table)
145 """Clear sqlalchemy's cache