[13] | 1 | from sqlalchemy import * |
---|
[991] | 2 | from sqlalchemy import orm |
---|
[946] | 3 | from sqlalchemy.orm import create_session, relation |
---|
[13] | 4 | |
---|
[18] | 5 | from sqlalchemy.ext.sessioncontext import SessionContext |
---|
| 6 | from sqlalchemy.ext.assignmapper import assign_mapper |
---|
| 7 | |
---|
[77] | 8 | __all__ = ['meta', |
---|
[991] | 9 | 'session', |
---|
[296] | 10 | 'clear_cache', |
---|
[77] | 11 | 'machine_table', |
---|
[243] | 12 | 'machine_access_table', |
---|
[77] | 13 | 'nic_table', |
---|
| 14 | 'disk_table', |
---|
| 15 | 'types_table', |
---|
[108] | 16 | 'cdroms_table', |
---|
[1361] | 17 | 'mirrors_table', |
---|
[443] | 18 | 'autoinstalls_table', |
---|
[2132] | 19 | 'owners_table', |
---|
[77] | 20 | 'Machine', |
---|
[243] | 21 | 'MachineAccess', |
---|
[77] | 22 | 'NIC', |
---|
| 23 | 'Disk', |
---|
[108] | 24 | 'Type', |
---|
[443] | 25 | 'CDROM', |
---|
[1361] | 26 | 'Mirror', |
---|
[443] | 27 | 'Autoinstall', |
---|
[2132] | 28 | 'Owner', |
---|
[874] | 29 | 'or_', |
---|
[443] | 30 | ] |
---|
[77] | 31 | |
---|
[946] | 32 | meta = ThreadLocalMetaData() |
---|
[1014] | 33 | session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False)) |
---|
[13] | 34 | |
---|
| 35 | machine_table = Table('machines', meta, |
---|
| 36 | Column('machine_id', Integer, primary_key=True, nullable=False), |
---|
| 37 | Column('name', String, nullable=False), |
---|
[609] | 38 | Column('description', String, nullable=False), |
---|
[13] | 39 | Column('memory', Integer, nullable=False), |
---|
| 40 | Column('owner', String, nullable=False), |
---|
| 41 | Column('contact', String, nullable=False), |
---|
| 42 | Column('uuid', String, nullable=False), |
---|
[1708] | 43 | Column('administrator', String, nullable=True, default=None), |
---|
[13] | 44 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
| 45 | Column('autorestart', Boolean, nullable=False, default=False), |
---|
[874] | 46 | Column('cpus', Integer, nullable=False, default=1), |
---|
| 47 | Column('adminable', Boolean, nullable=False, default=False)) |
---|
[13] | 48 | |
---|
| 49 | nic_table = Table('nics', meta, |
---|
| 50 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True), |
---|
[106] | 51 | Column('mac_addr', String, nullable=False, primary_key=True), |
---|
[107] | 52 | Column('ip', String, nullable=False, unique=True), |
---|
[106] | 53 | Column('hostname', String, nullable=True)) |
---|
[13] | 54 | |
---|
| 55 | disk_table = Table('disks', meta, |
---|
| 56 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False), |
---|
| 57 | Column('guest_device_name', String, nullable=False), |
---|
| 58 | Column('size', Integer, nullable=False), |
---|
| 59 | PrimaryKeyConstraint('machine_id', 'guest_device_name')) |
---|
| 60 | |
---|
| 61 | types_table = Table('types', meta, |
---|
| 62 | Column('type_id', String, primary_key=True, nullable=False), |
---|
| 63 | Column('description', String, nullable=False), |
---|
[64] | 64 | Column('hvm', Boolean, nullable=False), |
---|
[13] | 65 | Column('apic', Boolean, nullable=False), |
---|
| 66 | Column('acpi', Boolean, nullable=False), |
---|
| 67 | Column('pae', Boolean, nullable=False)) |
---|
| 68 | |
---|
[1247] | 69 | mirrors_table = Table('mirrors', meta, |
---|
| 70 | Column('mirror_id', String, primary_key=True, nullable=False), |
---|
| 71 | Column('uri_prefix', String, nullable=False)) |
---|
| 72 | |
---|
[108] | 73 | cdroms_table = Table('cdroms', meta, |
---|
| 74 | Column('cdrom_id', String, primary_key=True, nullable=False), |
---|
[1247] | 75 | Column('description', String, nullable=False), |
---|
[1250] | 76 | Column('mirror_id', String, ForeignKey('mirrors.mirror_id')), |
---|
[1247] | 77 | Column('uri_suffix', String)) |
---|
[18] | 78 | |
---|
[443] | 79 | autoinstalls_table = Table('autoinstalls', meta, |
---|
| 80 | Column('autoinstall_id', String, primary_key=True, nullable=False), |
---|
| 81 | Column('description', String, nullable=False), |
---|
[630] | 82 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
| 83 | Column('distribution', String, nullable=False), |
---|
[1694] | 84 | Column('mirror', String, nullable=False), |
---|
| 85 | Column('arch', String, nullable=False)) |
---|
[443] | 86 | |
---|
[2132] | 87 | owners_table = Table('owners', meta, |
---|
| 88 | Column('owner_id', String, primary_key=True, nullable=False), |
---|
| 89 | Column('ram_quota_total', Integer, nullable=True), |
---|
| 90 | Column('ram_quota_single', Integer, nullable=True)) |
---|
| 91 | |
---|
[238] | 92 | machine_access_table = Table('machine_access', meta, |
---|
[1174] | 93 | Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True), |
---|
[238] | 94 | Column('user', String, nullable=False, index=True), |
---|
[1174] | 95 | PrimaryKeyConstraint('machine_id', 'user')) |
---|
[108] | 96 | |
---|
[18] | 97 | class Machine(object): |
---|
[204] | 98 | def __repr__(self): |
---|
| 99 | return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner) |
---|
[18] | 100 | |
---|
[238] | 101 | class MachineAccess(object): |
---|
| 102 | def __repr__(self): |
---|
| 103 | return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user) |
---|
| 104 | |
---|
[18] | 105 | class NIC(object): |
---|
[204] | 106 | def __repr__(self): |
---|
| 107 | return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname) |
---|
[18] | 108 | |
---|
| 109 | class Disk(object): |
---|
[204] | 110 | def __repr__(self): |
---|
| 111 | return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size) |
---|
[18] | 112 | |
---|
| 113 | class Type(object): |
---|
[204] | 114 | def __repr__(self): |
---|
| 115 | return "<Type %s: %s>" % (self.type_id, self.description) |
---|
[18] | 116 | |
---|
[1247] | 117 | class Mirror(object): |
---|
| 118 | def __repr__(self): |
---|
| 119 | return "<Mirror %s>" % (self.mirror_id) |
---|
| 120 | |
---|
[108] | 121 | class CDROM(object): |
---|
[204] | 122 | def __repr__(self): |
---|
| 123 | return "<CDROM %s: %s>" % (self.cdrom_id, self.description) |
---|
[18] | 124 | |
---|
[443] | 125 | class Autoinstall(object): |
---|
| 126 | def __repr__(self): |
---|
| 127 | return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id) |
---|
| 128 | |
---|
[2132] | 129 | from owner import Owner |
---|
| 130 | |
---|
[991] | 131 | session.mapper(Machine, machine_table, |
---|
[1714] | 132 | properties={'nics': relation(NIC, backref="machine"), |
---|
| 133 | 'disks': relation(Disk, backref="machine"), |
---|
| 134 | 'type': relation(Type), |
---|
| 135 | 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")}); |
---|
[991] | 136 | session.mapper(MachineAccess, machine_access_table) |
---|
| 137 | session.mapper(NIC, nic_table) |
---|
| 138 | session.mapper(Disk, disk_table) |
---|
| 139 | session.mapper(Type, types_table) |
---|
[1247] | 140 | session.mapper(Mirror, mirrors_table) |
---|
[1363] | 141 | session.mapper(CDROM, cdroms_table, |
---|
| 142 | properties={'mirror': relation(Mirror, backref="cdroms")}) |
---|
[991] | 143 | session.mapper(Autoinstall, autoinstalls_table) |
---|
[2132] | 144 | session.mapper(Owner, owners_table) |
---|
[287] | 145 | |
---|
| 146 | def clear_cache(): |
---|
[1136] | 147 | """Clear sqlalchemy's cache |
---|
| 148 | """ |
---|
[287] | 149 | |
---|
[1136] | 150 | session.clear() |
---|