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