| 1 | from invirt.config import structs as config | 
|---|
| 2 | import sqlalchemy | 
|---|
| 3 |  | 
|---|
| 4 | from sqlalchemy import * | 
|---|
| 5 | from sqlalchemy import orm | 
|---|
| 6 | from sqlalchemy.orm import create_session, relation | 
|---|
| 7 |  | 
|---|
| 8 | from sqlalchemy.ext.sessioncontext import SessionContext | 
|---|
| 9 | from sqlalchemy.ext.assignmapper import assign_mapper | 
|---|
| 10 |  | 
|---|
| 11 | def 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 |  | 
|---|
| 20 | def clear_cache(): | 
|---|
| 21 |     """Clear sqlalchemy's cache | 
|---|
| 22 |     """ | 
|---|
| 23 |  | 
|---|
| 24 |     session.clear() | 
|---|
| 25 |  | 
|---|
| 26 | meta = MetaData() | 
|---|
| 27 | Session = orm.sessionmaker(transactional=False, autoflush=False) | 
|---|
| 28 |  | 
|---|
| 29 | machine_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 |  | 
|---|
| 43 | nic_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 |  | 
|---|
| 49 | disk_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 |  | 
|---|
| 55 | types_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 |  | 
|---|
| 63 | mirrors_table = Table('mirrors', meta, | 
|---|
| 64 |        Column('mirror_id', String, primary_key=True, nullable=False), | 
|---|
| 65 |        Column('uri_prefix', String, nullable=False)) | 
|---|
| 66 |  | 
|---|
| 67 | cdroms_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 |  | 
|---|
| 73 | autoinstalls_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 |  | 
|---|
| 80 | machine_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 |  | 
|---|
| 85 | class Machine(object): | 
|---|
| 86 |     def __repr__(self): | 
|---|
| 87 |         return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner) | 
|---|
| 88 |  | 
|---|
| 89 | class MachineAccess(object): | 
|---|
| 90 |     def __repr__(self): | 
|---|
| 91 |         return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user) | 
|---|
| 92 |  | 
|---|
| 93 | class 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 |  | 
|---|
| 97 | class 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 |  | 
|---|
| 101 | class Type(object): | 
|---|
| 102 |     def __repr__(self): | 
|---|
| 103 |         return "<Type %s: %s>" % (self.type_id, self.description) | 
|---|
| 104 |  | 
|---|
| 105 | class Mirror(object): | 
|---|
| 106 |     def __repr__(self): | 
|---|
| 107 |         return "<Mirror %s>" % (self.mirror_id) | 
|---|
| 108 |  | 
|---|
| 109 | class CDROM(object): | 
|---|
| 110 |     def __repr__(self): | 
|---|
| 111 |         return "<CDROM %s: %s>" % (self.cdrom_id, self.description) | 
|---|
| 112 |  | 
|---|
| 113 | class Autoinstall(object): | 
|---|
| 114 |     def __repr__(self): | 
|---|
| 115 |         return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id) | 
|---|
| 116 |  | 
|---|
| 117 | orm.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")}); | 
|---|
| 122 | orm.mapper(MachineAccess, machine_access_table) | 
|---|
| 123 | orm.mapper(NIC, nic_table) | 
|---|
| 124 | orm.mapper(Disk, disk_table) | 
|---|
| 125 | orm.mapper(Type, types_table) | 
|---|
| 126 | orm.mapper(Mirror, mirrors_table) | 
|---|
| 127 | orm.mapper(CDROM, cdroms_table, | 
|---|
| 128 |                properties={'mirror': relation(Mirror, backref="cdroms")}) | 
|---|
| 129 | orm.mapper(Autoinstall, autoinstalls_table) | 
|---|