1 from sqlalchemy import *
2 from sqlalchemy.orm import create_session, relation
4 from sqlalchemy.ext.sessioncontext import SessionContext
5 from sqlalchemy.ext.assignmapper import assign_mapper
11 'machine_access_table',
27 meta = ThreadLocalMetaData()
28 ctx = SessionContext(create_session)
30 machine_table = Table('machines', meta,
31 Column('machine_id', Integer, primary_key=True, nullable=False),
32 Column('name', String, nullable=False),
33 Column('description', String, nullable=False),
34 Column('memory', Integer, nullable=False),
35 Column('owner', String, nullable=False),
36 Column('contact', String, nullable=False),
37 Column('uuid', String, nullable=False),
38 Column('administrator', String, nullable=False, default=False),
39 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
40 Column('autorestart', Boolean, nullable=False, default=False),
41 Column('cpus', Integer, nullable=False, default=1),
42 Column('adminable', Boolean, nullable=False, default=False))
44 nic_table = Table('nics', meta,
45 Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
46 Column('mac_addr', String, nullable=False, primary_key=True),
47 Column('ip', String, nullable=False, unique=True),
48 Column('hostname', String, nullable=True))
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'))
56 types_table = Table('types', meta,
57 Column('type_id', String, primary_key=True, nullable=False),
58 Column('description', String, nullable=False),
59 Column('hvm', Boolean, nullable=False),
60 Column('apic', Boolean, nullable=False),
61 Column('acpi', Boolean, nullable=False),
62 Column('pae', Boolean, nullable=False))
64 cdroms_table = Table('cdroms', meta,
65 Column('cdrom_id', String, primary_key=True, nullable=False),
66 Column('description', String, nullable=False))
68 autoinstalls_table = Table('autoinstalls', meta,
69 Column('autoinstall_id', String, primary_key=True, nullable=False),
70 Column('description', String, nullable=False),
71 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
72 Column('distribution', String, nullable=False),
73 Column('mirror', String, nullable=False))
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),
78 PrimaryKeyConstraint('machine_id', 'user', ondelete='CASCADE'))
80 class Machine(object):
82 return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
84 class MachineAccess(object):
86 return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
90 return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
94 return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
98 return "<Type %s: %s>" % (self.type_id, self.description)
102 return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
104 class Autoinstall(object):
106 return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
108 assign_mapper(ctx, Machine, machine_table,
109 properties={'nics': relation(NIC, backref="machine", lazy=False),
110 'disks': relation(Disk, backref="machine", lazy=False),
111 'type': relation(Type, lazy=False),
112 'acl': relation(MachineAccess, backref="machine", lazy=False, passive_deletes=True, cascade="all, delete-orphan")});
113 assign_mapper(ctx, MachineAccess, machine_access_table)
114 assign_mapper(ctx, NIC, nic_table)
115 assign_mapper(ctx, Disk, disk_table)
116 assign_mapper(ctx, Type, types_table)
117 assign_mapper(ctx, CDROM, cdroms_table)
118 assign_mapper(ctx, Autoinstall, autoinstalls_table)
121 """Clear sqlalchemy's cache.
123 This _seems_ to be the way; it works, but the docs don't mention
124 it. Why is this so obscure?"""