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