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