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