source: trunk/scripts/lib/database.py @ 1611

Last change on this file since 1611 was 1611, checked in by broder, 15 years ago

Eliminate the need for a deep copy of database objects during the
migration with weird SQLAlchemy haxory.

This requires the migration script to have its own unique version of
the database library

File size: 5.1 KB
Line 
1from invirt.config import structs as config
2import sqlalchemy
3
4from sqlalchemy import *
5from sqlalchemy import orm
6from sqlalchemy.orm import create_session, relation
7
8from sqlalchemy.ext.sessioncontext import SessionContext
9from sqlalchemy.ext.assignmapper import assign_mapper
10
11def connect(uri = config.db.uri):
12    """ Connect to a given database URI"""
13    global session
14   
15    engine = sqlalchemy.create_engine(uri, pool_timeout=5)
16    meta.bind = engine
17   
18    session = Session(bind=engine)
19
20def clear_cache():
21    """Clear sqlalchemy's cache
22    """
23
24    session.clear()
25
26meta = MetaData()
27Session = orm.sessionmaker(transactional=False, autoflush=False)
28
29machine_table = Table('machines', meta,
30       Column('machine_id', Integer, primary_key=True, nullable=False),
31       Column('name', String, nullable=False),
32       Column('description', String, nullable=False),
33       Column('memory', Integer, nullable=False),
34       Column('owner', String, nullable=False),
35       Column('contact', String, nullable=False),
36       Column('uuid', String, nullable=False),
37       Column('administrator', String, nullable=False, default=False),
38       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
39       Column('autorestart', Boolean, nullable=False, default=False),
40       Column('cpus', Integer, nullable=False, default=1),
41       Column('adminable', Boolean, nullable=False, default=False))
42
43nic_table = Table('nics', meta,
44       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
45       Column('mac_addr', String, nullable=False, primary_key=True),
46       Column('ip', String, nullable=False, unique=True),
47       Column('hostname', String, nullable=True))
48
49disk_table = Table('disks', meta,
50       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False),
51       Column('guest_device_name', String, nullable=False),
52       Column('size', Integer, nullable=False),
53       PrimaryKeyConstraint('machine_id', 'guest_device_name'))
54
55types_table = Table('types', meta,
56       Column('type_id', String, primary_key=True, nullable=False),
57       Column('description', String, nullable=False),
58       Column('hvm', Boolean, nullable=False),
59       Column('apic', Boolean, nullable=False),
60       Column('acpi', Boolean, nullable=False),
61       Column('pae', Boolean, nullable=False))
62
63mirrors_table = Table('mirrors', meta,
64       Column('mirror_id', String, primary_key=True, nullable=False),
65       Column('uri_prefix', String, nullable=False))
66
67cdroms_table = Table('cdroms', meta,
68       Column('cdrom_id', String, primary_key=True, nullable=False),
69       Column('description', String, nullable=False),
70       Column('mirror_id', String, ForeignKey('mirrors.mirror_id')),
71       Column('uri_suffix', String))
72
73autoinstalls_table = Table('autoinstalls', meta,
74       Column('autoinstall_id', String, primary_key=True, nullable=False),
75       Column('description', String, nullable=False),
76       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
77       Column('distribution', String, nullable=False),
78       Column('mirror', String, nullable=False))
79
80machine_access_table = Table('machine_access', meta,
81       Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True),
82       Column('user', String, nullable=False, index=True),
83       PrimaryKeyConstraint('machine_id', 'user'))
84
85class Machine(object):
86    def __repr__(self):
87        return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
88
89class MachineAccess(object):
90    def __repr__(self):
91        return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
92
93class NIC(object):
94    def __repr__(self):
95        return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
96
97class Disk(object):
98    def __repr__(self):
99        return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
100
101class Type(object):
102    def __repr__(self):
103        return "<Type %s: %s>" % (self.type_id, self.description)
104
105class Mirror(object):
106    def __repr__(self):
107        return "<Mirror %s>" % (self.mirror_id)
108
109class CDROM(object):
110    def __repr__(self):
111        return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
112
113class Autoinstall(object):
114    def __repr__(self):
115        return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
116
117orm.mapper(Machine, machine_table,
118              properties={'nics': relation(NIC, backref="machine", lazy=False),
119                          'disks': relation(Disk, backref="machine", lazy=False),
120                          'type': relation(Type, lazy=False),
121                          'acl': relation(MachineAccess, backref="machine", lazy=False, passive_deletes=True, cascade="all, delete-orphan")});
122orm.mapper(MachineAccess, machine_access_table)
123orm.mapper(NIC, nic_table)
124orm.mapper(Disk, disk_table)
125orm.mapper(Type, types_table)
126orm.mapper(Mirror, mirrors_table)
127orm.mapper(CDROM, cdroms_table,
128               properties={'mirror': relation(Mirror, backref="cdroms")})
129orm.mapper(Autoinstall, autoinstalls_table)
Note: See TracBrowser for help on using the repository browser.