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