1 | from sqlalchemy import * |
---|
2 | |
---|
3 | from sqlalchemy.ext.sessioncontext import SessionContext |
---|
4 | from sqlalchemy.ext.assignmapper import assign_mapper |
---|
5 | |
---|
6 | __all__ = ['meta', |
---|
7 | 'ctx', |
---|
8 | 'machine_table', |
---|
9 | 'nic_table', |
---|
10 | 'disk_table', |
---|
11 | 'types_table', |
---|
12 | 'cdroms_table', |
---|
13 | 'Machine', |
---|
14 | 'NIC', |
---|
15 | 'Disk', |
---|
16 | 'Type', |
---|
17 | 'CDROM'] |
---|
18 | |
---|
19 | meta = DynamicMetaData() |
---|
20 | ctx = SessionContext(create_session) |
---|
21 | |
---|
22 | machine_table = Table('machines', meta, |
---|
23 | Column('machine_id', Integer, primary_key=True, nullable=False), |
---|
24 | Column('name', String, nullable=False), |
---|
25 | Column('memory', Integer, nullable=False), |
---|
26 | Column('owner', String, nullable=False), |
---|
27 | Column('contact', String, nullable=False), |
---|
28 | Column('uuid', String, nullable=False), |
---|
29 | Column('boot_off_cd', Boolean, nullable=False, default=False), |
---|
30 | Column('type_id', String, ForeignKey('types.type_id'), nullable=False), |
---|
31 | Column('autorestart', Boolean, nullable=False, default=False), |
---|
32 | Column('cpus', Integer, nullable=False, default=1)) |
---|
33 | |
---|
34 | nic_table = Table('nics', meta, |
---|
35 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True), |
---|
36 | Column('mac_addr', String, nullable=False, primary_key=True), |
---|
37 | Column('ip', String, nullable=False, unique=True), |
---|
38 | Column('hostname', String, nullable=True)) |
---|
39 | |
---|
40 | disk_table = Table('disks', meta, |
---|
41 | Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False), |
---|
42 | Column('guest_device_name', String, nullable=False), |
---|
43 | Column('size', Integer, nullable=False), |
---|
44 | PrimaryKeyConstraint('machine_id', 'guest_device_name')) |
---|
45 | |
---|
46 | types_table = Table('types', meta, |
---|
47 | Column('type_id', String, primary_key=True, nullable=False), |
---|
48 | Column('description', String, nullable=False), |
---|
49 | Column('hvm', Boolean, nullable=False), |
---|
50 | Column('apic', Boolean, nullable=False), |
---|
51 | Column('acpi', Boolean, nullable=False), |
---|
52 | Column('pae', Boolean, nullable=False)) |
---|
53 | |
---|
54 | cdroms_table = Table('cdroms', meta, |
---|
55 | Column('cdrom_id', String, primary_key=True, nullable=False), |
---|
56 | Column('description', String, nullable=False)) |
---|
57 | |
---|
58 | |
---|
59 | class Machine(object): |
---|
60 | pass |
---|
61 | |
---|
62 | class NIC(object): |
---|
63 | def __init__(self, machine_id, mac_addr, ip, hostname): |
---|
64 | self.machine_id = machine_id |
---|
65 | self.mac_addr = mac_addr |
---|
66 | self.ip = ip |
---|
67 | self.hostname = hostname |
---|
68 | |
---|
69 | class Disk(object): |
---|
70 | def __init__(self, machine_id, guest, size): |
---|
71 | self.machine_id = machine_id |
---|
72 | self.guest_device_name = guest |
---|
73 | self.size = size |
---|
74 | |
---|
75 | class Type(object): |
---|
76 | pass |
---|
77 | |
---|
78 | class CDROM(object): |
---|
79 | def __init__(self, cdrom_id, description): |
---|
80 | self.cdrom_id = cdrom_id |
---|
81 | self.description = description |
---|
82 | |
---|
83 | assign_mapper(ctx, Machine, machine_table, \ |
---|
84 | properties={'nics': relation(NIC), |
---|
85 | 'disks': relation(Disk)}); |
---|
86 | assign_mapper(ctx, NIC, nic_table) |
---|
87 | assign_mapper(ctx, Disk, disk_table) |
---|
88 | assign_mapper(ctx, Type, types_table) |
---|
89 | assign_mapper(ctx, CDROM, cdroms_table) |
---|
90 | |
---|