Refactor of code to allow several virt techs. Minor bugs. Closes #8.
[nodemanager.git] / sliver_lxc.py
1 #
2
3 """LXC slivers"""
4
5 import accounts
6 import logger
7 import subprocess
8 import os
9 import libvirt
10 import sys
11 from string import Template
12 import sliver_libvirt as lv
13
14 URI = 'lxc://'
15
16 class Sliver_LXC(accounts.Account):
17     """This class wraps LXC commands"""
18    
19     SHELL = '/bin/sshsh' 
20     
21     TYPE = 'sliver.LXC'
22     # Need to add a tag at myplc to actually use this account
23     # type = 'sliver.LXC'
24
25
26     @staticmethod
27     def create(name, rec=None):
28         conn = lv.getConnection(URI)
29         
30         # Template for libvirt sliver configuration
31         try:
32             with open('/vservers/.lvref/config_template.xml') as f:
33                 template = Template(f.read())
34                 config   = template.substitute(name=name)
35         except IOError:
36             logger.log('Cannot find XML template file')
37             return
38         
39         lv.create(name, config, rec, conn)
40
41     @staticmethod
42     def destroy(name):
43         conn = lv.getConnection(URI)
44         lv.destroy(name, conn)
45
46     def __init__(self, rec):
47         self.name = rec['name']
48         logger.verbose ('sliver_lxc: %s init'%(self.name))
49          
50         self.dir = '/vservers/%s'%(self.name)
51         
52         # Assume the directory with the image and config files
53         # are in place
54         
55         self.keys = ''
56         self.rspec = {}
57         self.slice_id = rec['slice_id']
58         self.disk_usage_initialized = False
59         self.initscript = ''
60         self.enabled = True
61         self.conn = lv.getConnection(URI)
62         try:
63             self.container = self.conn.lookupByName(self.name)
64         except:
65             logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(self.name, sys.exc_info()[0]))
66
67     def start(self, delay=0):
68         lv.start(self.container)
69
70     def stop(self):
71         lv.stop(self.container)
72
73     def is_running(self):
74         lv.is_running(self.container)
75
76     def configure(self, rec):
77         ''' Allocate resources and fancy configuration stuff '''
78         logger.verbose('sliver_libvirt: %s configure'%(self.name))
79         accounts.Account.configure(self, rec)