10 class Sliver_LXC(accounts.Account):
11 """This class wraps LXC commands"""
14 # Using /bin/bash triggers destroy root/site_admin destr
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/lxc_%s'%(self.name)
25 if not (os.path.isdir(self.dir) and
26 os.access(self.dir, os.R_OK | os.W_OK | os.X_OK)):
27 raise NoSuchVServer, "no such vserver: " + self.name
28 self.config = '/%s/config'%(self.dir)
29 self.fstab = '/%s/fstab'%(self.dir)
30 self.lxc_log = '/%s/lxc.log'%(self.dir)
33 self.slice_id = rec['slice_id']
34 self.disk_usage_initialized = False
40 def create(name, rec = None):
41 ''' Create dirs, copy fs image, lxc_create '''
42 print "LXC create %s"%(name)
43 logger.verbose ('sliver_lxc: %s create'%name)
44 dir = '/vservers/%s'%(name)
45 config = '%s/config'%(dir)
46 lxc_log = '%s/lxc.log'%(dir)
48 if not (os.path.isdir(dir) and
49 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
50 print 'lxc_create: directory %s does not exist or wrong perms'%(dir)
52 # Assume for now that the directory is there and with a FS
55 command += ['/bin/bash','-x',]
56 command += ['/usr/bin/lxc-create', '-n', name, '-f', config, '&']
58 print subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(lxc_log, 'a+'), stderr=subprocess.STDOUT, shell=False)
63 print "LXC destroy %s"%(name)
64 dir = '/vservers/lxc_%s'%(name)
65 lxc_log = '%s/lxc.log'%(dir)
68 command += ['/bin/bash','-x',]
69 command += ['/usr/bin/lxc-destroy', '-n', name]
71 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(lxc_log, 'a+'), stderr=subprocess.STDOUT, shell=False)
72 print "LXC destroy DONE"
74 def configure(self, rec):
75 print "LXC configure %s"%(self.name)
77 def start(self, delay=0):
78 ''' Check existence? lxc_start '''
79 print "LXC start %s"%(self.name)
82 command += ['/bin/bash','-x',]
83 command += ['/usr/bin/lxc-start', '-n', self.name, '-d']
85 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(self.lxc_log, 'w+'), stderr=subprocess.STDOUT, shell=False)
89 print "LXC stop %s"%(self.name)
92 print "LXC is_running %s"%(self.name)
94 command += ['/usr/bin/lxc-info -n %s'%(self.name)]
96 p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
97 state = p.communicate()[0].split(' ')[2]
99 if state == 'RUNNING': return True