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