11 from string import Template
13 import sliver_libvirt as lv
16 class Sliver_LXC(lv.Sliver_Libvirt):
17 """This class wraps LXC commands"""
22 # Need to add a tag at myplc to actually use this account
25 REF_IMG_BASE_DIR = '/vservers/.lvref'
26 CON_BASE_DIR = '/vservers'
29 def create(name, rec=None):
30 logger.verbose ('sliver_lxc: %s create'%(name))
31 conn = lv.getConnection(Sliver_LXC.TYPE)
33 ''' Create dirs, copy fs image, lxc_create '''
34 # Get the type of image from vref myplc tags specified as:
40 logger.log('sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-f14' % (name))
41 vref = "lxc-f14-x86_64"
43 refImgDir = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, vref)
44 containerDir = os.path.join(Sliver_LXC.CON_BASE_DIR, name)
46 # check the template exists -- there's probably a better way..
47 if not os.path.isdir(refImgDir):
48 logger.log('sliver_lxc: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref))
51 # Snapshot the reference image fs (assume the reference image is in its own
53 command = ['btrfs', 'subvolume', 'snapshot', refImgDir, containerDir]
54 logger.log_call(command, timeout=15*60)
55 command = ['chmod', '755', containerDir]
56 logger.log_call(command, timeout=15*60)
60 # Set hostname. A valid hostname cannot have '_'
61 #with open(os.path.join(containerDir, 'etc/hostname'), 'w') as f:
62 # print >>f, name.replace('_', '-')
64 # Add slices group if not already present
65 command = ['/usr/sbin/groupadd', 'slices']
66 logger.log_call(command, timeout=15*60)
68 # Add unix account (TYPE is specified in the subclass)
69 command = ['/usr/sbin/useradd', '-g', 'slices', '-s', Sliver_LXC.SHELL, name, '-p', '*']
70 logger.log_call(command, timeout=15*60)
71 command = ['mkdir', '/home/%s/.ssh'%name]
72 logger.log_call(command, timeout=15*60)
74 # Create PK pair keys to connect from the host to the guest without
75 # password... maybe remove the need for authentication inside the
77 command = ['su', '-s', '/bin/bash', '-c', 'ssh-keygen -t rsa -N "" -f /home/%s/.ssh/id_rsa'%(name)]
78 logger.log_call(command, timeout=15*60)
80 command = ['chown', '-R', '%s.slices'%name, '/home/%s/.ssh'%name]
81 logger.log_call(command, timeout=15*60)
83 command = ['mkdir', '%s/root/.ssh'%containerDir]
84 logger.log_call(command, timeout=15*60)
86 command = ['cp', '/home/%s/.ssh/id_rsa.pub'%name, '%s/root/.ssh/authorized_keys'%containerDir]
87 logger.log_call(command, timeout=15*60)
89 # Lookup for xid and create template after the user is created so we
90 # can get the correct xid based on the name of the slice
91 xid = bwlimit.get_xid(name)
93 # Template for libvirt sliver configuration
95 with open(Sliver_LXC.REF_IMG_BASE_DIR + '/config_template.xml') as f:
96 template = Template(f.read())
97 xml = template.substitute(name=name, xid=xid)
99 logger.log('Cannot find XML template file')
102 # Lookup for the sliver before actually
103 # defining it, just in case it was already defined.
105 dom = conn.lookupByName(name)
107 dom = conn.defineXML(xml)
108 logger.verbose('lxc_create: %s -> %s'%(name, lv.debuginfo(dom)))
113 logger.verbose ('sliver_lxc: %s destroy'%(name))
114 conn = lv.getConnection(Sliver_LXC.TYPE)
116 containerDir = Sliver_LXC.CON_BASE_DIR + '/%s'%(name)
119 # Destroy libvirt domain
120 dom = conn.lookupByName(name)
122 logger.verbose('sliver_lxc: Domain %s does not exist! UNEXPECTED'%name)
128 logger.verbose('sliver_lxc: Domain %s not running... continuing.'%name)
132 # Remove user after destroy domain to force logout
133 command = ['/usr/sbin/userdel', '-f', '-r', name]
134 logger.log_call(command, timeout=15*60)
136 # Remove rootfs of destroyed domain
137 command = ['btrfs', 'subvolume', 'delete', containerDir]
138 logger.log_call(command, timeout=15*60)
140 logger.verbose('sliver_libvirt: %s destroyed.'%name)