source: trunk/packages/sipb-xen-database/sipb_xen_database/models.py @ 630

Last change on this file since 630 was 630, checked in by ecprice, 16 years ago

Database changes for autoinstalls

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