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