Prevent the janitor from trying to clean up nonexistent LVs.
[invirt/packages/invirt-remote.git] / host / usr / sbin / invirt-availability
index 2afd5ea..1dcf9c8 100644 (file)
@@ -6,26 +6,41 @@ Gathers availability data for the VM host it's running on.
 from subprocess import PIPE, Popen, call
 import sys
 import yaml
+import os
 
-# return the amount of memory in kilobytes represented by s
+# return the amount of memory in kibibytes represented by s
 def parseUnits(s):
     num, unit = s.split(' ')
-    return int(num) * {'kb':1, 'mb':1024}[unit.lower()]
+    assert unit == 'kB', "unexpected unit"
+    return int(num)
 
 def main(argv):
+    """
+    Calculate the amount of memory available for new VMs
+    The numbers returned by xm info and xm info -c are in MiB
+    The numbers in /proc/xen/balloon are in KiB
+    All math is done in kibibytes for consistency
+    Output is in MiB
+
+    Bail if /etc/invirt/nocreate exists
+    """
+    try:
+        os.stat('/etc/invirt/nocreate')
+        print 0
+        return 0
+    except OSError:
+        pass
+
     p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE)
     output = p.communicate()[0]
     if p.returncode != 0:
         raise RuntimeError("Command '%s' returned non-zero exit status %d"
-                           % ('invirt-availability', p.returncode)) 
+                           % ('/usr/sbin/xm info', p.returncode)) 
     xminfo = yaml.load(output, yaml.CSafeLoader)
 
-    # In kilobytes
     free_memory = int(xminfo['free_memory']) * 1024
 
-    f = open('/proc/xen/balloon', 'r')
-    ballooninfo = yaml.load(f.read())
-    f.close()
+    ballooninfo = yaml.load(open('/proc/xen/balloon', 'r').read())
     currentallocation = parseUnits(ballooninfo['Current allocation'])
     minimumtarget = parseUnits(ballooninfo['Minimum target'])
 
@@ -33,10 +48,10 @@ def main(argv):
     output = p.communicate()[0]
     if p.returncode != 0:
         raise RuntimeError("Command '%s' returned non-zero exit status %d"
-                           % ('invirt-availability', p.returncode)) 
+                           % ('/usr/sbin/xm info -c', p.returncode)) 
     xminfoc = yaml.load(output, yaml.CSafeLoader)
 
-    # In kilobytes
+    # In kibibytes
     dom0minmem = int(xminfoc['dom0-min-mem']) * 1024
 
     dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem)