Refactor of code to allow several virt techs. Minor bugs. Closes #8.
[nodemanager.git] / sliver_lxc.py
index a93a215..e68a0b4 100644 (file)
@@ -3,33 +3,77 @@
 """LXC slivers"""
 
 import accounts
+import logger
+import subprocess
+import os
+import libvirt
+import sys
+from string import Template
+import sliver_libvirt as lv
+
+URI = 'lxc://'
 
 class Sliver_LXC(accounts.Account):
     """This class wraps LXC commands"""
-
-    SHELL = '/bin/bash'
+   
+    SHELL = '/bin/sshsh' 
+    
     TYPE = 'sliver.LXC'
     # Need to add a tag at myplc to actually use this account
     # type = 'sliver.LXC'
 
-    def __init__(self, rec):
-        print "TODO __init__"
-    
+
     @staticmethod
-    def create(name, rec = None):
-        print "TODO create"
+    def create(name, rec=None):
+        conn = lv.getConnection(URI)
+        
+        # Template for libvirt sliver configuration
+        try:
+            with open('/vservers/.lvref/config_template.xml') as f:
+                template = Template(f.read())
+                config   = template.substitute(name=name)
+        except IOError:
+            logger.log('Cannot find XML template file')
+            return
         
+        lv.create(name, config, rec, conn)
+
     @staticmethod
     def destroy(name):
-        print "TODO destroy"
+        conn = lv.getConnection(URI)
+        lv.destroy(name, conn)
+
+    def __init__(self, rec):
+        self.name = rec['name']
+        logger.verbose ('sliver_lxc: %s init'%(self.name))
+         
+        self.dir = '/vservers/%s'%(self.name)
+        
+        # Assume the directory with the image and config files
+        # are in place
+        
+        self.keys = ''
+        self.rspec = {}
+        self.slice_id = rec['slice_id']
+        self.disk_usage_initialized = False
+        self.initscript = ''
+        self.enabled = True
+        self.conn = lv.getConnection(URI)
+        try:
+            self.container = self.conn.lookupByName(self.name)
+        except:
+            logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(self.name, sys.exc_info()[0]))
 
     def start(self, delay=0):
-        print "TODO start"
-    
+        lv.start(self.container)
+
     def stop(self):
-        print "TODO stop"
-    
+        lv.stop(self.container)
+
     def is_running(self):
-        print "TODO is_running"
+        lv.is_running(self.container)
 
-    
+    def configure(self, rec):
+        ''' Allocate resources and fancy configuration stuff '''
+        logger.verbose('sliver_libvirt: %s configure'%(self.name))
+        accounts.Account.configure(self, rec)