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