RAM quotas at remctl; RAM quota exception script, table, and usage in -web and -remot...
[invirt/packages/invirt-remote.git] / server / usr / sbin / invirt-remote-create
index 991b3c8..bae10c0 100755 (executable)
@@ -12,6 +12,18 @@ from invirt.remote import bcast
 from subprocess import PIPE, Popen, call
 import sys
 import yaml
+import invirt.database
+
+def maxMemory(owner, xmlist):
+    """
+    Return the memory available for a new machine.
+    """
+    machines = invirt.database.Machine.query().filter_by(owner=owner)
+    (quota_total, quota_single) = invirt.database.Owner.getQuotas(owner)
+
+    active_machines = [m for m in machines if m.name in xmlist]
+    mem_usage = sum([x.memory for x in active_machines])
+    return min(quota_single, quota_total-mem_usage)
 
 def choose_host():
     # Query each of the hosts.
@@ -49,6 +61,18 @@ def main(argv):
                               % (machine_name, host))
         return 1
 
+    if operation == "create":
+        invirt.database.connect()
+        machine = invirt.database.Machine.query().filter_by(name=machine_name).first()
+
+        owner = machine.owner
+        vm_memory = machine.memory
+
+        max_memory = maxMemory(owner, vms.keys())
+        if vm_memory > max_memory:
+            print >>sys.stderr, "owner %s requested %d MB of memory for vm %s; %d MB allowed" % (owner, vm_memory, machine_name, max_memory)
+            return 1
+
     host = choose_host()
     print 'Creating on host %s...' % host
     sys.stdout.flush()