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