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