11 from string import Template
12 import sliver_libvirt as lv
15 class Sliver_LXC(lv.Sliver_Libvirt):
16 """This class wraps LXC commands"""
21 # Need to add a tag at myplc to actually use this account
24 REF_IMG_BASE_DIR = '/vservers/.lvref'
25 CON_BASE_DIR = '/vservers'
28 def create(name, rec=None):
29 logger.verbose ('sliver_lxc: %s create'%(name))
30 conn = lv.getConnection(Sliver_LXC.TYPE)
32 # Template for libvirt sliver configuration
34 with open(Sliver_LXC.REF_IMG_BASE_DIR + '/config_template.xml') as f:
35 template = Template(f.read())
36 xml = template.substitute(name=name)
38 logger.log('Cannot find XML template file')
41 ''' Create dirs, copy fs image, lxc_create '''
42 # Get the type of image from vref myplc tags specified as:
48 logger.log('sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-debian' % (name))
49 vref = "lxc-squeeze-x86_64"
51 refImgDir = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, vref)
52 containerDir = os.path.join(Sliver_LXC.CON_BASE_DIR, name)
54 # check the template exists -- there's probably a better way..
55 if not os.path.isdir(refImgDir):
56 logger.log('sliver_lxc: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref))
59 # Snapshot the reference image fs (assume the reference image is in its own
61 command = ['btrfs', 'subvolume', 'snapshot', refImgDir, containerDir]
62 logger.log_call(command, timeout=15*60)
66 # Set hostname. A valid hostname cannot have '_'
67 with open(os.path.join(containerDir, 'etc/hostname'), 'w') as f:
68 print >>f, name.replace('_', '-')
70 # Add slices group if not already present
71 command = ['/usr/sbin/groupadd', 'slices']
72 logger.log_call(command, timeout=15*60)
74 # Add unix account (TYPE is specified in the subclass)
75 command = ['/usr/sbin/useradd', '-g', 'slices', '-s', Sliver_LXC.SHELL, name, '-p', '*']
76 logger.log_call(command, timeout=15*60)
77 command = ['mkdir', '/home/%s/.ssh'%name]
78 logger.log_call(command, timeout=15*60)
80 # Create PK pair keys to connect from the host to the guest without
81 # password... maybe remove the need for authentication inside the
83 command = ['su', '-s', '/bin/bash', '-c', 'ssh-keygen -t rsa -N "" -f /home/%s/.ssh/id_rsa'%(name)]
84 logger.log_call(command, timeout=15*60)
86 command = ['chown', '-R', '%s.slices'%name, '/home/%s/.ssh'%name]
87 logger.log_call(command, timeout=15*60)
89 command = ['mkdir', '%s/root/.ssh'%containerDir]
90 logger.log_call(command, timeout=15*60)
92 command = ['cp', '/home/%s/.ssh/id_rsa.pub'%name, '%s/root/.ssh/authorized_keys'%containerDir]
93 logger.log_call(command, timeout=15*60)
95 # Lookup for the sliver before actually
96 # defining it, just in case it was already defined.
98 dom = conn.lookupByName(name)
100 dom = conn.defineXML(xml)
101 logger.verbose('lxc_create: %s -> %s'%(name, lv.debuginfo(dom)))
106 logger.verbose ('sliver_lxc: %s destroy'%(name))
107 conn = lv.getConnection(Sliver_LXC.TYPE)
109 containerDir = Sliver_LXC.CON_BASE_DIR + '/%s'%(name)
112 # Destroy libvirt domain
113 dom = conn.lookupByName(name)
115 logger.verbose('sliver_lxc: Domain %s does not exist! UNEXPECTED'%name)
121 logger.verbose('sliver_lxc: Domain %s not running... continuing.'%name)
125 # Remove user after destroy domain to force logout
126 command = ['/usr/sbin/userdel', '-f', '-r', name]
127 logger.log_call(command, timeout=15*60)
129 # Remove rootfs of destroyed domain
130 command = ['btrfs', 'subvolume', 'delete', containerDir]
131 logger.log_call(command, timeout=15*60)
133 logger.verbose('sliver_libvirt: %s destroyed.'%name)