1 from sqlalchemy import *
2 from sqlalchemy import orm
3 from sqlalchemy.orm import create_session, relation
5 from sqlalchemy.ext.sessioncontext import SessionContext
6 from sqlalchemy.ext.assignmapper import assign_mapper
10 from invirt.database import record
16 'machine_access_table',
40 meta = ThreadLocalMetaData()
41 session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False))
43 machine_table = Table('machines', meta,
44 Column('machine_id', Integer, primary_key=True, nullable=False),
45 Column('name', String, nullable=False, unique=True),
46 Column('description', String, nullable=False),
47 Column('memory', Integer, nullable=False),
48 Column('owner', String, nullable=False),
49 Column('contact', String, nullable=False),
50 Column('uuid', String, nullable=False),
51 Column('administrator', String, nullable=True, default=None),
52 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
53 Column('autorestart', Boolean, nullable=False, default=False),
54 Column('cpus', Integer, nullable=False, default=1),
55 Column('adminable', Boolean, nullable=False, default=False))
57 nic_table = Table('nics', meta,
58 Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
59 Column('mac_addr', String, nullable=False, primary_key=True),
60 Column('ip', String, nullable=False, unique=True),
61 Column('hostname', String, nullable=True),
62 Column('reusable', Boolean, nullable=False, default=True))
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'))
70 types_table = Table('types', meta,
71 Column('type_id', String, primary_key=True, nullable=False),
72 Column('description', String, nullable=False),
73 Column('hvm', Boolean, nullable=False),
74 Column('apic', Boolean, nullable=False),
75 Column('acpi', Boolean, nullable=False),
76 Column('pae', Boolean, nullable=False))
78 mirrors_table = Table('mirrors', meta,
79 Column('mirror_id', String, primary_key=True, nullable=False),
80 Column('uri_prefix', String, nullable=False))
82 cdroms_table = Table('cdroms', meta,
83 Column('cdrom_id', String, primary_key=True, nullable=False),
84 Column('description', String, nullable=False),
85 Column('mirror_id', String, ForeignKey('mirrors.mirror_id')),
86 Column('uri_suffix', String))
88 autoinstalls_table = Table('autoinstalls', meta,
89 Column('autoinstall_id', String, primary_key=True, nullable=False),
90 Column('description', String, nullable=False),
91 Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
92 Column('distribution', String, nullable=False),
93 Column('mirror', String, nullable=False),
94 Column('arch', String, nullable=False))
96 owners_table = Table('owners', meta,
97 Column('owner_id', String, primary_key=True, nullable=False),
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))
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),
114 Column('traceback', String, nullable=True, default=None),
115 Column('inserted_at', DateTime, nullable=False, default=datetime.datetime.utcnow))
117 machine_access_table = Table('machine_access', meta,
118 Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True),
119 Column('user', String, nullable=False, index=True),
120 PrimaryKeyConstraint('machine_id', 'user'))
122 admins_table = Table('admins', meta,
123 Column('user', String, nullable=False, index=True, primary_key=True))
125 class Machine(record.Record):
126 _identity_field = 'name'
128 class MachineAccess(record.Record):
131 class NIC(record.Record):
134 class Disk(record.Record):
137 class Type(record.Record):
138 _identity_field = 'type_id'
140 class Mirror(record.Record):
141 _identity_field = 'mirror_id'
143 class CDROM(record.Record):
144 _identity_field = 'cdrom_id'
146 class Autoinstall(record.Record):
147 _identity_field = 'autoinstall_id'
149 class Admin(record.Record):
150 _identity_field = 'user'
152 class Build(record.Record):
153 _identity_field = 'build_id'
155 from invirt.database.owner import Owner
157 session.mapper(Machine, machine_table,
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")});
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)
166 session.mapper(Mirror, mirrors_table)
167 session.mapper(CDROM, cdroms_table,
168 properties={'mirror': relation(Mirror, backref="cdroms")})
169 session.mapper(Autoinstall, autoinstalls_table)
170 session.mapper(Owner, owners_table)
171 session.mapper(Admin, admins_table)
172 session.mapper(Build, builds_table)
175 """Clear sqlalchemy's cache