10 from string import Template
12 import sliver_libvirt as lv
14 class Sliver_LXC(lv.Sliver_Libvirt):
15 """This class wraps LXC commands"""
19 # Need to add a tag at myplc to actually use this account
22 REF_IMG_BASE_DIR = '/vservers/.lvref'
23 CON_BASE_DIR = '/vservers'
26 def create(name, rec=None):
27 ''' Create dirs, copy fs image, lxc_create '''
28 logger.verbose ('sliver_lxc: %s create'%(name))
29 conn = lv.getConnection(Sliver_LXC.TYPE)
31 # Get the type of image from vref myplc tags specified as:
37 logger.log('sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-f14' % (name))
38 vref = "lxc-f14-x86_64"
40 refImgDir = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, vref)
41 containerDir = os.path.join(Sliver_LXC.CON_BASE_DIR, name)
43 # check the template exists -- there's probably a better way..
44 if not os.path.isdir(refImgDir):
45 logger.log('sliver_lxc: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref))
46 logger.log('sliver_lxc: %s: ERROR ctd expected reference image in %s'%(name,refImgDir))
49 # Snapshot the reference image fs (assume the reference image is in its own
51 command = ['btrfs', 'subvolume', 'snapshot', refImgDir, containerDir]
52 logger.log_call(command, timeout=15*60)
53 command = ['chmod', '755', containerDir]
54 logger.log_call(command, timeout=15*60)
58 # Set hostname. A valid hostname cannot have '_'
59 #with open(os.path.join(containerDir, 'etc/hostname'), 'w') as f:
60 # print >>f, name.replace('_', '-')
62 # Add slices group if not already present
63 command = ['/usr/sbin/groupadd', 'slices']
64 logger.log_call(command, timeout=15*60)
66 # Add unix account (TYPE is specified in the subclass)
67 command = ['/usr/sbin/useradd', '-g', 'slices', '-s', Sliver_LXC.SHELL, name, '-p', '*']
68 logger.log_call(command, timeout=15*60)
69 command = ['mkdir', '/home/%s/.ssh'%name]
70 logger.log_call(command, timeout=15*60)
72 # Create PK pair keys to connect from the host to the guest without
73 # password... maybe remove the need for authentication inside the
75 command = ['su', '-s', '/bin/bash', '-c', 'ssh-keygen -t rsa -N "" -f /home/%s/.ssh/id_rsa'%(name)]
76 logger.log_call(command, timeout=15*60)
78 command = ['chown', '-R', '%s.slices'%name, '/home/%s/.ssh'%name]
79 logger.log_call(command, timeout=15*60)
81 command = ['mkdir', '%s/root/.ssh'%containerDir]
82 logger.log_call(command, timeout=15*60)
84 command = ['cp', '/home/%s/.ssh/id_rsa.pub'%name, '%s/root/.ssh/authorized_keys'%containerDir]
85 logger.log_call(command, timeout=15*60)
87 # Lookup for xid and create template after the user is created so we
88 # can get the correct xid based on the name of the slice
89 xid = bwlimit.get_xid(name)
91 # Template for libvirt sliver configuration
92 # template_filename = Sliver_LXC.REF_IMG_BASE_DIR + '/lxc_template.xml'
93 # for compat with lxc-reference package, hopefully temporary
94 template_filename_lxcreference = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR,'config_template.xml')
95 template_filename_sliceimage = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR,'lxc_template.xml')
96 if os.path.isfile (template_filename_lxcreference):
97 logger.log("WARNING: using compat template %s"%template_filename_lxcreference)
98 template_filename=template_filename_lxcreference
99 elif os.path.isfile (template_filename_sliceimage):
100 logger.log("WARNING: using compat template %s"%template_filename_sliceimage)
101 template_filename=template_filename_sliceimage
103 logger.log("Cannot find XML template")
104 logger.log("neither %s"%template_filename_lxcreference)
105 logger.log("nor %s"%template_filename_sliceimage)
108 with open(template_filename) as f:
109 template = Template(f.read())
110 xml = template.substitute(name=name, xid=xid)
112 logger.log('Failed to parse or use XML template file %s'%template_filename)
115 # Lookup for the sliver before actually
116 # defining it, just in case it was already defined.
118 dom = conn.lookupByName(name)
120 dom = conn.defineXML(xml)
121 logger.verbose('lxc_create: %s -> %s'%(name, lv.debuginfo(dom)))
126 logger.verbose ('sliver_lxc: %s destroy'%(name))
127 conn = lv.getConnection(Sliver_LXC.TYPE)
129 containerDir = Sliver_LXC.CON_BASE_DIR + '/%s'%(name)
132 # Destroy libvirt domain
133 dom = conn.lookupByName(name)
135 logger.verbose('sliver_lxc: Domain %s does not exist! UNEXPECTED'%name)
141 logger.verbose('sliver_lxc: Domain %s not running... continuing.'%name)
145 # Remove user after destroy domain to force logout
146 command = ['/usr/sbin/userdel', '-f', '-r', name]
147 logger.log_call(command, timeout=15*60)
149 # Remove rootfs of destroyed domain
150 command = ['btrfs', 'subvolume', 'delete', containerDir]
151 logger.log_call(command, timeout=15*60)
153 logger.verbose('sliver_libvirt: %s destroyed.'%name)