1 from sqlalchemy import *
3 from sqlalchemy.ext.sessioncontext import SessionContext
4 from sqlalchemy.ext.assignmapper import assign_mapper
10 'machine_access_table',
26 meta = DynamicMetaData()
27 ctx = SessionContext(create_session)
29 machine_table = Table('machines', meta,
30 Column('machine_id', Integer, primary_key=True, nullable=False),
31 Column('name', String, nullable=False),
32 Column('description', String, nullable=False),
33 Column('memory', Integer, nullable=False),
34 Column('owner', String, nullable=False),
35 Column('contact', String, nullable=False),
36 Column('uuid', String, nullable=False),
37 Column('administrator', String, nullable=False, default=False),
38 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
39 Column('autorestart', Boolean, nullable=False, default=False),
40 Column('cpus', Integer, nullable=False, default=1),
41 Column('adminable', Boolean, nullable=False, default=False))
43 nic_table = Table('nics', meta,
44 Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
45 Column('mac_addr', String, nullable=False, primary_key=True),
46 Column('ip', String, nullable=False, unique=True),
47 Column('hostname', String, nullable=True))
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'))
55 types_table = Table('types', meta,
56 Column('type_id', String, primary_key=True, nullable=False),
57 Column('description', String, nullable=False),
58 Column('hvm', Boolean, nullable=False),
59 Column('apic', Boolean, nullable=False),
60 Column('acpi', Boolean, nullable=False),
61 Column('pae', Boolean, nullable=False))
63 cdroms_table = Table('cdroms', meta,
64 Column('cdrom_id', String, primary_key=True, nullable=False),
65 Column('description', String, nullable=False))
67 autoinstalls_table = Table('autoinstalls', meta,
68 Column('autoinstall_id', String, primary_key=True, nullable=False),
69 Column('description', String, nullable=False),
70 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
71 Column('distribution', String, nullable=False),
72 Column('mirror', String, nullable=False))
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),
77 PrimaryKeyConstraint('machine_id', 'user', ondelete='CASCADE'))
79 class Machine(object):
81 return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
83 class MachineAccess(object):
85 return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
89 return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
93 return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
97 return "<Type %s: %s>" % (self.type_id, self.description)
101 return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
103 class Autoinstall(object):
105 return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
107 assign_mapper(ctx, Machine, machine_table,
108 properties={'nics': relation(NIC, backref="machine", lazy=False),
109 'disks': relation(Disk, backref="machine", lazy=False),
110 'type': relation(Type, lazy=False),
111 'acl': relation(MachineAccess, backref="machine", lazy=False, passive_deletes=True, cascade="all, delete-orphan")});
112 assign_mapper(ctx, MachineAccess, machine_access_table)
113 assign_mapper(ctx, NIC, nic_table)
114 assign_mapper(ctx, Disk, disk_table)
115 assign_mapper(ctx, Type, types_table)
116 assign_mapper(ctx, CDROM, cdroms_table)
117 assign_mapper(ctx, Autoinstall, autoinstalls_table)
120 """Clear sqlalchemy's cache.
122 This _seems_ to be the way; it works, but the docs don't mention
123 it. Why is this so obscure?"""