import logger
import os
-import vserver
+import libvirt
from config import Config
CODEMUXCONF="/etc/codemux/codemux.conf"
try:
# Check to see if sliver is running. If not, continue
- if vserver.VServer(sliver['name']).is_running():
+ if isLXCDomRunning(sliver['name']):
# Check if new or needs updating
if (sliver['name'] not in slicesinconf.keys()) \
or (params not in slicesinconf.get(sliver['name'], [])):
logger.log("codemux: Stopping codemux service")
logger.log_call(["/etc/init.d/codemux", "stop", ])
logger.log_call(["/sbin/chkconfig", "codemux", "off"])
+
+def isLXCDomRunning(domName):
+ try:
+ running = False
+ conn = libvirt.open('lxc://')
+ dom = conn.lookupByName(domName)
+ running = dom.info()[0] == libvirt.VIR_DOMAIN_RUNNING
+ finally:
+ conn.close()
+ return running
+
-#
-
"""LibVirt slivers"""
import accounts
import shutil
import bwlimit
import cgroups
+import pprint
from string import Template
STATES = {
- libvirt.VIR_DOMAIN_NOSTATE: 'no state',
- libvirt.VIR_DOMAIN_RUNNING: 'running',
- libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
- libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
+ libvirt.VIR_DOMAIN_NOSTATE: 'no state',
+ libvirt.VIR_DOMAIN_RUNNING: 'running',
+ libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
+ libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
- libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
- libvirt.VIR_DOMAIN_CRASHED: 'crashed',
+ libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
+ libvirt.VIR_DOMAIN_CRASHED: 'crashed',
}
connections = dict()
def __init__(self, rec):
self.name = rec['name']
logger.verbose ('sliver_libvirt: %s init'%(self.name))
-
+
# Assume the directory with the image and config files
# are in place
-
+
self.keys = ''
self.rspec = {}
self.slice_id = rec['slice_id']
self.enabled = True
self.conn = getConnection(rec['type'])
self.xid = bwlimit.get_xid(self.name)
-
+
try:
self.dom = self.conn.lookupByName(self.name)
except:
# interface to the actual device so the filter canmatch against the mark
bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \
(self.xid, self.xid))
-
def stop(self):
logger.verbose('sliver_libvirt: %s stop'%(self.name))
-
+
# Remove the ebtables rule before stopping
bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \
(self.xid, self.xid))
-
+
try:
self.dom.destroy()
except:
logger.verbose('sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
print 'sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0])
-
+
def is_running(self):
''' Return True if the domain is running '''
logger.verbose('sliver_libvirt: %s is_running'%self.name)
# CPU allocation
# Only cpu_shares until figure out how to provide limits and guarantees
# (RT_SCHED?)
- if rec.has_key('cpu_share'):
+ if rec.has_key('cpu_share'):
cpu_share = rec['cpu_share']
cgroups.write(self.name, 'cpu.shares', cpu_share)
# Call the upper configure method (ssh keys...)
accounts.Account.configure(self, rec)
-
-
-
import bwlimit
import sliver_libvirt as lv
-
class Sliver_LXC(lv.Sliver_Libvirt):
"""This class wraps LXC commands"""
-
- SHELL = '/bin/sshsh'
-
+
+ SHELL = '/bin/sshsh'
TYPE = 'sliver.LXC'
# Need to add a tag at myplc to actually use this account
# type = 'sliver.LXC'
@staticmethod
def create(name, rec=None):
+ ''' Create dirs, copy fs image, lxc_create '''
logger.verbose ('sliver_lxc: %s create'%(name))
conn = lv.getConnection(Sliver_LXC.TYPE)
-
- ''' Create dirs, copy fs image, lxc_create '''
+
# Get the type of image from vref myplc tags specified as:
# pldistro = lxc
# fcdistro = squeeze
# Add slices group if not already present
command = ['/usr/sbin/groupadd', 'slices']
logger.log_call(command, timeout=15*60)
-
+
# Add unix account (TYPE is specified in the subclass)
command = ['/usr/sbin/useradd', '-g', 'slices', '-s', Sliver_LXC.SHELL, name, '-p', '*']
logger.log_call(command, timeout=15*60)
# guest?
command = ['su', '-s', '/bin/bash', '-c', 'ssh-keygen -t rsa -N "" -f /home/%s/.ssh/id_rsa'%(name)]
logger.log_call(command, timeout=15*60)
-
+
command = ['chown', '-R', '%s.slices'%name, '/home/%s/.ssh'%name]
logger.log_call(command, timeout=15*60)
-
+
command = ['mkdir', '%s/root/.ssh'%containerDir]
logger.log_call(command, timeout=15*60)
except IOError:
logger.log('Cannot find XML template file')
return
-
+
# Lookup for the sliver before actually
# defining it, just in case it was already defined.
try:
def destroy(name):
logger.verbose ('sliver_lxc: %s destroy'%(name))
conn = lv.getConnection(Sliver_LXC.TYPE)
-
+
containerDir = Sliver_LXC.CON_BASE_DIR + '/%s'%(name)
try:
logger.verbose('sliver_lxc: Domain %s does not exist! UNEXPECTED'%name)
return
- try:
+ try:
dom.destroy()
except:
logger.verbose('sliver_lxc: Domain %s not running... continuing.'%name)
-
+
dom.undefine()
# Remove user after destroy domain to force logout
command = ['/usr/sbin/userdel', '-f', '-r', name]
logger.log_call(command, timeout=15*60)
-
+
# Remove rootfs of destroyed domain
command = ['btrfs', 'subvolume', 'delete', containerDir]
logger.log_call(command, timeout=15*60)
if is_system_sliver(sliver):
sliver['reservation_alive']=True
continue
-
+
# regular slivers
if not active_lease:
# with 'idle_or_shared', just let the field out, behave like a shared node