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
14 'machine_access_table',
34 meta = ThreadLocalMetaData()
35 session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False))
37 machine_table = Table('machines', meta,
38 Column('machine_id', Integer, primary_key=True, nullable=False),
39 Column('name', String, nullable=False),
40 Column('description', String, nullable=False),
41 Column('memory', Integer, nullable=False),
42 Column('owner', String, nullable=False),
43 Column('contact', String, nullable=False),
44 Column('uuid', String, nullable=False),
45 Column('administrator', String, nullable=True, default=None),
46 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
47 Column('autorestart', Boolean, nullable=False, default=False),
48 Column('cpus', Integer, nullable=False, default=1),
49 Column('adminable', Boolean, nullable=False, default=False))
51 nic_table = Table('nics', meta,
52 Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
53 Column('mac_addr', String, nullable=False, primary_key=True),
54 Column('ip', String, nullable=False, unique=True),
55 Column('hostname', String, nullable=True),
56 Column('reusable', Boolean, nullable=False, default=True))
58 disk_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'))
64 types_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))
72 mirrors_table = Table('mirrors', meta,
73 Column('mirror_id', String, primary_key=True, nullable=False),
74 Column('uri_prefix', String, nullable=False))
76 cdroms_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))
82 autoinstalls_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))
90 owners_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))
99 machine_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'))
104 admins_table = Table('admins', meta,
105 Column('user', String, nullable=False, index=True, primary_key=True))
107 class Machine(record.Record):
108 _identity_field = 'name'
110 class MachineAccess(record.Record):
113 class NIC(record.Record):
116 class Disk(record.Record):
119 class Type(record.Record):
120 _identity_field = 'type_id'
122 class Mirror(record.Record):
123 _identity_field = 'mirror_id'
125 class CDROM(record.Record):
126 _identity_field = 'cdrom_id'
128 class Autoinstall(record.Record):
129 _identity_field = 'autoinstall_id'
131 class Admin(record.Record):
132 _identity_field = 'user'
134 from invirt.database.owner import Owner
136 session.mapper(Machine, machine_table,
137 properties={'nics': relation(NIC, backref="machine"),
138 'disks': relation(Disk, backref="machine"),
139 'type': relation(Type),
140 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")});
141 session.mapper(MachineAccess, machine_access_table)
142 session.mapper(NIC, nic_table)
143 session.mapper(Disk, disk_table)
144 session.mapper(Type, types_table)
145 session.mapper(Mirror, mirrors_table)
146 session.mapper(CDROM, cdroms_table,
147 properties={'mirror': relation(Mirror, backref="cdroms")})
148 session.mapper(Autoinstall, autoinstalls_table)
149 session.mapper(Owner, owners_table)
150 session.mapper(Admin, admins_table)
153 """Clear sqlalchemy's cache