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