10 class Sliver_LXC(accounts.Account):
11 """This class wraps LXC commands"""
14 # Using /bin/bash triggers destroy root/site_admin (?!?)
16 # Need to add a tag at myplc to actually use this account
19 def __init__(self, rec):
20 self.name = rec['name']
21 print "LXC __init__ %s"%(self.name)
22 logger.verbose ('sliver_lxc: %s init'%self.name)
24 self.dir = '/vservers/%s'%(self.name)
26 # Assume the directory with the image and config files
29 self.config = '%s/config'%(self.dir)
30 self.fstab = '%s/fstab'%(self.dir)
31 self.lxc_log = '%s/lxc.log'%(self.dir)
34 self.slice_id = rec['slice_id']
35 self.disk_usage_initialized = False
41 def create(name, rec = None):
42 ''' Create dirs, copy fs image, lxc_create '''
43 print "LXC create %s"%(name)
44 logger.verbose ('sliver_lxc: %s create'%name)
45 dir = '/vservers/%s'%(name)
46 config = '%s/config'%(dir)
47 lxc_log = '%s/lxc.log'%(dir)
49 if not (os.path.isdir(dir) and
50 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
51 print 'lxc_create: directory %s does not exist or wrong perms'%(dir)
53 # Assume for now that the directory is there and with a FS
56 command += ['/bin/bash','-x',]
57 command += ['/usr/bin/lxc-create', '-n', name, '-f', config, '&']
59 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
64 print "LXC destroy %s"%(name)
65 dir = '/vservers/%s'%(name)
66 lxc_log = '%s/lxc.log'%(dir)
68 command += ['/usr/bin/lxc-destroy', '-n', name]
70 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
71 print "LXC destroy DONE"
73 def configure(self, rec):
74 print "LXC configure %s"%(self.name)
76 def start(self, delay=0):
77 ''' Check existence? lxc_start '''
78 print "LXC start %s"%(self.name)
80 command += ['/usr/bin/lxc-start', '-n', self.name, '-d']
82 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
86 print "LXC stop %s"%(self.name)
89 print "LXC is_running %s"%(self.name)
91 command += ['/usr/bin/lxc-info -n %s'%(self.name)]
93 p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
94 state = p.communicate()[0].split(' ')[2]
96 if state == 'RUNNING': return True