Esempio n. 1
0
    def get(self, id):

        inst = self.get_instance(id, isowner=True)
        if not inst: return

        form = ResourceForm()
        form.cpus.data = inst.cpus
        form.memory.data = inst.memory

        d = { 'title': _('Edit Resource'),
              'instance': inst, 'form': form }

        self.render('instance/resource_edit.html', **d)
Esempio n. 2
0
    def post(self, id):

        inst = self.get_instance(id, isowner=True)
        if not inst: return

        form = ResourceForm( self.request.arguments )
        if form.validate():

            # TODO: resource limit
            RUNNING_INST_LIST = self.db2.query(Instance).filter( and_(
                    Instance.status.in_( settings.INSTANCE_SLIST_RUNING ),
                    Instance.user_id == inst.user_id ) )

            USED_CPUS, USED_MEMORY = 0, 0
            for I in RUNNING_INST_LIST:
                if I.is_running:
                    USED_CPUS += I.cpus
                    USED_MEMORY += I.memory

            profile = inst.user.profile
            if (form.cpus.data + USED_CPUS) > profile.cpus:
                form.cpus.errors.append( _('cpus can not greater than %s') % (profile.cpus - USED_CPUS) )
            if (form.memory.data + USED_MEMORY) > profile.memory:
                form.memory.errors.append( _('memory can not greater than %s') % (profile.memory - USED_MEMORY) )
            if form.cpus.errors or form.memory.errors:
                d = { 'title': _('Edit Resource'),
                      'instance': inst, 'form': form }
                self.render('instance/resource_edit.html', **d)
                

            inst.cpus = form.cpus.data
            inst.memory = form.memory.data
            if inst.is_running:
                inst.ischanged = True
            self.db2.commit()

            url = self.reverse_url('instance:view', id)
            url += '?view=resource'
            return self.redirect( url )

        # Get error
        d = { 'title': _('Edit Resource'),
              'instance': inst, 'form': form }
        self.render('instance/resource_edit.html', **d)