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',
25 mac = [ random.randint(0x00, 0xff),
26 random.randint(0x00, 0xff),
27 random.randint(0x00, 0xff),
28 random.randint(0x00, 0xff),
29 random.randint(0x00, 0xff),
30 random.randint(0x00, 0xff) ]
31 return ':'.join(map(lambda x: "%02x" % x, mac))
33 class Sliver_LV(accounts.Account):
34 """This class wraps LibVirt commands"""
38 # Need to add a tag at myplc to actually use this account
39 # type = 'sliver.LIBVIRT'
40 TYPE = 'sliver.LIBVIRT'
44 def create(name, rec = None):
45 ''' Create dirs, copy fs image, lxc_create '''
46 print "LIBVIRT %s create"%(name)
47 logger.verbose ('sliver_libvirt: %s create'%(name))
48 dir = '/vservers/%s'%(name)
50 # Template for sliver configuration
51 template = Template(open('/vservers/config_template.xml').read())
52 config = template.substitute(name=name)
54 lxc_log = '%s/log'%(dir)
56 # TODO: copy the sliver FS to the correct path if sliver does not
57 # exist. Update MAC addresses and insert an entry on the libvirt DHCP
58 # server to get an actual known IP. Some sort of pool?
59 if not (os.path.isdir(dir) and
60 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
61 logger.verbose('lxc_create: directory %s does not exist or wrong perms'%(dir))
65 file('/vservers/%s/rootfs/etc/hostname' % name, 'w').write(name)
67 # Get a connection and lookup for the sliver before actually
68 # defining it, just in case it was already defined.
69 conn = Sliver_LV.getConnection()
71 dom = conn.lookupByName(name)
73 dom = conn.defineXML(config)
74 print Sliver_LV.info(dom)
78 ''' NEVER CALLED... Figure out when and what to do... '''
80 print "LIBVIRT %s destroy"%(name)
81 logger.verbose ('sliver_libvirt: %s destroy'%(name))
83 dir = '/vservers/%s'%(name)
84 lxc_log = '%s/lxc.log'%(dir)
86 conn = conn.Sliver_LV.getConnection()
89 dom = conn.lookupByName(name)
92 print Sliver_LV.info(dom)
94 logger.verbose('sliver_libvirt: %s domain does not exists'%(name))
95 print "Unexpected error:", sys.exc_info()[0]
97 def __init__(self, rec):
98 self.name = rec['name']
99 print "LIBVIRT %s __init__"%(self.name)
100 logger.verbose ('sliver_libvirt: %s init'%(self.name))
102 self.dir = '/vservers/%s'%(self.name)
104 # Assume the directory with the image and config files
109 self.slice_id = rec['slice_id']
110 self.disk_usage_initialized = False
113 conn = Sliver_LV.getConnection()
115 self.container = conn.lookupByName(self.name)
117 print "Unexpected error:", sys.exc_info()[0]
119 def configure(self, rec):
120 ''' Allocate resources and fancy configuration stuff '''
121 print "LIBVIRT %s configure"%(self.name)
122 logger.verbose('sliver_libvirt: %s configure'%(self.name))
124 def start(self, delay=0):
125 ''' Just start the sliver '''
126 print "LIBVIRT %s start"%(self.name)
128 # Check if it's running to avoid throwing an exception if the
129 # domain was already running, create actually means start
130 if not self.is_running():
131 self.container.create()
133 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
136 ''' NEVER CALLED... Figure out when and what to do... '''
138 print "LIBVIRT %s stop"%(self.name)
139 logger.verbose('sliver_libvirt: %s stop'%(self.name))
142 self.container.destroy()
144 print "Unexpected error:", sys.exc_info()[0]
146 def is_running(self):
147 ''' Return True if the domain is running '''
148 print "LIBVIRT %s is_running"%(self.name)
149 logger.verbose('sliver_libvirt: %s is_running'%(self.name))
151 [state, _, _, _, _] = self.container.info()
152 if state == libvirt.VIR_DOMAIN_RUNNING:
153 logger.verbose('sliver_libvirt: %s is RUNNING'%(self.name))
156 info = Sliver_LV.info(self.container)
157 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
160 print "Unexpected error:", sys.exc_info()
162 ''' PRIVATE/HELPER/STATIC METHODS '''
165 ''' Helper method to get a connection to the LXC driver of Libvirt '''
166 conn = libvirt.open('lxc:///')
168 print 'Failed to open connection to LXC hypervisor'
174 ''' Helper method to get a "nice" output of the info struct for debug'''
175 [state, maxmem, mem, ncpu, cputime] = dom.info()
176 return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), states.get(state, state), maxmem, mem, ncpu, cputime)