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