12 from string import Template
15 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
16 libvirt.VIR_DOMAIN_RUNNING: 'running',
17 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
18 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
19 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
20 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
21 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
24 class Sliver_LV(accounts.Account):
25 """This class wraps LibVirt commands"""
29 # Need to add a tag at myplc to actually use this account
30 # type = 'sliver.LIBVIRT'
31 TYPE = 'sliver.LIBVIRT'
35 def create(name, rec = None):
36 ''' Create dirs, copy fs image, lxc_create '''
37 logger.verbose ('sliver_libvirt: %s create'%(name))
38 dir = '/vservers/%s'%(name)
40 # Template for sliver configuration
41 template = Template(open('/vservers/config_template.xml').read())
42 config = template.substitute(name=name)
44 lxc_log = '%s/log'%(dir)
46 # TODO: copy the sliver FS to the correct path if sliver does not
48 if not (os.path.isdir(dir) and
49 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
50 logger.verbose('lxc_create: directory %s does not exist or wrong perms'%(dir))
54 file('/vservers/%s/rootfs/etc/hostname' % name, 'w').write(name)
57 command = ['/usr/sbin/useradd', '-s', '/bin/sh', name]
58 logger.log_call(command, timeout=15*60)
60 # Get a connection and lookup for the sliver before actually
61 # defining it, just in case it was already defined.
62 conn = Sliver_LV.getConnection()
64 dom = conn.lookupByName(name)
66 dom = conn.defineXML(config)
67 logger.verbose('lxc_create: %s -> %s'%(name, Sliver_LV.info(dom)))
71 logger.verbose ('sliver_libvirt: %s destroy'%(name))
73 dir = '/vservers/%s'%(name)
74 lxc_log = '%s/lxc.log'%(dir)
76 conn = Sliver_LV.getConnection()
79 command = ['/usr/sbin/userdel', name]
80 logger.log_call(command, timeout=15*60)
82 dom = conn.lookupByName(name)
86 logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(name, sys.exc_info()[0]))
88 def __init__(self, rec):
89 self.name = rec['name']
90 logger.verbose ('sliver_libvirt: %s init'%(self.name))
92 self.dir = '/vservers/%s'%(self.name)
94 # Assume the directory with the image and config files
99 self.slice_id = rec['slice_id']
100 self.disk_usage_initialized = False
103 conn = Sliver_LV.getConnection()
105 self.container = conn.lookupByName(self.name)
107 logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(name, sys.exc_info()[0]))
108 print "Unexpected error:", sys.exc_info()[0]
110 def configure(self, rec):
111 ''' Allocate resources and fancy configuration stuff '''
112 logger.verbose('sliver_libvirt: %s configure'%(self.name))
113 accounts.Account.configure(self, rec)
115 def start(self, delay=0):
116 ''' Just start the sliver '''
117 print "LIBVIRT %s start"%(self.name)
119 # Check if it's running to avoid throwing an exception if the
120 # domain was already running, create actually means start
121 if not self.is_running():
122 self.container.create()
124 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
127 logger.verbose('sliver_libvirt: %s stop'%(self.name))
130 self.container.destroy()
132 print "Unexpected error:", sys.exc_info()[0]
134 def is_running(self):
135 ''' Return True if the domain is running '''
136 logger.verbose('sliver_libvirt: %s is_running'%(self.name))
138 [state, _, _, _, _] = self.container.info()
139 if state == libvirt.VIR_DOMAIN_RUNNING:
140 logger.verbose('sliver_libvirt: %s is RUNNING'%(self.name))
143 info = Sliver_LV.info(self.container)
144 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
147 print "Unexpected error:", sys.exc_info()
149 ''' PRIVATE/HELPER/STATIC METHODS '''
152 ''' Helper method to get a connection to the LXC driver of Libvirt '''
153 conn = libvirt.open('lxc:///')
155 print 'Failed to open connection to LXC hypervisor'
161 ''' Helper method to get a "nice" output of the info struct for debug'''
162 [state, maxmem, mem, ncpu, cputime] = dom.info()
163 return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), states.get(state, state), maxmem, mem, ncpu, cputime)