import logger
import subprocess
import os
+import os.path
import libvirt
import sys
+import shutil
from string import Template
libvirt.VIR_DOMAIN_CRASHED: 'crashed',
}
+REF_IMG_BASE_DIR = '/vservers/.lvref'
+CON_BASE_DIR = '/vservers'
+
class Sliver_LV(accounts.Account):
"""This class wraps LibVirt commands"""
def create(name, rec = None):
''' Create dirs, copy fs image, lxc_create '''
logger.verbose ('sliver_libvirt: %s create'%(name))
- dir = '/vservers/%s'%(name)
-
- # Template for sliver configuration
- template = Template(open('/vservers/config_template.xml').read())
- config = template.substitute(name=name)
+
+ # Template for libvirt sliver configuration
+ try:
+ with open('/vservers/.lvref/config_template.xml') as f:
+ template = Template(f.read())
+ config = template.substitute(name=name)
+ except IOError:
+ logger.log('Cannot find XML template file')
+ return
- lxc_log = '%s/log'%(dir)
-
- # TODO: copy the sliver FS to the correct path if sliver does not
- # exist.
- if not (os.path.isdir(dir) and
- os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
- logger.verbose('lxc_create: directory %s does not exist or wrong perms'%(dir))
+ # 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(REF_IMG_BASE_DIR, vref)
+ containerDir = os.path.join(CON_BASE_DIR, name)
+
+ # check the template exists -- there's probably a better way..
+ if not os.path.isdir(refImgDir):
+ logger.log('sliver_libvirt: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref))
return
- # Set hostname
- file('/vservers/%s/rootfs/etc/hostname' % name, 'w').write(name)
+ # Copy the reference image fs
+ # shutil.copytree("/vservers/.lvref/%s"%vref, "/vservers/%s"%name, symlinks=True)
+ command = ['cp', '-r', refImgDir, containerDir]
+ logger.log_call(command, timeout=15*60)
+
+ # 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
- command = ['/usr/sbin/useradd', '-s', '/bin/sh', name]
+ command = ['/usr/sbin/useradd', '-g', 'slices', '-s', '/bin/sh', name, '-p', '*']
logger.log_call(command, timeout=15*60)
# Get a connection and lookup for the sliver before actually
conn = Sliver_LV.getConnection()
try:
- command = ['/usr/sbin/userdel', name]
+ command = ['/usr/sbin/userdel', '-r', name]
logger.log_call(command, timeout=15*60)
+ # Destroy libvirt domain
dom = conn.lookupByName(name)
dom.destroy()
dom.undefine()
+
+ # Remove rootfs of destroyed domain
+ shutil.rmtree("/vservers/%s"%name)
except:
logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(name, sys.exc_info()[0]))
try:
self.container = conn.lookupByName(self.name)
except:
- logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(name, sys.exc_info()[0]))
- print "Unexpected error:", sys.exc_info()[0]
+ logger.verbose('sliver_libvirt: Unexpected error on %s: %s'%(self.name, sys.exc_info()[0]))
def configure(self, rec):
''' Allocate resources and fancy configuration stuff '''