13 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
14 libvirt.VIR_DOMAIN_RUNNING: 'running',
15 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
16 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
17 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
18 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
19 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
22 class Sliver_LV(accounts.Account):
23 """This class wraps LibVirt commands"""
26 # Using /bin/bash triggers destroy root/site_admin (?!?)
27 TYPE = 'sliver.LIBVIRT'
28 # Need to add a tag at myplc to actually use this account
29 # type = 'sliver.LIBVIRT'
31 def __init__(self, rec):
32 self.name = rec['name']
33 print "LIBVIRT %s __init__"%(self.name)
34 logger.verbose ('sliver_libvirt: %s init'%(self.name))
36 self.dir = '/vservers/%s'%(self.name)
38 # Assume the directory with the image and config files
41 self.config = '%s/config.xml'%(self.dir)
42 self.lxc_log = '%s/log'%(self.dir)
45 self.slice_id = rec['slice_id']
46 self.disk_usage_initialized = False
49 conn = Sliver_LV.getConnection()
51 self.container = conn.lookupByName(self.name)
53 print "Unexpected error:", sys.exc_info()[0]
56 def create(name, rec = None):
57 ''' Create dirs, copy fs image, lxc_create '''
58 print "LIBVIRT %s create"%(name)
59 logger.verbose ('sliver_libvirt: %s create'%(name))
60 dir = '/vservers/%s'%(name)
61 config = '%s/config.xml'%(dir)
62 lxc_log = '%s/log'%(dir)
64 if not (os.path.isdir(dir) and
65 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
66 logger.verbose('lxc_create: directory %s does not exist or wrong perms'%(dir))
69 # Get a connection and lookup for the sliver before actually
70 # defining it, just in case it was already defined.
71 conn = Sliver_LV.getConnection()
73 dom = conn.lookupByName(name)
75 xml = open(config).read()
76 dom = conn.defineXML(xml)
77 print Sliver_LV.info(dom)
81 ''' NEVER CALLED... Figure out when and what to do... '''
83 print "LIBVIRT %s destroy"%(name)
84 logger.verbose ('sliver_libvirt: %s destroy'%(name))
86 dir = '/vservers/%s'%(name)
87 lxc_log = '%s/lxc.log'%(dir)
89 conn = conn.Sliver_LV.getConnection()
92 dom = conn.lookupByName(name)
95 print Sliver_LV.info(dom)
97 logger.verbose('sliver_libvirt: %s domain does not exists'%(name))
98 print "Unexpected error:", sys.exc_info()[0]
101 def configure(self, rec):
102 ''' Allocate resources and fancy configuration stuff '''
103 print "LIBVIRT %s configure"%(self.name)
104 logger.verbose('sliver_libvirt: %s configure'%(self.name))
106 def start(self, delay=0):
107 ''' Just start the sliver '''
108 print "LIBVIRT %s start"%(self.name)
109 if self.rspec['enabled'] <= 0:
110 logger.log('sliver_libvirt: not starting %s, is not enabled'%(self.name))
113 logger.log('sliver_libvirt: %s starting...' % (self.name))
115 # Check if it's running to avoid throwing an exception if the
116 # domain was already running, create actually means start
117 if not self.is_running():
118 self.container.create()
120 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
123 ''' NEVER CALLED... Figure out when and what to do... '''
125 print "LIBVIRT %s stop"%(self.name)
126 logger.verbose('sliver_libvirt: %s stop'%(self.name))
129 self.container.destroy()
131 print "Unexpected error:", sys.exc_info()[0]
133 def is_running(self):
134 ''' Return True if the domain is running '''
135 print "LIBVIRT %s is_running"%(self.name)
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)