9 print 'Content-Type: text/html\n'
10 sys.stderr = sys.stdout
11 sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
13 from Cheetah.Template import Template
14 from sipb_xen_database import *
17 # ... and stolen from xend/uuid.py
19 """Generate a random UUID."""
21 return [ random.randint(0, 255) for _ in range(0, 16) ]
24 return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
25 "%02x" * 6]) % tuple(u)
31 def haveAccess(user, machine):
35 def error(op, user, fields, errorMessage):
38 errorMessage=errorMessage)
39 print Template(file='error.tmpl',
42 def validMachineName(name):
45 charset = string.ascii_letters + string.digits + '-'
46 if name[0] == '-' or len(name) > 22:
48 return all(x in charset for x in name)
51 keytab = '/etc/tabbott.keytab'
52 username = 'tabbott/extra'
53 p = subprocess.Popen(['kinit', "-k", "-t", keytab,
58 p = subprocess.Popen(['klist', '-s'])
64 p = subprocess.Popen(['remctl', 'black-mesa.mit.edu']
66 stdout=subprocess.PIPE,
67 stderr=subprocess.PIPE)
69 print >> sys.stderr, 'ERROR on remctl ', args
70 print >> sys.stderr, p.stderr.read()
73 remctl('lvcreate','all')
75 def bootMachine(machine, cdtype):
76 if cdtype is not None:
77 remctl('vmboot', 'cdrom', str(machine.name),
80 remctl('vmboot', 'cdrom', str(machine.name))
82 def createVm(user, name, memory, disk, is_hvm, cdrom):
83 # put stuff in the table
84 transaction = ctx.current.create_transaction()
86 res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')')
87 id = res.fetchone()[0]
89 machine.machine_id = id
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,
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"
105 nic.machine_id = machine.machine_id
107 ctx.current.save(nic)
108 ctx.current.save(disk)
111 transaction.rollback()
114 # tell it to boot with cdrom
115 bootMachine(machine, cdrom)
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)
126 if Machine.get_by(name=name):
127 return error('create', user, fields,
128 "A machine named '%s' already exists" % name)
130 memory = fields.getfirst('memory')
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")
142 disk = fields.getfirst('disk')
145 disk = int(disk * 1024)
149 return error('create', user, fields,
150 "Invalid disk amount")
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')
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)
163 machine = createVm(user, name, memory, disk, is_hvm, cdrom)
164 if isinstance(machine, basestring):
165 return error('create', user, fields,
169 print Template(file='create.tmpl',
172 def listVms(user, fields):
173 machines = Machine.select()
176 cdroms=CDROM.select())
178 print Template(file='list.tmpl', searchList=d)
180 def testMachineId(user, machineId, exists=True):
181 if machineId is None:
182 error('vnc', user, fields,
183 "No machine ID specified")
186 machineId = int(machineId)
188 error('vnc', user, fields,
189 "Invalid machine ID '%s'"
192 machine = Machine.get(machineId)
193 if exists and machine is None:
194 error('vnc', user, fields,
195 "No such machine ID '%s'"
198 if not haveAccess(user, machine):
199 error('vnc', user, fields,
200 "No access to machine ID '%s'"
205 def vnc(user, fields):
206 machine = testMachineId(user, fields.getfirst('machine_id'))
207 if machine is None: #gave error page already
212 hostname='localhost',
214 print Template(file='vnc.tmpl',
217 def info(user, fields):
218 machine = testMachineId(user, fields.getfirst('machine_id'))
219 if machine is None: #gave error page already
224 print Template(file='info.tmpl',
227 mapping = dict(list=listVms,
232 if __name__ == '__main__':
233 fields = cgi.FieldStorage()
236 email = 'moo@cow.com'
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:]
245 fun = mapping.get(operation,
247 error(operation, u, e,
248 "Invalid operation '%'" % operation))