rename sipb_xen_database -> invirt.database
[invirt/packages/invirt-database.git] / client / etc / xen / sipb-database
1 # -*- mode: python; -*-
2 import sipb_xen_database.models as models
3 from sipb_xen_database import connect
4 import re
5 import tempfile
6 from subprocess import call
7
8 connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
9 prefix = "d_"
10
11 # 'machine_name', and optionally 'cdrom_image', should get passed in
12 # from the xm create call
13
14 def check(b):
15     if not b:
16         import sys
17         sys.exit(1)
18
19 machine = models.Machine.get_by(name=machine_name)
20 check(machine is not None)
21 machine_type = models.Type.get_by(type_id=machine.type_id)
22
23 memory = machine.memory
24 maxmem = memory
25 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
26 name = prefix + machine.name
27 check(re.match('^[0-9a-f-]+$', machine.uuid))
28 uuid = machine.uuid
29
30 vcpus = machine.cpus
31
32 diskioemu = ""
33 viftype = ""
34
35 pae = machine_type.pae
36 acpi = machine_type.acpi
37 apic = machine_type.apic
38
39 vif = []
40
41 disk = []
42
43 if machine_type.hvm:
44     codepath = 'hvm'
45 else:
46     codepath = 'paravm'
47
48 if 'mirror' in locals(): #Installer
49     disk.append('phy:/dev/xenvg/d_foobar4_hda,hdb,r')
50
51     kernel = '/boot/vmlinuz-2.6.24-19-xen' #From hardy
52     ramdisk = '/boot/initrd.img-2.6.24-19-xen'
53
54     if not machine.nics:
55         raise RuntimeError('You must have a nic to autoinstall')
56     n = machine.nics[0]
57     extra = 'ro noresume'
58     extra += ' ip=%s::18.181.0.1:255.255.0.0:%s:eth0:off' % (n.ip, machine.name)
59     extra += ' mirror=%s dist=%s' % (mirror, dist)
60     extra += ' imagesize=%s' % imagesize
61     #extra += ' noinstall'
62     root = '/dev/hdb1'
63     codepath = None
64 elif 'cdrom_image' in locals():
65     check(re.match('^[A-Za-z0-9][A-Za-z0-9_.-]*$', cdrom_image))
66     disk.append('file:/srv/images/' + cdrom_image + '.iso,hdc:cdrom,r')
67     boot = 'd'
68     codepath = 'hvm'
69
70 if codepath == 'hvm':
71     ioemu = "ioemu:"
72     viftype = "type=ioemu, "
73     kernel = 'hvmloader'
74     builder = 'hvm'
75     vnc = 1
76     vncpasswd = 'moocow'
77     device_model = '/usr/sbin/qemu-dm-sipb'
78     serial = "pty"
79 elif codepath == 'paravm':
80     bootloader = '/usr/lib/xen-default/bin/pygrub'
81
82
83 for n in machine.nics:
84     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
85     d = '%smac=%s, ip=%s, bridge=xenbr0, script=vif-sipbroute' % (viftype, n.mac_addr, n.ip)
86     vif.append(d)
87
88 for d in machine.disks:
89     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
90     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
91     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
92     disk.append(dspec)
93
94 usbdevice = 'tablet'
95
96 on_poweroff = 'destroy'
97 on_reboot = 'restart'
98 on_crash = 'destroy'
99 if machine.autorestart:
100     on_crash = 'restart'