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