merge the vs and lxc nodemanagers
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 27 Jun 2012 09:48:03 +0000 (11:48 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 27 Jun 2012 09:48:03 +0000 (11:48 +0200)
api_calls.py
net.py
plugins/codemux.py
slivermanager.py

index 084bf66..592335d 100644 (file)
@@ -18,7 +18,7 @@ import socket
 import struct
 import threading
 import xmlrpclib
-import sliver_lxc
+import slivermanager
 
 try:
     from PLC.Parameter import Parameter, Mixed
@@ -36,8 +36,6 @@ import logger
 # A better approach will involve more extensive code splitting, I think.
 try: import database
 except: import logger as database
-#try: import sliver_vs
-#except: import logger as sliver_vs
 import ticket as ticket_module
 import tools
 
@@ -148,7 +146,7 @@ def AdminTicket(ticket):
 @export_to_api(0)
 def GetXIDs():
     """Return an dictionary mapping Slice names to XIDs"""
-    return dict([(pwent[0], pwent[2]) for pwent in pwd.getpwall() if pwent[6] == sliver_lxc.Sliver_LXC.SHELL])
+    return dict([(pwent[0], pwent[2]) for pwent in pwd.getpwall() if pwent[6] == slivermanager.sliver_password_shell])
 
 @export_to_docbook(roles=['self'],
                    accepts=[],
diff --git a/net.py b/net.py
index a2b28ca..b1f234d 100644 (file)
--- a/net.py
+++ b/net.py
@@ -7,7 +7,8 @@ import os, string, time, socket
 import sioc, plnet
 
 # local modules
-import bwlimitlxc as bwlimit
+try:    import bwlimit as bwlimit
+except: import bwlimitlxc as bwlimit
 import logger, iptables, tools
 
 # we can't do anything without a network
index 7f2200d..49ada18 100644 (file)
@@ -2,8 +2,9 @@
 
 import logger
 import os
-import libvirt
+
 from config import Config
+import slivermanager
 
 CODEMUXCONF="/etc/codemux/codemux.conf"
 
@@ -51,7 +52,7 @@ def GetSlivers(data, config, plc = None):
 
                 try:
                     # Check to see if sliver is running.  If not, continue
-                    if isLXCDomRunning(sliver['name']):
+                    if slivermanager.is_running(sliver['name']):
                         # Check if new or needs updating
                         if (sliver['name'] not in slicesinconf.keys()) \
                         or (params not in slicesinconf.get(sliver['name'], [])):
@@ -166,13 +167,3 @@ def stopService():
         logger.log_call(["/etc/init.d/codemux", "stop", ])
     logger.log_call(["/sbin/chkconfig", "codemux", "off"])
 
-def isLXCDomRunning(domName):
-    try:
-        running = False
-        conn = libvirt.open('lxc://')
-        dom  = conn.lookupByName(domName)
-        running = dom.info()[0] == libvirt.VIR_DOMAIN_RUNNING
-    finally:
-        conn.close()
-    return running
-
index ea446fc..ce1f255 100644 (file)
@@ -1,4 +1,3 @@
-#
 """Sliver manager.
 
 The sliver manager has several functions.  It is responsible for
@@ -16,10 +15,26 @@ import api, api_calls
 import database
 import account
 import controller
-import sliver_lxc
 
-try: from bwlimitlxc import bwmin, bwmax
-except ImportError: bwmin, bwmax = 8, 1000*1000*1000
+try:
+    import sliver_lxc
+    implementation='lxc'
+    sliver_default_type='sliver.LXC'
+    sliver_class_to_register = sliver_lxc.Sliver_LXC
+    sliver_password_shell = sliver_lxc.Sliver_LXC.SHELL
+except:
+    import sliver_vs
+    implementation='vs'
+    sliver_default_type='sliver.VServer'
+    sliver_class_to_register = sliver_vs.Sliver_VS
+    sliver_password_shell = sliver_vs.Sliver_VS.SHELL
+
+# temporary - hopefully bwlimit will be packaged separately and there will be no need to do this any longer
+try: 
+    from bwlimitlxc import bwmin, bwmax
+except: 
+    try : from bwlimit import bwmin, bwmax
+    except: bwmin, bwmax = 8, 1000*1000*1000
 
 priority=10
 
@@ -159,7 +174,7 @@ def GetSlivers(data, config = None, plc=None, fullupdate=True):
         if rec['instantiation'].lower() == 'nm-controller':
             rec.setdefault('type', attributes.get('type', 'controller.Controller'))
         else:
-            rec.setdefault('type', attributes.get('type', 'sliver.LXC'))
+            rec.setdefault('type', attributes.get('type', sliver_default_type))
 
         # set the vserver reference.  If none, set to default.
         rec.setdefault('vref', attributes.get('vref', 'default'))
@@ -208,11 +223,29 @@ def deliver_ticket(data):
 def start():
     # No default allocation values for LXC yet, think if its necessary given
     # that they are also default allocation values in this module
-    #for resname, default_amount in sliver_vs.DEFAULT_ALLOCATION.iteritems():
-    #    DEFAULT_ALLOCATION[resname]=default_amount
+    if implementation == 'vs':
+        for resname, default_amount in sliver_vs.DEFAULT_ALLOCATION.iteritems():
+            DEFAULT_ALLOCATION[resname]=default_amount
 
-    account.register_class(sliver_lxc.Sliver_LXC)
+    account.register_class(sliver_class_to_register)
     account.register_class(controller.Controller)
     database.start()
     api_calls.deliver_ticket = deliver_ticket
     api.start()
+
+### check if a sliver is running
+### a first step to a unified code for codemux
+def is_running (name):
+    if implementation=='vs':
+        import vserver
+        return vserver.VServer(name).is_running()
+    else:
+        import libvirt
+        running = False
+        try:
+            conn = libvirt.open('lxc://')
+            dom  = conn.lookupByName(name)
+            running = dom.info()[0] == libvirt.VIR_DOMAIN_RUNNING
+        finally:
+            conn.close()
+        return running