tweak privatebridge to remove exception message when ovs is not installed - prints...
[nodemanager.git] / tools.py
index 4cbfa3b..02e2ab9 100644 (file)
--- a/tools.py
+++ b/tools.py
@@ -9,6 +9,7 @@ import threading
 import subprocess
 import shutil
 import sys
+import signal
 
 import logger
 
@@ -300,24 +301,49 @@ def get_node_virt ():
     except:
         pass
     logger.log("Computing virt..")
-    vs_retcod=subprocess.call ([ 'vserver', '--help' ])
-    if vs_retcod == 0:
-        virt='vs'
-    else:
+    try: 
+        if subprocess.call ([ 'vserver', '--help' ]) ==0: virt='vs'
+        else:                                             virt='lxc'      
+    except:
         virt='lxc'
     with file(virt_stamp,"w") as f:
         f.write(virt)
     return virt
 
+### this return True or False to indicate that systemctl is present on that box
+# cache result in memory as _has_systemctl
+_has_systemctl=None
+def has_systemctl ():
+    global _has_systemctl
+    if _has_systemctl is None:
+        _has_systemctl = (subprocess.call([ 'systemctl', '--help' ]) == 0)
+    return _has_systemctl
+
+# how to run a command in a slice
+# now this is a painful matter
+# the problem is with capsh that forces a bash command to be injected in its exec'ed command
+# so because lxcsu uses capsh, you cannot exec anything else than bash
+# bottom line is, what actually needs to be called is
+# vs:  vserver exec slicename command and its arguments
+# lxc: lxcsu slicename "command and its arguments"
+# which, OK, is no big deal as long as the command is simple enough, 
+# but do not stretch it with arguments that have spaces or need quoting as that will become a nightmare
 def command_in_slice (slicename, argv):
-    # with vserver this can be done using vserver .. exec <trigger-script>
-    # with lxc this is less clear as we are still discussing how lxcsu should behave
-    # ideally we'd need to run lxcsu .. <trigger-script>
     virt=get_node_virt()
     if virt=='vs':
         return [ 'vserver', slicename, 'exec', ] + argv
     elif virt=='lxc':
-        return [ 'lxcsu', slicename, ] + argv
+        # wrap up argv in a single string for -c
+        return [ 'lxcsu', slicename, ] + [ " ".join(argv) ]
     logger.log("command_in_slice: WARNING: could not find a valid virt")
     return argv
 
+####################
+def init_signals ():
+    def handler (signum, frame):
+        logger.log("Received signal %d - exiting"%signum)
+        os._exit(1)
+    signal.signal(signal.SIGHUP,handler)
+    signal.signal(signal.SIGQUIT,handler)
+    signal.signal(signal.SIGINT,handler)
+    signal.signal(signal.SIGTERM,handler)