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, unique=True), |
---|
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('traceback', String, nullable=True, default=None), |
---|
115 | Column('inserted_at', DateTime, nullable=False, default=datetime.datetime.utcnow)) |
---|
116 | |
---|
117 | machine_access_table = Table('machine_access', meta, |
---|
118 | Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True), |
---|
119 | Column('user', String, nullable=False, index=True), |
---|
120 | PrimaryKeyConstraint('machine_id', 'user')) |
---|
121 | |
---|
122 | admins_table = Table('admins', meta, |
---|
123 | Column('user', String, nullable=False, index=True, primary_key=True)) |
---|
124 | |
---|
125 | class Machine(record.Record): |
---|
126 | _identity_field = 'name' |
---|
127 | |
---|
128 | class MachineAccess(record.Record): |
---|
129 | pass |
---|
130 | |
---|
131 | class NIC(record.Record): |
---|
132 | pass |
---|
133 | |
---|
134 | class Disk(record.Record): |
---|
135 | pass |
---|
136 | |
---|
137 | class Type(record.Record): |
---|
138 | _identity_field = 'type_id' |
---|
139 | |
---|
140 | class Mirror(record.Record): |
---|
141 | _identity_field = 'mirror_id' |
---|
142 | |
---|
143 | class CDROM(record.Record): |
---|
144 | _identity_field = 'cdrom_id' |
---|
145 | |
---|
146 | class Autoinstall(record.Record): |
---|
147 | _identity_field = 'autoinstall_id' |
---|
148 | |
---|
149 | class Admin(record.Record): |
---|
150 | _identity_field = 'user' |
---|
151 | |
---|
152 | class Build(record.Record): |
---|
153 | _identity_field = 'build_id' |
---|
154 | |
---|
155 | from invirt.database.owner import Owner |
---|
156 | |
---|
157 | session.mapper(Machine, machine_table, |
---|
158 | properties={'nics': relation(NIC, backref="machine"), |
---|
159 | 'disks': relation(Disk, backref="machine"), |
---|
160 | 'type': relation(Type), |
---|
161 | 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")}); |
---|
162 | session.mapper(MachineAccess, machine_access_table) |
---|
163 | session.mapper(NIC, nic_table) |
---|
164 | session.mapper(Disk, disk_table) |
---|
165 | session.mapper(Type, types_table) |
---|
166 | session.mapper(Mirror, mirrors_table) |
---|
167 | session.mapper(CDROM, cdroms_table, |
---|
168 | properties={'mirror': relation(Mirror, backref="cdroms")}) |
---|
169 | session.mapper(Autoinstall, autoinstalls_table) |
---|
170 | session.mapper(Owner, owners_table) |
---|
171 | session.mapper(Admin, admins_table) |
---|
172 | session.mapper(Build, builds_table) |
---|
173 | |
---|
174 | def clear_cache(): |
---|
175 | """Clear sqlalchemy's cache |
---|
176 | """ |
---|
177 | |
---|
178 | session.clear() |
---|