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