lxc skeleton. create, is_runnng works. Need clean up.
[nodemanager.git] / sliver_lxc.py
1 #
2
3 """LXC slivers"""
4
5 import accounts
6 import logger
7 import subprocess
8 import os
9
10 class Sliver_LXC(accounts.Account):
11     """This class wraps LXC commands"""
12
13     SHELL = '/bin/sh' 
14     # Using /bin/bash triggers destroy root/site_admin destr
15     TYPE = 'sliver.LXC'
16     # Need to add a tag at myplc to actually use this account
17     # type = 'sliver.LXC'
18
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)
23     
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)
31         self.keys = ''
32         self.rspec = {}
33         self.slice_id = rec['slice_id']
34         self.disk_usage_initialized = False
35         self.initscript = ''
36         self.enabled = True
37         self.configure(rec)
38
39     @staticmethod
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)
47         
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)
51             return
52         # Assume for now that the directory is there and with a FS
53         command=[]
54         # be verbose
55         command += ['/bin/bash','-x',]
56         command += ['/usr/bin/lxc-create', '-n', name, '-f', config, '&']
57         print command
58         print subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(lxc_log, 'a+'), stderr=subprocess.STDOUT, shell=False)
59         
60     @staticmethod
61     def destroy(name):
62         ''' lxc_destroy '''
63         print "LXC destroy %s"%(name)
64         dir = '/vservers/lxc_%s'%(name)
65         lxc_log = '%s/lxc.log'%(dir)
66         command=[]
67         # be verbose
68         command += ['/bin/bash','-x',]
69         command += ['/usr/bin/lxc-destroy', '-n', name]
70
71         subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(lxc_log, 'a+'), stderr=subprocess.STDOUT, shell=False)
72         print "LXC destroy DONE"
73
74     def configure(self, rec):
75         print "LXC configure %s"%(self.name) 
76
77     def start(self, delay=0):
78         ''' Check existence? lxc_start '''
79         print "LXC start %s"%(self.name)
80         command=[]
81         # be verbose
82         command += ['/bin/bash','-x',]
83         command += ['/usr/bin/lxc-start', '-n', self.name, '-d']
84         print command
85         subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open(self.lxc_log, 'w+'), stderr=subprocess.STDOUT, shell=False)
86
87     def stop(self):
88         ''' lxc_stop '''
89         print "LXC stop %s"%(self.name)
90     
91     def is_running(self):
92         print "LXC is_running %s"%(self.name)
93         command = []
94         command += ['/usr/bin/lxc-info -n %s'%(self.name)]
95         print command
96         p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
97         state = p.communicate()[0].split(' ')[2]
98         print state
99         if state == 'RUNNING': return True
100         else: return False
101
102