13 from string import Template
16 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
17 libvirt.VIR_DOMAIN_RUNNING: 'running',
18 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
19 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
20 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
21 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
22 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
25 class Sliver_LV(accounts.Account):
26 """This class wraps LibVirt commands"""
30 # Need to add a tag at myplc to actually use this account
31 # type = 'sliver.LIBVIRT'
32 TYPE = 'sliver.LIBVIRT'
36 def create(name, rec = None):
37 ''' Create dirs, copy fs image, lxc_create '''
38 logger.verbose ('sliver_libvirt: %s create'%(name))
39 dir = '/vservers/%s'%(name)
41 # Template for libvirt sliver configuration
42 template = Template(open('/vservers/.lvref/config_template.xml').read())
43 config = template.substitute(name=name)
45 lxc_log = '%s/log'%(dir)
47 # Get the type of image from vref myplc tags specified as:
53 logger.log("sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-debian"%(name))
54 vref = "lxc-squeeze-x86_64"
56 # check the template exists -- there's probably a better way..
57 if not os.path.isdir ("/vservers/.lvref/%s"%vref):
58 logger.log ("sliver_libvirt: %s: ERROR Could not create sliver - reference image %s not found"%(name,vref))
61 # Copy the reference image fs
62 # shutil.copytree("/vservers/.lvref/%s"%vref, "/vservers/%s"%name, symlinks=True)
63 command = ['cp', '-r', '/vservers/.lvref/%s'%vref, '/vservers/%s'%name]
64 logger.log_call(command, timeout=15*60)
67 file('/vservers/%s/etc/hostname' % name, 'w').write(name)
69 # Add slices group if not already present
70 command = ['/usr/sbin/groupadd slices']
71 logger.log_call(command, timeout=15*60)
73 command = ['/usr/sbin/useradd', '-g', 'slices', '-s', '/bin/sh', name, '-p', '*']
74 logger.log_call(command, timeout=15*60)
76 # Get a connection and lookup for the sliver before actually
77 # defining it, just in case it was already defined.
78 conn = Sliver_LV.getConnection()
80 dom = conn.lookupByName(name)
82 dom = conn.defineXML(config)
83 logger.verbose('lxc_create: %s -> %s'%(name, Sliver_LV.info(dom)))
87 logger.verbose ('sliver_libvirt: %s destroy'%(name))
89 dir = '/vservers/%s'%(name)
90 lxc_log = '%s/lxc.log'%(dir)
92 conn = Sliver_LV.getConnection()
95 command = ['/usr/sbin/userdel', '-r', name]
96 logger.log_call(command, timeout=15*60)
98 # Destroy libvirt domain
99 dom = conn.lookupByName(name)
103 # Remove rootfs of destroyed domain
104 shutil.rmtree("/vservers/%s"%name)
106 logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(name, sys.exc_info()[0]))
108 def __init__(self, rec):
109 self.name = rec['name']
110 logger.verbose ('sliver_libvirt: %s init'%(self.name))
112 self.dir = '/vservers/%s'%(self.name)
114 # Assume the directory with the image and config files
119 self.slice_id = rec['slice_id']
120 self.disk_usage_initialized = False
123 conn = Sliver_LV.getConnection()
125 self.container = conn.lookupByName(self.name)
127 logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(self.name, sys.exc_info()[0]))
129 def configure(self, rec):
130 ''' Allocate resources and fancy configuration stuff '''
131 logger.verbose('sliver_libvirt: %s configure'%(self.name))
132 accounts.Account.configure(self, rec)
134 def start(self, delay=0):
135 ''' Just start the sliver '''
136 print "LIBVIRT %s start"%(self.name)
138 # Check if it's running to avoid throwing an exception if the
139 # domain was already running, create actually means start
140 if not self.is_running():
141 self.container.create()
143 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
146 logger.verbose('sliver_libvirt: %s stop'%(self.name))
149 self.container.destroy()
151 print "Unexpected error:", sys.exc_info()[0]
153 def is_running(self):
154 ''' Return True if the domain is running '''
155 logger.verbose('sliver_libvirt: %s is_running'%(self.name))
157 [state, _, _, _, _] = self.container.info()
158 if state == libvirt.VIR_DOMAIN_RUNNING:
159 logger.verbose('sliver_libvirt: %s is RUNNING'%(self.name))
162 info = Sliver_LV.info(self.container)
163 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
166 print "Unexpected error:", sys.exc_info()
168 ''' PRIVATE/HELPER/STATIC METHODS '''
171 ''' Helper method to get a connection to the LXC driver of Libvirt '''
172 conn = libvirt.open('lxc:///')
174 print 'Failed to open connection to LXC hypervisor'
180 ''' Helper method to get a "nice" output of the info struct for debug'''
181 [state, maxmem, mem, ncpu, cputime] = dom.info()
182 return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), states.get(state, state), maxmem, mem, ncpu, cputime)