From c6bcc5ecda35a373b8232a5015a6209a63e5c825 Mon Sep 17 00:00:00 2001 From: Marco Yuen Date: Fri, 6 Jan 2012 12:24:51 -0500 Subject: [PATCH] Mostly aesthetic changes and fix a bug in codemux. Remove a bunch of white spaces in the sliver* files. Inside codemux there is a reference to the vserver module which is installed by util-vserver; implement a method that uses libvirt to check whether a LXC domain is running or not. --- plugins/codemux.py | 15 +++++++++++++-- sliver_libvirt.py | 33 ++++++++++++++------------------- sliver_lxc.py | 26 ++++++++++++-------------- slivermanager.py | 2 +- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/plugins/codemux.py b/plugins/codemux.py index 6ac314d..c3a6a8f 100644 --- a/plugins/codemux.py +++ b/plugins/codemux.py @@ -5,7 +5,7 @@ import logger import os -import vserver +import libvirt from config import Config CODEMUXCONF="/etc/codemux/codemux.conf" @@ -54,7 +54,7 @@ def GetSlivers(data, config, plc = None): 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'], [])): @@ -168,3 +168,14 @@ def stopService(): 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 + diff --git a/sliver_libvirt.py b/sliver_libvirt.py index 52c8bde..76c8aa5 100644 --- a/sliver_libvirt.py +++ b/sliver_libvirt.py @@ -1,5 +1,3 @@ -# - """LibVirt slivers""" import accounts @@ -12,17 +10,18 @@ import sys 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() @@ -48,17 +47,17 @@ class Sliver_Libvirt(accounts.Account): 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: @@ -81,21 +80,20 @@ class Sliver_Libvirt(accounts.Account): # 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) @@ -148,13 +146,10 @@ class Sliver_Libvirt(accounts.Account): # 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) - - - diff --git a/sliver_lxc.py b/sliver_lxc.py index b1dc99f..6e78ede 100644 --- a/sliver_lxc.py +++ b/sliver_lxc.py @@ -12,12 +12,10 @@ from string import Template 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' @@ -27,10 +25,10 @@ class Sliver_LXC(lv.Sliver_Libvirt): @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 @@ -64,7 +62,7 @@ class Sliver_LXC(lv.Sliver_Libvirt): # 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) @@ -76,10 +74,10 @@ class Sliver_LXC(lv.Sliver_Libvirt): # 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) @@ -98,7 +96,7 @@ class Sliver_LXC(lv.Sliver_Libvirt): 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: @@ -112,7 +110,7 @@ class Sliver_LXC(lv.Sliver_Libvirt): 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: @@ -122,17 +120,17 @@ class Sliver_LXC(lv.Sliver_Libvirt): 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) diff --git a/slivermanager.py b/slivermanager.py index 739b5c0..6264571 100644 --- a/slivermanager.py +++ b/slivermanager.py @@ -92,7 +92,7 @@ def adjustReservedSlivers (data): 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 -- 2.43.0