From: Marco Yuen Date: Tue, 6 Dec 2011 17:22:02 +0000 (-0500) Subject: Merge branch 'master' into lxc_devel X-Git-Tag: lxcbuild^0 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=da256268978e70f27e39f26012abd615e26a99c4;hp=067abb138667e15c83204ce4a46814fb5e7b1993;p=nodemanager.git Merge branch 'master' into lxc_devel --- diff --git a/config_template.xml b/config_template.xml new file mode 100644 index 0000000..59379ac --- /dev/null +++ b/config_template.xml @@ -0,0 +1,24 @@ + + $name + 32768 + + exe + /sbin/init + + 1 + + destroy + restart + destroy + + /usr/libexec/libvirt_lxc + + + + + + + + + + diff --git a/sliver_libvirt.py b/sliver_libvirt.py new file mode 100644 index 0000000..930a984 --- /dev/null +++ b/sliver_libvirt.py @@ -0,0 +1,151 @@ +# + +"""LibVirt slivers""" + +import accounts +import logger +import subprocess +import os +import os.path +import libvirt +import sys +import shutil + +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_SHUTDOWN: 'being shut down', + libvirt.VIR_DOMAIN_SHUTOFF: 'shut off', + libvirt.VIR_DOMAIN_CRASHED: 'crashed', +} + +REF_IMG_BASE_DIR = '/vservers/.lvref' +CON_BASE_DIR = '/vservers' + +connections = dict() + +# Helper methods + +def getConnection(sliver_type): + # TODO: error checking + # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu + vtype = sliver_type.split('.')[1].lower() + uri = vtype + '://' + return connections.setdefault(uri, libvirt.open(uri)) + +def debuginfo(dom): + ''' Helper method to get a "nice" output of the info struct for debug''' + [state, maxmem, mem, ncpu, cputime] = dom.info() + return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), STATES.get(state, state), maxmem, mem, ncpu, cputime) + +# Common Libvirt code + +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']) + + try: + self.dom = self.conn.lookupByName(self.name) + except: + logger.verbose('sliver_libvirt: Domain %s does not exist UNEXPECTED: %s'%(self.name, sys.exc_info()[0])) + + + def start(self, delay=0): + ''' Just start the sliver ''' + logger.verbose('sliver_libvirt: %s start'%(self.name)) + + # Check if it's running to avoid throwing an exception if the + # domain was already running, create actually means start + if not self.is_running(): + self.dom.create() + else: + logger.verbose('sliver_libvirt: sliver %s already started'%(dom.name())) + + + def stop(self): + logger.verbose('sliver_libvirt: %s stop'%(self.name)) + + 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) + try: + [state, _, _, _, _] = self.dom.info() + if state == libvirt.VIR_DOMAIN_RUNNING: + logger.verbose('sliver_libvirt: %s is RUNNING'%self.name) + return True + else: + info = debuginfo(self.dom) + logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info)) + return False + except: + logger.verbose('sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0])) + print 'sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0]) + + def configure(self, rec): + + sliver_type = rec['type'].split('.')[1] #sliver.[lxc/qemu] + + BASE_DIR = '/cgroup/libvirt/%s/%s/'%(sliver_type, self.name) + # Disk allocation + # No way through cgroups... figure out how to do that with user/dir quotas. + # There is no way to do quota per directory. Chown-ing would create + # problems as username namespaces are not yet implemented (and thus, host + # and containers share the same name ids + + # Btrfs support quota per volumes + + # It will depend on the FS selection + if rec.has_key('disk_max'): + disk_max = rec['disk_max'] + if disk_max == 0: + # unlimited + pass + else: + # limit to certain number + pass + + # Memory allocation + if rec.has_key('memlock_hard'): + mem = rec['memlock_hard'] * 1024 # hard limit in bytes + with open(os.path.join(BASE_DIR, 'memory.limit_in_bytes'), 'w') as f: + print >>f, mem + if rec.has_key('memlock_soft'): + mem = rec['memlock_soft'] * 1024 # soft limit in bytes + with open(os.path.join(BASE_DIR, 'memory.soft_limit_in_bytes'), 'w') as f: + print >>f, mem + + # CPU allocation + # Only cpu_shares until figure out how to provide limits and guarantees + # (RT_SCHED?) + if rec.has_key('cpu_share'): + cpu_share = rec['cpu_share'] + with open(os.path.join(BASE_DIR, 'cpu.shares'), 'w') as f: + print >>f, cpu_share + + # Call the upper configure method (ssh keys...) + accounts.Account.configure(self, rec) + + + + diff --git a/sliver_lxc.py b/sliver_lxc.py new file mode 100644 index 0000000..4c2a599 --- /dev/null +++ b/sliver_lxc.py @@ -0,0 +1,134 @@ +# + +"""LXC slivers""" + +import accounts +import logger +import subprocess +import os +import libvirt +import sys +from string import Template +import sliver_libvirt as lv + + +class Sliver_LXC(lv.Sliver_Libvirt): + """This class wraps LXC commands""" + + SHELL = '/bin/sshsh' + + TYPE = 'sliver.LXC' + # Need to add a tag at myplc to actually use this account + # type = 'sliver.LXC' + + REF_IMG_BASE_DIR = '/vservers/.lvref' + CON_BASE_DIR = '/vservers' + + @staticmethod + def create(name, rec=None): + logger.verbose ('sliver_lxc: %s create'%(name)) + conn = lv.getConnection(Sliver_LXC.TYPE) + + # Template for libvirt sliver configuration + try: + with open(Sliver_LXC.REF_IMG_BASE_DIR + '/config_template.xml') as f: + template = Template(f.read()) + xml = template.substitute(name=name) + except IOError: + logger.log('Cannot find XML template file') + return + + ''' Create dirs, copy fs image, lxc_create ''' + # Get the type of image from vref myplc tags specified as: + # pldistro = lxc + # fcdistro = squeeze + # arch x86_64 + vref = rec['vref'] + if vref is None: + logger.log('sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-debian' % (name)) + vref = "lxc-squeeze-x86_64" + + refImgDir = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, vref) + containerDir = os.path.join(Sliver_LXC.CON_BASE_DIR, name) + + # check the template exists -- there's probably a better way.. + if not os.path.isdir(refImgDir): + logger.log('sliver_lxc: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref)) + return + + # Snapshot the reference image fs (assume the reference image is in its own + # subvolume) + command = ['btrfs', 'subvolume', 'snapshot', refImgDir, containerDir] + logger.log_call(command, timeout=15*60) + + # TODO: set quotas... + + # Set hostname. A valid hostname cannot have '_' + with open(os.path.join(containerDir, 'etc/hostname'), 'w') as f: + print >>f, name.replace('_', '-') + + # 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) + command = ['mkdir', '/home/%s/.ssh'%name] + logger.log_call(command, timeout=15*60) + + # Create PK pair keys to connect from the host to the guest without + # password... maybe remove the need for authentication inside the + # 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) + + command = ['cp', '/home/%s/.ssh/id_rsa.pub'%name, '%s/root/.ssh/authorized_keys'%containerDir] + logger.log_call(command, timeout=15*60) + + # Lookup for the sliver before actually + # defining it, just in case it was already defined. + try: + dom = conn.lookupByName(name) + except: + dom = conn.defineXML(xml) + logger.verbose('lxc_create: %s -> %s'%(name, lv.debuginfo(dom))) + + + @staticmethod + 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: + # Destroy libvirt domain + dom = conn.lookupByName(name) + except: + logger.verbose('sliver_lxc: Domain %s does not exist! UNEXPECTED'%name) + return + + 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) + + logger.verbose('sliver_libvirt: %s destroyed.'%name) + diff --git a/sliver_vs.py b/sliver_vs.py index 8382c6d..2febb37 100644 --- a/sliver_vs.py +++ b/sliver_vs.py @@ -67,7 +67,6 @@ class Sliver_VS(accounts.Account, vserver.VServer): self.create(name, rec) logger.log("sliver_vs: %s: second chance..."%name) vserver.VServer.__init__(self, name,logfile='/var/log/nodemanager') - self.keys = '' self.rspec = {} self.slice_id = rec['slice_id'] @@ -187,7 +186,7 @@ class Sliver_VS(accounts.Account, vserver.VServer): if code: logger.log("vsliver_vs: %s: Installed new initscript in %s"%(self.name,sliver_initscript)) if self.is_running(): - # Only need to rerun the initscript if the vserver is + # Only need to rerun the initscript if the vserver is # already running. If the vserver isn't running, then the # initscript will automatically be started by # /etc/rc.d/vinit when the vserver is started. diff --git a/slivermanager.py b/slivermanager.py index 383379e..3e4fb1f 100644 --- a/slivermanager.py +++ b/slivermanager.py @@ -17,6 +17,7 @@ import database import accounts import controller import sliver_vs +import sliver_lxc try: from bwlimit import bwmin, bwmax except ImportError: bwmin, bwmax = 8, 1000*1000*1000 @@ -208,7 +209,9 @@ def start(): for resname, default_amount in sliver_vs.DEFAULT_ALLOCATION.iteritems(): DEFAULT_ALLOCATION[resname]=default_amount - accounts.register_class(sliver_vs.Sliver_VS) + #accounts.register_class(sliver_vs.Sliver_VS) + #accounts.register_class(sliver_libvirt.Sliver_LV) + accounts.register_class(sliver_lxc.Sliver_LXC) accounts.register_class(controller.Controller) database.start() api_calls.deliver_ticket = deliver_ticket diff --git a/sshsh b/sshsh new file mode 100755 index 0000000..edcd667 --- /dev/null +++ b/sshsh @@ -0,0 +1,5 @@ +#!/bin/bash +MAC=$(virsh --connect lxc:// --readonly dumpxml $USER|grep mac|awk -F\' '{print $2}') +IP=$(cat /var/lib/libvirt/dnsmasq/default.leases | grep $MAC | awk '{print $3}') +shift +ssh root@$IP "$@"