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),
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('inserted_at', DateTime, nullable=False, default=datetime.datetime.utcnow))
116 machine_access_table = Table('machine_access', meta,
117 Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True),
118 Column('user', String, nullable=False, index=True),
119 PrimaryKeyConstraint('machine_id', 'user'))
121 admins_table = Table('admins', meta,
122 Column('user', String, nullable=False, index=True, primary_key=True))
124 class Machine(record.Record):
125 _identity_field = 'name'
127 class MachineAccess(record.Record):
130 class NIC(record.Record):
133 class Disk(record.Record):
136 class Type(record.Record):
137 _identity_field = 'type_id'
139 class Mirror(record.Record):
140 _identity_field = 'mirror_id'
142 class CDROM(record.Record):
143 _identity_field = 'cdrom_id'
145 class Autoinstall(record.Record):
146 _identity_field = 'autoinstall_id'
148 class Admin(record.Record):
149 _identity_field = 'user'
151 class Build(record.Record):
152 _identity_field = 'build_id'
154 from invirt.database.owner import Owner
156 session.mapper(Machine, machine_table,
157 properties={'nics': relation(NIC, backref="machine"),
158 'disks': relation(Disk, backref="machine"),
159 'type': relation(Type),
160 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")});
161 session.mapper(MachineAccess, machine_access_table)
162 session.mapper(NIC, nic_table)
163 session.mapper(Disk, disk_table)
164 session.mapper(Type, types_table)
165 session.mapper(Mirror, mirrors_table)
166 session.mapper(CDROM, cdroms_table,
167 properties={'mirror': relation(Mirror, backref="cdroms")})
168 session.mapper(Autoinstall, autoinstalls_table)
169 session.mapper(Owner, owners_table)
170 session.mapper(Admin, admins_table)
171 session.mapper(Build, builds_table)
174 """Clear sqlalchemy's cache