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