Autoinstallers for Debian Buster and Ubuntu Bionic
[invirt/packages/invirt-web.git] / code / validation.py
index d288a69..da01d8d 100755 (executable)
@@ -57,13 +57,13 @@ class Validate:
         if vmtype is not None:
             self.vmtype = validVmType(vmtype)
         if cdrom is not None:
-            if not CDROM.query().get(cdrom):
+            if not CDROM.query.get(cdrom):
                 raise CodeError("Invalid cdrom type '%s'" % cdrom)
             self.cdrom = cdrom
         if autoinstall is not None:
             #raise InvalidInput('autoinstall', 'install',
             #                   "The autoinstaller has been temporarily disabled")
-            self.autoinstall = Autoinstall.query().get(autoinstall)
+            self.autoinstall = Autoinstall.query.get(autoinstall)
 
 
 def getMachinesByOwner(owner, machine=None):
@@ -74,7 +74,7 @@ def getMachinesByOwner(owner, machine=None):
     """
     if machine:
         owner = machine.owner
-    return Machine.query().filter_by(owner=owner)
+    return Machine.query.filter_by(owner=owner)
 
 def maxMemory(owner, g, machine=None, on=True):
     """Return the maximum memory for a machine or a user.
@@ -99,18 +99,26 @@ def maxDisk(owner, machine=None):
     """Return the maximum disk that a machine can reach.
 
     If machine is None, the maximum disk for a new machine. Otherwise,
-    return the maximum that a given machine can be changed to.
+    return the maximum that a given machine can be changed to.  If the
+    disk currently exceeds the quotas, it can be changed to anything up
+    to its current size.
     """
     (quota_total, quota_single) = Owner.getDiskQuotas(machine.owner if machine else owner)
 
-    if machine is not None:
+    machine_id, current_size = None, 0
+    if machine is not None and machine.disks:
         machine_id = machine.machine_id
-    else:
-        machine_id = None
-    disk_usage = Disk.query().filter(Disk.c.machine_id != machine_id).\
-                     join('machine').\
-                     filter_by(owner=owner).sum(Disk.c.size) or 0
-    return min(quota_single, quota_total-disk_usage/1024.)
+        # XXX The machine modification form doesn't currently handle the
+        # case of machines with multiple disks.  It simply addresses the "first"
+        # disk (which is possibly nondeterministic and wrong).  Since we're
+        # doing validation for that form, we have to use the same logic it
+        # does.
+        current_size = machine.disks[0].size / 1024.
+    disk_usage_query = Disk.query.filter(Disk.machine_id != machine_id).\
+        join('machine').filter_by(owner=owner)
+
+    disk_usage = sum([m.size for m in disk_usage_query]) or 0
+    return max(current_size, min(quota_single, quota_total-disk_usage/1024.))
 
 def cantAddVm(owner, g):
     machines = getMachinesByOwner(owner)
@@ -181,7 +189,7 @@ def validDisk(owner, g, disk, machine=None):
 def validVmType(vm_type):
     if vm_type is None:
         return None
-    t = Type.query().get(vm_type)
+    t = Type.query.get(vm_type)
     if t is None:
         raise CodeError("Invalid vm type '%s'"  % vm_type)
     return t
@@ -198,7 +206,7 @@ def testMachineId(user, state, machine_id, exists=True):
         machine_id = int(machine_id)
     except ValueError:
         raise InvalidInput('machine_id', machine_id, "Must be an integer.")
-    machine = Machine.query().get(machine_id)
+    machine = Machine.query.get(machine_id)
     if exists and machine is None:
         raise InvalidInput('machine_id', machine_id, "Does not exist.")
     if machine is not None and not haveAccess(user, state, machine):
@@ -243,7 +251,8 @@ def testOwner(user, owner, machine=None):
     try:
         if user not in authz.expandOwner(owner):
             raise InvalidInput('owner', owner, 'You do not have access to the '
-                               + owner + ' locker')
+                               + owner + ' locker (Is system:anyuser missing '
+                               + 'the l permission?)')
     except getafsgroups.AfsProcessError, e:
         raise InvalidInput('owner', owner, str(e))
     return owner