1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import cgi |
---|
5 | import os |
---|
6 | import string |
---|
7 | import subprocess |
---|
8 | |
---|
9 | print 'Content-Type: text/html\n' |
---|
10 | sys.stderr = sys.stdout |
---|
11 | sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages') |
---|
12 | |
---|
13 | from Cheetah.Template import Template |
---|
14 | from sipb_xen_database import * |
---|
15 | import random |
---|
16 | |
---|
17 | # ... and stolen from xend/uuid.py |
---|
18 | def randomUUID(): |
---|
19 | """Generate a random UUID.""" |
---|
20 | |
---|
21 | return [ random.randint(0, 255) for _ in range(0, 16) ] |
---|
22 | |
---|
23 | def uuidToString(u): |
---|
24 | return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, |
---|
25 | "%02x" * 6]) % tuple(u) |
---|
26 | |
---|
27 | |
---|
28 | def maxMemory(user): |
---|
29 | return 256 |
---|
30 | |
---|
31 | def haveAccess(user, machine): |
---|
32 | return True |
---|
33 | |
---|
34 | |
---|
35 | def error(op, user, fields, errorMessage): |
---|
36 | d = dict(op=op, |
---|
37 | user=user, |
---|
38 | errorMessage=errorMessage) |
---|
39 | print Template(file='error.tmpl', |
---|
40 | searchList=d); |
---|
41 | |
---|
42 | def validMachineName(name): |
---|
43 | if not name: |
---|
44 | return False |
---|
45 | charset = string.ascii_letters + string.digits + '-' |
---|
46 | if name[0] == '-' or len(name) > 22: |
---|
47 | return False |
---|
48 | return all(x in charset for x in name) |
---|
49 | |
---|
50 | def kinit(): |
---|
51 | keytab = '/etc/tabbott.keytab' |
---|
52 | username = 'tabbott/extra' |
---|
53 | p = subprocess.Popen(['kinit', "-k", "-t", keytab, |
---|
54 | username]) |
---|
55 | p.wait() |
---|
56 | |
---|
57 | def checkKinit(): |
---|
58 | p = subprocess.Popen(['klist', '-s']) |
---|
59 | if p.wait(): |
---|
60 | kinit() |
---|
61 | |
---|
62 | def remctl(*args): |
---|
63 | checkKinit() |
---|
64 | p = subprocess.Popen(['remctl', 'black-mesa.mit.edu'] |
---|
65 | + list(args), |
---|
66 | stdout=subprocess.PIPE, |
---|
67 | stderr=subprocess.PIPE) |
---|
68 | if p.wait(): |
---|
69 | print >> sys.stderr, 'ERROR on remctl ', args |
---|
70 | print >> sys.stderr, p.stderr.read() |
---|
71 | |
---|
72 | def makeDisks(): |
---|
73 | remctl('lvcreate','all') |
---|
74 | |
---|
75 | def bootMachine(machine, cdtype): |
---|
76 | if cdtype is not None: |
---|
77 | remctl('vmboot', 'cdrom', str(machine.name), |
---|
78 | cdtype) |
---|
79 | else: |
---|
80 | remctl('vmboot', 'cdrom', str(machine.name)) |
---|
81 | |
---|
82 | def createVm(user, name, memory, disk, is_hvm, cdrom): |
---|
83 | # put stuff in the table |
---|
84 | transaction = ctx.current.create_transaction() |
---|
85 | try: |
---|
86 | res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')') |
---|
87 | id = res.fetchone()[0] |
---|
88 | machine = Machine() |
---|
89 | machine.machine_id = id |
---|
90 | machine.name = name |
---|
91 | machine.memory = memory |
---|
92 | machine.owner = user.username |
---|
93 | machine.contact = user.email |
---|
94 | machine.uuid = uuidToString(randomUUID()) |
---|
95 | machine.boot_off_cd = True |
---|
96 | machine_type = Type.get_by(hvm=is_hvm) |
---|
97 | machine.type_id = machine_type.type_id |
---|
98 | ctx.current.save(machine) |
---|
99 | disk = Disk(machine.machine_id, |
---|
100 | 'hda', disk) |
---|
101 | open = NIC.select_by(machine_id=None) |
---|
102 | if not open: #No IPs left! |
---|
103 | return "No IP addresses left! Contact sipb-xen-dev@mit.edu" |
---|
104 | nic = open[0] |
---|
105 | nic.machine_id = machine.machine_id |
---|
106 | nic.hostname = name |
---|
107 | ctx.current.save(nic) |
---|
108 | ctx.current.save(disk) |
---|
109 | transaction.commit() |
---|
110 | except: |
---|
111 | transaction.rollback() |
---|
112 | raise |
---|
113 | makeDisks() |
---|
114 | # tell it to boot with cdrom |
---|
115 | bootMachine(machine, cdrom) |
---|
116 | |
---|
117 | return machine |
---|
118 | |
---|
119 | def create(user, fields): |
---|
120 | name = fields.getfirst('name') |
---|
121 | if not validMachineName(name): |
---|
122 | return error('create', user, fields, |
---|
123 | "Invalid name '%s'" % name) |
---|
124 | name = name.lower() |
---|
125 | |
---|
126 | if Machine.get_by(name=name): |
---|
127 | return error('create', user, fields, |
---|
128 | "A machine named '%s' already exists" % name) |
---|
129 | |
---|
130 | memory = fields.getfirst('memory') |
---|
131 | try: |
---|
132 | memory = int(memory) |
---|
133 | if memory <= 0: |
---|
134 | raise ValueError |
---|
135 | except ValueError: |
---|
136 | return error('create', user, fields, |
---|
137 | "Invalid memory amount") |
---|
138 | if memory > maxMemory(user): |
---|
139 | return error('create', user, fields, |
---|
140 | "Too much memory requested") |
---|
141 | |
---|
142 | disk = fields.getfirst('disk') |
---|
143 | try: |
---|
144 | disk = float(disk) |
---|
145 | disk = int(disk * 1024) |
---|
146 | if disk <= 0: |
---|
147 | raise ValueError |
---|
148 | except ValueError: |
---|
149 | return error('create', user, fields, |
---|
150 | "Invalid disk amount") |
---|
151 | |
---|
152 | vm_type = fields.getfirst('vmtype') |
---|
153 | if vm_type not in ('hvm', 'paravm'): |
---|
154 | return error('create', user, fields, |
---|
155 | "Invalid vm type '%s'" % vm_type) |
---|
156 | is_hvm = (vm_type == 'hvm') |
---|
157 | |
---|
158 | cdrom = fields.getfirst('cdrom') |
---|
159 | if cdrom is not None and not CDROM.get(cdrom): |
---|
160 | return error('create', user, fields, |
---|
161 | "Invalid cdrom type '%s'" % cdrom) |
---|
162 | |
---|
163 | machine = createVm(user, name, memory, disk, is_hvm, cdrom) |
---|
164 | if isinstance(machine, basestring): |
---|
165 | return error('create', user, fields, |
---|
166 | machine) |
---|
167 | d = dict(user=user, |
---|
168 | machine=machine) |
---|
169 | print Template(file='create.tmpl', |
---|
170 | searchList=d); |
---|
171 | |
---|
172 | def listVms(user, fields): |
---|
173 | machines = Machine.select() |
---|
174 | d = dict(user=user, |
---|
175 | machines=machines, |
---|
176 | cdroms=CDROM.select()) |
---|
177 | |
---|
178 | print Template(file='list.tmpl', searchList=d) |
---|
179 | |
---|
180 | def testMachineId(user, machineId, exists=True): |
---|
181 | if machineId is None: |
---|
182 | error('vnc', user, fields, |
---|
183 | "No machine ID specified") |
---|
184 | return False |
---|
185 | try: |
---|
186 | machineId = int(machineId) |
---|
187 | except ValueError: |
---|
188 | error('vnc', user, fields, |
---|
189 | "Invalid machine ID '%s'" |
---|
190 | % machineId) |
---|
191 | return False |
---|
192 | machine = Machine.get(machineId) |
---|
193 | if exists and machine is None: |
---|
194 | error('vnc', user, fields, |
---|
195 | "No such machine ID '%s'" |
---|
196 | % machineId) |
---|
197 | return False |
---|
198 | if not haveAccess(user, machine): |
---|
199 | error('vnc', user, fields, |
---|
200 | "No access to machine ID '%s'" |
---|
201 | % machineId) |
---|
202 | return False |
---|
203 | return machine |
---|
204 | |
---|
205 | def vnc(user, fields): |
---|
206 | machine = testMachineId(user, fields.getfirst('machine_id')) |
---|
207 | if machine is None: #gave error page already |
---|
208 | return |
---|
209 | token = 'quentin' |
---|
210 | d = dict(user=user, |
---|
211 | machine=machine, |
---|
212 | hostname='localhost', |
---|
213 | authtoken=token) |
---|
214 | print Template(file='vnc.tmpl', |
---|
215 | searchList=d) |
---|
216 | |
---|
217 | def info(user, fields): |
---|
218 | machine = testMachineId(user, fields.getfirst('machine_id')) |
---|
219 | if machine is None: #gave error page already |
---|
220 | return |
---|
221 | |
---|
222 | d = dict(user=user, |
---|
223 | machine=machine) |
---|
224 | print Template(file='info.tmpl', |
---|
225 | searchList=d) |
---|
226 | |
---|
227 | mapping = dict(list=listVms, |
---|
228 | vnc=vnc, |
---|
229 | info=info, |
---|
230 | create=create) |
---|
231 | |
---|
232 | if __name__ == '__main__': |
---|
233 | fields = cgi.FieldStorage() |
---|
234 | class C: |
---|
235 | username = "moo" |
---|
236 | email = 'moo@cow.com' |
---|
237 | u = C() |
---|
238 | connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen') |
---|
239 | operation = os.environ.get('PATH_INFO', '') |
---|
240 | if operation.startswith('/'): |
---|
241 | operation = operation[1:] |
---|
242 | if not operation: |
---|
243 | operation = 'list' |
---|
244 | |
---|
245 | fun = mapping.get(operation, |
---|
246 | lambda u, e: |
---|
247 | error(operation, u, e, |
---|
248 | "Invalid operation '%'" % operation)) |
---|
249 | fun(u, fields) |
---|