1 | from sqlalchemy import * |
---|
2 | |
---|
3 | from sqlalchemy.ext.sessioncontext import SessionContext |
---|
4 | from sqlalchemy.ext.assignmapper import assign_mapper |
---|
5 | |
---|
6 | __all__ = ['meta', |
---|
7 | 'ctx', |
---|
8 | 'clear_cache', |
---|
9 | 'machine_table', |
---|
10 | 'machine_access_table', |
---|
11 | 'nic_table', |
---|
12 | 'disk_table', |
---|
13 | 'types_table', |
---|
14 | 'cdroms_table', |
---|
15 | 'autoinstalls_table', |
---|
16 | 'Machine', |
---|
17 | 'MachineAccess', |
---|
18 | 'NIC', |
---|
19 | 'Disk', |
---|
20 | 'Type', |
---|
21 | 'CDROM', |
---|
22 | 'Autoinstall', |
---|
23 | 'or_', |
---|
24 | ] |
---|
25 | |
---|
26 | meta = DynamicMetaData() |
---|
27 | ctx = SessionContext(create_session) |
---|
28 | |
---|
29 | machine_table = Table('machines', meta, |
---|
30 | Column('machine_id', Integer, primary_key=True, nullable=False), |
---|
31 | Column('name', String, nullable=False), |
---|
32 | Column('description', String, nullable=False), |
---|
33 | Column('memory', Integer, nullable=False), |
---|
34 | Column('owner', String, nullable=False), |
---|
35 | Column('contact', String, nullable=False), |
---|
36 | Column('uuid', String, nullable=False), |
---|
37 | Column('administrator', String, nullable=False, default=False), |
---|
38 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
39 | Column('autorestart', Boolean, nullable=False, default=False), |
---|
40 | Column('cpus', Integer, nullable=False, default=1), |
---|
41 | Column('adminable', Boolean, nullable=False, default=False)) |
---|
42 | |
---|
43 | nic_table = Table('nics', meta, |
---|
44 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True), |
---|
45 | Column('mac_addr', String, nullable=False, primary_key=True), |
---|
46 | Column('ip', String, nullable=False, unique=True), |
---|
47 | Column('hostname', String, nullable=True)) |
---|
48 | |
---|
49 | disk_table = Table('disks', meta, |
---|
50 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False), |
---|
51 | Column('guest_device_name', String, nullable=False), |
---|
52 | Column('size', Integer, nullable=False), |
---|
53 | PrimaryKeyConstraint('machine_id', 'guest_device_name')) |
---|
54 | |
---|
55 | types_table = Table('types', meta, |
---|
56 | Column('type_id', String, primary_key=True, nullable=False), |
---|
57 | Column('description', String, nullable=False), |
---|
58 | Column('hvm', Boolean, nullable=False), |
---|
59 | Column('apic', Boolean, nullable=False), |
---|
60 | Column('acpi', Boolean, nullable=False), |
---|
61 | Column('pae', Boolean, nullable=False)) |
---|
62 | |
---|
63 | cdroms_table = Table('cdroms', meta, |
---|
64 | Column('cdrom_id', String, primary_key=True, nullable=False), |
---|
65 | Column('description', String, nullable=False)) |
---|
66 | |
---|
67 | autoinstalls_table = Table('autoinstalls', meta, |
---|
68 | Column('autoinstall_id', String, primary_key=True, nullable=False), |
---|
69 | Column('description', String, nullable=False), |
---|
70 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
71 | Column('distribution', String, nullable=False), |
---|
72 | Column('mirror', String, nullable=False)) |
---|
73 | |
---|
74 | machine_access_table = Table('machine_access', meta, |
---|
75 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False, index=True), |
---|
76 | Column('user', String, nullable=False, index=True), |
---|
77 | PrimaryKeyConstraint('machine_id', 'user', ondelete='CASCADE')) |
---|
78 | |
---|
79 | class Machine(object): |
---|
80 | def __repr__(self): |
---|
81 | return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner) |
---|
82 | |
---|
83 | class MachineAccess(object): |
---|
84 | def __repr__(self): |
---|
85 | return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user) |
---|
86 | |
---|
87 | class NIC(object): |
---|
88 | def __repr__(self): |
---|
89 | return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname) |
---|
90 | |
---|
91 | class Disk(object): |
---|
92 | def __repr__(self): |
---|
93 | return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size) |
---|
94 | |
---|
95 | class Type(object): |
---|
96 | def __repr__(self): |
---|
97 | return "<Type %s: %s>" % (self.type_id, self.description) |
---|
98 | |
---|
99 | class CDROM(object): |
---|
100 | def __repr__(self): |
---|
101 | return "<CDROM %s: %s>" % (self.cdrom_id, self.description) |
---|
102 | |
---|
103 | class Autoinstall(object): |
---|
104 | def __repr__(self): |
---|
105 | return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id) |
---|
106 | |
---|
107 | assign_mapper(ctx, Machine, machine_table, |
---|
108 | properties={'nics': relation(NIC, backref="machine", lazy=False), |
---|
109 | 'disks': relation(Disk, backref="machine", lazy=False), |
---|
110 | 'type': relation(Type, lazy=False), |
---|
111 | 'acl': relation(MachineAccess, backref="machine", lazy=False, passive_deletes=True, cascade="all, delete-orphan")}); |
---|
112 | assign_mapper(ctx, MachineAccess, machine_access_table) |
---|
113 | assign_mapper(ctx, NIC, nic_table) |
---|
114 | assign_mapper(ctx, Disk, disk_table) |
---|
115 | assign_mapper(ctx, Type, types_table) |
---|
116 | assign_mapper(ctx, CDROM, cdroms_table) |
---|
117 | assign_mapper(ctx, Autoinstall, autoinstalls_table) |
---|
118 | |
---|
119 | def clear_cache(): |
---|
120 | """Clear sqlalchemy's cache. |
---|
121 | |
---|
122 | This _seems_ to be the way; it works, but the docs don't mention |
---|
123 | it. Why is this so obscure?""" |
---|
124 | |
---|
125 | ctx.registry.clear() |
---|