~malcscott/+junk/nebula

« back to all changes in this revision

Viewing changes to nebula

  • Committer: debianpkg at org
  • Date: 2014-03-31 12:46:28 UTC
  • Revision ID: debianpkg@malc.org.uk-20140331124628-3y6vy53m77nl919d
Cope with VMs which don't need disks provisioning (e.g. if the disk is part of the template); better cope with the VM already existing; make JSON errors work

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
def error_response(start_response, status, text):
54
54
        start_response(status, [])
 
55
        return {'Error': text}
 
56
 
 
57
def error_response_json(start_response, status, text):
 
58
        r = error_response(start_response, status, text)
55
59
        enc = JSONEncoder(indent=2)
56
 
        return enc.iterencode({'Error': text}, functools.partial(start_response_wrap, start_response))
 
60
        return enc.iterencode(r, functools.partial(start_response_wrap, start_response))
57
61
 
58
62
class XenApiException(Exception):
59
63
        def __init__(self, desc):
66
70
                self.url = url
67
71
                self.auth_handler = auth_handler
68
72
                self.headers = headers
69
 
                print "Initialising redirector to %s %s" % (url, self.headers)
70
73
 
71
74
        def do_redirect(self, env, start_response, op=None):
72
75
                url = self.url
121
124
                if not self.config.has_section(section):
122
125
                        return error_response(start_response, '400 Bad VM Type', 'I don\'t know how to create a VM of type %s' % vmtype)
123
126
 
 
127
                x = self.xenapi()
 
128
 
 
129
                if len(x.VM.get_by_name_label(name)) > 0:
 
130
                        return error_response(start_response, '409 VM Already Exists', 'A VM named "%s" already exists %s' % (name, x.VM.get_by_name_label(name)))
 
131
 
124
132
                # TODO: extensions, addons...
125
133
                ram = self.config.getint(section, 'RAM')*1048576
126
 
                disk = self.config.getint(section, 'disk')*1048576*1024
 
134
                if self.config.has_option(section, 'disk'):
 
135
                        disk = self.config.getint(section, 'disk')*1048576*1024
 
136
                else:
 
137
                        disk = None
127
138
                for addon in request.getlist('addon'):
128
139
                        addon_section = "VMtype:%s:%s" % (vmtype, addon)
129
140
                        if not self.config.has_section(addon_section):
133
144
                        if self.config.has_option(addon_section, 'disk'):
134
145
                                disk += self.config.getint(addon_section, 'disk')*1048576*1024
135
146
 
136
 
                x = self.xenapi()
137
147
                template = x.VM.get_by_uuid(self.config.get(section, 'uuid'))
138
148
                vm = x.VM.clone(template, name)
139
149
 
143
153
                        x.VM.set_memory_dynamic_max(vm, str(ram))
144
154
                        x.VM.set_memory_dynamic_min(vm, str(ram))
145
155
 
146
 
                        ps = provision.parseProvisionSpec(x.VM.get_other_config(vm)['disks'])
147
 
                        for d in ps.disks:
148
 
                                disk -= int(d.size)
149
 
                        d.size = str(int(d.size) + disk)
150
 
                        x.VM.remove_from_other_config(vm, 'disks')
151
 
                        x.VM.add_to_other_config(vm, 'disks', provision.printProvisionSpec(ps))
 
156
                        if disk is not None:
 
157
                                ps = provision.parseProvisionSpec(x.VM.get_other_config(vm)['disks'])
 
158
                                for d in ps.disks:
 
159
                                        disk -= int(d.size)
 
160
                                d.size = str(int(d.size) + disk)
 
161
                                x.VM.remove_from_other_config(vm, 'disks')
 
162
                                x.VM.add_to_other_config(vm, 'disks', provision.printProvisionSpec(ps))
152
163
 
153
164
                        x.VM.provision(vm)
154
165
                        vm_data = x.VM.get_record(vm)
208
219
                        authresult, authdata = self.auth.auth_init(request, env, AuthInteractionRequiredException)
209
220
                except AuthInteractionRequiredException as e:
210
221
                        if 'op' in request:
211
 
                                return e.do_redirection(env, start_response, request.getfirst('op'))
 
222
                                return e.do_redirect(env, start_response, request.getfirst('op'))
212
223
                        else:
213
224
                                return e.do_redirect(env, start_response)
214
225
 
215
 
                print >>env['wsgi.errors'], "Authenticated: %s" % (authdata)
216
 
 
217
226
                if not authresult:
218
 
                        return error_response(start_response, '403 Access Forbidden', 'Access denied: %s' % authdata)
 
227
                        return error_response_json(start_response, '403 Access Forbidden', 'Access denied: %s' % authdata)
219
228
                if 'op' not in request:
220
 
                        return error_response(start_response, '400 No Request Given', 'Must specify an op parameter')
 
229
                        return error_response_json(start_response, '400 No Request Given', 'Must specify an op parameter')
221
230
                op = request.getfirst('op')
222
231
                if op in self.ops:
223
232
                        if self.auth.op_allowed(op):
224
233
                                return self.ops[op](self, start_response, request)
225
234
                        else:
226
 
                                return error_response(start_response, '403 Operation Forbidden', 'Access to operation %s denied' % op)
 
235
                                return error_response_json(start_response, '403 Operation Forbidden', 'Access to operation %s denied' % op)
227
236
                else:
228
 
                        return error_response(start_response, '501 Not Implemented', 'Cannot handle op %s' % op)
 
237
                        return error_response_json(start_response, '501 Not Implemented', 'Cannot handle op %s' % op)
229
238
 
230
239
 
231
240
if __name__ == "__main__":